简体   繁体   English

在Google图表上为Piechart区域设置Alpha不透明度

[英]Set alpha opacity for Piechart area on Google charts

I am having Area chart for timeline data and PieChart for current distribution data like this 我有时间图数据的面积图和目前分布数据的PieChart这样的 谷歌图表 Both having the same set of colors. 两者具有相同的颜色集。 Area chart's area has some transparency and the Piechart look brighter. 面积图的区域具有一定的透明度,“饼图”看起来更亮。 Is there any configuration to make Piechart area also be lighter? 是否有任何配置可以使Piechart区域也更轻巧?

PS: I can make Area chart brighter by setting 'areaOpacity': 10 but my expectation is to make piechart lighter. PS:我可以通过将'areaOpacity': 10设置为'areaOpacity': 10来使“面积图”变亮,但我希望'areaOpacity': 10亮度。

no standard options for alpha opacity exist for the pie chart 饼图不存在alpha不透明度的标准选项
however, you can manually modify the chart, once it has drawn 但是,绘制图表后,您可以手动修改图表

use standard hex colors to draw the chart, 使用标准的十六进制颜色绘制图表,
then replace the fill attribute on the chart elements, 然后替换图表元素上的填充属性,
with an rgba version of the same color 带有相同颜色的RGBA版本

in order to replace the colors, you will need to use a MutationObserver 为了替换颜色,您将需要使用MutationObserver
as you interact with the chart, elements are added / redrawn 与图表进行交互时,将添加/重绘元素
so you must replace the color every time this occurs 因此您必须在每次发生这种情况时更换颜色

also, you'll want to use lowercase hex strings, 另外,您将要使用小写的十六进制字符串,
in order to find the color on an element 为了找到元素的颜色
they are converted to lowercase when the chart is drawn 绘制图表时它们将转换为小写

see following working snippet... 请参阅以下工作片段...

 google.charts.load('current', { packages: ['corechart'] }).then(function () { var data = google.visualization.arrayToDataTable([ ['Task', 'Hours per Day'], ['Work', 16], ['Eat', 2], ['Sleep', 6] ]); colorsHex = [ '#922b21', '#1e8449', '#007fff' ]; colorsRgba = [ 'rgba(146,43,33,0.6)', 'rgba(30,132,73,0.6)', 'rgba(0,127,255,0.6)' ]; var options = { colors: colorsHex, height: 400, title: 'My Daily Activities' }; var container = document.getElementById('chart_div'); var chart = new google.visualization.PieChart(container); var observer = new MutationObserver(function (mutations) { mutations.forEach(function (mutation) { mutation.addedNodes.forEach(function (node) { changeColor(node); }); }); }); observer.observe(container, { childList: true, subtree: true }); chart.draw(data, options); function changeColor(element) { if (element.getAttribute('fill') !== null) { var colorIndex = colorsHex.indexOf(element.getAttribute('fill')); if (colorIndex > -1) { element.setAttribute('fill', colorsRgba[colorIndex]); } } else { Array.prototype.forEach.call(element.children, function(child) { changeColor(child); }); } } }); 
 <script src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div"></div> 

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

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