JackNapier
JackNapier

Reputation: 448

Google Timeline - change color of data depending on name

I have a google timeline as in the code below, and want to change the color of any of the entries that are title "BLACK". I can't seem to get it to work, and I've tried a few different solutions. Here is my code:

http://jsfiddle.net/5j0umkcx/1/


Html:

<div id='chart_div'></div>

Javascript:

var myData = [
        ['Category', 'Name', {role: 'style'}, 'Start', 'End'],
        ['Foo', 'BLACK', '', new Date(2014, 7, 1), new Date(2014, 7, 5)],
        ['Foo', 'Homer', '', new Date(2014, 7, 6), new Date(2014, 7, 8)],
        ['Bar', 'Marge', '', new Date(2014, 7, 2), new Date(2014, 7, 9)]
    ]

        for(i=1;i<myData.length;i++){
    if(myData[i].Name == 'BLACK'){
        data[i].style = '#000000';
    }}


function drawChart() {

    var data = google.visualization.arrayToDataTable(myData);



    var chart = new google.visualization.Timeline(document.querySelector('#chart_div'));
    chart.draw(data, {
        height: 300,
        width: 600
    });
}
google.load('visualization', '1', {packages:['timeline'], callback: drawChart});

Upvotes: 1

Views: 144

Answers (1)

Snel23
Snel23

Reputation: 1379

The problem here is that you are working with arrays, but you are treating them as Objects. myData[i] has no property of Name, because it is an Array. Though, you can access where the Name is, by getting myData[i][1] (The slot in the array in which the name is stored). You can replace your for loop code with this to change the colour attribute (myData[i][2])

for(i=1;i<myData.length;i++){
    if(myData[i][1] == 'BLACK'){
        myData[i][2] = '#000000';
    }
}

Upvotes: 1

Related Questions