Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Charts Legend Position Not Working

This is driving me nuts. I can't get the legend to move at all. This produces a chart with the legend in it's default location on the right.

I clearly have legend position declared as "bottom" but it's not working. Yet this is exactly what the docs say.

google.charts.load('current',{'packages':['bar']});
google.charts.setOnLoadCallback(drawStuff);

function drawStuff(){
var data = new google.visualization.DataTable();
data.addColumn('string', 'Name');
data.addColumn('number', 'Count');
data.addColumn('number', 'Variance');
data.addRows([
  ['Smith',		35,		{v: -.1126,   f: '-11.26%'} ],
  ['Chalk',		53,		{v: -.0126,   f: '-1.26%'}  ],
  ['Hank',		84,		{v: -.0252,   f: '-2.52%'}  ],
  ['Jordan',	46, 	{v: .0688,    f: '6.88%'}   ],
  ['Bernie',	1,  	{v: 0,        f: '-'}       ],
  ['Ralph',		105,	{v: -.0548,   f: '-5.48%'}  ]
]);

var options = {
  series: {
    0: {axis: 'Quotes'},
    1: {axis: 'Percent'}
  },
  axes: {
    y: {
      Quotes: {label: 'Subdmission Count'},
      Percent: {label: '% Percent'}
    }
  },
  legend: { 
    position : 'bottom'
  }
};

var table = new google.charts.Bar(document.getElementById('table1'));

table.draw(data, options);
}
<html>

<head>
  <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
</head>

<body>
  <div id='table1'></div>
</body>

</html>
like image 313
Tom Avatar asked Nov 01 '16 14:11

Tom


People also ask

How do I change the position of my legend in a graph?

Click the chart, and then click the Chart Design tab. Click Add Chart Element > Legend. To change the position of the legend, choose Right, Top, Left, or Bottom. To change the format of the legend, click More Legend Options, and then make the format changes that you want.

How do I get rid of the legend in my Google chart?

To show or hide the legends of every chart in a dashboard, in the dashboard toolbar, click settings Settings and then select Show all legends or Hide all legends.

How do I customize my Google bar graph?

Customize a bar chartChoose an option: Chart style: Change how the chart looks. Chart & axis titles: Edit or format title text. Series: Change bar colors, axis location, or add error bars, data labels, or trendline.


1 Answers

legend.position: ['top', 'bottom'] -- are just a couple of the many options that don't work on material charts

see Tracking Issue for Material Chart Feature Parity #2143 for an extensive list

however, these options will work on a core chart...

core --> google.visualization.ColumnChart -- using --> packages: ['corechart']

material --> google.charts.Bar -- using --> packages: ['bar']

there is also an option to get a core chart close to the look & feel of material

theme: 'material'

see following working snippet using core chart instead...

google.charts.load('current',{'packages':['corechart']});
google.charts.setOnLoadCallback(drawStuff);

function drawStuff(){
var data = new google.visualization.DataTable();
data.addColumn('string', 'Name');
data.addColumn('number', 'Count');
data.addColumn('number', 'Variance');
data.addRows([
  ['Smith',		35,		{v: -.1126,   f: '-11.26%'} ],
  ['Chalk',		53,		{v: -.0126,   f: '-1.26%'}  ],
  ['Hank',		84,		{v: -.0252,   f: '-2.52%'}  ],
  ['Jordan',	46, 	{v: .0688,    f: '6.88%'}   ],
  ['Bernie',	1,  	{v: 0,        f: '-'}       ],
  ['Ralph',		105,	{v: -.0548,   f: '-5.48%'}  ]
]);

var options = {
  chartArea: {
    height: '56%'
  },
  series: {
    1: {
      targetAxisIndex: 1
    }
  },
  hAxis: {
    title: 'Name'
  },
  vAxes: {
    0: {
      title: 'Submission Count'
    },
    1: {
      title: '% Percent'
    }
  },
  theme: 'material',
  legend: { 
    position : 'bottom'
  }
};

var table = new google.visualization.ColumnChart(document.getElementById('table1'));

table.draw(data, options);
}
<html>

<head>
  <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
</head>

<body>
  <div id='table1'></div>
</body>

</html>
like image 72
WhiteHat Avatar answered Oct 12 '22 23:10

WhiteHat