简体   繁体   English

如何捕获Google Charts饼图切片的颜色值?

[英]How do I capture the color value of a Google Charts pie charts slice?

I've got a working example of how to make a pie chart interact with a table. 我有一个有效的示例,说明了如何使饼图与表进行交互。 What I'd like to add is a visual cue of their correlation. 我想添加的是它们之间相关性的视觉提示。 The only examples I've seen are where the table row is highlighted by adding a listener . 我看到的唯一示例是通过添加侦听器突出显示表行的位置。 I'd like to add an empty cell within the table who's background color is the color of the pie slice it corresponds to. 我想在表格中添加一个空单元格,其背景颜色是其对应的饼图的颜色。 How do I capture the color of the pie slice? 我如何捕获饼图的颜色? JSFiddle JSFiddle

What I have now: 我现在所拥有的:

  google.load("visualization", "1", {packages:["corechart","table"]});
  google.setOnLoadCallback(drawProductAllocationChart);
  function drawProductAllocationChart() {
    var data = google.visualization.arrayToDataTable([
      ['Name', 'Allocation'],
      ['Product A', 25],
      ['Product B', 50],
      ['Product C', 15],
      ['Product D', 10]
    ]);

    var options = {
      //title: 'My Daily Activities'
      is3D: true
    };

    var productAllocationChart = new google.visualization.PieChart(document.getElementById('product-allocation-chart'));
    productAllocationChart.draw(data, options);

    var productAllocationTable = new google.visualization.Table(document.getElementById('product-allocation-table'));
    productAllocationTable.draw(data, null);

    // When the table is selected, update the orgchart.
    google.visualization.events.addListener(productAllocationTable, 'select', function() {
      portfolioAllocationChart.setSelection(productAllocationTable.getSelection());
    });

    // When the orgchart is selected, update the table visualization.
    google.visualization.events.addListener(productAllocationChart, 'select', function() {
      productAllocationTable.setSelection(productAllocationChart.getSelection());
    });
  }

Thanks! 谢谢!

My first thought is to just create a map of the default colors for Google Charts. 我的第一个想法就是为Google图表创建默认颜色的地图。 Although, I haven't used the data tables for the visualization api yet. 虽然,我还没有将数据表用于可视化api。

From the Google Group I got this list of 31 default colors: Google网上论坛,我得到了以下31种默认颜色的列表:

["#3366cc","#dc3912","#ff9900","#109618","#990099","#0099c6","#dd4477","#66aa00","#b82e2e","#316395","#994499","#22aa99","#aaaa11","#6633cc","#e67300","#8b0707","#651067","#329262","#5574a6","#3b3eac","#b77322","#16d620","#b91383","#f4359e","#9c5935","#a9c413","#2a778d","#668d1c","#bea413","#0c5922","#743411"]

I assume you know the order that things are being added to the pie chart, and it most likely corresponds to the order of rows within the table. 我假设您知道事物被添加到饼图中的顺序,并且最有可能对应于表中行的顺序。 If that's the case, then just map the array to the row. 如果是这种情况,则只需将数组映射到行即可。

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

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