Anderson
Anderson

Reputation: 101

Google Chart API JavaScript change max hAxis value using a function

I was wondering, once the chart is loaded, is there a way to change Horizontal Max Window value (date range) with a function WITHOUT having to call the drawChart function again?

For example:

function changeWindow(){
    chart.setOptions(hAxis.viewWindow.max = 2);
}

Original Code:

var chart;
function drawChart() {
   var data = google.visualization.arrayToDataTable([
      ['Year', 'Sales', 'Expenses'],
      ['2004',  1000,      400],
      ['2005',  1170,      460],
      ['2006',  660,       1120],
      ['2007',  1030,      540]
    ]);

    var options = {
      title: 'Company Performance',
      hAxis: {title: 'Year',  titleTextStyle: {color: 'red'}}
    };

    chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }

Upvotes: 1

Views: 728

Answers (1)

jmac
jmac

Reputation: 7128

When you draw the chart, Google Visualization will populate three different elements in the target <div> you have. Taking the sample from Google Playground for Line Chart, this is the code that it generates:

<div style="position: relative; width: 500px; height: 400px;" dir="ltr">
<div style="position: absolute; left: 0px; top: 0px; width: 100%; height: 100%;">
<svg width="500" height="400" style="overflow: hidden;">
<defs id="defs">
<clipPath id="_ABSTRACT_RENDERER_ID_0">
<rect x="96" y="77" width="309" height="247">
</clipPath>
</defs>
<rect x="0" y="0" width="500" height="400" stroke="none" stroke-width="0" fill="#ffffff">
<g>
<rect x="417" y="77" width="71" height="50" stroke="none" stroke-width="0" fill-opacity="0" fill="#ffffff">
<g>
<rect x="417" y="77" width="71" height="12" stroke="none" stroke-width="0" fill-opacity="0" fill="#ffffff">
<g>
<text text-anchor="start" x="434" y="87.2" font-family="Arial" font-size="12" stroke="none" stroke-width="0" fill="#222222">Cats</text>
</g>
<rect x="417" y="77" width="12" height="12" stroke="none" stroke-width="0" fill="#3366cc">
</g>
<g>
<rect x="417" y="96" width="71" height="12" stroke="none" stroke-width="0" fill-opacity="0" fill="#ffffff">
<g>
<text text-anchor="start" x="434" y="106.2" font-family="Arial" font-size="12" stroke="none" stroke-width="0" fill="#222222">Blanket 1</text>
</g>
<rect x="417" y="96" width="12" height="12" stroke="none" stroke-width="0" fill="#dc3912">
</g>
<g>
<rect x="417" y="115" width="71" height="12" stroke="none" stroke-width="0" fill-opacity="0" fill="#ffffff">
<g>
<text text-anchor="start" x="434" y="125.2" font-family="Arial" font-size="12" stroke="none" stroke-width="0" fill="#222222">Blanket 2</text>
</g>
<rect x="417" y="115" width="12" height="12" stroke="none" stroke-width="0" fill="#ff9900">
</g>
</g>
<g>
<rect x="96" y="77" width="309" height="247" stroke="none" stroke-width="0" fill-opacity="0" fill="#ffffff">
<g clip-path="url(#_ABSTRACT_RENDERER_ID_0)">
<g>
<rect x="96" y="323" width="309" height="1" stroke="none" stroke-width="0" fill="#cccccc">
<rect x="96" y="262" width="309" height="1" stroke="none" stroke-width="0" fill="#cccccc">
<rect x="96" y="200" width="309" height="1" stroke="none" stroke-width="0" fill="#cccccc">
<rect x="96" y="139" width="309" height="1" stroke="none" stroke-width="0" fill="#cccccc">
<rect x="96" y="77" width="309" height="1" stroke="none" stroke-width="0" fill="#cccccc">
</g>
<g>
<rect x="96" y="323" width="309" height="1" stroke="none" stroke-width="0" fill="#333333">
</g>
<g>
</g>
<g>
<g>
</g>
<g>
</svg>
</div>
</div>

In order to change the size of the chart, you would have to adjust the two internal divs, the size of the SVG itself, and the X/Y coordinates of every single element within that chart. You can do this with Javascript, but it is going to be far easier just to call draw() again.

Upvotes: 1

Related Questions