Coloring Google Stepped Area Charts?

萝らか妹 提交于 2019-12-24 11:27:05

问题


is it possible to color each step indivual? Now all steps are blue but I want the first step blue, second yellow, third red, ...

<div id="chart_div" style="width: 100%; height: 200px;"></div>
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {
        var data = google.visualization.arrayToDataTable([
            ['Step', 'Equity'],
          ['1', -40],
          ['2', -40],
          ['3', -40],
          ['4', -40],
          ['5',     -30],
          ['6',     -30],
          ['7',        10],
          ['8',        10],
          ['9',        10],
          ['10',      50]
        ]);

        var options = {
        legend: 'none',
                vAxis: { ticks: [{v:-50, f:'0%'}, {v:0, f:'50%'}, {v:50, f:'100%'}] },
        enableInteractivity: false,
        hAxis: { textPosition: 'none' },
         chartArea:{top:10,left:40,bottom:10,right:5, width:"100%",height:"100%"},
        };

        var chart = new google.visualization.SteppedAreaChart(document.getElementById('chart_div'));

        chart.draw(data, options);
      }

回答1:


you can use a 'style' column role

see following working snippet...

google.charts.load('current', {
  callback: function () {

    var data = google.visualization.arrayToDataTable([
      ['Step', 'Equity', {role: 'style'}],  // <-- define style column
      ['1', -40, 'blue'],
      ['2', -40, 'yellow'],
      ['3', -40, 'red'],
      ['4', -40, 'green'],
      ['5', -30, 'purple'],
      ['6', -30, 'orange'],
      ['7', 10, 'cyan'],
      ['8', 10, 'indigo'],
      ['9', 10, 'magenta'],
      ['10',50, 'lime']
    ]);

    var options = {
      legend: 'none',
      vAxis: { ticks: [{v:-50, f:'0%'}, {v:0, f:'50%'}, {v:50, f:'100%'}] },
      enableInteractivity: false,
      hAxis: { textPosition: 'none' },
      chartArea:{top:10,left:40,bottom:10,right:5, width:"100%",height:"100%"},
    };

    var chart = new google.visualization.SteppedAreaChart(document.getElementById('chart_div'));

    chart.draw(data, options);
  },
  packages:['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>


来源:https://stackoverflow.com/questions/43765947/coloring-google-stepped-area-charts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!