Search code examples
javascriptamcharts4candlestick-chart

Weekly data not displaying correctly in amCharts candlestick chart


I am having some trouble getting my weekly data to display correct in candlestick form on amCharts.

When I have setting: dateAxis.skipEmptyPeriods = false when I mouse over a candle, the date in the tooltip, doesn't correspond to the start date of the candle. For example, in the example below, the date on the second candle is 2020-01-06, but when I mouse over, it displays 2020-01-04.

When I have setting: dateAxis.skipEmptyPeriods = true many of the candles disappear. For example, the first candle is 2019-12-30, the next candle is 2020-02-02 (no candle for Jan), and the next candle is 2021-03-09.

Does anybody have a working example of weekly candlestick data on amCharts, or have insight into why the above is happening?

I hope that I'm able to explain my problem properly. It's simple to see when looking at the graph.

Here is a basic script with some weekly data that shows the problems:

am4core.ready(function() {

// Themes begin
//am4core.useTheme(am4themes_animated);
// Themes end

var chart = am4core.create("chartdiv", am4charts.XYChart);
chart.paddingRight = 20;

chart.dateFormatter.inputDateFormat = "yyyy-MM-dd";

var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.skipEmptyPeriods = true;
//dateAxis.renderer.grid.template.location = 0;

var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.tooltip.disabled = true;

var series = chart.series.push(new am4charts.CandlestickSeries());
series.dataFields.dateX = "date";
series.dataFields.valueY = "close";
series.dataFields.openValueY = "open";
series.dataFields.lowValueY = "low";
series.dataFields.highValueY = "high";
series.simplifiedProcessing = true;
series.tooltipText = "Open:${openValueY.value}\nLow:${lowValueY.value}\nHigh:${highValueY.value}\nClose:${valueY.value}";

chart.cursor = new am4charts.XYCursor();

// a separate series for scrollbar
var lineSeries = chart.series.push(new am4charts.LineSeries());
lineSeries.dataFields.dateX = "date";
lineSeries.dataFields.valueY = "close";
// need to set on default state, as initially series is "show"
lineSeries.defaultState.properties.visible = false;

// hide from legend too (in case there is one)
lineSeries.hiddenInLegend = true;
lineSeries.fillOpacity = 0.5;
lineSeries.strokeOpacity = 0.5;

var scrollbarX = new am4charts.XYChartScrollbar();
scrollbarX.series.push(lineSeries);
chart.scrollbarX = scrollbarX;

chart.data = [ {
    "date": "2019-12-30",
    "open": "136.65",
    "high": "136.96",
    "low": "134.15",
    "close": "136.49"
  }, {
    "date": "2020-01-06",
    "open": "135.26",
    "high": "135.95",
    "low": "131.50",
    "close": "131.85"
  }, {
    "date": "2020-01-13",
    "open": "132.90",
    "high": "135.27",
    "low": "128.30",
    "close": "135.25"
  }, {
    "date": "2020-01-20",
    "open": "134.94",
    "high": "137.24",
    "low": "132.63",
    "close": "135.03"
  }, {
    "date": "2020-01-27",
    "open": "136.76",
    "high": "136.86",
    "low": "132.00",
    "close": "134.01"
  }, {
    "date": "2020-02-03",
    "open": "131.11",
    "high": "133.00",
    "low": "125.09",
    "close": "126.39"
  }, {
    "date": "2020-02-10",
    "open": "123.12",
    "high": "127.75",
    "low": "120.30",
    "close": "125.00"
  }, {
    "date": "2020-02-17",
    "open": "128.32",
    "high": "129.35",
    "low": "126.50",
    "close": "127.79"
  }, {
    "date": "2020-02-24",
    "open": "128.29",
    "high": "128.30",
    "low": "123.71",
    "close": "124.03"
  }, {
    "date": "2020-03-02",
    "open": "122.74",
    "high": "124.86",
    "low": "119.65",
    "close": "119.90"
  }, {
    "date": "2020-03-09",
    "open": "117.01",
    "high": "118.50",
    "low": "111.62",
    "close": "117.05"
  }, {
    "date": "2020-03-16",
    "open": "122.01",
    "high": "123.50",
    "low": "119.82",
    "close": "122.06"
  }, {
    "date": "2020-03-23",
    "open": "123.96",
    "high": "124.50",
    "low": "120.50",
    "close": "122.22"
  }, {
    "date": "2020-03-30",
    "open": "122.21",
    "high": "128.96",
    "low": "121.00",
    "close": "127.57"
  }, {
    "date": "2020-04-06",
    "open": "131.22",
    "high": "132.75",
    "low": "130.33",
    "close": "132.51"
  }, {
    "date": "2020-04-14",
    "open": "133.09",
    "high": "133.34",
    "low": "129.76",
    "close": "131.07"
  }, {
    "date": "2020-04-20",
    "open": "130.53",
    "high": "135.37",
    "low": "129.81",
    "close": "135.30"
  }, {
    "date": "2020-04-28",
    "open": "133.39",
    "high": "134.66",
    "low": "132.10",
    "close": "132.25"
  }, {
    "date": "2020-05-04",
    "open": "130.99",
    "high": "132.41",
    "low": "126.63",
    "close": "126.82"
  }, {
    "date": "2020-05-11",
    "open": "129.88",
    "high": "134.18",
    "low": "129.54",
    "close": "134.08"
  }, {
    "date": "2020-05-18",
    "open": "132.67",
    "high": "138.25",
    "low": "132.30",
    "close": "136.25"
  }, {
    "date": "2020-05-25",
    "open": "139.49",
    "high": "139.65",
    "low": "137.41",
    "close": "138.48"
  }, {
    "date": "2020-06-01",
    "open": "139.94",
    "high": "145.73",
    "low": "139.84",
    "close": "144.16"
  }, {
    "date": "2020-06-08",
    "open": "144.97",
    "high": "145.84",
    "low": "136.10",
    "close": "136.76"
  }, {
    "date": "2020-06-15",
    "open": "135.56",
    "high": "137.57",
    "low": "132.71",
    "close": "135.01"
  }, {
    "date": "2020-06-22",
    "open": "132.01",
    "high": "132.30",
    "low": "130.00",
    "close": "131.77"
  }, {
    "date": "2020-06-29",
    "open": "136.99",
    "high": "138.04",
    "low": "133.95",
    "close": "136.71"
  }, {
    "date": "2020-07-06",
    "open": "137.90",
    "high": "138.30",
    "low": "133.75",
    "close": "135.49"
  }, {
    "date": "2020-07-13",
    "open": "135.99",
    "high": "139.40",
    "low": "135.75",
    "close": "136.85"
  }];

}); // end am4core.ready()
<html>
<head>
<script src="http://www.amcharts.com/lib/4/core.js"></script>
<script src="http://www.amcharts.com/lib/4/charts.js"></script>
</head>
<body>
<div id="chartdiv" style="width: 100%; height: 500px;"></div>
</body>
</html>


Solution

  • Sometimes you need to manually specify a baseInterval on your axis if the auto-detected granularity doesn't match your data (reference). In this case, setting a baseInterval of 1 day will fix your chart:

    dateAxis.baseInterval = {
      timeUnit: 'day',
      count: 1
    };
    

    Updated code below:

    am4core.ready(function() {
    
    // Themes begin
    //am4core.useTheme(am4themes_animated);
    // Themes end
    
    var chart = am4core.create("chartdiv", am4charts.XYChart);
    chart.paddingRight = 20;
    
    chart.dateFormatter.inputDateFormat = "yyyy-MM-dd";
    
    var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
    dateAxis.skipEmptyPeriods = true;
    //dateAxis.renderer.grid.template.location = 0;
    
    dateAxis.baseInterval = {
      timeUnit: 'day',
      count: 1
    };
    
    var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
    valueAxis.tooltip.disabled = true;
    
    var series = chart.series.push(new am4charts.CandlestickSeries());
    series.dataFields.dateX = "date";
    series.dataFields.valueY = "close";
    series.dataFields.openValueY = "open";
    series.dataFields.lowValueY = "low";
    series.dataFields.highValueY = "high";
    series.simplifiedProcessing = true;
    series.tooltipText = "Open:${openValueY.value}\nLow:${lowValueY.value}\nHigh:${highValueY.value}\nClose:${valueY.value}";
    
    chart.cursor = new am4charts.XYCursor();
    
    // a separate series for scrollbar
    var lineSeries = chart.series.push(new am4charts.LineSeries());
    lineSeries.dataFields.dateX = "date";
    lineSeries.dataFields.valueY = "close";
    // need to set on default state, as initially series is "show"
    lineSeries.defaultState.properties.visible = false;
    
    // hide from legend too (in case there is one)
    lineSeries.hiddenInLegend = true;
    lineSeries.fillOpacity = 0.5;
    lineSeries.strokeOpacity = 0.5;
    
    var scrollbarX = new am4charts.XYChartScrollbar();
    scrollbarX.series.push(lineSeries);
    chart.scrollbarX = scrollbarX;
    
    chart.data = [ {
        "date": "2019-12-30",
        "open": "136.65",
        "high": "136.96",
        "low": "134.15",
        "close": "136.49"
      }, {
        "date": "2020-01-06",
        "open": "135.26",
        "high": "135.95",
        "low": "131.50",
        "close": "131.85"
      }, {
        "date": "2020-01-13",
        "open": "132.90",
        "high": "135.27",
        "low": "128.30",
        "close": "135.25"
      }, {
        "date": "2020-01-20",
        "open": "134.94",
        "high": "137.24",
        "low": "132.63",
        "close": "135.03"
      }, {
        "date": "2020-01-27",
        "open": "136.76",
        "high": "136.86",
        "low": "132.00",
        "close": "134.01"
      }, {
        "date": "2020-02-03",
        "open": "131.11",
        "high": "133.00",
        "low": "125.09",
        "close": "126.39"
      }, {
        "date": "2020-02-10",
        "open": "123.12",
        "high": "127.75",
        "low": "120.30",
        "close": "125.00"
      }, {
        "date": "2020-02-17",
        "open": "128.32",
        "high": "129.35",
        "low": "126.50",
        "close": "127.79"
      }, {
        "date": "2020-02-24",
        "open": "128.29",
        "high": "128.30",
        "low": "123.71",
        "close": "124.03"
      }, {
        "date": "2020-03-02",
        "open": "122.74",
        "high": "124.86",
        "low": "119.65",
        "close": "119.90"
      }, {
        "date": "2020-03-09",
        "open": "117.01",
        "high": "118.50",
        "low": "111.62",
        "close": "117.05"
      }, {
        "date": "2020-03-16",
        "open": "122.01",
        "high": "123.50",
        "low": "119.82",
        "close": "122.06"
      }, {
        "date": "2020-03-23",
        "open": "123.96",
        "high": "124.50",
        "low": "120.50",
        "close": "122.22"
      }, {
        "date": "2020-03-30",
        "open": "122.21",
        "high": "128.96",
        "low": "121.00",
        "close": "127.57"
      }, {
        "date": "2020-04-06",
        "open": "131.22",
        "high": "132.75",
        "low": "130.33",
        "close": "132.51"
      }, {
        "date": "2020-04-14",
        "open": "133.09",
        "high": "133.34",
        "low": "129.76",
        "close": "131.07"
      }, {
        "date": "2020-04-20",
        "open": "130.53",
        "high": "135.37",
        "low": "129.81",
        "close": "135.30"
      }, {
        "date": "2020-04-28",
        "open": "133.39",
        "high": "134.66",
        "low": "132.10",
        "close": "132.25"
      }, {
        "date": "2020-05-04",
        "open": "130.99",
        "high": "132.41",
        "low": "126.63",
        "close": "126.82"
      }, {
        "date": "2020-05-11",
        "open": "129.88",
        "high": "134.18",
        "low": "129.54",
        "close": "134.08"
      }, {
        "date": "2020-05-18",
        "open": "132.67",
        "high": "138.25",
        "low": "132.30",
        "close": "136.25"
      }, {
        "date": "2020-05-25",
        "open": "139.49",
        "high": "139.65",
        "low": "137.41",
        "close": "138.48"
      }, {
        "date": "2020-06-01",
        "open": "139.94",
        "high": "145.73",
        "low": "139.84",
        "close": "144.16"
      }, {
        "date": "2020-06-08",
        "open": "144.97",
        "high": "145.84",
        "low": "136.10",
        "close": "136.76"
      }, {
        "date": "2020-06-15",
        "open": "135.56",
        "high": "137.57",
        "low": "132.71",
        "close": "135.01"
      }, {
        "date": "2020-06-22",
        "open": "132.01",
        "high": "132.30",
        "low": "130.00",
        "close": "131.77"
      }, {
        "date": "2020-06-29",
        "open": "136.99",
        "high": "138.04",
        "low": "133.95",
        "close": "136.71"
      }, {
        "date": "2020-07-06",
        "open": "137.90",
        "high": "138.30",
        "low": "133.75",
        "close": "135.49"
      }, {
        "date": "2020-07-13",
        "open": "135.99",
        "high": "139.40",
        "low": "135.75",
        "close": "136.85"
      }];
    
    }); // end am4core.ready()
    <html>
    <head>
    <script src="http://www.amcharts.com/lib/4/core.js"></script>
    <script src="http://www.amcharts.com/lib/4/charts.js"></script>
    </head>
    <body>
    <div id="chartdiv" style="width: 100%; height: 500px;"></div>
    </body>
    </html>