像设置argumentField或valueField一样从数据源设置颜色?

时间:2014-01-03 18:17:17

标签: javascript json charts chart.js

鉴于以下数据,我可以利用“颜色”字段设置系列颜色吗?

{ severity: "minor", count: 30, color: 'yellow' },
{ severity: "major", count: 23, color: 'orange' },
{ severity: "critical", count: 12, color: 'red' },
{ severity: "resolved", count: 35, color: 'green' }

这是图表定义。我可以使用argumentField指定severity和valueField来指定count,但是没有这样的颜色参数:

dxPieChart: {
    dataSource: dsAlarmsBySeverity,
    size: {
        width: 275,
        height: 150
    },
    series: [{
        type: 'doughnut',
        argumentField: 'severity',
        valueField: 'count',
        label: {
            visible: false
        }
    }]
}

颜色只需要基于严重程度,所以我会接受任何能够实现这一目标的答案。

1 个答案:

答案 0 :(得分:1)

解决方案如下,您需要添加带有指示返回的customize_point字段:

dxPieChart: {
    dataSource: dsAlarmsBySeverity,
    ...,
    customizePoint: function (point) {
        return {
            color: dsAlarmsBySeverity[point.index].color
        }
    }
}