question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using text labels on gauges

See original GitHub issue

Can I use a text label on a gauge?

I want to draw directly in the canvas. But I am using npm.

So, I forked npm and tried the following: But it does not work

This is my modified code.

GaugePointer.prototype.render = function() {
      var angle, centerX, centerY, endX, endY, imgX, imgY, startX, startY, x, y;
      angle = this.gauge.getAngle.call(this, this.displayedValue);
      centerX = this.canvas.width / 2;
      centerY = this.canvas.height * 0.9;
      x = Math.round(this.length * Math.cos(angle));
      y = Math.round(this.length * Math.sin(angle));
      startX = Math.round(this.strokeWidth * Math.cos(angle - Math.PI / 2));
      startY = Math.round(this.strokeWidth * Math.sin(angle - Math.PI / 2));
      endX = Math.round(this.strokeWidth * Math.cos(angle + Math.PI / 2));
      endY = Math.round(this.strokeWidth * Math.sin(angle + Math.PI / 2));
      this.ctx.beginPath();
      this.ctx.fillStyle = this.options.color;
      this.ctx.arc(0, 0, this.strokeWidth, 0, Math.PI * 2, false);
      this.ctx.fill();
      this.ctx.beginPath();
      this.ctx.moveTo(startX, startY);
      this.ctx.lineTo(x, y);
      this.ctx.lineTo(endX, endY);
      this.ctx.fill();
      

      /* ---------- Here!!!! ------- */
      this.ctx.font = this.ctx.font.replace(/\d+px/, "16px");      
      this.ctx.fillText("Minimum", centerX / 4 - 12, this.canvas.height - 9);
      this.ctx.fillText("Maximum", centerX + (centerX / 2) + 12 - 15, this.canvas.height - 9);

      if (this.img) {
        imgX = Math.round(this.img.width * this.options.iconScale);
        imgY = Math.round(this.img.height * this.options.iconScale);
        this.ctx.save();
        this.ctx.translate(x, y);
        this.ctx.rotate(angle + Math.PI / 180.0 * (90 + this.options.iconAngle));
        this.ctx.drawImage(this.img, -imgX / 2, -imgY / 2, imgX, imgY);
        return this.ctx.restore();
      }

      return this.ctx.fill();
    };

Example I think

gauge_ex

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
AlphaSierraHotelcommented, Aug 5, 2019

@paulobezerr How did you manage to show percentage instead of number?

See https://github.com/bernii/gauge.js/issues/4#issuecomment-7761988 for an example of how to do this. I updated it to also account for the minimum value. My use case is a gauge that shows battery voltage (in millivolts) with a label that represents it as a percentage value with 2.8 volts = 0% and 3.6 volts = 100%. Here’s my take on it.

var gauge = new Gauge(target).setOptions(opts);

gauge.maxValue = 3600;
gauge.setMinValue(2800);

var textRenderer = new TextRenderer(document.getElementById("preview-textfield"))

textRenderer.render = function(gauge){
   percentage = (gauge.displayedValue-gauge.minValue) / (gauge.maxValue-gauge.minValue)
   this.el.innerHTML = (percentage * 100).toFixed(2) + "%"
};
gauge.setTextField(textRenderer);

image

0reactions
pgbezerracommented, Aug 14, 2019

I move out from the company that has this change. Maybe @raphaelneumann can help you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Gauge chart with text labels for Value and Target
I would like to create a gauge chart that has text labels for the Target and Actual fields. I added text boxes to...
Read more >
Tickmarks with Text | Gauge | JavaScript Wijmo Demos
Wijmo's gauges allow you to show tickmarks along their faces using the showTicks ... To add text to each tickmark, set the showTickText...
Read more >
Gauges: How to add a label whose text resizes with the gauge
Hi Kevin, You can use NLength with relative unit (parent percentage) to specify the text size of the gauge label texts - for...
Read more >
Highcharts Gauge Chart with text labels - Stack Overflow
You can use a yAxis.labels.formatter function to check what the label value is, and return a text of your choice depending on that...
Read more >
Adding Text Labels to Numeric Objects with Scale - NI
Right-click the text display and select Visible Items»Digital Display from the shortcut menu to display the numeric values associated with the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found