Giter VIP home page Giter VIP logo

Comments (3)

naikus avatar naikus commented on June 9, 2024

Hi @AndreaMinato can you give me a code example for this?

from svg-gauge.

AndreaMinato avatar AndreaMinato commented on June 9, 2024

The framework

I'm using vueJs and I want tot wrap this library into a component, I don't want to use vue-gauge since I need to keep the fewer dependencies I can.

Some details

In the mounted function (called as soon as my component is rendered) I create the gauge setting the values as some defaults, than I call LoadData that send a request to a server to get the data.
It would be Handy to have the chance to change the max property when I get the response from the server.

Workaround

Right know I just use the 100 max value, but it cause some graphics inconsistencies since if i had a cooking time of 600 the value itself (calculated like this return Math.round(this.timeElapsed / this.totaltime * 100);) change once every 6 seconds.

The code

import Vue from "vue";

import Gauge from "svg-gauge";
import axios from "axios";

export default Vue.extend({
  name: "Gauge",

  data() {
    return {
      interval: null,
      gauge: null,
      totaltime: 0,
      timeleft: 0,
      status: "",
      lidError: "",
      UpperPlateTemperature: "",
      BottomPlateTemperature: "",
      recipe: ""
    };
  },

  computed: {
    timeElapsed() {
      return this.totaltime - this.timeleft;
    },
    value() {
      return Math.round(this.timeElapsed / this.totaltime * 100);
    },
    finished() {
      if (this.timeleft <= 0) {
        this.$emit("finished");
        return true;
      }
      return false;
    }
  },
  mounted() {
    this.createGauge();
    this.loadData();
  },
  methods: {
    tickIt() {
      this.interval = setInterval(() => {
        this.timeleft--;
        this.gauge.setValue(this.value);
      }, 1000);

      this.$on("finished", () => {
        clearInterval(this.interval);
      });
    },
    createGauge() {
      this.gauge = Gauge(this.$refs.gauge, {
        max: 100,
        value: 0,
        showValue: false,
        dialRadius: 48,
        gaugeClass: "gauge",
        dialClass: "dial",
        valueDialClass: "value",
        valueTextClass: "value-text"
      });
    },
    async loadData() {
      clearInterval(this.interval);
      try {
        let res = await axios.get(`/gaugeValues`);
        this.totaltime = res.data["CookingTime"];
        this.timeleft = res.data["RunTime"];
      } catch (error) {
       console.log(error);
      }

      this.gauge.setValueAnimated(this.value, 1);
      this.tickIt();
    }
  }
});

from svg-gauge.

naikus avatar naikus commented on June 9, 2024

Hi @AndreaMinato I'll take a look into this.

from svg-gauge.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.