由series.columns.template.propertyFields.fill设置时,图例不会遵循series的颜色,就像在series.fill中设置时一样

时间:2019-03-11 08:01:50

标签: javascript colors legend

Legend由series.columns.template.propertyFields.fill设置时不遵循series的颜色,就像在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 个答案:

答案 0 :(得分:0)

图例标记从系列填充中获取颜色。当您通过属性字段绑定用填充序列的注释掉左侧时。图例标记的dataItem.dataContext是它们本身相关的系列,因此列的数据无关紧要,这些数据永远不会与标记关联。即使列/它们的数据在某种程度上是相关的,图表也将如何知道要从哪些列中选择样式来设置图例项?

您可以做的是每次给列着色时,更新列系列的图例标记(或等待直到列被实例化,即columns.template.once("datavalidated", ...)而不是适配器,因此只完成一次)。 / p>

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/