I am using amcharts4 and trying to display last value label in a single line chart as shown in this image
Picture : last value label :
My chart's data come from an external data, .csv file
I am using code from : show-last-value-at-the-end-of-lineseries, which originally using : "function generateChartData()" as random source of data.
Everything work fine when I replace source of data, with the new code like this :
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>csv read</title>
<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: 500px;
background-color: #222222;
}
</style>
<script>
window.console = window.console || function(t) {};
</script>
<script>
if (document.location.search.match(/type=embed/gi)) {
window.parent.postMessage("resize", "*");
}
</script>
</head>
<body translate="no" >
<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>
<script id="rendered-js" >
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.dataSource.url = "./sampel.csv";
chart.dataSource.updateCurrentData = true;
chart.dataSource.reloadFrequency = 1000;
chart.dataSource.parser = new am4core.CSVParser();
chart.dataSource.parser.options.useColumnNames = true;
// Create axes
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.minGridDistance = 50;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.renderer.opposite = true;
// Create series
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.valueY = "visits";
series.dataFields.dateX = "date";
series.strokeWidth = 2;
series.minBulletDistance = 10;
series.tooltipText = "{valueY}";
series.tooltip.pointerOrientation = "vertical";
series.tooltip.background.cornerRadius = 20;
series.tooltip.background.fillOpacity = 0.5;
series.tooltip.label.padding(12, 12, 12, 12);
var range = valueAxis.axisRanges.create();
// range.value = chart.data[chart.data.length - 1].visits;
range.grid.stroke = am4core.color("#396478");
range.grid.strokeOpacity = 0;
range.label.text = "" + range.value;
range.label.background.fill = series.stroke;
range.label.fill = am4core.color("#fff");
range.label.padding(5, 10, 5, 10);
// Add scrollbar
// chart.scrollbarX = new am4charts.XYChartScrollbar();
// chart.scrollbarX.series.push(series);
// Add cursor
chart.cursor = new am4charts.XYCursor();
chart.cursor.xAxis = dateAxis;
chart.cursor.snapToSeries = series;
</script>
</body>
</html>
The csv data I used are :
date,visits
10/21/2021,167
10/19/2021,145
10/18/2021,140
10/15/2021,135
10/14/2021,130
10/13/2021,125
10/12/2021,120
10/11/2021,115
10/8/2021,110
10/7/2021,105
10/6/2021,100
10/5/2021,95
I intentionally disabled the code:
// range.value = chart.data[chart.data.length - 1].visits;
,because the code can not work in external data like csv file in order to display last value data in Y axes. Kindly need suggestion to replace it with correct code. Thanks.
