Skip to content
Advertisement

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.

I would like to apply these properties to the bullet 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.

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

value > 0

Here is bar with value === 0

value === 0

This is the desired change:

if value === 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?

Advertisement

Answer

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.

Advertisement