SVG Pie charts based meters
Here are three Pie charts
that have been configured to look like gauges
showing percentage values. They're all regular Pie charts
with two colors but then a
big black circle is added over the top of them which makes them look like donut charts
.
And finally, the text is added to the center using the draw
event.
There are also three Horizontal Progress bars
placed at the bottom of the svg
drawing area. These progress bars are actually Horizontal Bar charts
that
are configured with just a single data point so that there's just one bar -
which makes them look like progress bars. You can make Vertical Progress
bars in the same way by using the regular Bar chart
.
Like the canvas
chart libraries, you can make other types of
charts using the regular svg
chart libraries and combining
them together. Eg a combined Bar chart
and Line chart
.
<script src="RGraph.svg.common.core.js"></script> <script src="RGraph.svg.hbar.js"></script> <script src="RGraph.svg.pie.js"></script>Put this where you want the chart to show up:
<div> <div id="chart-container" style="background-color: black; width: 610px; height:325px"></div> </div>This is the code that generates the chart - it should be placed AFTER the
div
tag:
<script> // Create the first Pie chart. It uses the roundRobin() // effect and is configured to appear as a donut chart. // With there being only two values given to the chart // it makes the donut chart look like a circular Progress // bar. pie1 = new RGraph.SVG.Pie({ id: 'chart-container', data: [15,85], options: { centerx: 500, centery: 100, radius: 90, colors: ['red', 'transparent'], donut: true, donutWidth: 20 } }).on('draw', function (obj) { // This serves as the background for the chart. Just // setting the second color of the Pie chart to gray // gives a different style compared to adding this // circle as the 'background'. RGraph.SVG.create({ svg: obj.svg, type: 'circle', parent: obj.layers.background1, attr: { cx: obj.centerx, cy: obj.centery, r: obj.radius, fill: 'gray' } }); // This smaller black circle turns the gray circle that // was just added into a donut. RGraph.SVG.create({ svg: obj.svg, type: 'circle', parent: obj.layers.background1, attr: { cx: obj.centerx, cy: obj.centery, r: obj.radius - obj.properties.donutWidth, fill: 'black' } }); // Add the text label that indicates the 'value' of the // first Pie segment RGraph.SVG.text({ object: obj, parent: obj.svg.all, text: obj.data[0] + '%', x: obj.centerx, y: obj.centery, halign: 'center', valign: 'center', size: 40, bold: true, color: '#999' }); // Add the smaller text label that sits under the main percentage label. RGraph.SVG.text({ object: obj, parent: obj.svg.all, text: 'Charles', x: obj.centerx, y: obj.centery + 20, halign: 'center', valign: 'top', size: 16, bold: true, color: '#999' }); // Use the roundRobin() effect for the red Pie segment. }).roundRobin({frames: 45}); // Add the second 'meter' that shows Rickys progress pie2 = new RGraph.SVG.Pie({ id: 'chart-container', data: [50,50], options: { centerx: 300, centery: 100, radius: 90, colors: ['red', 'transparent'], donut: true, donutWidth: 20 } }).on('draw', function (obj) { RGraph.SVG.create({ svg: obj.svg, type: 'circle', parent: pie1.layers.background1, attr: { cx: obj.centerx, cy: obj.centery, r: obj.radius, fill: 'gray' } }); RGraph.SVG.create({ svg: obj.svg, type: 'circle', parent: pie1.layers.background1, attr: { cx: obj.centerx, cy: obj.centery, r: obj.radius - obj.properties.donutWidth, fill: 'black' } }); // Add the text label that indicates the 'value' of the // second Pie segment RGraph.SVG.text({ object: obj, parent: obj.svg.all, text: obj.data[0] + '%', x: obj.centerx, y: obj.centery, halign: 'center', valign: 'center', size: 40, bold: true, color: '#999' }); // Add the smaller text label that sits under the main // percentage label. RGraph.SVG.text({ object: obj, parent: obj.svgAllElements, text: 'Ricky', x: obj.centerx, y: obj.centery + 20, halign: 'center', valign: 'top', size: 16, bold: true, color: '#999' }); }).roundRobin({frames: 45}); // Add the third 'meter' that shows Freddys progress pie3 = new RGraph.SVG.Pie({ id: 'chart-container', data: [42,58], options: { centerx: 100, centery: 100, radius: 90, colors: ['red', 'transparent'], donut: true, donutWidth: 20 } }).on('draw', function (obj) { RGraph.SVG.create({ svg: obj.svg, type: 'circle', parent: pie1.layers.background1, attr: { cx: obj.centerx, cy: obj.centery, r: obj.radius, fill: 'gray' } }); RGraph.SVG.create({ svg: obj.svg, type: 'circle', parent: pie1.layers.background1, attr: { cx: obj.centerx, cy: obj.centery, r: obj.radius - obj.properties.donutWidth, fill: 'black' } }); // Add the text label that indicates the 'value' of the // third Pie segment RGraph.SVG.text({ object: obj, parent: obj.svg.all, text: obj.data[0] + '%', x: obj.centerx, y: obj.centery, halign: 'center', valign: 'center', size: 40, bold: true, color: '#999' }); // Add the smaller text label that sits under the main percentage label. RGraph.SVG.text({ object: obj, parent: obj.svg.all, text: 'Freddy', x: obj.centerx, y: obj.centery + 20, halign: 'center', valign: 'top', size: 16, bold: true, color: '#999' }); }).roundRobin({frames: 45}); // This is the common configuration for the red progress bars conf = { xaxis: false, xaxisScale: false, xaxisScaleMax: 100, xaxisLabels: false, yaxis: false, colors: ['red'], marginTop: 40, marginBottom: 5, backgroundGrid: false }; // These Horizontal Bar charts become the gray backgrounds for the progress bars new RGraph.SVG.HBar({id: 'chart-container', data: [100], options: {...conf, marginTop: 200,marginBottom: 95, colors: ['gray']}}).draw(); new RGraph.SVG.HBar({id: 'chart-container', data: [100], options: {...conf, marginTop: 240,marginBottom: 55, colors: ['gray']}}).draw(); new RGraph.SVG.HBar({id: 'chart-container', data: [100], options: {...conf, marginTop: 280,marginBottom: 15, colors: ['gray']}}).draw(); // These Horizontal Bar charts become the red foregrounds for the progress bars new RGraph.SVG.HBar({id: 'chart-container', data: [42], options: {...conf, marginTop: 200,marginBottom: 95, colors: ['red']}}).grow(); new RGraph.SVG.HBar({id: 'chart-container', data: [50], options: {...conf, marginTop: 240,marginBottom: 55, colors: ['red']}}).grow(); new RGraph.SVG.HBar({id: 'chart-container', data: [15], options: {...conf, marginTop: 280,marginBottom: 15, colors: ['red']}}).grow(); </script>