Search code examples
javascriptchartsgoogle-visualizationlines

Google Chart 2 Lines with not the same number of data


I'm trying to create a dynamic Google Chart line. I have 2 lines in a range of a day (from 00h00 to 23h50 - 0am to 12pm) with a point for each lines every 10 minutes (00h00, 00h10, 00h20 etc...). So for each lines I have 144 values for a day (1*6*24 - 6 / hours because I have 1 every 10 minutes). For the first line, I have all the values at the start of the day (144 values) but for the other one, I'm drawing it according the current hour (exemple if it's 01:00 - 1am, I have only 6 values).

So i'm trying to draw the lines but I have a problem if my secondes lines doesn't have 144 values.

Here is the function to draw the chart with the different lines

function drawChart(chart_div) {
var data = get_data();

var chart = new google.visualization.ComboChart(document.getElementById(chart_div));
chart.draw(data, {
    height: 300,
    width: 1000,
    chartArea:{left:40,top:5,width:"100%",height:"80%"},
    seriesType: 'line',
    series: {
        0: {
            color: '#0080FF'
        },
        1: {
            color: '#bdbdbd',
            enableInteractivity: false
        }
    },
    tooltip: {
        isHtml: true
    },
    legend: {
        position: 'none'
    },
    vAxis: {
        gridlines: {
            color: '#e5e5e1'
        }
    },
    hAxis: {
        viewWindow: {
            min: 0,
            max: 144
        },
        ticks: [0, 24, 48, 72, 96, 120, 144] // display labels every 24
    }
});}

Here is the code to launch the function to draw the chart

google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(function() {
    drawChart("chart_one");
});

And here is the function to generate the data (They are currently randomly generate but I'll take them from DB later)

function get_data(){
var data = new google.visualization.DataTable();
data.addColumn('string', 'Time');
data.addColumn('number', 'Heartbeat (%)');
data.addColumn('number', 'Heartbeat moy (%)');
var total = 0;
for(i = 0; i < 24; i++){
    for(j=0; j <= 5; j++){
        var num = "";
        if(i < 10){
            num += "0";
        }
        if(total < 98){
            data.addRows([[num + i + "h"+j+"0", Math.floor((Math.random() * 10) + 91), Math.floor((Math.random() * 10) + 91)]]);
        } else {
            data.addRows([[num + i + "h"+j+"0", "", Math.floor((Math.random() * 10) + 91)]]);
        }
        total++;
    }
}
return data;
}

So basically I would have something like that :

Google chart

But if I'm setting the secondes line values to "", it say

Error: Type mismatch. Value does not match type number in column index 2

Does anyone have any idea how to bypass the value for the seconde lines if I doesn't have it ?


Solution

  • Use null instead of an empty string "" for points that don't have any data