Question

When trying to plot a Line Chart using the Google Charts code I get this error

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

The '0.8' is referring to the value p1 in the code.

function drawChart() {

// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('number', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
    [p1,1.89],
[ch_period[17],5],
[3,2],
[5,2],
[5,2],
[6,7]
 ]);

// Set chart options
var options = {'title':'How Much Pizza I Ate Last Night',
             'width':400,
             'height':300};

// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
Was it helpful?

Solution

I've made a jsfiddle with your code that works: http://jsfiddle.net/kychan/Dfx4V/1/

var p1        = parseInt('4'),
    ch_period = {'17':4};

function drawChart() {

    // Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('number', 'Topping');
    data.addColumn('number', 'Slices');
    data.addRows([
        [p1, 1.89],
        [ch_period[17], 5],
        [3, 2],
        [5, 2],
        [5, 2],
        [6, 7]
    ]);

    // Set chart options
    var options = {
        'title': 'How Much Pizza I Ate Last Night',
            'width': 400,
            'height': 300
    };

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}


drawChart();

The problem was that p1 (and maybe ch_period) isn't the type number. Thus you must make it a number using parseInt(p1) / parseInt(ch_period) or manually assign it to a number.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top