当由 series.columns.template.propertyFields.fill 设置时,图例不遵循系列的颜色,就像使用 series.fill 时一样
Posted
技术标签:
【中文标题】当由 series.columns.template.propertyFields.fill 设置时,图例不遵循系列的颜色,就像使用 series.fill 时一样【英文标题】:legend do not follow colors of series when it is set by series.columns.template.propertyFields.fill as when it is with series.fill 【发布时间】:2019-08-01 12:12:05 【问题描述】:当由 series.columns.template.propertyFields.fill 设置时,Legend 不会像使用 series.fill 时那样遵循系列的颜色 该示例位于 https://jsfiddle.net/bosiljkakostic1/de16nbu7/4/ 当你取消注释时
//series1.fill = "red";
//series2.fill = "blue";
//series3.fill = "green";
例子效果很好,但如果颜色是由
series1.columns.template.propertyFields.fill = "color1";
series2.columns.template.propertyFields.fill = "color2";
series3.columns.template.propertyFields.fill = "color3";
图例颜色不遵循系列颜色 完整代码:
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<div id="chartdiv"></div>
<script>
/**
* ---------------------------------------
* This demo was created using amCharts 4.
*
* For more information visit:
* https://www.amcharts.com/
*
* Documentation is available at:
* https://www.amcharts.com/docs/v4/
* ---------------------------------------
* MODIFIED by Bosiljka Kosic, adding legend
* and more series to describe the problem :
* legend do not follow colors of series
* when it is set by
* series.columns.template.propertyFields.fill
* as when it is with series.fill
*/
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.data = [
"category": "Research & Development",
"value1": 450,
"color1": "red",
"value2": 1200,
"color2": "blue",
"value3": 1500,
"color3": "green"
,
"category": "Marketing",
"value1": 700,
"color1": "red",
"value2": 1000,
"color2": "blue",
"value3": 1200,
"color3": "green"
,
"category": "Distribution",
"value1": 600,
"color1": "red",
"value2": 400,
"color2": "blue",
"value3": 1100,
"color3": "green"
];
// Create axes
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "category";
categoryAxis.renderer.grid.template.location = 0;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
// Create series
var series1 = chart.series.push(new am4charts.ColumnSeries());
series1.dataFields.valueY = "value1";
series1.dataFields.categoryX = "category";
series1.columns.template.propertyFields.fill = "color1";
var series2 = chart.series.push(new am4charts.ColumnSeries());
series2.dataFields.valueY = "value2";
series2.dataFields.categoryX = "category";
series2.columns.template.propertyFields.stroke = "color2";
var series3 = chart.series.push(new am4charts.ColumnSeries());
series3.dataFields.valueY = "value3";
series3.dataFields.categoryX = "category";
series3.columns.template.propertyFields.stroke= "color3";
series1.columns.template.propertyFields.stroke = "color1";
series2.columns.template.propertyFields.fill = "color2";
series3.columns.template.propertyFields.fill = "color3";
//series1.fill = "red";
//series2.fill = "blue";
//series3.fill = "green";
series1.name="2017";
series2.name="2018";
series3.name="2019";
chart.legend = new am4charts.Legend();
</script>
<style>
body
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
#chartdiv
width: 100%;
height: 300px;
</style>
【问题讨论】:
【参考方案1】:图例标记从系列的填充中获取颜色。当您通过属性字段绑定填充系列的列时,将其注释掉。图例标记的dataItem.dataContext
是它们相关的系列本身,因此列的数据无关紧要,那些永远不会到达标记。即使列/它们的数据以某种方式相关,图表如何知道要选择哪一列来设置图例项的样式?
您可以做的是,每次列着色时,更新列系列的图例标记(或等到列被实例化,即 columns.template.once("datavalidated", ...)
而不是适配器,所以它只完成一次)。
为了customize the legend markers,我们需要将useDefaultMarker
设置为true
。
chart.legend.markers.template.useDefaultMarker = true;
chart.series.each(function(series)
series.columns.template.adapter.add("fill", function(fill, target)
chart.legend.markers.each(function(marker)
if (marker.dataItem.dataContext === target.dataItem.component)
marker.children.getIndex(0).fill = fill;
);
return fill;
);
);
你的代码的分支:
https://jsfiddle.net/notacouch/ot5uadez/
【讨论】:
您可以检查系列类型,例如 if(series.className === "ColumnSeries") else if (series.className === "LineSeries") 并像这样更改检查 LineSeries 事件: series .adapter.add("fill", function (fill, target) @UmutBebek 啊,我明白了。在这种情况下,您可以将series.columns.template
替换为根据您提到的条件进行调整的变量。我还没有测试过这些,但这是让代码灵活/抽象的一种可能性。以上是关于当由 series.columns.template.propertyFields.fill 设置时,图例不遵循系列的颜色,就像使用 series.fill 时一样的主要内容,如果未能解决你的问题,请参考以下文章
当由 series.columns.template.propertyFields.fill 设置时,图例不遵循系列的颜色,就像使用 series.fill 时一样