Color in Google's Timeline Chart bars based in the a specific value

后端 未结 7 1752
说谎
说谎 2021-01-02 01:31

I would like to create groups of colors for the bars. The example below is raw. I would like to add a column with a category type, and based on that category I will color th

7条回答
  •  情话喂你
    2021-01-02 02:19

    //set fixed colors
    var colors = [];
    var colorMap = {
      'Maschine in Produktion':                       '#7baaf7',
      'Behälterwechsel':                              '#e67c73',
      'Auftragsgemäßes Rüsten von W.Z. und Material': '#f7cb4d',
      'Unterbrechung':                                '#57ba8a',
      'neues Werkzeug einfahren':                     '#c47ed0',
      'Produktqualität(vermessen der Teile)':         '#4dc5d4',
      'frei':                                         '#ff9b7b',
      'Werkzeugschaden':                              '#bbba66',
      'Coilmaterial (nicht geeignet)':                '#8d97d3',
      'neues Werkzeug einfahren':                     '#f5cfff'
    }
    
    for (var i = 0; i < data.getNumberOfRows(); i++) {      
      colors.push(colorMap[data.getValue(i, 0)]);
    }
    
    var unique = colors.filter(function(itm, i, a) {
      return i == colors.indexOf(itm);
    });
    
    
    var options = {
      height:   800, 
      hAxis:        {format: isDay == 1 ? 'HH:MM:SS' : 'dd.MM.yyyy HH:MM:SS'},
      tooltip:  {isHtml: true},
      legend:   'none',
      avoidOverlappingGridLines: false,
      colors:   unique
    };
    
    chart.draw(data, options);
    

    This works perfectly for me. The main Problem with the old code is, that the colors array....is just an array and not a unique Hashmap. You can just use the custom filter function in order to get the unique colors.

提交回复
热议问题