Google graphics in different colors with separate series. Material table

Using {role: 'style'} I can apply different colors to a single series chart. But if I use the new Google maps "Material", I can not.

"regular" schedules (work) Google:

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

function drawChart() {

   var data = google.visualization.arrayToDataTable([
       ['Element', 'Density', {role: 'style'} ],
       ['Copper', 8.94, 'color: #FF9900'],
       ['Silver', 10.49,'color: #109618'],
       ['Gold', 19.30,'color: #3B3EAC'],
       ['Platinum', 21.45,'color: #0099C6']
    ]);       

    var view = new google.visualization.DataView(data);


    var options = {
        title: "Density of Precious Metals, in g/cm^3",
        width: 600,
        height: 400,
        bar: {groupWidth: '85%'},
        legend: { position: 'none' },
    };

    var chart = new google.visualization.ColumnChart(document.getElementById('barchart_values'));
    chart.draw(view, options);
}

The same graph, but using the Google Material histogram (different colors not applicable)

google.load("visualization", "1.1", {packages:["bar"]});
google.setOnLoadCallback(drawChart);
function drawChart() {

   var data = google.visualization.arrayToDataTable([
       ['Element', 'Density', { role: 'style' } ],
       ['Copper', 8.94, 'color: #FF9900'],
       ['Silver', 10.49,'color: #109618'],
       ['Gold', 19.30,'color: #3B3EAC'],
       ['Platinum', 21.45,'color: #0099C6']
  ]);   

  var view = new google.visualization.DataView(data);


  var options = {
    title: "Density of Precious Metals, in g/cm^3",
    width: 600,
    height: 400,
    bar: {groupWidth: '95%'},
    legend: { position: 'none' },
  };
    options = google.charts.Bar.convertOptions(options);
    var chart = new google.charts.Bar(document.getElementById('barchart_values'));
    chart.draw(view, options);
}
+4
source share
2 answers

. , , , colors: [...], . , (?).

, :

function colorize() {
   var colors = ['#FF9900', '#109618', '#3B3EAC', '#0099C6'],
       svg = document.getElementById('barchart_values').querySelector('svg'),
       bars = svg.querySelectorAll('path');

   for (var i=0;i<bars.length;i++) {
       if (i !== selected) bars[i].setAttribute('fill', colors[i]);
   }    
}

colors[] - data DataTable . <path>, , , , <svg>.

:

google.visualization.events.addListener(chart, 'ready', colorize);    

select onmouseover, :

google.visualization.events.addListener(chart, 'select', colorize);
google.visualization.events.addListener(chart, 'onmouseover', colorize);

, .. :

function colorize() {
   var colors = ['#FF9900', '#109618', '#3B3EAC', '#0099C6'],
       selection = chart.getSelection(),
       selected = selection[0] ? selection[0].row : -1,
       svg = document.getElementById('barchart_values').querySelector('svg'),
       bars = svg.querySelectorAll('path');

   for (var i=0;i<bars.length;i++) {
       if (i !== selected) bars[i].setAttribute('fill', colors[i]);
   }    
}

​​ → http://jsfiddle.net/o00oayvu/

+2

 var tmpColors = new Array(['orange'],['purple'],['red'],['green']);
    loop{
    .....
    .....
    options.colors = tmpColors[i];
    ....
    ....
    }
0

All Articles