简体   繁体   中英

Adding a new line to a google chart, having troubles with the data table layout

I have a selection of people I want to plot points on a line graph for. The axis is date and how many times the person drank a coffee.

My google chart must have a list of the people down the side of it, when I hover over the points plotted on the graph, I want it to say something like "John - 18".

I had achieved this by laying my data out like this:

data.addColumn('date', 'Date');   
data.addColumn('number', 'All People');     
data.addColumn('number', 'Bob');     
data.addColumn('number', 'Bill');     
data.addColumn('number', 'Willie');     
data.addColumn('number', 'Joel'); 

data.addRows( [
    [ new Date( 2011,  6,  10 ), 30, 20, 10, 12, 0 ],
    [ new Date( 2011,  6,  11 ), 1, 2, 4, 3, 0 ],
    [ new Date( 2011,  6,  12 ), 15, 2, 0, 3, 0 ],
    [ new Date( 2011,  6,  13 ), 5, 6, 0, 3, 0 ],
    [ new Date( 2011,  6,  14 ), 3, 6, 0, 3, 0 ]
] );

Problem is, when I want to add another line to the chart, I have no idea how i'm suppose to do that... Is there a way of iterating over the current rows and pushing a new data into it and add one more column called "Jeffrey" and then redrawing the entire graph?

The code might look like this:

data.addColumn( 'number', 'Jeffrey' );

// coffeeDrank is an array of coffee drunk organised in the same order as the data rows 
for( a in data.rows )
{
    data.rows[ a ].push( coffeeDrank[ a ] );
}
    var columnCount = data.getNumberOfColumns();                 
    var rowCount = data.getNumberOfRows();


    for( a = 0; a < rowCount; a++ )
    {
        if( a == 0 )
        {
            data.setCell( a, ( columnCount - 1 ), 0 );
        }
        else
        {                                               
            data.setCell( a, ( columnCount - 1 ), coffeeDrank[ a ] );
        }
    }      

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM