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:
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.
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.
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.
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.
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.
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.
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:
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.
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] ];
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With