Search code examples
amcharts

Line in LineSeries disappearing when Scrollbar is used


The problem appearing when the chart have Scrollbar and we try to use it. Line just disappearing from chart. Using AmChart 4

Here is codepen with example: https://codepen.io/smertelny/pen/jdyGZR

Already tried to change values from decimal to integers (just removed comma). No effect.

let data = [
    {
        "value": 27.75,
        "date": new Date(2019, 0, 31)
    },
    {
        "value": 27.77,
        "date": new Date(2019, 0, 30)
    },
    {
        "value":  27.79,
        "date": new Date(2019, 0, 29)
    },
    {
        "value": 27.81,
        "date": new Date(2019, 0, 28)
    },
    {
        "value": 27.78,
        "date": new Date(2019, 0, 27)
    }
]

let chart = am4core.create("chart", am4charts.XYChart);
let xAxis = chart.xAxes.push(new am4charts.DateAxis());
let yAxis = chart.yAxes.push(new am4charts.ValueAxis())

xAxis.dataFields.category = "date";
xAxis.title.text = "Date";

let series = chart.series.push(new am4charts.LineSeries())
let bullet = series.bullets.push(new am4charts.CircleBullet())
series.dataFields.dateX = "date";
series.dataFields.valueY = "value";
series.tooltipText = "{valueY}"
chart.cursor = new am4charts.XYCursor();
series.name = "Value";
series.strokeWidth = 2;
chart.scrollbarX = new am4core.Scrollbar();

chart.data = data;

Am I missing something?


Solution

  • Your dates need to be in ascending order for it to work with a DateAxis. Your dates are descending, which can lead into issues like the scrollbar breaking. Just call reverse() on your array:

    let data = [
        {
            "value": 27.75,
            "date": new Date(2019, 0, 31)
        },
        {
            "value": 27.77,
            "date": new Date(2019, 0, 30)
        },
        {
            "value":  27.79,
            "date": new Date(2019, 0, 29)
        },
        {
            "value": 27.81,
            "date": new Date(2019, 0, 28)
        },
        {
            "value": 27.78,
            "date": new Date(2019, 0, 27)
        }
    ].reverse();
    
    let chart = am4core.create("chart", am4charts.XYChart);
    chart.data = data;
    let xAxis = chart.xAxes.push(new am4charts.DateAxis());
    let yAxis = chart.yAxes.push(new am4charts.ValueAxis())
    
    xAxis.dataFields.category = "date";
    xAxis.title.text = "Date";
    
    let series = chart.series.push(new am4charts.LineSeries())
    let bullet = series.bullets.push(new am4charts.CircleBullet())
    series.dataFields.dateX = "date";
    series.dataFields.valueY = "value";
    series.tooltipText = "{valueY}"
    chart.cursor = new am4charts.XYCursor();
    series.name = "Value";
    series.strokeWidth = 2;
    chart.scrollbarX = new am4core.Scrollbar();
    div {
      width: 100%;
      height: 300px;
    }
    <script src="https://www.amcharts.com/lib/4/core.js"></script>
    <script src="https://www.amcharts.com/lib/4/charts.js"></script>
    <div id="chart"></div>