【问题标题】:Google Visualization How do change the background color column chart based on logicGoogle Visualization 如何根据逻辑更改背景颜色柱形图
【发布时间】:2013-09-13 10:58:56
【问题描述】:

我有一个如下图的柱形图。我想要设计出色的绿色酒吧,因为它超过平均水平,非常适合蓝色,适合红色。这可能吗?

根据条形图/单元格的值,我想更改柱形图的背景颜色

【问题讨论】:

  • 您想根据数值为它们着色还是应该“优秀”始终为绿色、“非常好”始终为蓝色、“一般”始终为红色?
  • @asgallant 嘿,感谢您回来,它基于数值。例如,如果优秀中的值低于 80,则变为黄色,高于 80 变为绿色。那么 NumericFormatter 会起作用吗?它不适合我
  • 不,NumberFormatter 不会做你想做的事。我写了一个按值更改条形颜色的技巧,请参阅下面的答案。

标签: charts logic google-visualization bar-chart


【解决方案1】:

由于所有条形都按数据系列着色,因此您必须将条形分成不同的系列以使它们具有不同的颜色。您可以使用 DataView 中的计算列来完成此操作:

function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Name');
    data.addColumn('number', 'Value');
    data.addRows([
        ['Foo', 2],
        ['Bar', 4],
        ['Baz', -3],
        ['Cud', 6],
        ['Waq', -8],
        ['Bat', -2],
        ['Cad', 5],
        ['Giv', 3],
        ['Muj', 9],
        ['Lof', -7],
        ['Duq', 5],
        ['Kaf', 1],
        ['Zij', 4]
    ]);

    var view = new google.visualization.DataView(data);
    // add as many different series as needed
    // make sure all possible values are covered, so you don't have any gaps where a data point can't be assigned to a series
    view.setColumns([0, {
        type: 'number',
        label: 'Value',
        calc: function (dt, row) {
            // if the value is less than 0, add to the red series
            return (dt.getValue(row, 1) < 0) ? dt.getValue(row, 1) : null;
        }
    }, {
        type: 'number',
        label: 'Value',
        calc: function (dt, row) {
            // if the value is greater than or equal to 0, add to the green series
            return (dt.getValue(row, 1) >= 0) ? dt.getValue(row, 1) : null;
        }
    }]);

    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(view, {
        legend: 'none',
        isStacked: true,
        height: 300,
        width: 800,
        // set the colors to use for the series (in the same order as the columns in the view)
        colors: ['red', 'green']
        /* or you can use the series.<series index>.color option to assign colors to specific series:
        series: {
            0: {
                color: 'red'
            },
            1: {
                color: 'green'
            }
        }
        */
    });
}
google.load('visualization', '1', {packages: ['corechart'], callback: drawChart});

在这里查看它的工作原理:http://jsfiddle.net/asgallant/dMWWk/

【讨论】:

  • 啊,现在我明白了,但是用户设置的优秀目标将是 80。嗯,我也应该开始适应它
  • 这更多的是我的期望,只是我想要一个指示目标的红色虚线值 100(因为它是目标)jsfiddle.net/dMWWk/4 你能编辑小提琴以匹配那个吗,我也在做同样的事情
  • 这是一个调整后的小提琴:jsfiddle.net/asgallant/dMWWk/5。如果要绘制线条,则需要多于 1 行数据。
  • 我看不到目标行,但代码在那里。我应该做更多的事情来显示目标线
  • 至少需要两行数据才能画线。
猜你喜欢
  • 2018-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2016-08-27
  • 2015-12-31
相关资源
最近更新 更多