{"id":34885,"date":"2016-04-07T06:00:06","date_gmt":"2016-04-07T13:00:06","guid":{"rendered":"http:\/\/webdesignledger.com\/?p=34885"},"modified":"2016-04-22T12:00:41","modified_gmt":"2016-04-22T19:00:41","slug":"coding-radar-chart-jquery","status":"publish","type":"post","link":"https:\/\/webdesignledger.com\/coding-radar-chart-jquery\/","title":{"rendered":"Coding a Radar Chart in jQuery"},"content":{"rendered":"<p id=\"coding-a-radar-chart-in-jquery\">If you are into front-end development, you can\u2019t escape from two things &#8211; jQuery and data visualization. jQuery is one of the most popular JavaScript libraries which is being used by more than 70% of top one million websites (<a href=\"https:\/\/libscore.com\/#jQuery\">source<\/a>). And with the increasing amount of data, sooner or later you will have to get your hands dirty with making charts.<\/p>\n<p>That\u2019s why this article combines both &#8211; jQuery and creating charts. I am going to walk you through the process of creating beautiful charts using <a href=\"http:\/\/www.fusioncharts.com\/\">FusionCharts<\/a> and its <a href=\"http:\/\/www.fusioncharts.com\/jquery-charts\/\">jQuery charts<\/a> plugin. Although I am going to make a radar chart, you can use this process to make any other chart that is part of FusionCharts\u2019 library of 90+ charts as well.<\/p>\n<h2 id=\"creating-a-radar-chart-4-step-process\">Creating a Radar Chart &#8211; 4 Step Process<\/h2>\n<p>I have divided the process of making our chart into four easy-to-follow steps. But before we begin, here is a quick look into what we are trying to make:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-34886 lazyload\" data-src=\"https:\/\/storage.googleapis.com\/webdesignledger.pub.network\/WDL\/2016\/04\/radar-chart.png\" alt=\"radar-chart\" width=\"1260\" height=\"980\" data-srcset=\"https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/04\/radar-chart.png 1260w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/04\/radar-chart-500x389.png 500w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/04\/radar-chart-768x597.png 768w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/04\/radar-chart-1024x796.png 1024w\" data-sizes=\"(max-width: 1260px) 100vw, 1260px\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" style=\"--smush-placeholder-width: 1260px; --smush-placeholder-aspect-ratio: 1260\/980;\" \/><noscript><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-34886\" src=\"https:\/\/storage.googleapis.com\/webdesignledger.pub.network\/WDL\/2016\/04\/radar-chart.png\" alt=\"radar-chart\" width=\"1260\" height=\"980\" srcset=\"https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/04\/radar-chart.png 1260w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/04\/radar-chart-500x389.png 500w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/04\/radar-chart-768x597.png 768w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/04\/radar-chart-1024x796.png 1024w\" sizes=\"(max-width: 1260px) 100vw, 1260px\" \/><\/noscript><\/p>\n<p>You can see the live version at <a href=\"http:\/\/jsfiddle.net\/LzLwptLt\/embedded\/result\/\">this JSFiddle<\/a>. To view the source-code, click the \u2018Edit in JSFiddle\u2019 button on top right of the screen.<\/p>\n<h3 id=\"step-1-get-the-data\">Step-1: Get the Data<\/h3>\n<p>This is usually the first step for making any kind of chart or graph. For the purpose of this tutorial, I am going to to use dummy data representing allocated budget and actual spending of a company (Acme Inc.).<\/p>\n<p>FusionCharts accepts both JSON and XML data formats. I am going to use JSON as it has now become the standard data exchange format of the web. First we need to define a <code>category<\/code> array which will contain all the categories under which budget was allocated. It will be an array of objects like this:<\/p>\n<pre class=\"prettyprint\"><code class=\" hljs cs\"><span class=\"hljs-string\">\"categories\"<\/span>: [{\r\n  <span class=\"hljs-string\">\"category\"<\/span>: [{\r\n      <span class=\"hljs-string\">\"label\"<\/span>: <span class=\"hljs-string\">\"Engineering\"<\/span>\r\n    }, {\r\n      <span class=\"hljs-string\">\"label\"<\/span>: <span class=\"hljs-string\">\"Sales\"<\/span>\r\n    }\r\n    <span class=\"hljs-comment\">\/\/ more categories<\/span>\r\n  ]\r\n}]<\/code><\/pre>\n<p>Next we need to put the data that we want to plot in <code>dataset<\/code> array. Inside <code>dataset<\/code> you can have one object for each series of data you want to plot. In our case we have two series &#8211; <code>Allocated Budget<\/code> and <code>Actual Spend<\/code>.<\/p>\n<pre class=\"prettyprint\"><code class=\" hljs haskell\"><span class=\"hljs-string\">\"dataset\"<\/span>: [{\r\n  <span class=\"hljs-string\">\"seriesname\"<\/span>: <span class=\"hljs-string\">\"Allocated Budget\"<\/span>,\r\n  <span class=\"hljs-string\">\"data\"<\/span>: [{\r\n      <span class=\"hljs-string\">\"value\"<\/span>: <span class=\"hljs-string\">\"10000\"<\/span>\r\n    }, {\r\n      <span class=\"hljs-string\">\"value\"<\/span>: <span class=\"hljs-string\">\"16500\"<\/span>\r\n    }\r\n    \/\/ more <span class=\"hljs-typedef\"><span class=\"hljs-keyword\">data<\/span> points for first series<\/span>\r\n  ]\r\n}, {\r\n  <span class=\"hljs-string\">\"seriesname\"<\/span>: <span class=\"hljs-string\">\"Actual Spend\"<\/span>,\r\n  <span class=\"hljs-string\">\"data\"<\/span>: [{\r\n      <span class=\"hljs-string\">\"value\"<\/span>: <span class=\"hljs-string\">\"8000\"<\/span>\r\n    }, {\r\n      <span class=\"hljs-string\">\"value\"<\/span>: <span class=\"hljs-string\">\"9500\"<\/span>\r\n    }\r\n    \/\/ more <span class=\"hljs-typedef\"><span class=\"hljs-keyword\">data<\/span> points for first series<\/span>\r\n  ]\r\n}]<\/code><\/pre>\n<h3 id=\"step-2-include-dependencies\">Step-2: Include Dependencies<\/h3>\n<p>Our project is dependent on following JavaScript files:<\/p>\n<ul>\n<li>jQuery &#8211; download it from <a href=\"http:\/\/jquery.com\/download\/\">here<\/a> or include it via CDN.<\/li>\n<li>3 FusionCharts JS files: <code>fusioncharts.js<\/code>, <code>fusioncharts.charts.js<\/code> and <code>powercharts.js<\/code>. You can download all the files from <a href=\"http:\/\/www.fusioncharts.com\/download\/\">this page<\/a> (see under JS folder).<\/li>\n<li>jQuery charts plugin &#8211; download it from <a href=\"http:\/\/www.fusioncharts.com\/jquery-charts\/\">plugin page<\/a>.<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\" hljs xml\"><span class=\"hljs-comment\">&lt;!-- jQuery --&gt;<\/span>\r\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">script<\/span> <span class=\"hljs-attribute\">type<\/span>=<span class=\"hljs-value\">\"text\/javascript\"<\/span> <span class=\"hljs-attribute\">src<\/span>=<span class=\"hljs-value\">\"jquery.js\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-title\">script<\/span>&gt;<\/span>\r\n\r\n<span class=\"hljs-comment\">&lt;!-- FusionCharts files--&gt;<\/span>\r\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">script<\/span> <span class=\"hljs-attribute\">type<\/span>=<span class=\"hljs-value\">\"text\/javascript\"<\/span> <span class=\"hljs-attribute\">src<\/span>=<span class=\"hljs-value\">\"fusioncharts.js\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-title\">script<\/span>&gt;<\/span>\r\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">script<\/span> <span class=\"hljs-attribute\">type<\/span>=<span class=\"hljs-value\">\"text\/javascript\"<\/span> <span class=\"hljs-attribute\">src<\/span>=<span class=\"hljs-value\">\"fusioncharts.charts.js\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-title\">script<\/span>&gt;<\/span>\r\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">script<\/span> <span class=\"hljs-attribute\">type<\/span>=<span class=\"hljs-value\">\"text\/javascript\"<\/span> <span class=\"hljs-attribute\">src<\/span>=<span class=\"hljs-value\">\"powercharts.js\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-title\">script<\/span>&gt;<\/span>\r\n\r\n<span class=\"hljs-comment\">&lt;!-- jQuery plugin --&gt;<\/span>\r\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">script<\/span> <span class=\"hljs-attribute\">type<\/span>=<span class=\"hljs-value\">\"text\/javascript\"<\/span> <span class=\"hljs-attribute\">src<\/span>=<span class=\"hljs-value\">\"jquery-plugin.js\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-title\">script<\/span>&gt;<\/span><\/code><\/pre>\n<h3 id=\"step-3-create-and-select-a-chart-container\">Step-3: Create and Select a Chart Container<\/h3>\n<p>HTML <code>&lt;div&gt;<\/code> elements work best for housing a chart. You should have a separate <code>&lt;div&gt;<\/code> container for each chart on you page. This is how we define a container and select it via jQuery\u2019s <code>$<\/code> selector:<\/p>\n<p>HTML:<\/p>\n<pre class=\"prettyprint\"><code class=\" hljs livecodeserver\">&lt;<span class=\"hljs-operator\">div<\/span> id=<span class=\"hljs-string\">\"radar-chart\"<\/span>&gt;Radar chart will <span class=\"hljs-built_in\">load<\/span> here.&lt;\/<span class=\"hljs-operator\">div<\/span>&gt;<\/code><\/pre>\n<p>Selecting it via jQuery:<\/p>\n<pre class=\"prettyprint\"><code class=\" hljs ruby\"><span class=\"hljs-variable\">$(<\/span><span class=\"hljs-string\">\"#radar-chart\"<\/span>)<\/code><\/pre>\n<h3 id=\"step-4-insert-the-chart\">Step-4: Insert the Chart<\/h3>\n<p>We are almost there. Now we just need to use the <code>insertFusionCharts<\/code> method provided by the plugin to insert the chart into our page. Here is how we do it (explanation after code snippet):<\/p>\n<pre class=\"prettyprint\"><code class=\" hljs ruby\"><span class=\"hljs-variable\">$(<\/span><span class=\"hljs-string\">\"#radar-chart\"<\/span>).insertFusionCharts({\r\n  <span class=\"hljs-symbol\">type:<\/span> <span class=\"hljs-string\">'radar'<\/span>,\r\n  <span class=\"hljs-symbol\">width:<\/span> <span class=\"hljs-string\">'100%'<\/span>,\r\n  <span class=\"hljs-symbol\">height:<\/span> <span class=\"hljs-string\">'500'<\/span>,\r\n  <span class=\"hljs-symbol\">dataFormat:<\/span> <span class=\"hljs-string\">'json'<\/span>,\r\n  <span class=\"hljs-symbol\">dataSource:<\/span> {\r\n    <span class=\"hljs-string\">\"chart\"<\/span><span class=\"hljs-symbol\">:<\/span> {\r\n      <span class=\"hljs-string\">\"caption\"<\/span><span class=\"hljs-symbol\">:<\/span> <span class=\"hljs-string\">\"2015 Budget for Acme Inc.\"<\/span>,\r\n      <span class=\"hljs-string\">\"captionFontSize\"<\/span><span class=\"hljs-symbol\">:<\/span> <span class=\"hljs-string\">\"22\"<\/span>,\r\n      <span class=\"hljs-regexp\">\/\/<\/span> more chart configuration options\r\n    },\r\n    <span class=\"hljs-string\">\"categories\"<\/span><span class=\"hljs-symbol\">:<\/span> [\r\n      <span class=\"hljs-regexp\">\/\/<\/span> explained <span class=\"hljs-keyword\">in<\/span> step-<span class=\"hljs-number\">1<\/span>\r\n    ],\r\n    <span class=\"hljs-string\">\"dataset\"<\/span><span class=\"hljs-symbol\">:<\/span> [\r\n      <span class=\"hljs-regexp\">\/\/<\/span> explained <span class=\"hljs-keyword\">in<\/span> step-<span class=\"hljs-number\">1<\/span>\r\n    ]\r\n  }\r\n});<\/code><\/pre>\n<p>Most of the terms in the above code snippet are self-explanatory, so I will keep the description short and to the point:<\/p>\n<ul>\n<li><code>type<\/code> sets the type of chart we want to plot.<\/li>\n<li><code>width<\/code> and <code>height<\/code> define the dimension of the chart.<\/li>\n<li><code>dataFormat<\/code> sets the format in which we will pass the data (json or xml).<\/li>\n<li><code>dataSource<\/code> contains chart configuration options and the data we want to plot. Chart configuration options will go inside <code>chart<\/code> object and are technically known as <a href=\"http:\/\/www.fusioncharts.com\/dev\/chart-attributes.html\">chart attributes<\/a> (in FusionCharts). <code>dataset<\/code> and <code>categories<\/code> arrays were covered in step-1.<\/li>\n<\/ul>\n<h2 id=\"quick-note-on-improving-design\">Quick Note on Improving Design<\/h2>\n<p>If you notice the above code snippet carefully, you will see that our <code>chart<\/code> object only had two chart attributes, while the source code for the chart has more than 20!<\/p>\n<p>What is happening here?<\/p>\n<p>Well, in reality there are more than 100 different attributes you can use to customize a chart. Since it is not possible to cover everything, I am going to leave you with a resource that will be immensely helpful if you try to customize a chart.<\/p>\n<p>Open <a href=\"http:\/\/www.fusioncharts.com\/dev\/chart-attributes.html\">this page<\/a> and type the name of the chart you want to customize. It will open up complete list of attributes that can be used on that chart, along with their short descriptions and acceptable values. For example, to customize a radar chart you will reference <a href=\"http:\/\/www.fusioncharts.com\/dev\/chart-attributes.html?chart=radar\">radar chart\u2019s page<\/a>.<\/p>\n<p>That\u2019s it! If you were following along and did everything I described above, you must have got a working chart by now. If not, head over to the <a href=\"http:\/\/jsfiddle.net\/LzLwptLt\/\">JSFiddle demo<\/a> I created and take few mins to understand where you went wrong.<\/p>\n<p>Have any questions? Leave a comment below and I will be glad to help!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are into front-end development, you can\u2019t escape from two things &#8211; jQuery and data visualization. jQuery is one of the most popular JavaScript libraries which is being used by more than 70% of top one million websites (source). And with the increasing amount of data, sooner or later you will have to get [&hellip;]<\/p>\n","protected":false},"author":1883,"featured_media":34886,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[476,377,378,73,491,11],"tags":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/posts\/34885"}],"collection":[{"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/users\/1883"}],"replies":[{"embeddable":true,"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/comments?post=34885"}],"version-history":[{"count":0,"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/posts\/34885\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/media\/34886"}],"wp:attachment":[{"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/media?parent=34885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/categories?post=34885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/tags?post=34885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}