Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hAxis label is cut off in Google chart

Tags:

To track number of visitor comes through which website and do some analysis on the same. We are creating a column chart to show the analysis report in graphical form.

All the things are showing correctly on chart, but we are showing website name on haxis. As website name is too long like "www.google.com", www.facebook.com, this label are being cut off on haxis.

Code to draw chart is given below:

function createTodayChart(chartData){   var data = new google.visualization.DataTable();   data.addColumn('string', 'Sources');   data.addColumn('number', 'Total Sales');    for (var i in chartData){     //alert(chartData[i][0]+'=>'+ parseInt(chartData[i][1]));     data.addRow([chartData[i][0], parseInt(chartData[i][1])]);   }    var options = {     legend: {position:'top'},     hAxis: {title: 'Sources', titleTextStyle: {color: 'black'}, count: -1, viewWindowMode: 'pretty', slantedText: true},     vAxis: {title: 'Total Sales (in USD)', titleTextStyle: {color: 'black'}, count: -1, format: '$#'},     colors: ['#F1CA3A']   };    var chart = new google.visualization.ColumnChart(document.getElementById('my_div'));   chart.draw(data, options);     }   

Data in chartData variable is in array form as:

var chartData = [];   cartData.push('www.w3school.com', 106);   cartData.push('www.google.com', 210);  

Width and height of "my_div" are 350px and 300px respectively. We have also attached screen shot of this issue given below:

enter image description here

Can anyone help me that how can we prevent this cutting issue. Or, Is any method available in google chart API to prevent this?

Waiting for solution.

like image 451
user2393886 Avatar asked Jul 08 '14 10:07

user2393886


People also ask

Is Google charts deprecated?

While the dynamic and interactive Google Charts are actively maintained, we officially deprecated the static Google Image Charts way back in 2012. This gives us the right to turn it off without notice, which may happen soon.

How do I change the data in a Google Chart?

Double-click the chart you want to change. At the right, click Setup. Select the cells you want to include in your chart. Optional: To add more data to the chart, click Add another range.

What is continuous axis?

An imaginary line running around the ice surface that serves as a basis for a dance pattern . Usually the continuous axis consists of two lines running parallel to the long axis of the ice surface, approximately halfway between the long axis and the perimeter of the rink.

How do I customize the axis labels?

Label positioning and style - You can customize label positioning and style using the hAxis/vAxis.textPosition and hAxis/vAxis.textStyle options. Axis title text and style - You can customize the axis title text and style using the hAxis/vAxis.title and hAxis/vAxis.titleTextStyle options.

How do I change the size of the x-axis on a chart?

You could select the horizontal axis and then reduce the size of the font using the buttons on the Home ribbon to make them fit or alternatively you can drag the bottom of the chart area upwards to make more space for the x-axis.

How do I localize the strings in Google Charts?

When using a format that employs text (e.g., the long format, which will render 8,000,000 as "8 million", you can localize the strings into other languages by specifying a language code when you load the Google Charts library. For instance, to change "8 million" into the Russian "8 миллиона", call the library like so:

How do I customize the Axis properties of a chart?

When you create a chart with axes you can customize some of their properties: Direction - You can customize the direction using the hAxis/vAxis.direction option. Label positioning and style - You can customize label positioning and style using the hAxis/vAxis.textPosition and hAxis/vAxis.textStyle options.


1 Answers

This is an always recurring issue in google visualization, in my opinion :( There are a few "tricks" one can experiment with : chartArea and hAxis.textPosition. Here is your code in a jsFiddle with the following chartData, reproducing the problem above :

var chartData = [     ['www.facebook.com', 45],     ['http://www.google.com', 67],     ['www.stackoverflow.com', 11]     ]; 

enter image description here

fiddle -> http://jsfiddle.net/a6WYw/


chartArea can be used to adjust the upper "padding" taking space from the legend / hAxis below along with the internal height of the bars (the chart itself without axis and legend). For example

chartArea: {    top: 55,    height: '40%'  } 

Will shrink the chartArea, giving room for the legend on the hAxis.

enter image description here

fiddle -> http://jsfiddle.net/Swtv3/


My personal favourite is to place the hAxis legend inside the chart by

hAxis : { textPosition : 'in' }  

This will honor both short and long descriptions, and does not make the chart looking too "weird" when there is a few very long strings.

enter image description here

fiddle -> http://jsfiddle.net/7HBmX/


As per comment - move the "in" labels outside the chart. There is to my knowledge no native way to do this, but we can always alter the <svg>. This can be a difficult task, but in this case we know that the only <text> elements who has the text-anchor="middle" attribute is the h-axis labels and the overall h-axis description. So

var y, labels =  document.querySelectorAll('[text-anchor="middle"]'); for (var i=0;i<labels.length-2;i++) {     y = parseInt(labels[i].getAttribute('y'));    labels[i].setAttribute('y', y+30); } 

to move the labels outside the chart. demo -> http://jsfiddle.net/970opuu0/

enter image description here

like image 175
davidkonrad Avatar answered Oct 11 '22 21:10

davidkonrad