Here's an interesting example where I have arranged column series with bullet labels. However, you may notice that there are some 0
values present as well. My goal is to establish a minimum width for the series so that even if the value is small or 0
, the bullet label remains visible.
For instance, take a look at the series for the year 2017
, which starts with a 0
. Unfortunately, the label is not fully visible in this case.
/**
* ---------------------------------------
* This demonstration was crafted using amCharts 4.
*
* For more details, please visit:
* https://www.amcharts.com/
*
* Comprehensive documentation can be found at:
* https://www.amcharts.com/docs/v4/
* ---------------------------------------
*/
// Initializing themes
am4core.useTheme(am4themes_animated);
// Theme settings complete
// Generating chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Adding data
chart.data = [{
"year": "2016",
"europe": 1.5,
"namerica": 1.5,
"asia": 1.3,
"lamerica": 0.3,
"meast": 0.2,
"africa": 0
}, {
"year": "2017",
"europe": 0,
"namerica": 1.8,
"asia": 1.4,
"lamerica": 0.3,
"meast": 0.3,
"africa": 0.1
}, {
"year": "2018",
"europe": 1.9,
"namerica": 2.0,
"asia": 0,
"lamerica": 0.4,
"meast": 0.3,
"africa": 0.1
}];
chart.legend = new am4charts.Legend();
chart.legend.position = "right";
// Establishing axes
var categoryAxis = chart.yAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "year";
categoryAxis.renderer.grid.template.opacity = 0;
var valueAxis = chart.xAxes.push(new am4charts.ValueAxis());
valueAxis.min = 0;
valueAxis.renderer.grid.template.opacity = 0;
valueAxis.renderer.ticks.template.strokeOpacity = 0.5;
valueAxis.renderer.ticks.template.stroke = am4core.color("#495C43");
valueAxis.renderer.ticks.template.length = 10;
valueAxis.renderer.line.strokeOpacity = 0.5;
valueAxis.renderer.baseGrid.disabled = true;
valueAxis.renderer.minGridDistance = 40;
// Creating series
function createSeries(field, name) {
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueX = field;
series.dataFields.categoryY = "year";
series.stacked = true;
series.name = name;
series.minWidth = `100px`;
var labelBullet = series.bullets.push(new am4charts.LabelBullet());
labelBullet.locationX = 0.5;
labelBullet.label.text = "{valueX}";
labelBullet.label.fill = am4core.color("#fff");
}
createSeries("europe", "Europe");
createSeries("namerica", "North America");
createSeries("asia", "Asia");
createSeries("lamerica", "Latin America");
createSeries("meast", "Middle East");
createSeries("africa", "Africa");
chart.legend.position = 'bottom';
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: 500px;
}
<script src="https://cdn.amcharts.com/lib/4/core.js"></script>
<script src="https://cdn.amcharts.com/lib/4/charts.js"></script>
<script src="https://cdn.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>