Search code examples
jquerygoogle-visualizationgoogle-datatable

Find lowest and highest values from Google Charts DataTable


I'm drawing a chart (Google Charts API) with this DataTable:

chart_data = new google.visualization.DataTable();
chart_data.addColumn('string','date');
chart_data.addColumn('number','value');

chart_data.addRows([
    ["10 May",0.23],
    ["11 May",0.26],
    ["12 May",0.29],
    ["13 May",0.23],
    ["14 May",0.21],
    ["15 May",0.25],
    ["16 May",0.28]
]);

I need to figure out which are the lowest and highest values of the value column in the DataTable. There are some functions called google.visualization.data.max but there is not any actual code example on how to implement it. Anybody has used that feature before? Or, has someone another solution for this issue? Thanks!


Solution

  • The function getColumnRange will return max and min. It also looks like you need to declare the column type and name for the number values.

    chart_data = new google.visualization.DataTable();
    chart_data.addColumn('string','date');
    chart_data.addColumn('number','some number');
    
    chart_data.addRows([
        ["10 May",0.23],
    ...
        ["16 May",0.28]
    ]);
    
    alert("Max: " + chart_data.getColumnRange(1).max);
    alert("Min: " + chart_data.getColumnRange(1).min);