{"id":1610,"date":"2012-06-27T09:01:27","date_gmt":"2012-06-27T16:01:27","guid":{"rendered":"http:\/\/juixe.com\/techknow\/?p=1610"},"modified":"2012-06-27T09:25:42","modified_gmt":"2012-06-27T16:25:42","slug":"javascript-charting-library-jqplot-bar-graph","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2012\/06\/27\/javascript-charting-library-jqplot-bar-graph\/","title":{"rendered":"Using JavaScript Charting Library jqPlot with Bar Charts"},"content":{"rendered":"<p><a href=\"http:\/\/www.jqplot.com\/\">jqPlot<\/a> is a plugin for the jQuery JavaScript framework used to generate awesome looking charts and graphs.  Even though it has a lot of features and supports many chart types it is incredibly easy to use.  To create a simple chart you will use will use something like the following template.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n$.jqplot(divId, dataArray, optionsObject);\r\n<\/pre>\n<p>In the above example, divId is the String object containing the name of the div on which to draw the chart.  The dataArray variable will hold the data for the chart and the optionsObject will have configuration properties used to display the chart.<\/p>\n<p><strong>Getting Started<\/strong><br \/>\nTo get started with jqPlot, you will need to download the latest release.  Because jqPlot offers a wide selection of chart types, it is broken down into different JavaScript files.  You&#8217;ll only need to load the JavaScript files for the chart type you will be working with.  For example, to display a bar chart you will need to include the following JavaScript source files.<\/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.barRenderer.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script src=&quot;jqplot\/plugins\/jqplot.categoryAxisRenderer.min.js&quot;&gt;&lt;\/script&gt;\r\n<\/pre>\n<p>For the purpose of this tutorial, assume that I have a div tag that will be used to draw the chart when the the page loads.  On the page load event, a JavaScript function will fire which has the jqPlot code to draw the chart.  Here is an example of the what the div tag looks.<\/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:400px; height:300px;&quot;&gt;&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n<\/pre>\n<p>The following code for drawChart will hard code the values for a bar chart with three series for Jan, Feb, and Mar.  This could be used to track the sales of three different items over that month or other similar data.  Here is the jqPlot JavaScript code to draw a bar chart inside the chartDivId div.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction drawChart() {\r\n  var s1 = &#x5B;2, 6, 7];\r\n  var s2 = &#x5B;7, 5, 3];\r\n  var s3 = &#x5B;2, 3, 5];\r\n  var s4 = &#x5B;1, 7, 2];\r\n  \r\n  \/\/ chart data\r\n  var dataArray = &#x5B;s1, s2, s3, 24];\r\n  \r\n  \/\/ x-axis ticks\r\n  var ticks = &#x5B;'Jan', 'Feb', 'Mar'];\r\n  \r\n  \/\/ chart rendering options\r\n  var options = {\r\n    seriesDefaults: {\r\n      renderer:$.jqplot.BarRenderer\r\n    },\r\n    axes: {\r\n      xaxis: {\r\n        renderer: $.jqplot.CategoryAxisRenderer,\r\n        ticks: ticks\r\n      }\r\n    }\r\n  };\r\n\r\n  \/\/ draw the chart\r\n  $.jqplot('chartDivId', dataArray, options);\r\n}\r\n<\/pre>\n<p>For a bar chart, the chart data is an array of nested arrays holding the values each tick along the x-axis.  In the above example, we are tracking three points along the x-axis so our we have three data points for each series.  The options object is used to configure how the rendering of the chart, it specifies that this should be a bar chart and sets the x-axis.<\/p>\n<div id=\"attachment_1611\" style=\"width: 545px\" class=\"wp-caption aligncenter\"><span class=\"frame-outer  size-large wp-image-1611\"><span><span><span><span><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1611\" data-attachment-id=\"1611\" data-permalink=\"http:\/\/juixe.com\/techknow\/index.php\/2012\/06\/27\/javascript-charting-library-jqplot-bar-graph\/jqplot_bar_graph\/\" data-orig-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Bar_Graph.png\" data-orig-size=\"656,332\" 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_Bar_Graph\" data-image-description=\"&lt;p&gt;jqPlot Bar Graph&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;jqPlot Bar Graph&lt;\/p&gt;\n\" data-medium-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Bar_Graph-300x151.png\" data-large-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Bar_Graph-535x270.png\" src=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Bar_Graph-535x270.png\" alt=\"jqPlot Bar Graph\" title=\"jqPlot_Bar_Graph\" width=\"415\" height=\"209\" class=\"size-large wp-image-1611\" srcset=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Bar_Graph-535x270.png 535w, http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Bar_Graph-300x151.png 300w, http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Bar_Graph.png 656w\" sizes=\"(max-width: 535px) 100vw, 535px\" \/><\/span><\/span><\/span><\/span><\/span><p id=\"caption-attachment-1611\" class=\"wp-caption-text\">jqPlot Bar Graph<\/p><\/div>\n<p><strong>jqPlot Bar Chart Options<\/strong><br \/>\nWe can easily change this from a bar chart to a bar stacking chart by adding one property to the the options object (stackSeries: true).  By adding one line of code we can drastically change the chart we generate.  Another options you may want to set is the title, to add a title to your graph setht the title poperty.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/ chart rendering options\r\nvar options = {\r\n  stackSeries: true,\r\n  title: 'Q1 Sales',\r\n  seriesDefaults: {\r\n    renderer:$.jqplot.BarRenderer\r\n  },\r\n  axes: {\r\n    xaxis: {\r\n      renderer: $.jqplot.CategoryAxisRenderer,\r\n      ticks: ticks\r\n    }\r\n  }\r\n};\r\n<\/pre>\n<p>Another quick options you might consider enabling is the legend.  You can position the location of the legend by using the following compass directions: nw, n, ne, e, se, s, sw, w<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/ chart rendering options\r\nvar options = {\r\n  stackSeries: true,\r\n  title: 'Q1 Sales',\r\n  legend: {\r\n    show: true,\r\n    location: 'ne'\r\n  },\r\n  seriesDefaults: {\r\n    renderer:$.jqplot.BarRenderer\r\n  },\r\n  axes: {\r\n    xaxis: {\r\n      renderer: $.jqplot.CategoryAxisRenderer,\r\n      ticks: ticks\r\n    }\r\n  }\r\n};\r\n<\/pre>\n<p>You can further customize the label for each of your series by adding the series option object.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/ chart rendering options\r\nvar options = {\r\n  stackSeries: true,\r\n  title: 'Q1 Sales',\r\n  legend: {\r\n    show: true,\r\n    location: 'ne'\r\n  },\r\n  series: &#x5B;\r\n    {label: 'Memberships'},\r\n    {label: 'eBooks'},\r\n    {label: 'Conference Tickets'},\r\n    {label: 'Support'}\r\n  ],\r\n  seriesDefaults: {\r\n    renderer:$.jqplot.BarRenderer\r\n  },\r\n  axes: {\r\n    xaxis: {\r\n      renderer: $.jqplot.CategoryAxisRenderer,\r\n      ticks: ticks\r\n    }\r\n  }\r\n};\r\n<\/pre>\n<div id=\"attachment_1612\" style=\"width: 545px\" class=\"wp-caption aligncenter\"><span class=\"frame-outer  size-large wp-image-1612\"><span><span><span><span><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1612\" data-attachment-id=\"1612\" data-permalink=\"http:\/\/juixe.com\/techknow\/index.php\/2012\/06\/27\/javascript-charting-library-jqplot-bar-graph\/jqplot_stacking_bar_graph\/\" data-orig-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Stacking_Bar_Graph.png\" data-orig-size=\"661,334\" 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_Stacking_Bar_Graph\" data-image-description=\"&lt;p&gt;jqPlot Stacking Bar Graph&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;jqPlot Stacking Bar Graph&lt;\/p&gt;\n\" data-medium-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Stacking_Bar_Graph-300x151.png\" data-large-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Stacking_Bar_Graph-535x270.png\" src=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Stacking_Bar_Graph-535x270.png\" alt=\"jqPlot Stacking Bar Graph\" title=\"jqPlot_Stacking_Bar_Graph\" width=\"415\" height=\"209\" class=\"size-large wp-image-1612\" srcset=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Stacking_Bar_Graph-535x270.png 535w, http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Stacking_Bar_Graph-300x151.png 300w, http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/jqPlot_Stacking_Bar_Graph.png 661w\" sizes=\"(max-width: 535px) 100vw, 535px\" \/><\/span><\/span><\/span><\/span><\/span><p id=\"caption-attachment-1612\" class=\"wp-caption-text\">jqPlot Stacking Bar Graph<\/p><\/div>\n<p>There is a lot more that can be done with jqPlot.  As a jQuery plugin, jqPlot will easily fit into any web developer&#8217;s toolbox as the go to JavaScript charting library.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>jqPlot is a plugin for the jQuery JavaScript framework used to generate awesome looking charts and graphs. Even though it has a lot of features and supports many chart types it is incredibly easy to use. To create a simple chart you will use will use something like the following template. $.jqplot(divId, dataArray, optionsObject); In [&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":[727,721,722,333,723,373,726,99],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-pY","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1610"}],"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=1610"}],"version-history":[{"count":3,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1610\/revisions"}],"predecessor-version":[{"id":1619,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1610\/revisions\/1619"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=1610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=1610"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=1610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}