Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Google Charts legend width in JavaScript?

I am generating this Google Line Chart using the Google JS API. As you can see, the labels are very narrow. How do I make it so that the whole label text is visible?

enter image description here

like image 752
f.ardelian Avatar asked Jul 29 '11 07:07

f.ardelian


People also ask

How do you change the width of a Google Chart?

Specify Chart Size One very common option to set is the chart height and width. You can specify the chart size in two places: in the HTML of the container <div> element, or in the chart options. If you specify the size in both locations, the chart will generally defer to the size specified in the HTML.

How do I use Google charts in JavaScript?

The most common way to use Google Charts is with simple JavaScript that you embed in your web page. You load some Google Chart libraries, list the data to be charted, select options to customize your chart, and finally create a chart object with an id that you choose.

Which is better chart JS or Google charts?

Google Charts is an interactive web service that creates graphical charts from user-supplied information. Chart. js is an open-source JavaScript library that allows you to draw different types of charts by using the HTML5 canvas element.


2 Answers

Here are some samples based on the google code playground line charts. Adjusting the chartArea width option gives more space for labels:

new google.visualization.LineChart(document.getElementById('visualization')).     draw(data, {curveType: "function",                 width: 500, height: 400,                 vAxis: {maxValue: 10},                 chartArea: {width: '50%'}}         ); 

If it's an option, you could also position the labels beneath the chart, which gives considerably more space:

new google.visualization.LineChart(document.getElementById('visualization')).     draw(data, {curveType: "function",                 width: 500, height: 400,                 vAxis: {maxValue: 10},                 legend: 'bottom'}         ); 
like image 94
oli Avatar answered Oct 02 '22 18:10

oli


Expanding the chartArea option to a width of 100% solved the problem for me. Contrary to the documentation, the chartArea does include the legend. I used a PieChart but the same option is available for the LineChart.

var options = {'title':title,'width':w,'height':h,'chartArea':{left:0,top:10,width:"100%"}}; var chart = new google.visualization.PieChart(document.getElementById(chartDiv)); chart.draw(data,options); 

Reference.

like image 42
Nettie Avatar answered Oct 02 '22 17:10

Nettie