{"id":17518,"date":"2015-02-20T17:25:06","date_gmt":"2015-02-20T11:55:06","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=17518"},"modified":"2015-02-26T12:34:54","modified_gmt":"2015-02-26T07:04:54","slug":"vertical-labels-with-google-chart-api","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/vertical-labels-with-google-chart-api\/","title":{"rendered":"Vertical Labels with Google Chart API"},"content":{"rendered":"<p>While working with Google charts, we usually face issues with long labels on the horizontal axis.<\/p>\n<p>The chart displays\u00a0well, however the X-axis labels are not completely\u00a0visible and displays the numbers like this:\u00a024\/3.. 25\/3.. 26\/3.. 27\/3.. 28\/3.. 30\/3.. 31\/3.. instead of<br \/>\n24\/3\/2006, 25\/3\/2006, 6\/3\/2006, 27\/3\/2006, 28\/3\/2006, 30\/3\/2006, 31\/3\/2006<\/p>\n<p>The usual practice that we all apply is that we\u00a0shorten the labels and create a legend to explain what the labels actually represent.<\/p>\n<p>This blog will help you with an alternate solution to this issue by using\u00a0vertical labels.<\/p>\n<p>Here you go:<\/p>\n<p>Step 1 : Include Google API in your head tag :<\/p>\n<p>[java]google.load(&quot;visualization&quot;, &quot;1&quot;, {packages: [&quot;corechart&quot;]});<br \/>\ngoogle.setOnLoadCallback(drawMyChart)[\/java]<\/p>\n<p>Step 2 : Create a DIV, where you want to draw your chart :<\/p>\n<p>[java]&lt;div id=&quot;myChart&quot;&gt;&lt;\/div&gt;<br \/>\n[\/java]<\/p>\n<p>Step 3 : Create a data for your ComboChart :<\/p>\n<p>[java]var data = google.visualization.arrayToDataTable([<br \/>\n   [&#8216;Month&#8217;, &#8216;Bolivia&#8217;, &#8216;Ecuador&#8217;, &#8216;Madagascar&#8217;, &#8216;Papua New Guinea&#8217;, &#8216;Rwanda&#8217;, &#8216;Average&#8217;],<br \/>\n   [&#8216;2004\/05\/01&#8217;, 165, 938, 522, 998, 450, 614.6],<br \/>\n   [&#8216;2005\/06\/01&#8217;, 135, 1120, 599, 1268, 288, 682],<br \/>\n   [&#8216;2006\/07\/01&#8217;, 157, 1167, 587, 807, 397, 623],<br \/>\n   [&#8216;2007\/08\/01&#8217;, 139, 1110, 615, 968, 215, 609.4],<br \/>\n   [&#8216;2008\/09\/01&#8217;, 136, 691, 629, 1026, 366, 569.6]<br \/>\n]);<br \/>\n[\/java]<\/p>\n<p>Step 4 : Define options for your chart :<\/p>\n<p>[java] var options = {<br \/>\n   title: &#8216;Monthly Coffee Production by Country&#8217;,<br \/>\n   height: 350,<br \/>\n   seriesType: &quot;bars&quot;,<br \/>\n   vAxis: {<br \/>\n       title: &quot;Cups&quot;<br \/>\n   },<br \/>\n   hAxis: {<br \/>\n       title: &quot;Month&quot;,<br \/>\n       slantedText: true,  \/* Enable slantedText for horizontal axis *\/<br \/>\n       slantedTextAngle: 90 \/* Define slant Angle *\/<br \/>\n   },<br \/>\n   &#8216;chartArea&#8217;: { &#8216;width&#8217;: &#8216;82%&#8217;, height: &#8216;60%&#8217;, top: &#8216;9%&#8217;, left: &#8216;15%&#8217;, right: &#8216;3%&#8217;, bottom: &#8216;0&#8217;} \/* Adjust chart alignment to fit vertical labels for horizontal axis*\/<br \/>\n};<br \/>\n[\/java]<\/p>\n<p>Add parameter options with &#8216;slantedTextAngle&#8217; : 90 degree to show label vertically<\/p>\n<p>Step 5: Add above 2 block of codes inside &#8216;drawMyChart&#8217; function, and then draw your Combo Chart:<\/p>\n<p>[java]function drawMyChart(){<br \/>\nvar data = &#8230;<br \/>\nvar options =<br \/>\n   &#8230;<br \/>\n}<\/p>\n<p>var chart = new google.visualization.ComboChart(document.getElementById(&#8216;chart_div&#8217;)); \/* Define your ComboChart object *\/<br \/>\nchart.draw(data, options); \/* draw the chart using above define &#8216;data&#8217; and &#8216;options&#8217; *\/<\/p>\n<p>}[\/java]<\/p>\n<p>The\u00a0chart looks like this now:<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/02\/vertical-labels-with-google-charts-w800.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-17533\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/02\/vertical-labels-with-google-charts-w800.png\" alt=\"vertical-labels-with-google-charts-w800\" width=\"800\" height=\"292\" \/><\/a><\/p>\n<p>Hope this helps everyone!<\/p>\n<p>Reach out to me for any queries at\u00a0mansi[at]intelligrape[dot]com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While working with Google charts, we usually face issues with long labels on the horizontal axis. The chart displays\u00a0well, however the X-axis labels are not completely\u00a0visible and displays the numbers like this:\u00a024\/3.. 25\/3.. 26\/3.. 27\/3.. 28\/3.. 30\/3.. 31\/3.. instead of 24\/3\/2006, 25\/3\/2006, 6\/3\/2006, 27\/3\/2006, 28\/3\/2006, 30\/3\/2006, 31\/3\/2006 The usual practice that we all apply is [&hellip;]<\/p>\n","protected":false},"author":79,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":48},"categories":[7],"tags":[1648,4842,55,1649,1650],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17518"}],"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\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=17518"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17518\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=17518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=17518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=17518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}