{"id":10681,"date":"2013-09-05T16:16:27","date_gmt":"2013-09-05T10:46:27","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=10681"},"modified":"2017-08-03T14:01:44","modified_gmt":"2017-08-03T08:31:44","slug":"creating-doughnut-pie-chart-with-google-chart-api-using-it-with-angular-js-directive","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/creating-doughnut-pie-chart-with-google-chart-api-using-it-with-angular-js-directive\/","title":{"rendered":"Creating DOUGHNUT Pie Chart with Google Chart API &amp; using it with Angular.js Directive"},"content":{"rendered":"<p><span style=\"color: #000000;\">Showing data in the form of Graphical\/Pictorial Representation is always a better way to present data than simple text. Users find data represented in pictorial manner more interactive and easy to understand than data in complex tables or lists.<\/span><\/p>\n<p><span style=\"color: #000000;\">Cases where Pictorial data representation (Charts or Graphs) can be more user friendly:<\/span><\/p>\n<p><span style=\"color: #000000;\">1. If you want to show the number of hits on your website using a BAR or LINE CHART representation.<\/span><\/p>\n<p><span style=\"color: #000000;\">2. If you have personalized pages for users to show how active they have been.<\/span><\/p>\n<p><span style=\"color: #000000;\">and last but not the least, the scenario which intrigued me most to write this blog is:<\/span><\/p>\n<p><span style=\"color: #000000;\">3. To represent a user profile completeness in the form of Doughnut Chart\u00a0(a category of pie chart) using real time data.<\/span><\/p>\n<p><span style=\"color: #000000;\">In this blog, we will be discussing the last point i.e., creating doughnut charts for real time data representation; though other two points are as important as well.<\/span><\/p>\n<p><span style=\"color: #000000;\">To accomplish this task, Google has provided developers with an API known as <strong>Google Chart API<\/strong> which gives developer an option to make charts in either of the two very simple ways:<\/span><\/p>\n<p><span style=\"color: #000000;\">1. <strong>Using\u00a0JS\u00a0<\/strong>: Copy the JS provided by Google API, make changes as per your requirement, supply data and its ready to use.(\u00a0<\/span><span style=\"color: #000000;\"><a href=\"https:\/\/developers.google.com\/chart\/interactive\/docs\/gallery\">https:\/\/developers.google.com\/chart\/interactive\/docs\/gallery<\/a>\u00a0)<\/span><\/p>\n<p><span style=\"color: #000000;\">2. <strong>Using URL<\/strong>: Generate an URL fro Google API, \u00a0pass it in the image tag &lt;img&gt;\u00a0and you will get the chart image supplied directly from Google.<\/span>(\u00a0<span style=\"color: #000000;\"><a href=\"https:\/\/developers.google.com\/chart\/image\/docs\/making_charts\">https:\/\/developers.google.com\/chart\/image\/docs\/making_charts<\/a>\u00a0)<\/span><\/p>\n<p><span style=\"color: #000000;\">The main difference between them are: the chart provided by JS way gives you an interactive chart, displaying data associated with the chart on events like hover or click on the chart; whereas chart provided by &#8220;URL way&#8221; is in the form of images.<\/span><br \/>\n<span style=\"color: #000000;\">Now lets turn our focus towards the &#8216;URL way&#8217; of getting the chart image from Google API:<\/span><\/p>\n<p><span style=\"color: #000000;\">Basic URL format which is to be used:<\/span><\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #ff6600;\">;<em>chart_type<\/em>&gt;&amp;chd=&lt;<em>chart_data<\/em>&gt;&amp;chs=<\/span><em><span style=\"color: #ff6600;\">&lt;chart_size&gt;&amp;<\/span> ...additional_parameters<\/em>...<\/span><\/pre>\n<p><span style=\"color: #000000;\">For my project, I have used:<\/span><\/p>\n<pre><span style=\"color: #ff6600;\"><a href=\"https:\/\/chart.googleapis.com\/chart?cht=pc&amp;chd=t:-1|-1|-1|-1|70,30&amp;chco=FFFFFF,FFFFFF,FFFFFF,FFFFFF,74D3EF|EDEDED&amp;chs=122x122&amp;chp=4.7\"><span style=\"color: #ff6600;\">https:\/\/chart.googleapis.com\/chart?cht=pc&amp;chd=t:-1|-1|-1|-1|70,30&amp;chco=FFFFFF,FFFFFF,FFFFFF,FFFFFF,74D3EF|EDEDED&amp;chs=122x122&amp;chp=4.7<\/span><\/a><\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">In this case, we do not want a simple pie chart, instead a pie chart which is blank from center; forming a &#8216;Doughnut&#8217; shape. To get the same, we will make a few changes in current URL.<\/span><\/p>\n<ul>\n<li><strong>cht<\/strong>\u00a0: specifies the chart type as PieChart<\/li>\n<li><strong>chd<\/strong>\u00a0: specifies that there will be 5 concentric circles in the pie chart. Here -1 tells that there is no data to be supplied so here first 4 circles starting from the center have no data. The last 70,30 shows the data we want to show, in my case the sum of the pair will always result in 100.<\/li>\n<li><strong>chco<\/strong>\u00a0: specifies the color of each field supplied in the chd field. Here I have given the color of 4 innermost concentric circles as white and the outermost as the pair of two colors representing 70,30 of the chd field.<\/li>\n<li><strong>chs<\/strong>\u00a0: specifies the dimensions of the image you want.<\/li>\n<li><strong>chp<\/strong>\u00a0: specifies the starting angle of the chart, by default it starts from 3 o&#8217;clock mark.<\/li>\n<\/ul>\n<p><span style=\"color: #000000;\">Here, I have modified the &#8220;chd&#8221; and &#8220;chco&#8221; parameters to give you a better understanding of &#8220;chd&#8221;.\u00a0After making all the changes, our URL modifies to:<\/span><\/p>\n<pre><span style=\"color: #ff6600;\"><span style=\"color: #ff6600;\"><a href=\"https:\/\/chart.googleapis.com\/chart?cht=pc&amp;chd=t:150,50|70,30|80,20|40,60|70,30&amp;chco=FFFFFF,EEEEEE,DDDDDD,CCCCCC,74D3EF|EDEDED&amp;chs=122x122&amp;chp=4.7\">https:\/\/chart.googleapis.com\/chart?cht=pc&amp;chd=t:150,50|70,30|80,20|40,60|70,30&amp;chco=FFFFFF,EEEEEE,DDDDDD,CCCCCC,74D3EF|EDEDED&amp;chs=122x122&amp;chp=4.7<\/a><\/span><\/span><\/pre>\n<p><span style=\"color: #000000;\">As we have URL of pie chart now, lets get our focus on the directive part :<\/span><\/p>\n<p><span style=\"color: #000000;\">To include this URL in your project and use it to display charts on different pages, you can create a directive and add the same anywhere in html and you got it all working.<\/span><\/p>\n<p><span style=\"color: #000000;\">Lets have a look at the sample code:<\/span><\/p>\n<p>[js].directive(&#8216;userProfileRating&#8217;, function () {<br \/>\nreturn{<br \/>\nrestrict: &#8216;A&#8217;,<br \/>\nreplace: true,<br \/>\ntemplate: &#8216;&amp;lt;img style=&quot;margin:-12px&quot; src=&quot;https:\/\/chart.googleapis.com\/chart?cht=pc&amp;amp;chco=FFFFFF,FFFFFF,FFFFFF,FFFFFF,74D3EF|EDEDED&amp;amp;chd=t:-1|-1|-1|-1|70,30&amp;amp;chs=122&#215;122&amp;amp;chp=4.7&quot;\/&amp;gt;&#8217;<br \/>\n}<br \/>\n });[\/js]<\/p>\n<p><span style=\"color: #000000;\">One more thing to notice is that the URL which we were using, uses &#8216;&amp;&#8217; but when we have to use that URL in &lt;img&gt; tag we will replace &#8216;&amp;&#8217; with &#8216;&amp;amp;&#8217; to make it work successfully.<\/span><\/p>\n<p><span style=\"color: #000000;\">Thanks for reading.<\/span><\/p>\n<p><a href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Building Intuitive Frontend Interfaces with AngularJS<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Showing data in the form of Graphical\/Pictorial Representation is always a better way to present data than simple text. Users find data represented in pictorial manner more interactive and easy to understand than data in complex tables or lists. Cases where Pictorial data representation (Charts or Graphs) can be more user friendly: 1. If you [&hellip;]<\/p>\n","protected":false},"author":86,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[1439,3429],"tags":[3955,1186,4697,1189,1187,1188,1124],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/10681"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/86"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=10681"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/10681\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=10681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=10681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=10681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}