{"id":2222,"date":"2019-11-20T07:33:57","date_gmt":"2019-11-20T07:33:57","guid":{"rendered":"https:\/\/mindfusion.eu\/blog\/?p=2222"},"modified":"2021-01-25T16:04:10","modified_gmt":"2021-01-25T16:04:10","slug":"cost-meter-gauge-in-javascript","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/cost-meter-gauge-in-javascript\/","title":{"rendered":"Cost Meter Gauge in JavaScript"},"content":{"rendered":"<p>In this post we will look at the steps we need to make if we want to create this beautiful gauge below:<\/p>\n<p><a href=\"https:\/\/mindfusion.dev\/javascript-demo.html?sample=ValueGauge\"><img decoding=\"async\" title=\"JavaScript Value Cost Gauge\" src=\"https:\/\/mindfusion.dev\/samples\/javascript\/chart\/value_gauge.png\" \/><\/a><\/p>\n<p>The gauge is done with <a title=\"MindFusion JavaScript Charts and Gauges\" href=\"https:\/\/mindfusion.dev\/javascript-chart.html\">MindFusion Charts and Gauges for JavaScript library<\/a>. You can download the sample together with all needed libraries from <a title=\"JavaScript Value Cost Gauge\" href=\"https:\/\/mindfusion.dev\/samples\/javascript\/chart\/ValueGauge.zip\">this link.<\/a><\/p>\n<p><strong>I. Project Setup<\/strong><\/p>\n<p>We will build the gauge using the <a title=\"Charts and Gauges for JavaScript: OvalGauge\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_OvalGauge_1.htm\">OvalGauge<\/a> library from MindFusion JS Charts and Gauges control. We add two references, needed for the control to work properly:<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">script<\/span> <span class=\"attribute-name\">type<\/span>=\"<a class=\"attribute-value\">text\/javascript<\/a>\" <span class=\"attribute-name\">src<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/cost-meter-gauge-in-javascript\/Scripts\/MindFusion.Common.js\">Scripts\/MindFusion.Common.js<\/a>\"&gt;&lt;\/<span class=\"end-tag\">script<\/span>&gt;\n<span id=\"line323\"><\/span>&lt;<span class=\"start-tag\">script<\/span> <span class=\"attribute-name\">type<\/span>=\"<a class=\"attribute-value\">text\/javascript<\/a>\" <span class=\"attribute-name\">src<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/cost-meter-gauge-in-javascript\/Scripts\/MindFusion.Gauges.js\">Scripts\/MindFusion.Gauges.js<\/a>\"&gt;&lt;\/<span class=\"end-tag\">script<\/span>&gt;<\/pre>\n<p>We have placed those files in a Scripts folder. We will write the JavaScript code for the gauge in a separate file, which we call ValueGauge.js. This file is at the same directory where the web page is. We add a reference to it as well:<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">script<\/span> <span class=\"attribute-name\">type<\/span>=\"<a class=\"attribute-value\">text\/javascript<\/a>\" <span class=\"attribute-name\">src<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/cost-meter-gauge-in-javascript\/ValueGauge.js\">ValueGauge.js<\/a>\"&gt;&lt;\/<span class=\"end-tag\">script<\/span>&gt;<\/pre>\n<p>The web page with our sample contains a table. We use the table to place the control together with a text box. The text box is not needed, but we will use it to give the user the option to set the value of the gauge by typing it not only by dragging the pointer.<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">table<\/span> <span class=\"attribute-name\">cellpadding<\/span>=\"<a class=\"attribute-value\">10<\/a>\"&gt;\n<span id=\"line329\"><\/span>    &lt;<span class=\"start-tag\">tbody<\/span>&gt;\n<span id=\"line330\"><\/span>        &lt;<span class=\"start-tag\">tr<\/span>&gt;\n<span id=\"line331\"><\/span>            &lt;<span class=\"start-tag\">td<\/span>&gt;Project Cost (in thousands)&lt;\/<span class=\"end-tag\">td<\/span>&gt;\n<span id=\"line332\"><\/span>        &lt;\/<span class=\"end-tag\">tr<\/span>&gt;\n<span id=\"line333\"><\/span>        &lt;<span class=\"start-tag\">tr<\/span>&gt;\n<span id=\"line334\"><\/span>            &lt;<span class=\"start-tag\">td<\/span>&gt;&lt;<span class=\"start-tag\">canvas<\/span> <span class=\"attribute-name\">id<\/span>=\"<a class=\"attribute-value\">value_meter<\/a>\" <span class=\"attribute-name\">width<\/span>=\"<a class=\"attribute-value\">300<\/a>\" <span class=\"attribute-name\">height<\/span>=\"<a class=\"attribute-value\">300<\/a>\"&gt;&lt;\/<span class=\"end-tag\">canvas<\/span>&gt;&lt;\/<span class=\"end-tag\">td<\/span>&gt;\n<span id=\"line335\"><\/span>        &lt;\/<span class=\"end-tag\">tr<\/span>&gt;\n<span id=\"line336\"><\/span>        &lt;<span class=\"start-tag\">tr<\/span>&gt;\n<span id=\"line337\"><\/span>            &lt;<span class=\"start-tag\">td<\/span>&gt;Cost &lt;<span class=\"start-tag\">input<\/span> <span class=\"attribute-name\">id<\/span>=\"<a class=\"attribute-value\">cost<\/a>\" <span class=\"attribute-name\">style<\/span>=\"<a class=\"attribute-value\">width: 80px<\/a>\"&gt;&lt;\/<span class=\"end-tag\">td<\/span>&gt;\n<span id=\"line338\"><\/span>        &lt;\/<span class=\"end-tag\">tr<\/span>&gt;\n<span id=\"line339\"><\/span>    &lt;\/<span class=\"end-tag\">tbody<\/span>&gt;\n<span id=\"line340\"><\/span>&lt;\/<span class=\"end-tag\">table<\/span>&gt;<\/pre>\n<p>The gauge will be rendered through an HTML Canvas element. The location and size of the Canvas determine the location and the size of the gauge. It is important that we add an id the Canvas &#8211; this way we can reference it in the JavaScript code page, which will be necessary.<\/p>\n<p><strong>II. The Control<\/strong><\/p>\n<p>Now we start editing the ValueGauge.js file and there we first add mappings to the namespaces of Mindusion.Gauges.js that we will use:<\/p>\n<pre>\/\/\/ \nvar Gauges = MindFusion.Gauges;\n\nvar d = MindFusion.Drawing;\nvar OvalScale = Gauges.OvalScale;\nvar Length = Gauges.Length;\nvar LengthType = Gauges.LengthType;\nvar Thickness = Gauges.Thickness;\nvar Alignment = Gauges.Alignment;\nvar LabelRotation = Gauges.LabelRotation;\nvar TickShape = Gauges.TickShape;\nvar PointerShape = Gauges.PointerShape;<\/pre>\n<p>The first line is a reference to the Intellisense file that allows us to use code completion of the API members, if supported by our JavaScript IDE.<\/p>\n<p>Now we need to get the DOM Element that corresponds to the gauge Canvas and use it to create an instance of the <a title=\"Charts and Gauges for JavaScript: OvalGauge\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_OvalGauge_Members_1.htm\">OvalGauge<\/a> class:<\/p>\n<pre>var value_meter = Gauges.OvalGauge.create(document.getElementById('value_meter'), false);<\/pre>\n<p><strong>III. Gauge Scales<\/strong><\/p>\n<p>Gauge scales depend on the type of the gauge. For oval gauges we use <a title=\"Charts and Gauges for JavaScript: OvalScale\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_OvalScale_1.htm\">OvalScale<\/a> The <a title=\"Charts and Gauges for JavaScript: OvalScale\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_OvalScale_Members_1.htm\">OvalScale<\/a> needs to be associated with a gauge and here is how we create it:<\/p>\n<pre>var valueScale = new Gauges.OvalScale(value_meter);\nvalueScale.setMinValue(0);\nvalueScale.setMaxValue(100);\nvalueScale.setStartAngle(120);\nvalueScale.setEndAngle(420);<\/pre>\n<p>The <a title=\"Charts and Gauges for JavaScript: OvalScale\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_OvalScale_Members_1.htm\">OvalScale<\/a> class offers the full set of properties needed to customize the scale. We use the <a title=\"Charts and Gauges for JavaScript: setMinValue\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?M_MindFusion_Gauges_BaseScale_setMinValue_1_Number_0.htm\">setMinValue<\/a> and <a title=\"Charts and Gauges for JavaScript: setMaxValue\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?M_MindFusion_Gauges_BaseScale_setMaxValue_1_Number_0.htm\">setMaxValue<\/a> methods to specify the value range o the gauge. The <a title=\"Charts and Gauges for JavaScript: setStartAngle\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?M_MindFusion_Gauges_OvalScale_setStartAngle_1_Number_0.htm\">setStartAngle<\/a> and <a title=\"Charts and Gauges for JavaScript: setEndAngle\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?M_MindFusion_Gauges_OvalScale_setEndAngle_1_Number_0.htm\">setEndAngle<\/a> specify the arc of the gauge and we set them to 120 and 420 respectively. You see that the arc is 300 degrees, which is less than a full circle &#8211; exactly how we want it to be.<\/p>\n<p>We continue our customization by setting the fill and stroke of the scale. We actually do not want the default scale to be rendered at all, so we use <a title=\"Charts and Gauges for JavaScript: setFill\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?M_MindFusion_Gauges_TickSettings_setFill_1_Object_0.htm\">setFill<\/a> and <a title=\"Charts and Gauges for JavaScript: setStroke\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?M_MindFusion_Gauges_TickSettings_setStroke_1_Object_0.htm\">setStroke<\/a> to specify transparent colors:<\/p>\n<pre>valueScale.setFill('Transparent');\nvalueScale.setStroke('Transparent');\nvalueScale.setMargin(new Gauges.Thickness(0.075, 0.075, 0.075, 0.075, true));<\/pre>\n<p>Now we can continue with the ticks. Each gauge can have major, middle and minor ticks. Those ticks are not rendered by default.<\/p>\n<pre>var majorSettings = valueScale.majorTickSettings;\nmajorSettings.setTickShape(Gauges.TickShape.Line);\nmajorSettings.setTickWidth(new Gauges.Length(10, Gauges.LengthType.Relative));\nmajorSettings.setTickHeight(new Gauges.Length(10, Gauges.LengthType.Relative));\nmajorSettings.setFontSize(new Length(14, LengthType.Relative));\nmajorSettings.setNumberPrecision(0);\nmajorSettings.setFill('rgb(46, 52, 66)');\nmajorSettings.setStroke('rgb(46, 52, 66)');\nmajorSettings.setLabelForeground('rgb(175, 175, 175)');\nmajorSettings.setLabelAlignment(Alignment.InnerCenter);\nmajorSettings.setLabelRotation(LabelRotation.Auto);\nmajorSettings.setLabelOffset(new Length(6, LengthType.Relative));\nmajorSettings.setStep(20);\nmajorSettings.setTickAlignment (Alignment.OuterOutside);<\/pre>\n<p>We start the customization with the majorTickSettings They will render labels and want to have one tick with a tep of 20. So, we use <a title=\"Charts and Gauges for JavaScript: setStep\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?M_MindFusion_Gauges_TickSettings_setStep_1_Number_0.htm\">setStep<\/a> to specify 20 as an interval and use <a title=\"Charts and Gauges for JavaScript: setTickWidth\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?M_MindFusion_Gauges_TickSettings_setTickWidth_1_Length_0.htm\">setTickWidth<\/a> and <a title=\"Charts and Gauges for JavaScript: setTickHeight\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?M_MindFusion_Gauges_TickSettings_setTickHeight_1_Length_0.htm\">setTickHeight<\/a> to set the size of the ticks. Those properties can be set to an absolute or relative value &#8211; see the <a title=\"Charts and Gauges for JavaScript: LengthType\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_LengthType_1.htm\">LengthType<\/a> enumeration. We also need to change the default shape of the pointer &#8211; we use <a title=\"Charts and Gauges for JavaScript: TickShape\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_TickShape_1.htm\">TickShape<\/a> rest of the settings are intuitive &#8211; setFill and <a title=\"Charts and Gauges for JavaScript: setStroke\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?M_MindFusion_Gauges_TickSettings_setStroke_1_Object_0.htm\">setStroke<\/a> specify how the ticks are colored. We also use <a title=\"Charts and Gauges for JavaScript: setLabelAlignment\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?M_MindFusion_Gauges_TickSettings_setLabelAlignment_1_Alignment_0.htm\">setLabelAlignment<\/a> to position the labels outside the ticks. <a title=\"Charts and Gauges for JavaScript: setTickAlignment\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?M_MindFusion_Gauges_TickSettings_setTickAlignment_1_Alignment_0.htm\">setTickAlignment<\/a> is also an important property -it allows us to change the alignment of the ticks, so they can be drawn inside the scale.<\/p>\n<p>The <a title=\"Charts and Gauges for JavaScript: TickSettings\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_MiddleTickSettings_1.htm\">TickSettings<\/a> object is similar to <a title=\"Charts and Gauges for JavaScript: MajorTickSettings\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_MajorTickSettings_Members_1.htm\">MajorTickSettings<\/a><\/p>\n<pre>var middleSettings = valueScale.middleTickSettings;\nmiddleSettings.setTickShape(TickShape.Line);\nmiddleSettings.setTickWidth(new Gauges.Length(10, Gauges.LengthType.Relative));\nmiddleSettings.setTickHeight(new Gauges.Length(10, Gauges.LengthType.Relative));\nmiddleSettings.setTickAlignment (Alignment.OuterOutside);\nmiddleSettings.setShowTicks(true);\nmiddleSettings.setShowLabels(false);\nmiddleSettings.setFill('rgb(46, 52, 66)');\nmiddleSettings.setStroke('rgb(46, 52, 66)');\nmiddleSettings.setCount(5);<\/pre>\n<p>We should note here that <a title=\"Charts and Gauges for JavaScript: setShowLabels\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?M_MindFusion_Gauges_TickSettings_setShowLabels_1_Boolean_0.htm\">setShowLabels<\/a> is false because we want the labels to appear only at intervals of 20. We also use <a title=\"Charts and Gauges for JavaScript: setCount\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?M_MindFusion_Gauges_TickSettings_setCount_1_Number_0.htm\">setCount<\/a> to specify how many ticks we want between each two adjacent major ticks. The rest of the settings are the same as for <a title=\"Charts and Gauges for JavaScript: MajorTickSettings\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_MajorTickSettings_Members_1.htm\">MajorTickSettings<\/a>.<\/p>\n<p><strong>IV. Custom Painting<\/strong><\/p>\n<p>The painting of the colored sections at the edge of the gauge is custom code. The gauges library provides various events that allow the developer to replace the default gauge drawing with custom drawing &#8211; see the <a title=\"Charts and Gauges for JavaScript: Events\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_Events_0.htm\">Events<\/a> section of the <a title=\"Charts and Gauges for JavaScript: OvalGauge\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_OvalGauge_Members_1.htm\">OvalGauge<\/a> class.<\/p>\n<p>In our sample we will handle two events:<\/p>\n<pre>value_meter.addEventListener(Gauges.Events.prepaintBackground, onPrepaintBackground.bind(this));\nvalue_meter.addEventListener(Gauges.Events.prepaintForeground, onPrepaintForeground.bind(this));<\/pre>\n<p>prepaintBackground is raised before the background is painted. We can cancel the default painting or add some custom drawing to it. The same is true for <a title=\"Charts and Gauges for JavaScript: prepaintForeground\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?F_MindFusion_Gauges_Events_prepaintForeground_0.htm\">prepaintForeground<\/a><\/p>\n<pre>function onPrepaintBackground(sender, args)\n{\n\targs.setCancelDefaultPainting(true);\n\n\tvar context = args.getContext();\n\tvar element = args.getElement();\n\tvar bounds = new d.Rect(0, 0, element.getRenderSize().width, element.getRenderSize().height);\n        ..................................\n}<\/pre>\n<p>In the <a title=\"Charts and Gauges for JavaScript: prepaintBackground\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?F_MindFusion_Gauges_Events_prepaintBackground_0.htm\">prepaintBackground<\/a> event handler we first get the handler to the CanvasRenderingContext2D instance. Then we get the bounds of the painted element. This is the inside of the gauge. Each o the colored segments is pained as an arc. We do not create a path figure to fill &#8211; instead we set a very thick lineWidth of the stroke:<\/p>\n<pre>context.lineWidth = 25;\nvar correction = context.lineWidth \/ 2;\n\t\n\/\/light green segment\ncontext.beginPath();\ncontext.strokeStyle = 'rgb(0, 205, 154)';\ncontext.arc(bounds.center().x, bounds.center().y, bounds.width \/ 2-correction, 0.665*Math.PI, 1*Math.PI, false);\t\ncontext.stroke();<\/pre>\n<p>We go on painting this way all colored sections of the gauge, only changing the start and end angles. When we are ready we paint the inside of the gauge. We do it with a full arc:<\/p>\n<pre>context.beginPath();\nbounds.x += 25;\nbounds.y += 25;\nbounds.width -= 50;\nbounds.height -= 50;\ncontext.fillStyle = '#2e3442';\n\ncontext.arc(bounds.center().x, bounds.center().y, bounds.width \/ 2, 0*Math.PI, 2*Math.PI, false);\ncontext.fill();<\/pre>\n<p>The complete drawing is done inside the <a title=\"Charts and Gauges for JavaScript: prepaintBackground\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?F_MindFusion_Gauges_Events_prepaintBackground_0.htm\">prepaintBackground<\/a> event handler. So, in the <a title=\"Charts and Gauges for JavaScript: prepaintForeground\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?F_MindFusion_Gauges_Events_prepaintForeground_0.htm\">prepaintForeground<\/a> handler we only need to cancel the default painting:<\/p>\n<pre>function onPrepaintForeground(sender, args)\n{\n    args.setCancelDefaultPainting(true);\n\n};<\/pre>\n<p><strong>V. The Gauge Pointer<\/strong><\/p>\n<p>We need to add a <a title=\"Charts and Gauges for JavaScript: Pointer\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_Pointer_Members_1.htm\">Pointer<\/a> to the <a title=\"Charts and Gauges for JavaScript: OvalScale\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_OvalScale_Members_1.htm\">OvalScale<\/a> of the gauge instance if we want to show one:<\/p>\n<pre>var pointer = new Gauges.Pointer();\npointer.setFill('white');\npointer.setStroke(\"#333333\");\n\npointer.setPointerWidth(new Gauges.Length(90, Gauges.LengthType.Relative));\npointer.setPointerHeight(new Gauges.Length(20, Gauges.LengthType.Relative));\n\npointer.setShape(Gauges.PointerShape.Needle2);\npointer.setIsInteractive(true);\n\nvalueScale.addPointer(pointer);<\/pre>\n<p>The size of the pointer is also set in <a title=\"Charts and Gauges for JavaScript: LengthType\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_LengthType_1.htm\">LengthType<\/a> units. This allows us to have the same pointer size relative to the size of the gauge even if we change the size of the Canvas. We use the <a title=\"Charts and Gauges for JavaScript: PointerShape\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_PointerShape_1.htm\">PointerShape<\/a> enumeration to specify the type of pointer we want and then we make it interactive with <a title=\"Charts and Gauges for JavaScript: setIsInteractive\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?M_MindFusion_Gauges_Pointer_setIsInteractive_1_Boolean_0.htm\">setIsInteractive<\/a> As an addition to the default needle of the pointer we want to render a circle at the base of the pointer. We do it with custom drawing:<\/p>\n<pre>value_meter.addEventListener(Gauges.Events.prepaintPointer, onPrepaintPointer.bind(this));<\/pre>\n<p>First we need to handle the <a title=\"Charts and Gauges for JavaScript: prepaintPointer\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?F_MindFusion_Gauges_Events_prepaintPointer_0.htm\">prepaintPointer<\/a> event. In the event handling code we do the drawing:<\/p>\n<pre>function onPrepaintPointer(sender, args)\n{\t\n\t\/\/args.setCancelDefaultPainting(true);\n\n\tvar context = args.getContext();\n\tvar element = args.getElement();\n\tvar size = element.getRenderSize();\n\tvar psize = new d.Size(0.2 * size.width, size.height);\n\n\tcontext.save();\n\tcontext.transform.apply(context, element.transform.matrix());\n\n\tcontext.beginPath();\n\tcontext.arc(psize.width \/ 2, psize.height \/ 2, psize.height*0.75, 0, 2 * Math.PI, false);\n\tvar fill = element.getFill();\n\tcontext.fillStyle = Gauges.Utils.getBrush(context, fill, new d.Rect(0, 0, size.width, size.height), false);\n\tcontext.fill();\n\tcontext.strokeStyle = '#333333';\n\tcontext.stroke();\n\n\tcontext.restore();\n};<\/pre>\n<p>Note that in this case we do not cancel the default painting &#8211; we will add to it, rather than replace it. Then we get the CanvasRenderingContext2D and size of the rendered element. What is new here is the transform of the CanvasRenderingContext2D object to the center of the gauge. Then we get the Brush that is used to paint the rest of the pointer and use it to fill the custom part as well. We can set the brush directly, but we prefer to take it from the base element &#8211; the <a title=\"Charts and Gauges for JavaScript: Pointer\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_Pointer_Members_1.htm\">Pointer<\/a> This way if we change settings of the <a title=\"Charts and Gauges for JavaScript: Pointer\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_Pointer_Members_1.htm\">Pointer<\/a> the color of the custom drawn circle will change automatically as well.<\/p>\n<p><strong>VI. Data Binding<\/strong><\/p>\n<p>What we would like to do now is bind a change in the text field to the value of the gauge scale. We add a method that does it:<\/p>\n<pre>function valueChanged(id)\n{\n\tif (isNaN(this.value)) return;\n\tvar gauge = Gauges.OvalGauge.find(id);\n\tvar pointer = gauge.scales[0].pointers[0];\n\tpointer.setValue(+this.value);\n};<\/pre>\n<p>When we call the valueChanged method with the instance of the <a title=\"Charts and Gauges for JavaScript: OvalGauge\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.javascript\/index.htm?T_MindFusion_Gauges_OvalGauge_Members_1.htm\">OvalGauge<\/a> as an argument, we can get its pointer and set its value to the value of &#8216;this&#8217;. We call the valueChanged in such way, that the &#8216;this&#8217; reference will be the text field:<\/p>\n<pre>var cost = document.getElementById('cost');\ncost.onchange = valueChanged.bind(cost, ['value_meter']);<\/pre>\n<p>Now when the value changes, the event handler takes the pointer and set its value to the value the user has types.<\/p>\n<p>That is the end of this tutorial. You can download the source code of the sample, together with all MindFusion libraries used from the following link:<\/p>\n<p align=\"center\"><a title=\"Value Gauge in JavaScript\" href=\"https:\/\/mindfusion.dev\/samples\/javascript\/chart\/ValueGauge.zip\">Download Value Gauge in JavaScript Source Code<\/a><\/p>\n<p>You can use the <a title=\"MindFusion JavaScrit Charts and Gauges - Discussion Board\" href=\"https:\/\/mindfusion.dev\/Forum\/YaBB.pl?board=jschart_disc\">discussion forum<\/a> to post your questions, comments and recommendations about the sample or MindFusion charts and gauges.<\/p>\n<p><i>About MindFusion JavaScript Gauges: A set of two gauge controls: oval and rectangular, with the option to add unlimited nuber of scales and gauges. All gauge elements support complete customization of their appearance. Custom drawing is also possible, where you can replace the default rendering of the gauge element or add to it. The gauge controls include a variety of samples that offer beautiful implementations of the most popular applications of gauges: thermometer, car dashboard, functions, compass, clock, cost meter and more.<br \/>\nGauges for JavaScript is part of MindFusion charts and Dashboards for JavaScript. Details at <a title=\"MindFusion JavaScript Dashboard Library\" href=\"https:\/\/mindfusion.dev\/javascript-chart.html\">https:\/\/mindfusion.dev\/javascript-chart.html<\/a>.<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we will look at the steps we need to make if we want to create this beautiful gauge below: The gauge is done with MindFusion Charts and Gauges for JavaScript library. You can download the sample together &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/cost-meter-gauge-in-javascript\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[292,513,74],"tags":[606,607,568,608,611,609,612,610],"class_list":["post-2222","post","type-post","status-publish","format-standard","hentry","category-gauges","category-javascript","category-sample-code","tag-cost-gauge","tag-cost-meter","tag-javascript-code","tag-javascript-gauge","tag-js-gauge","tag-js-gauge-library","tag-js-tutorial","tag-value-gauge"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-zQ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=2222"}],"version-history":[{"count":11,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2222\/revisions"}],"predecessor-version":[{"id":2649,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2222\/revisions\/2649"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=2222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=2222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=2222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}