{"id":26503,"date":"2016-05-24T23:01:24","date_gmt":"2016-05-24T15:01:24","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=26503"},"modified":"2018-04-09T17:00:02","modified_gmt":"2018-04-09T09:00:02","slug":"svg-meter-gauge-tutorial","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/","title":{"rendered":"How to Make an Animated SVG Speedometer"},"content":{"rendered":"<p>A <strong>gauge meter<\/strong> is a tool that visually indicates a value within a given range. In computers, a \u201cdisk space indicator\u201d uses a gauge meter to show how much disk space is used from the total available. Gauges have zones or regions across its range, each differentiated by its own color. In front-end development, we can use the <code><a href=\"https:\/\/www.hongkiat.com\/blog\/style-html5-meter\/\" target=\"_blank\">&lt;meter&gt;<\/a><\/code> HTML5 tag to display data within a specific range.<\/p>\n<p>In this post, we\u2019ll make an <strong>SVG gauge meter of semi-circular shape<\/strong>, and animate it. Take a look at this preview GIF that shows <strong>how the final version will work in Firefox<\/strong>:<\/p>\n<p>The meter\u2019s <strong>range is 0-100<\/strong>, and it displays <strong>three equal zones<\/strong> in yellow, blue and red. You can change the range and the number of the zones according to your needs.<\/p>\n<p>For explanation purposes, I\u2019ll perform manual calculations, and use inline SVG attributes\/properties in the following steps.<\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/svg-meter-gauge-tutorial\/svg-meter-gif.gif\" alt=\"Demo GIF\"><\/figure>\n<p>My final demo, however, uses CSS and JavaScript for the calculation and insertion of SVG properties in order to make it more flexible.<\/p>\n<h2>1. Draw a circle<\/h2>\n<p>Let\u2019s draw a simple circle in SVG. HTML5\u2019s new <code><a href=\"https:\/\/www.w3schools.com\/html\/html5_svg.asp\" target=\"_blank\">&lt;svg&gt;<\/a><\/code> tag allows us to add SVG right into the HTML code. Inside the <code>&lt;svg&gt;<\/code> tag, we add the <code><a href=\"https:\/\/www.w3schools.com\/svg\/svg_circle.asp\" target=\"_blank\">&lt;circle&gt;<\/a><\/code> SVG shape like this:<\/p>\n<pre>\r\n&lt;div id=\"wrapper\"&gt;\r\n  &lt;svg id=\"meter\"&gt;\r\n    &lt;circle r=\"150\" cx=\"50%\" cy=\"50%\"&gt;&lt;\/circle&gt;\r\n  &lt;\/svg&gt;\r\n&lt;\/div&gt;<\/pre>\n<figure><img decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/svg-meter-gauge-tutorial\/svg-meter-draw-a-circle.jpg\" alt=\"A Circle\"><\/figure>\n<p>In CSS, let\u2019s add <code>width<\/code> and <code>height<\/code> properties to the wrapper, both greater than or equal to the diameter of the circle (it\u2019s 300px in our example). We also need to set the width and height of the <code>#meter<\/code> element to 100%.<\/p>\n<pre>\r\n#wrapper{\r\n  width: 400px;\r\n  height: 400px;\r\n}\r\n\r\n#meter{\r\n  width: 100%;\r\n  height: 100%;\r\n}<\/pre>\n<h2>2. Add outline to the circle and remove fill<\/h2>\n<p>With the help of the <a href=\"https:\/\/www.w3schools.com\/svg\/svg_stroking.asp\" target=\"_blank\" rel=\"nofollow\"><code>stroke<\/code> and <code>stroke-width<\/code><\/a> SVG properties we add an outline to the circle, and by using the <code>fill=\"none\"<\/code> property we remove the circle\u2019s fill as well.<\/p>\n<pre>\r\n&lt;circle id=\"low\" r=\"150\" cx=\"50%\" cy=\"50%\" stroke=\"#FDE47F\"\r\nstroke-width=\"60\" fill=\"none\"&gt;&lt;\/circle&gt;<\/pre>\n<figure><img decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/svg-meter-gauge-tutorial\/svg-meter-draw-outline.jpg\" alt=\"A Circle\"><\/figure>\n<h2>3. cover only half of the circle<\/h2>\n<p>The <a href=\"https:\/\/developer.mozilla.org\/en\/docs\/Web\/SVG\/Attribute\/stroke-dasharray\" target=\"_blank\"><code>stroke-dasharray<\/code><\/a> SVG property creates a dashed outline, and takes two values, <code>dash length<\/code> and <code>gap length<\/code>.<\/p>\n<p>For the semi-circle outline, the <code>dash length<\/code> value needs be equal to the circle\u2019s semi-circumference, so that the dash covers half of the circle\u2019s circumference, and the <code>gap length<\/code> value needs be either equal to or more than the remaining circumference.<\/p>\n<p>When it\u2019s more, it will be converted to the remaining circumference by the browser, hence we will use the full circumference value for the <code>gap length<\/code>. This way we can avoid calculating the remaining circumference.<\/p>\n<p>Let\u2019s see the calculations:<\/p>\n<p><math><mrow><mi>circumference<\/mi> <mo>=<\/mo> <mn>2<\/mn> <mo>\u00c3-<\/mo> <mi>\u03c0<\/mi> <mo>\u00d7<\/mo> <mi>r<\/mi><\/mrow><\/math><\/p>\n<p>where <strong>r<\/strong> is the radius. For a radius of 150, the circumference is:<\/p>\n<p><math><mtable><mtr><mrow><mi>circumference<\/mi> <mo>=<\/mo> <mn>2<\/mn> <mo>\u00c3-<\/mo> <mi>\u03c0<\/mi> <mo>\u00d7<\/mo> <mi>150<\/mi><\/mrow><\/mtr><mtr><mrow><mi>circumference<\/mi> <mo>=<\/mo> <mn>942.48<\/mn><\/mrow><\/mtr><\/mtable><\/math><\/p>\n<p>If we divide it by 2, we get 471.24 for semi-circumference, so the value of the <code>stroke-dasharray<\/code> property for a semi-circle outline in a 150 radius circle is <code>471, 943<\/code>. This semi circle will be used to denote the Low Range Zone of the meter.<\/p>\n<pre>\r\n&lt;!-- Low Range Zone (Yellow) --&gt;\r\n&lt;circle id=\"low\" r=\"150\" cx=\"50%\" cy=\"50%\" stroke=\"#FDE47F\"\r\nstroke-width=\"60\" stroke-dasharray=\"471, 943\" fill=\"none\"&gt;\r\n&lt;\/circle&gt;<\/pre>\n<figure><img decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/svg-meter-gauge-tutorial\/svg-meter-semi-circle-upside-down.jpg\" alt=\"An upside down semi-circle\"><\/figure>\n<p>As can you see, it\u2019s upside down, so let\u2019s turn the SVG up by adding the <code><a href=\"https:\/\/www.w3schools.com\/cssref\/css3_pr_transform.asp\" target=\"_blank\">transform<\/a><\/code> CSS property with the value of <code>rotateX(180deg)<\/code> to the <code>&lt;svg id=\"meter\"&gt;<\/code> HTML element.<\/p>\n<pre>\r\n#meter {\r\n  transform: rotateX(180deg);\r\n}<\/pre>\n<figure><img decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/svg-meter-gauge-tutorial\/svg-meter-semi-circle.jpg\" alt=\"A semi-circle\"><\/figure>\n<h2>4. Add the other zones<\/h2>\n<p>The <strong>middle zone<\/strong> (blue) has to cover the \u2154 portion of the semi-circle, and \u2154  of 471 is 314. So, let\u2019s add another circle to our SVG by using the <code>stroke-dasharray<\/code> property again, but now with the value of <code>314, 943<\/code>.<\/p>\n<pre>\r\n&lt;!-- Low Range Zone (Yellow) --&gt;\r\n&lt;circle id=\"low\" r=\"150\" cx=\"50%\" cy=\"50%\" stroke=\"#FDE47F\"\r\nstroke-width=\"60\" stroke-dasharray=\"471, 943\" fill=\"none\"&gt;&lt;\r\n\/circle&gt;\r\n\r\n&lt;!-- Average Range Zone (Blue) --&gt;\r\n&lt;circle id=\"avg\" r=\"150\" cx=\"50%\" cy=\"50%\" stroke=\"#7CCCE5\"\r\nstroke-width=\"60\" stroke-dasharray=\"314, 943\"\r\nfill=\"none\"&gt;&lt;\/circle&gt;<\/pre>\n<figure><img decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/svg-meter-gauge-tutorial\/svg-meter-avg-zone.jpg\" alt=\"avg zone\"><\/figure>\n<p>The <strong>final zone<\/strong> (red) has to cover the last \u2153 part of the semi-circle, and \u2153 of 471 is 157, so we will add this value to the <code>stroke-dasharray<\/code> property of the third circle.<\/p>\n<pre>\r\n&lt;!-- Low Range Zone (Yellow) --&gt;\r\n&lt;circle id=\"low\" r=\"150\" cx=\"50%\" cy=\"50%\" stroke=\"#FDE47F\"\r\nstroke-width=\"60\" stroke-dasharray=\"471, 943\" fill=\"none\"&gt;\r\n&lt;\/circle&gt;\r\n\r\n&lt;!-- Average Range Zone (Blue) --&gt;\r\n&lt;circle id=\"avg\" r=\"150\" cx=\"50%\" cy=\"50%\" stroke=\"#7CCCE5\"\r\nstroke-width=\"60\" stroke-dasharray=\"314, 943\" fill=\"none\"&gt;\r\n&lt;\/circle&gt;\r\n\r\n&lt;!-- High Range Zone (Red) --&gt;\r\n&lt;circle id=\"high\" r=\"150\" cx=\"50%\" cy=\"50%\" stroke=\"#E04644\"\r\nstroke-width=\"60\" stroke-dasharray=\"157, 943\" fill=\"none\"&gt;\r\n&lt;\/circle&gt;<\/pre>\n<figure><img decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/svg-meter-gauge-tutorial\/svg-meter-hight-sone.jpg\" alt=\"high zone\"><\/figure>\n<h2>5. Add The Meter Outline<\/h2>\n<p>Let\u2019s add a grey outline to the meter in order to make it look better. The <code>dash length<\/code> of the outline circle needs to be equal to the semi-circumference. We  place it before all the other circles in the code, so that it will be <strong>rendered first by the browser<\/strong>, and therefore will be <strong>displayed beneath the region circles<\/strong> on the screen.<\/p>\n<p>The <code>stroke-width<\/code> property needs to be a little bit greater than that of the other circles, in order to give the appearance of a real outline.<\/p>\n<pre>\r\n&lt;!-- Meter Outline (Grey) --&gt;\r\n&lt;circle id=\"outline_curves\" r=\"150\" cx=\"50%\" cy=\"50%\"\r\nstroke=\"#f6f6f6\" stroke-width=\"65\"\r\nstroke-dasharray=\"471, 943\" fill=\"none\"&gt;\r\n&lt;\/circle&gt;\r\n\r\n&lt;!-- Low Range Zone (Yellow) --&gt;\r\n&lt;circle id=\"low\" r=\"150\" cx=\"50%\" cy=\"50%\" stroke=\"#FDE47F\"\r\nstroke-width=\"60\" stroke-dasharray=\"471, 943\" fill=\"none\"&gt;&lt;\r\n\/circle&gt;\r\n\r\n&lt;!-- Average Range Zone (Blue) --&gt;\r\n&lt;circle id=\"avg\" r=\"150\" cx=\"50%\" cy=\"50%\" stroke=\"#7CCCE5\"\r\nstroke-width=\"60\" stroke-dasharray=\"314, 943\" fill=\"none\"&gt;\r\n&lt;\/circle&gt;\r\n\r\n&lt;!-- High Range Zone (Red) --&gt;\r\n&lt;circle id=\"high\" r=\"150\" cx=\"50%\" cy=\"50%\" stroke=\"#E04644\"\r\nstroke-width=\"60\" stroke-dasharray=\"157, 943\" fill=\"none\"&gt;\r\n&lt;\/circle&gt;<\/pre>\n<p><strong>Outline Ends<\/strong><\/p>\n<p>As the outline doesn\u2019t cover the ends of the semi-circle, we also add 2 lines of about 2px to the ends by adding another circle with a <code>dash length<\/code> of 2px and a <code>gap length<\/code> of the semi-circumference minus 2px. Therefore the value of the <code>stroke-dasharray<\/code> property of this circle is <code>2, 469<\/code>.<\/p>\n<pre>\r\n&lt;!-- Outline Ends (Grey) --&gt;\r\n&lt;circle id=\"outline_ends\" r=\"150\" cx=\"50%\" cy=\"50%\" stroke=\"#f9f9f9\"\r\nstroke-width=\"65\" stroke-dasharray=\"2, 469\" fill=\"none\"&gt;&lt;\/circle&gt;<\/pre>\n<p><strong>Mask<\/strong><\/p>\n<p>Now let\u2019s add another circle after the low, average, and high range zones. The new circle will function as a mask to hide the unnecessary zone regions when the gauge meter will be operated.<\/p>\n<p>Its properties will be the same as those of the outline circle, and its stroke color will also be grey. The mask will later be resized with Javascript to reveal the zones beneath it in response to an input slider.<\/p>\n<p>The combined code so far is as below.<\/p>\n<pre>\r\n&lt;!-- Meter Outline --&gt;\r\n&lt;circle id=\"outline_curves\" r=\"150\" cx=\"50%\" cy=\"50%\"\r\nstroke=\"#f6f6f6\" stroke-width=\"65\"\r\nstroke-dasharray=\"471, 943\" fill=\"none\"&gt;\r\n&lt;\/circle&gt;\r\n\r\n&lt;!-- Low Range Zone (Yellow) --&gt;\r\n&lt;circle id=\"low\" r=\"150\" cx=\"50%\" cy=\"50%\" stroke=\"#FDE47F\"\r\nstroke-width=\"60\" stroke-dasharray=\"471, 943\" fill=\"none\"&gt;\r\n&lt;\/circle&gt;\r\n\r\n&lt;!-- Average Range Zone (Blue) --&gt;\r\n&lt;circle id=\"avg\" r=\"150\" cx=\"50%\" cy=\"50%\" stroke=\"#7CCCE5\"\r\nstroke-width=\"60\" stroke-dasharray=\"314, 943\" fill=\"none\"&gt;\r\n&lt;\/circle&gt;\r\n\r\n&lt;!-- High Range Zone (Red) --&gt;\r\n&lt;circle id=\"high\"  r=\"150\" cx=\"50%\" cy=\"50%\" stroke=\"#E04644\"\r\nstroke-width=\"60\" stroke-dasharray=\"157, 943\" fill=\"none\"&gt;\r\n&lt;\/circle&gt;\r\n\r\n&lt;!-- Mask --&gt;\r\n&lt;circle id=\"mask\" r=\"150\" cx=\"50%\" cy=\"50%\" stroke=\"#f6f6f6\"\r\nstroke-width=\"65\" stroke-dasharray=\"471, 943\" fill=\"none\"&gt;\r\n&lt;\/circle&gt;\r\n\r\n&lt;!-- Outline Ends --&gt;\r\n&lt;circle id=\"outline_ends\" r=\"150\" cx=\"50%\" cy=\"50%\"\r\nstroke=\"#f9f9f9\" stroke-width=\"65\" stroke-dasharray=\"2, 469\"\r\nfill=\"none\"&gt;\r\n&lt;\/circle&gt;<\/pre>\n<figure><img decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/svg-meter-gauge-tutorial\/svg-meter-masked.jpg\" alt=\"masked meter\"><\/figure>\n<p>If we want to reveal a region under the mask, we need to reduce the size of the mask\u2019s <code>dash length<\/code>. For instance, when the value of the <code>stroke-dasharray<\/code> property of the mask circle is <code>157, 943<\/code>, the arcs will stand in the following state:<\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/svg-meter-gauge-tutorial\/svg-meter-partial-mask.jpg\" alt=\"masked meter\"><\/figure>\n<p>So, all we have to do now is to adjust the <code>stroke-dasharray<\/code> of the mask using JavaScript for animation. But before we do that, as I mentioned before, for my final demo I used CSS and JavaScript to calculate and add most of the SVG properties.<\/p>\n<p>Below you can find the HTML, CSS, and JavaScript code that leads to the same result as above.<\/p>\n<h2>HTML<\/h2>\n<p>I added a needle image (<code>gauge-needle.svg<\/code>), a range slider (<code>input#slider<\/code>) to the user input, and a label (<code>label#lbl<\/code>) to display the slider value in the range of 0-100.<\/p>\n<pre>&lt;div id=\"wrapper\"&gt;\r\n    &lt;svg id=\"meter\"&gt;\r\n        &lt;circle id=\"outline_curves\" class=\"circle outline\"  \r\n        cx=\"50%\" cy=\"50%\"&gt;&lt;\/circle&gt;\r\n        \r\n        &lt;circle id=\"low\" class=\"circle range\" cx=\"50%\" cy=\"50%\" \r\n        stroke=\"#FDE47F\"&gt;&lt;\/circle&gt;\r\n        \r\n        &lt;circle id=\"avg\" class=\"circle range\" cx=\"50%\" cy=\"50%\" \r\n        stroke=\"#7CCCE5\"&gt;&lt;\/circle&gt;\r\n        \r\n        &lt;circle id=\"high\" class=\"circle range\" cx=\"50%\" cy=\"50%\" \r\n        stroke=\"#E04644\"&gt;&lt;\/circle&gt;\r\n        \r\n        &lt;circle id=\"mask\" class=\"circle\" cx=\"50%\" cy=\"50%\" &gt;\r\n        &lt;\/circle&gt;\r\n        \r\n        &lt;circle id=\"outline_ends\" class=\"circle outline\" \r\n        cx=\"50%\" cy=\"50%\"&gt;&lt;\/circle&gt;\r\n    &lt;\/svg&gt;\r\n    &lt;img id=\"meter_needle\" src=\"gauge-needle.svg\" alt=\"\"&gt;\r\n    &lt;input id=\"slider\" type=\"range\" min=\"0\" max=\"100\" value=\"0\" \/&gt;\r\n    &lt;label id=\"lbl\" id=\"value\" for=\"\"&gt;0&lt;\/label&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<h2>CSS<\/h2>\n<p>The CSS code below adds style rules to the SVG, as SVG shapes can be styled the same way as HTML elements. If you want to read more on how to style SVG with CSS, take a look at <a href=\"https:\/\/www.hongkiat.com\/blog\/scalable-vector-graphic-css-styling\/\" target=\"_blank\">this post<\/a>. For styling the slider, check out <a href=\"https:\/\/www.hongkiat.com\/blog\/html5-range-slider-style\/\" target=\"_blank\">this post<\/a>.<\/p>\n<pre>\r\n#wrapper {\r\n  position: relative;\r\n  margin: auto;\r\n}\r\n#meter {\r\n  width: 100%; height: 100%;\r\n  transform: rotateX(180deg);\r\n}\r\n.circle {\r\n  fill: none;\r\n}\r\n.outline, #mask {\r\n  stroke: #F1F1F1;\r\n  stroke-width: 65;\r\n}\r\n.range {\r\n  stroke-width: 60;\r\n}\r\n#slider, #lbl {\r\n  position: absolute;\r\n}\r\n#slider {\r\n  cursor: pointer;\r\n  left: 0;\r\n  margin: auto;\r\n  right: 0;\r\n  top: 58%;\r\n  width: 94%;\r\n}\r\n#lbl {\r\n  background-color: #4B4C51;\r\n  border-radius: 2px;\r\n  color: white;\r\n  font-family: 'courier new';\r\n  font-size: 15pt;\r\n  font-weight: bold;\r\n  padding: 4px 4px 2px 4px;\r\n  right: -48px;\r\n  top: 57%;\r\n}\r\n#meter_needle {\r\n  height: 40%;\r\n  left: 0;\r\n  margin: auto;\r\n  position: absolute;\r\n  right: 0;\r\n  top: 10%;\r\n  transform-origin: bottom center;\r\n  \/*orientation fix*\/\r\n  transform: rotate(270deg);\r\n}<\/pre>\n<h2>JavaScript<\/h2>\n<p>In the JavaScript, first we calculate and set the dimensions of the wrapper and all arcs, then we add the appropriate <code>stroke-dasharray<\/code> values to the circles. After that, we will be binding a custom event to the range slider in order to perform the animation.<\/p>\n<pre>\r\n\/* Set radius for all circles *\/\r\nvar r = 250;\r\nvar circles = document.querySelectorAll('.circle');\r\nvar total_circles = circles.length;\r\nfor (var i = 0; i &lt; total_circles; i++) {\r\n    circles[i].setAttribute('r', r);\r\n}\r\n\r\n\/* Set meter's wrapper dimension *\/\r\nvar meter_dimension = (r * 2) + 100;\r\nvar wrapper = document.querySelector(\"#wrapper\");\r\nwrapper.style.width = meter_dimension + \"\";\r\nwrapper.style.height = meter_dimension + \"\";\r\n\r\n\/* Add strokes to circles  *\/\r\nvar cf = 2 * Math.PI * r;\r\nvar semi_cf = cf \/ 2;\r\nvar semi_cf_1by3 = semi_cf \/ 3;\r\nvar semi_cf_2by3 = semi_cf_1by3 * 2;\r\ndocument.querySelector(\"#outline_curves\")\r\n    .setAttribute(\"stroke-dasharray\", semi_cf + \",\" + cf);\r\ndocument.querySelector(\"#low\")\r\n    .setAttribute(\"stroke-dasharray\", semi_cf + \",\" + cf);\r\ndocument.querySelector(\"#avg\")\r\n    .setAttribute(\"stroke-dasharray\", semi_cf_2by3 + \",\" + cf);\r\ndocument.querySelector(\"#high\")\r\n    .setAttribute(\"stroke-dasharray\", semi_cf_1by3 + \",\" + cf);\r\ndocument.querySelector(\"#outline_ends\")\r\n    .setAttribute(\"stroke-dasharray\", 2 + \",\" + (semi_cf - 2));\r\ndocument.querySelector(\"#mask\")\r\n    .setAttribute(\"stroke-dasharray\", semi_cf + \",\" + cf);\r\n\r\n\/* Bind range slider event*\/\r\nvar slider = document.querySelector(\"#slider\");\r\nvar lbl = document.querySelector(\"#lbl\");\r\nvar mask = document.querySelector(\"#mask\");\r\nvar meter_needle =  document.querySelector(\"#meter_needle\");\r\n\r\nfunction range_change_event() {\r\n    var percent = slider.value;\r\n    var meter_value = semi_cf - ((percent * semi_cf) \/ 100);\r\n    mask.setAttribute(\"stroke-dasharray\", meter_value + \",\" + cf);\r\n    meter_needle.style.transform = \"rotate(\" + \r\n        (270 + ((percent * 180) \/ 100)) + \"deg)\";\r\n    lbl.textContent = percent + \"%\";\r\n}\r\nslider.addEventListener(\"input\", range_change_event);\r\n<\/pre>\n<h2>The Custom <code>range_change_event()<\/code> Function<\/h2>\n<p>The behaviour of the meter is performed by the <code>range_change_event()<\/code> custom function that is responsible for the adjustment of the mask size and the animation of the needle.<\/p>\n<p>It takes the slider value (user input) which\u2019s between 0-100, converts it to the semi-circumference equivalent (<code>meter_value<\/code>) of a value between 471-0 (471 is the semi-circumference for radius 150), and sets that <code>meter_value<\/code> as the <code>dash length<\/code> of the mask\u2019s <code>stroke-dasharray<\/code> property.<\/p>\n<p>The <code>range_change_event()<\/code> custom function also rotates the needle after converting the user input (coming in the 0-100 range) to its degree equivalent of 0-180.<\/p>\n<p>270\u00b0 is added to the needle\u2019s rotation in the above code because the image I used is of an upright needle and I had to initially rotate it 270\u00b0 to make it lie flat to its left.<\/p>\n<p>Finally, I bound the <code>range_change_event()<\/code> function to the range slider, so that the gauge meter can be operated with it.<\/p>\n<p>Check out the <a href=\"https:\/\/hongkiat.github.io\/svg-meter-gauge\/\" target=\"_blank\"><strong>demo<\/strong><\/a> or take a look at the source code at our <a href=\"https:\/\/github.com\/hongkiat\/svg-meter-gauge\/\" target=\"_blank\"><strong>Github repository<\/strong><\/a>.<\/p>\n<p class=\"note\"><strong>Read more: <\/strong><a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/svg-animations\/\">Click here to check out more SVG animations<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>A gauge meter is a tool that visually indicates a value within a given range. In computers, a \u201cdisk space indicator\u201d uses a gauge meter to show how much disk space is used from the total available. Gauges have zones or regions across its range, each differentiated by its own color. In front-end development, we&hellip;<\/p>\n","protected":false},"author":145,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3392],"tags":[507,3498,4501],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Create an SVG Gauge Meter Animation<\/title>\n<meta name=\"description\" content=\"A gauge meter is a tool that visually indicates a value within a given range. In computers, a &quot;disk space indicator&quot; uses a gauge meter to show how much\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Make an Animated SVG Speedometer\" \/>\n<meta property=\"og:description\" content=\"A gauge meter is a tool that visually indicates a value within a given range. In computers, a &quot;disk space indicator&quot; uses a gauge meter to show how much\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2016-05-24T15:01:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-04-09T09:00:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/assets.hongkiat.com\/uploads\/svg-meter-gauge-tutorial\/svg-meter-gif.gif\" \/>\n<meta name=\"author\" content=\"Preethi Ranjit\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Preethi Ranjit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/svg-meter-gauge-tutorial\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/svg-meter-gauge-tutorial\\\/\"},\"author\":{\"name\":\"Preethi Ranjit\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\"},\"headline\":\"How to Make an Animated SVG Speedometer\",\"datePublished\":\"2016-05-24T15:01:24+00:00\",\"dateModified\":\"2018-04-09T09:00:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/svg-meter-gauge-tutorial\\\/\"},\"wordCount\":1143,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/svg-meter-gauge-tutorial\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/svg-meter-gauge-tutorial\\\/svg-meter-gif.gif\",\"keywords\":[\"CSS\",\"CSS Animation\",\"CSS Tutorials\"],\"articleSection\":[\"Coding\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/svg-meter-gauge-tutorial\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/svg-meter-gauge-tutorial\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/svg-meter-gauge-tutorial\\\/\",\"name\":\"How to Create an SVG Gauge Meter Animation\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/svg-meter-gauge-tutorial\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/svg-meter-gauge-tutorial\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/svg-meter-gauge-tutorial\\\/svg-meter-gif.gif\",\"datePublished\":\"2016-05-24T15:01:24+00:00\",\"dateModified\":\"2018-04-09T09:00:02+00:00\",\"description\":\"A gauge meter is a tool that visually indicates a value within a given range. In computers, a \\\"disk space indicator\\\" uses a gauge meter to show how much\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/svg-meter-gauge-tutorial\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/svg-meter-gauge-tutorial\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/svg-meter-gauge-tutorial\\\/#primaryimage\",\"url\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/svg-meter-gauge-tutorial\\\/svg-meter-gif.gif\",\"contentUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/svg-meter-gauge-tutorial\\\/svg-meter-gif.gif\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/svg-meter-gauge-tutorial\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Make an Animated SVG Speedometer\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\",\"name\":\"Preethi Ranjit\",\"description\":\"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/preethi\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Create an SVG Gauge Meter Animation","description":"A gauge meter is a tool that visually indicates a value within a given range. In computers, a \"disk space indicator\" uses a gauge meter to show how much","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"How to Make an Animated SVG Speedometer","og_description":"A gauge meter is a tool that visually indicates a value within a given range. In computers, a \"disk space indicator\" uses a gauge meter to show how much","og_url":"https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2016-05-24T15:01:24+00:00","article_modified_time":"2018-04-09T09:00:02+00:00","og_image":[{"url":"https:\/\/assets.hongkiat.com\/uploads\/svg-meter-gauge-tutorial\/svg-meter-gif.gif","type":"","width":"","height":""}],"author":"Preethi Ranjit","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Preethi Ranjit","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/"},"author":{"name":"Preethi Ranjit","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3"},"headline":"How to Make an Animated SVG Speedometer","datePublished":"2016-05-24T15:01:24+00:00","dateModified":"2018-04-09T09:00:02+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/"},"wordCount":1143,"commentCount":2,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/svg-meter-gauge-tutorial\/svg-meter-gif.gif","keywords":["CSS","CSS Animation","CSS Tutorials"],"articleSection":["Coding"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/","url":"https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/","name":"How to Create an SVG Gauge Meter Animation","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/svg-meter-gauge-tutorial\/svg-meter-gif.gif","datePublished":"2016-05-24T15:01:24+00:00","dateModified":"2018-04-09T09:00:02+00:00","description":"A gauge meter is a tool that visually indicates a value within a given range. In computers, a \"disk space indicator\" uses a gauge meter to show how much","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/#primaryimage","url":"https:\/\/assets.hongkiat.com\/uploads\/svg-meter-gauge-tutorial\/svg-meter-gif.gif","contentUrl":"https:\/\/assets.hongkiat.com\/uploads\/svg-meter-gauge-tutorial\/svg-meter-gif.gif"},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/svg-meter-gauge-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Make an Animated SVG Speedometer"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3","name":"Preethi Ranjit","description":"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.","url":"https:\/\/www.hongkiat.com\/blog\/author\/preethi\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-6Tt","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/26503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/145"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=26503"}],"version-history":[{"count":3,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/26503\/revisions"}],"predecessor-version":[{"id":39195,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/26503\/revisions\/39195"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=26503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=26503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=26503"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=26503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}