javascript - 设置堆积条的颜色(谷歌图表)

标签 javascript charts google-visualization

使用谷歌图表,我正在尝试自定义 this 上某些部分的颜色堆积条

$(function() {
    google.load('visualization', '1', { 'callback': drawChart, 'packages': ['corechart'] });
        function drawChart() {
            var data = google.visualization.arrayToDataTable([
                  ['Grade', 'NG (% of total Results)', 'F (% of total Results)', 'E (% of total Results)', 'D3 (% of total Results)',
                   'D2(% of total Results)', 'D1 (% of total Results)','C3 (% of total Results)','C2 (% of total Results)','C1 (% of total Results)', 'B3 (% of total Results)','B2 (% of total Results)', 'B1 (% of total Results)','A2 (% of total Results)', 'A1 (% of total Results)',  { role: 'annotation' } ],
                  ["English", 5, 5, 5,5, 5, 5, 5, 5,5, 5,5, 5,5 ,35, ''],                   //use german[2] index of variables for each percent
                  ["Maths", 5, 5, 5,5, 5, 5, 5, 5,5, 5,5, 5,20 ,20, '']
                ]);

            var view = new google.visualization.DataView(data);
                var options = {
                  width: 600,
                  height: 400,
                  legend: { position: 'none'},
                  bar: { groupWidth: '75%' },
                  colors: ['#dfdfdf','#d4d4d4','#d2d2d2','#d8d8d8','#e3e3e3','#d4d4d4','#dcdcdc','d7d7d7','dadada','d9d9d9','d6d6d6'],
                  isStacked: true,
                  hAxis: { 
                        viewWindowMode:'explicit',
                        viewWindow: {
                            max:101,
                            min:0
                        }
                    },
                 chartArea: {'height': '90%'}
                };

                var chart = new google.visualization.BarChart(document.getElementById("columnchart_values"));
                chart.draw(view, options);
}

我想更改每个栏上一个部分的颜色,例如英语的 35 值和数学的最后 20 值。我无法使用样式 Angular 色列将其硬编码,因为我需要根据用户信息更改颜色的值。我尝试了以下各种方法,但均无济于事。谢谢。

data.setProperty(0,0,{                          
    type: "string",
    role: "style" }, 'red');

最佳答案

我认为这里的问题是你有 14 个不同的系列和 2 行。 如果您尝试使用类似 series: { 4: { color: "blue"}} 按系列设置样式,您将更改两行该系列的颜色。

如果您尝试向数据添加新列以设置如下所示的样式:

        var data = google.visualization.arrayToDataTable([
              ['Grade', 'NG (% of total Results)', 'F (% of total Results)', 'E (% of total Results)', 'D3 (% of total Results)',
               'D2(% of total Results)', 'D1 (% of total Results)','C3 (% of total Results)','C2 (% of total Results)','C1 (% of total Results)', 'B3 (% of total Results)','B2 (% of total Results)', 'B1 (% of total Results)','A2 (% of total Results)', 'A1 (% of total Results)',  { role: 'annotation' }, {type: 'string', role: 'style'} ],
            ["English", 5, 5, 5,5, 5, 5, 5, 5,5, 5,5, 5,5 ,35, '', 'color: #cfc'],                   //use german[2] index of variables for each percent
            ["Maths", 5, 5, 5,5, 5, 5, 5, 5,5, 5,5, 5,20 ,20, '', 'color: #0f0']
            ]);

        data.setCell(1,16, "color: red");

您只能设置该行的样式,而无法控制哪一列。

Output

这些都不是您真正想要的。我的直觉是,数据实际上并没有以适合此目的的方式格式化,但我目前没有任何想法。我想我应该向您展示我能做什么,也许这足以让您走上正确的道路,即使我看不到那是什么。

关于javascript - 设置堆积条的颜色(谷歌图表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29334826/

相关文章:

javascript - 同一用户问题的 XMPP 多个 session

javascript - Twitter Bootstrap 和 Snap.js

charts - JavaFX 2.x : How to Remove XY Line Chart once plotted?

jquery - 以月或年 xaxis 进行 float

javascript - 如何从谷歌图表的工具提示中删除数字和百分比?

api - 我可以在Google Geochart图表中自定义工具提示文本吗?

javascript - function1 完成后运行 function2

excel - 如何使用 VBA 将我的图片放置在现有 powerpoint 幻灯片的幻灯片中心?

javascript - 谷歌图表中的条形重叠问题 - 组合图表

javascript - 是否可以在没有 ActiveX 的情况下运行 JS/html 中的批处理文件?