简体   繁体   English

更改组合图中填充区域的不透明度(谷歌图表)

[英]Change opacity of fill area in combo chart (Google charts)

I was working on the combo chart (bar + scatter chart) of Google charts.我正在研究 Google 图表的组合图(条形图 + 散点图)。 I was not able to change the opacity of the data point and the fill area inside the bars.我无法更改数据点的不透明度和条形内的填充区域。 There was not much information provided in the documentation and I tried to change opacity using methods mentioned for other charts but nothing seemed to work.文档中没有提供太多信息,我尝试使用其他图表中提到的方法更改不透明度,但似乎没有任何效果。

Here is the link to the chart I was trying to work on- jsFiddle这是我尝试使用的图表的链接-jsFiddle

google.charts.load('current', {
    'packages': ['corechart']
  });
  google.charts.setOnLoadCallback(drawVisualization);

  function drawVisualization() {
    // Some raw data (not necessarily accurate)
    var sett = 'fill-color: #76A7FA; fill-opacity: 0.5; stroke-color: #76A7FA; stroke-width: 1;';
    var data = google.visualization.arrayToDataTable([
      ['Month', 'Bolivia', 'Average', {
        role: 'style'
      }],
      ['2004/05', 450, 614.6, sett],
      ['2005/06', 288, 682, sett],
      ['2006/07', 397, 623, sett],
      ['2007/08', 215, 609.4, sett],
      ['2008/09', 366, 569.6, sett],
      ['2009/05', 450, 614.6, sett],
    ]);

    var options = {
      title: 'Monthly Coffee Production by Country',
      vAxis: {
        title: 'Cups'
      },
      hAxis: {
        title: 'Month'
      },
      legend: 'none',
      fillOpacity: 0.3,
      pointShape: {
        type: 'triangle',
        rotation: 180
      },
      pointSize: 7,
      series: {
        0: {
          type: 'bars',
          areaOpacity: 0.3
        },
        1: {
          type: 'scatter',
          opacity: 0.5,
          color: 'blue'
        }
      }
    };

    var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }

you can use a 'style' column role to change the opacity,您可以使用'style'列角色来更改不透明度,
which it appears you have看起来你有

however, the style role must follow each series column to which you want to apply the style但是,样式角色必须遵循要应用样式的每个系列列

so to make the columns opaque, add the style following the second column,所以为了使列不透明,在第二列后面添加样式,
see following working snippet...请参阅以下工作片段...

 google.charts.load('current', { 'packages': ['corechart'] }); google.charts.setOnLoadCallback(drawVisualization); function drawVisualization() { // Some raw data (not necessarily accurate) var sett = 'fill-color: #76A7FA; fill-opacity: 0.5; stroke-color: #76A7FA; stroke-width: 1;'; var data = google.visualization.arrayToDataTable([ ['Month', 'Bolivia', {role: 'style'}, 'Average', {role: 'style'}], ['2004/05', 450, sett, 614.6, sett], ['2005/06', 288, sett, 682, sett], ['2006/07', 397, sett, 623, sett], ['2007/08', 215, sett, 609.4, sett], ['2008/09', 366, sett, 569.6, sett], ['2009/05', 450, sett, 614.6, sett], ]); var options = { title: 'Monthly Coffee Production by Country', vAxis: { title: 'Cups' }, hAxis: { title: 'Month' }, legend: 'none', fillOpacity: 0.3, pointShape: { type: 'triangle', rotation: 180 }, pointSize: 7, series: { 0: { type: 'bars', areaOpacity: 0.3 }, 1: { type: 'scatter', opacity: 0.5, color: 'blue' } } }; var chart = new google.visualization.ComboChart(document.getElementById('chart_div')); chart.draw(data, options); }
 <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div" style="width: 100%; height: 500px;"></div>

I was facing the same issue and found an easier way to control the opacity of bar charts in Google Charts.我遇到了同样的问题,并找到了一种更简单的方法来控制 Google Charts 中条形图的不透明度。

I found an easy way of doing this and want to contribute my answer for those who come next!我找到了一种简单的方法来做到这一点,并希望为接下来的人贡献我的答案!

We can control the opacity of individual series (including bars) by using dataOpacity :我们可以使用dataOpacity控制单个系列(包括条形)的不透明度:

 google.charts.load('current', { 'packages': ['corechart'] }); google.charts.setOnLoadCallback(drawVisualization); function drawVisualization() { var data = google.visualization.arrayToDataTable([ ['Month', 'Bolivia', 'Average'], ['2004/05', 450, 614.6], ['2005/06', 288, 682], ['2006/07', 397, 623], ['2007/08', 215, 609.4], ['2008/09', 366, 569.6], ['2009/05', 450, 614.6], ]); var options = { title: 'Monthly Coffee Production by Country', vAxis: { title: 'Cups' }, hAxis: { title: 'Month' }, legend: 'none', pointShape: { type: 'triangle', rotation: 180 }, pointSize: 7, series: { 0: { type: 'bars', dataOpacity: 0.3 }, 1: { type: 'scatter', color: 'blue', dataOpacity: 0.6 } } }; var chart = new google.visualization.ComboChart(document.getElementById('chart_div')); chart.draw(data, options); }
 <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div" style="width: 100%; height: 500px;"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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