Search code examples
javascriptgoogle-apigoogle-visualization

Changing a Google visualization chart object


I'm currently using ChartWrapper and ControlWrapper to develop some utilities in a project I have been working on. Let me use this example from Controls and Dashboards from the example:

var pieChart = new google.visualization.ChartWrapper({
         'chartType': 'PieChart',
         'containerId': 'chart_div',
         'options': {
           'width': 300,
           'height': 300,
           'pieSliceText': 'value',
           'legend': 'right'
         }
       });

I know pieChart is of the type ChartWrapper, but I'm using a PieChart object to draw the chart. Now, how do I do to set up the options of the PieChart object, and not of the ChartWrapper? I tried to use getChart, but that always returned me null. I suppose ChartWrapper has an object of the type PieChart. If somehow I could access that object, so I could set up some options to fulfil my needs. Does someone know how to do it?


Solution

  • From the getChart documentation, which probably explains why you're getting null:

    This will return null until after you have called draw() on the ChartWrapper object, and it throws a ready event.

    If you want to set the options, you can just use set them directly on the ChartWrapper object.

    pieChart.setOption("width", 400);
    

    Or you can set all the options from scratch:

    pieChart.setOptions( {
           'width': 300,
           'height': 300,
           'pieSliceText': 'value',
           'legend': 'right'
         } );