{"id":1620,"date":"2012-06-29T09:01:02","date_gmt":"2012-06-29T16:01:02","guid":{"rendered":"http:\/\/juixe.com\/techknow\/?p=1620"},"modified":"2012-06-28T19:38:50","modified_gmt":"2012-06-29T02:38:50","slug":"pie-charts-jqplot-javascript-charting-library","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2012\/06\/29\/pie-charts-jqplot-javascript-charting-library\/","title":{"rendered":"Pie Charts with jqPlot JavaScript Charting Library"},"content":{"rendered":"<p>It&#8217;s incredibly easy to generate great looking pie charts with <a href=\"http:\/\/juixe.com\/techknow\/index.php\/2012\/06\/26\/javascript-charting-library\/\">jqPlot<\/a>.  jqPlot is a jQuery plugin that can be use to generate pure <strong>HTML5 charts<\/strong>.  Create pie charts in jqPlot is as easy as it is to create bar charts, the only difference is how we pack the data.<\/p>\n<p><strong>Getting Started<\/strong><br \/>\nThe code snippets for this example assume you have already downloaded jqPlot and have included the following JavaScript and CSS source files from the distribution.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;jqplot\/jquery.jqplot.min.css&quot; \/&gt;\r\n&lt;script src=&quot;jqplot\/jquery.min.js&quot; &gt;&lt;\/script&gt;\r\n&lt;script src=&quot;jqplot\/jquery.jqplot.min.js&quot; &gt;&lt;\/script&gt;    \r\n&lt;script src=&quot;jqplot\/plugins\/jqplot.pieRenderer.min.js&quot;&gt;&lt;\/script&gt;\r\n<\/pre>\n<p>The jqPlot graphs are drawn on a div tag and this example will require the following HTML snippet.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;body onload='drawChart()'&gt;\r\n   &lt;div id=&quot;chartDivId&quot; style=&quot;margin-top:20px; margin-left:20px; width:600px; height:300px;&quot;&gt;&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n<\/pre>\n<p><strong>Pie Chart Data<\/strong><br \/>\nA single slice of a pie chart is stored in a two element array where the first elemnt contains the string label and the second element stores the slice data value.  A single pie series is made up of one ore more slices, and a pie chart may have one or more series.  The typical pie chart is made up of one series, but there are some charts that can allow for multiple pie series.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction drawChart() {\r\n  var slice_1 = &#x5B;'North America', 150];\r\n  var slice_2 = &#x5B;'Europe', 50];\r\n  var series = &#x5B;slice_1, slice_2];\r\n  var data = &#x5B;series];\r\n\r\n  var options = {\r\n    title: 'Sales by Region',\r\n    seriesDefaults: {\r\n      renderer: jQuery.jqplot.PieRenderer\r\n    },\r\n    legend: { show:true, location: 'e' }\r\n  };\r\n  \r\n  $.jqplot('chartDivId', data, options);\r\n}\r\n<\/pre>\n<p>jqPlot will all compute the percent for each slice based on it&#8217;s value, so the slice for North America will be 75%, and Europe will be 25% for the the values 150 and 50, respectively.  The pie chart generated from the above code will be simple and undecorated.<\/p>\n<div id=\"attachment_1621\" style=\"width: 545px\" class=\"wp-caption aligncenter\"><span class=\"frame-outer  size-large wp-image-1621\"><span><span><span><span><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1621\" data-attachment-id=\"1621\" data-permalink=\"http:\/\/juixe.com\/techknow\/index.php\/2012\/06\/29\/pie-charts-jqplot-javascript-charting-library\/jqplot_pie_chart\/\" data-orig-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Pie_Chart.png\" data-orig-size=\"629,410\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"jqPlot Pie Chart\" data-image-description=\"&lt;p&gt;jqPlot Pie Chart&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;jqPlot Pie Chart&lt;\/p&gt;\n\" data-medium-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Pie_Chart-300x195.png\" data-large-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Pie_Chart-535x348.png\" src=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Pie_Chart-535x348.png\" alt=\"jqPlot Pie Chart\" title=\"jqPlot Pie Chart\" width=\"415\" height=\"270\" class=\"size-large wp-image-1621\" srcset=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Pie_Chart-535x348.png 535w, http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Pie_Chart-300x195.png 300w, http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Pie_Chart.png 629w\" sizes=\"(max-width: 535px) 100vw, 535px\" \/><\/span><\/span><\/span><\/span><\/span><p id=\"caption-attachment-1621\" class=\"wp-caption-text\">jqPlot Pie Chart<\/p><\/div>\n<p>We can add additional options, for example, maybe we want to display the percent amount for each slice.  To do so, we can update the options object with the showDataLabels property such as the in the following JavaScript code snippet. <\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar options = {\r\n  title: 'Sales by Region',\r\n  seriesDefaults: {\r\n    renderer: jQuery.jqplot.PieRenderer,\r\n    rendererOptions: {\r\n      showDataLabels: true\r\n    }\r\n  },\r\n  legend: { show:true, location: 'e' }\r\n};\r\n<\/pre>\n<p>By default, the showDataLabels property shows the percent of the slice.  To show the slice data value, rather than the percent, set the dataLabels property to &#8216;value&#8217; such as the following code.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar options = {\r\n  title: 'Sales by Region',\r\n  seriesDefaults: {\r\n    renderer: jQuery.jqplot.PieRenderer,\r\n    rendererOptions: {\r\n      showDataLabels: true,\r\n      dataLabels: 'value'\r\n    }\r\n  },\r\n  legend: { show:true, location: 'e' }\r\n};\r\n<\/pre>\n<p>You can further change how the pie chart is rendered by setting the fill, sliceMargin, and lineWith rendererOptions object properties.  The fill property accepts a boolean value, and you can specify the sliceMargin and lineWith with an interger value.  You can also specify the angle from which to start with the startAngle property.  Putting all this together, you can create a pie chart like the following.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar options = {\r\n  title: 'Sales by Region',\r\n  seriesDefaults: {\r\n    renderer: jQuery.jqplot.PieRenderer,\r\n    rendererOptions: {\r\n      showDataLabels: true,\r\n      dataLabels: 'value',\r\n      fill: false,\r\n      sliceMargin: 5,\r\n      lineWidth: 5,\r\n      startAngle: 45\r\n    }\r\n  },\r\n  legend: { show:true, location: 'e' }\r\n};\r\n<\/pre>\n<div id=\"attachment_1622\" style=\"width: 545px\" class=\"wp-caption aligncenter\"><span class=\"frame-outer  size-large wp-image-1622\"><span><span><span><span><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1622\" data-attachment-id=\"1622\" data-permalink=\"http:\/\/juixe.com\/techknow\/index.php\/2012\/06\/29\/pie-charts-jqplot-javascript-charting-library\/jqplot_pie_chart_no_fill\/\" data-orig-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Pie_Chart_No_Fill.png\" data-orig-size=\"627,411\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"jqPlot_Pie_Chart with No Fill\" data-image-description=\"&lt;p&gt;jqPlot_Pie_Chart with No Fill&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;jqPlot_Pie_Chart with No Fill&lt;\/p&gt;\n\" data-medium-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Pie_Chart_No_Fill-300x196.png\" data-large-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Pie_Chart_No_Fill-535x350.png\" src=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Pie_Chart_No_Fill-535x350.png\" alt=\"jqPlot_Pie_Chart with No Fill\" title=\"jqPlot_Pie_Chart with No Fill\" width=\"415\" height=\"271\" class=\"size-large wp-image-1622\" srcset=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Pie_Chart_No_Fill-535x350.png 535w, http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Pie_Chart_No_Fill-300x196.png 300w, http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Pie_Chart_No_Fill.png 627w\" sizes=\"(max-width: 535px) 100vw, 535px\" \/><\/span><\/span><\/span><\/span><\/span><p id=\"caption-attachment-1622\" class=\"wp-caption-text\">jqPlot_Pie_Chart with No Fill<\/p><\/div>\n<p><strong>Donut Chart<\/strong><br \/>\nUsing the same data and same options object, you can render a pie chart as a donut.  To display a donut chart you will need to load int the jqPlot donut renderer.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;script src=&quot;jqplot\/plugins\/jqplot.donutRenderer.min.js&quot;&gt;&lt;\/script&gt;\r\n<\/pre>\n<p>Once you have loaded the donut renderer, just update the render property in the options object.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar options = {\r\n  title: 'Sales by Region',\r\n  seriesDefaults: {\r\n    renderer: jQuery.jqplot.DonutRenderer,\r\n    rendererOptions: {\r\n      showDataLabels: true,\r\n      dataLabels: 'value',\r\n      fill: false,\r\n      sliceMargin: 5,\r\n      startAngle: 45\r\n    }\r\n  },\r\n  legend: { show:true, location: 'e' }\r\n};\r\n<\/pre>\n<div id=\"attachment_1623\" style=\"width: 545px\" class=\"wp-caption aligncenter\"><span class=\"frame-outer  size-large wp-image-1623\"><span><span><span><span><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1623\" data-attachment-id=\"1623\" data-permalink=\"http:\/\/juixe.com\/techknow\/index.php\/2012\/06\/29\/pie-charts-jqplot-javascript-charting-library\/jqplot_donut_chart\/\" data-orig-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Donut_Chart.png\" data-orig-size=\"632,411\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"jqPlot Donut Chart\" data-image-description=\"&lt;p&gt;jqPlot Donut Chart&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;jqPlot Donut Chart&lt;\/p&gt;\n\" data-medium-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Donut_Chart-300x195.png\" data-large-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Donut_Chart-535x347.png\" src=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Donut_Chart-535x347.png\" alt=\"jqPlot Donut Chart\" title=\"jqPlot Donut Chart\" width=\"415\" height=\"269\" class=\"size-large wp-image-1623\" srcset=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Donut_Chart-535x347.png 535w, http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Donut_Chart-300x195.png 300w, http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Donut_Chart.png 632w\" sizes=\"(max-width: 535px) 100vw, 535px\" \/><\/span><\/span><\/span><\/span><\/span><p id=\"caption-attachment-1623\" class=\"wp-caption-text\">jqPlot Donut Chart<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s incredibly easy to generate great looking pie charts with jqPlot. jqPlot is a jQuery plugin that can be use to generate pure HTML5 charts. Create pie charts in jqPlot is as easy as it is to create bar charts, the only difference is how we pack the data. Getting Started The code snippets for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[13,16,3],"tags":[728,147,722,723,373,729,804,99,343],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-q8","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1620"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=1620"}],"version-history":[{"count":4,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1620\/revisions"}],"predecessor-version":[{"id":1627,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1620\/revisions\/1627"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=1620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=1620"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=1620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}