简体   繁体   English

如何在 amCharts v4 中有条件地设置 XY 条形图的属性?

[英]How to conditionally set properties on XY bar chart in amCharts v4?

Hi I would like to format my XY Bar chart so that if a data value is 0, instead of showing the bulletLabel for that value in the center of the bar it will show it located at the front of the chart outside.嗨,我想格式化我的 XY 条形图,以便如果数据值为 0,而不是在条形中心显示该值的 bulletLabel,而是将其显示在外部图表的前面。

I would like to apply these properties to the bullet label:我想将这些属性应用于子弹 label:

labelBullet.label.horizontalCenter = 'left';
labelBullet.locationX = 0;
labelBullet.label.truncate = false;
labelBullet.label.hideOversized = false;

I would thought about using an adapter, but I am not sure how to write it.我会考虑使用适配器,但我不知道如何编写它。 Here is my sad attempt:这是我悲伤的尝试:

labelBullet.label.adapter.add('label', function (value, target) {
    if (!target.dataItem) {
    return value;
    }
    var values = target.dataItem.values;
    if (values.valueY.value === 0) {
       labelBullet.label.horizontalCenter = 'left';
       labelBullet.locationX = 0;
       labelBullet.label.truncate = false;
       labelBullet.label.hideOversized = false;
    }
});

I am not sure how to check the data value or tell it that I want to apply those properties if the data point value is 0.如果数据点值为 0,我不确定如何检查数据值或告诉它我要应用这些属性。

Here is my series creator:这是我的系列创作者:

let series = chart.series.push(new am4charts.ColumnSeries());
series.name = id[0].toUpperCase() + `${id}`.slice(1);
series.dataFields.valueX = field;
series.dataFields.categoryY = 'school';
series.sequencedInterpolation = true;
series.columns.template.height = am4core.percent(30);
series.columns.template.tooltipText = '{categoryY}\n {name}: [bold]{valueX}[/]';

let labelBullet = series.bullets.push(new am4charts.LabelBullet());
labelBullet.locationX = 0.5;
labelBullet.label.text = '{valueX}';
labelBullet.label.fill = am4core.color('#fff');
labelBullet.label.hideOversized = true;

Here is a sample of data:这是一个数据示例:

[
    {
     school: "Fort Lewis College",
     totalInBaseField: "33"
    },
    {
     school: "Adams State University",
     totalInBaseField: "0"
    }
    {
     school: "University of Colorado Colorado Springs",
     totalInBaseField: "141"
    }
]

The result should like like this: Here is bar with value > 0结果应该是这样的: 这是值 > 0 的条形图

value > 0值 > 0

Here is bar with value === 0这是值 === 0 的条形图

value === 0值 === 0

This is the desired change:这是所需的更改:

if value === 0如果值 === 0

I was trying to follow this post as an example: How to conditionally set colors and make gradient to pie chart slices in amCharts v4?我试图以这篇文章为例: 如何有条件地设置 colors 并在 amCharts v4 中对饼图切片进行渐变?

To set these properties:要设置这些属性:

labelBullet.label.horizontalCenter = 'left';
labelBullet.locationX = 0;
labelBullet.label.truncate = false;
labelBullet.label.hideOversized = false;

you should try an adapter for each property.您应该为每个属性尝试一个适配器。

Note that an adapter must return a value.请注意,适配器必须返回一个值。 So you should try something like that:所以你应该尝试这样的事情:

labelBullet.label.adapter.add('horizontalCenter', function(x, target) {
    if(!target.dataItem) {
      return x;
    }
    var values = target.dataItem.values;
    if(values.valueY.value === 0) {
       return 'left';
    } else {
      return 'right';
    }
});

labelBullet.adapter.add('locationX', function(x, target) {
    if(!target.dataItem) {
      return x;
    }
    var values = target.dataItem.values;
    if(values.valueY.value === 0) {
       return 0;
    } else {
      return SomethingElse;
    }
});

labelBullet.label.adapter.add('truncate', function(x, target) {
    if(!target.dataItem) {
      return x;
    }
    var values = target.dataItem.values;
    if(values.valueY.value === 0) {
       return false;
    } else {
      return true;
    }
});

etc.等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM