javascriptanychartanychart-8.2

How to sort x Axis based on dates with anychart Library?


I have a problem with multiples series data. I have three series data (sometimes two), where the x values are dates and the y values are numbers. I'm not able to sort the column bar based on date.

I reproduced the issue here: https://playground.anychart.com/WuqV384h

As you can see, the series data are showed as are written inside the "series" key. The dates should be sorted from 2019 to 2020. A similar issue was asked here.

However, even using the DateTime, I'm not able to visualize the dates in the right order. If I use DateTime, the chart doesn't show the real value of the date (es. x: 2020-02-02), and the chart doesn't fit the container.

Thank you in advance.


Solution

  • The Cartesian Column chart uses an Ordinal scale. It doesn't provide any sorting because the Ordinal scale works with categories (names), it doesn't differ dates or names. So, there are two available solutions:

    1. Use real dateTime xScale that sorts points automatically. But it is a linear scale. Here is the sample.

    2. Use the ordinal scale, but in this case, you need to preprocess your data to get the sorted array of categories and apply it to the xScale. Here is the sample.