dis*_*ney 3 amcharts amcharts4
我正在尝试向柱形图添加自定义颜色,因此每列都有不同的颜色。我有以下代码:
__this._chartColours = ['#2776BD', '#00A1D0','#00C195','#7ED321','#A8C600','#C9B600','#E3A600', '#F7941E', '#FC7149'];
__this._chart = am4core.create(__this._tileChartDiv[0], am4charts.XYChart);
if(result.chartDataMap != null)
{
var colorSet = new am4core.ColorSet();
var counter = 0;
$.each(result.chartDataMap, function(xAxis, yAxis)
{
__this._dataProvider.push({"category": xAxis, "column-1": yAxis});
__this._chart.colors.list.push(am4core.color(__this._chartColours[counter]));
});
__this._chart.data = __this._dataProvider;
__this._chart.padding(40, 40, 40, 40);
var categoryAxis = __this._chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.dataFields.category = "category";
categoryAxis.renderer.minGridDistance = 60;
categoryAxis.title.text = result.xAxisTitle;
var label = categoryAxis.renderer.labels.template;
label.wrap = true;
label.maxWidth = 120;
var valueAxis = __this._chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.title.text = result.yAxisTitle;
var series = __this._chart.series.push(new am4charts.ColumnSeries());
series.dataFields.categoryX = "category";
series.dataFields.valueY = "column-1";
series.tooltipText = "{valueY.value}"
series.columns.template.strokeOpacity = 0;
__this._chart.cursor = new am4charts.XYCursor();
}
Run Code Online (Sandbox Code Playgroud)
它根据我创建的数据提供程序很好地呈现图表,但它没有设置颜色。我从这里得到了颜色代码:https : //www.amcharts.com/docs/v4/concepts/colors/。XY 图表和衍生图表部分
我尝试使用主题,但这也不起作用:
function am4themes_myTheme(target)
{
if (target instanceof am4core.ColorSet)
{
$.each(__this._chartColours, function(index, item)
{
target.list.push(am4core.color(item));
});
}
}
am4core.useTheme(am4themes_myTheme);
Run Code Online (Sandbox Code Playgroud)
它将所有列设置为第一种颜色。然后我尝试为每列的 dataProvider 添加一个颜色属性,但它再次将它们全部设置为第一种颜色。
我几乎没有想法。
这里有几个问题。
首先,如果您希望图表仅使用您的颜色,而不是附加到默认图表的ColorSet,您必须通过将s数组分配给(而不是ing 值)来手动覆盖它。Colorchart.colors.listpush
接下来,列的颜色 ( fill) 默认基于其系列。因此,即使您填充图表的ColorSet,也只有每个新系列会获得不同的颜色,而不是每列。
要设置单个列的颜色,它将类似于:
column.fill = am4core.color("#2776BD");
Run Code Online (Sandbox Code Playgroud)
为了让每一列都有自己的颜色,我们可以在列的第一次实例化时设置它,即在它template的event 上。此外,列将具有对其 的属性/引用,因此我们可以使用它 with的方法按顺序分配颜色。inited dataItemindexColorSetgetIndex
因此,您的最终代码可能如下所示:
__this._chart.colors.list = [
am4core.color("#2776BD"),
am4core.color("#00A1D0"),
am4core.color("#00C195"),
am4core.color("#7ED321"),
am4core.color("#A8C600"),
am4core.color("#C9B600"),
am4core.color("#E3A600"),
am4core.color("#F7941E"),
am4core.color("#FC7149")
];
series.columns.template.events.once("inited", function(event){
event.target.fill = chart.colors.getIndex(event.target.dataItem.index);
});
Run Code Online (Sandbox Code Playgroud)
这是我们的简单柱状图演示的一个分支,带有上述代码和您的自定义颜色:
https://codepen.io/team/amcharts/pen/2ef06f392b347412c61bcdcd3439a5c6
| 归档时间: |
|
| 查看次数: |
4599 次 |
| 最近记录: |