Download Example (add a counter as data)
Try these steps:
- import at least the following libraries and include them in your Section: amcharts.js, gauge.js
- click on File > Add Data > Generate counters and set the number of records to 10. Skip this if you have an actual datamapper from which you will be getting the gauge value. This example is based on a counter generated by the Designer.
- add a box\div on the page with an a css ID of #chartdiv
- add a script in the Script pane with the Selector #chartdiv an dthe following code:
var gaugeValue = '<input id="gaugeValue" value="' + parseFloat(record.fields.counter)*10 + '" hidden="">';
results.html(gaugeValue);
This script uses the parseFloat(record.fields.counter)*10 as the gauge value, if you have a field in your datamapper for this, use it instead. The purpose of this script is to create a hidden HTML element with a css ID (#gaugeValue) in the DOM which accepts a value, (the gauge value in this case). We will later search the DOM for this element (though its css ID), to get the gauge value and pass it to the function which builds the chart. I have used the <input> element but you could use any other element which accepts value as as attribute.
- Now that we have our value, add the following script to the JavaScript folder and make sure it is included as the last script in your Section. You may name the JavaScript anything you like. I have named it animatedGauge.js
var gaugeValue = document.getElementById("gaugeValue").value;
AmCharts.makeChart("chartdiv", {
"theme": "light",
"type": "gauge",
"axes": [{
"topTextFontSize": 20,
"topTextYOffset": 70,
"axisColor": "#31d6ea",
"axisThickness": 1,
"endValue": 100,
"gridInside": true,
"inside": true,
"radius": "50%",
"valueInterval": 10,
"tickColor": "#67b7dc",
"startAngle": -90,
"endAngle": 90,
"unit": "%",
"bandOutlineAlpha": 0,
"bands": [{
"color": "#0080ff",
"endValue": 100,
"innerRadius": "105%",
"radius": "170%",
"gradientRatio": [0.5, 0, -0.5],
"startValue": 0
}, {
"color": "#3cd3a3",
"endValue": 0,
"innerRadius": "105%",
"radius": "170%",
"gradientRatio": [0.5, 0, -0.5],
"startValue": 0
}]
}],
"arrows": [{
"alpha": 1,
"innerRadius": "35%",
"nailRadius": 0,
"radius": "170%",
"value": gaugeValue
}]
});
Please note that this is an animated chart and as such may not work as expected when generating print output. You may have to go into the amchart.js and gauge.js libraries to comment out the lines of code which animate the chart.