Pavlin Petkov
Pavlin Petkov

Reputation: 1142

How to make highcharts with dynamic datetime range with monthly interval

I have an array of values

[2.6, 3.2, 4.2, 3.3, 2.7, 4.5, 5.9, 2.7, 1.8, 1.7, 1.3, 1.8, 1, 1.4, 1.3, 1.4, 1.1, 1.1, 1, 1.4, 1, 1.4, 1.3, 1.5, 1.1, 1.2, 1.3, 1.3, 0.8, 0.9, 1, 1.1, 1, 1, 0.8]

As well as a starting date - 2016/08/01, which is Sep 2016

And also an end date - 2019/05/01, which is May 2019

I need to make a chart which has a yAxis with

min: 0.0
max: 6.0

and xAxis which is datetime type

So far I have this config

{
  yAxis: [
    {
      min: 0.0,
      max: 6.0,
      title: {
        text: 'Fill Days'
      }
    }
  ],
  xAxis: {
    type: 'datetime',
    min: Date.UTC(2016, 8, 1),
    max: Date.UTC(2019, 5, 1)
  },
  series: [{
    name: 'Some Name',
    type: 'spline',
    data: data.map((item, i) => [Date.UTC(Math.floor(2016 + (i / 12)), (8 * i) / 12, 1), item])
  }]
};

However The Highchart doesnt look right - https://jsfiddle.net/1utjnyfL/1/

I want to have it incremented Monthly from Sep 16 till May 19 where each element in the data array is taken one by one.

Sep 16 - 2.6
Oct 16 - 3.2
Nov 16 - 4.2
...
May 19 - 0.8

Upvotes: 1

Views: 632

Answers (1)

Ashu
Ashu

Reputation: 2266

You are not considering that you are starting from Sep of a year. Once you change the mapping it will work fine.

var series = data.map((item, i) => [Date.UTC(Math.floor(2016 + ((7 + i) / 12)), (((7 + i) % 12) + 1), 1), item])

Please check the below working example:

data = [2.6, 3.2, 4.2, 3.3, 2.7, 4.5, 5.9, 2.7, 1.8, 1.7, 1.3, 1.8, 1, 1.4, 1.3, 1.4, 1.1, 1.1, 1, 1.4, 1, 1.4, 1.3, 1.5, 1.1, 1.2, 1.3, 1.3, 0.8, 0.9, 1, 1.1, 1, 1, 0.8]

var series = data.map((item, i) => [Date.UTC(Math.floor(2016 + ((7 + i) / 12)), (((7 + i) % 12) + 1), 1), item])


Highcharts.chart('container', {
  yAxis: [{
    min: 0.0,
    max: 6.0,
    title: {
      text: 'Some Text'
    }
  }],
  xAxis: {
    type: 'datetime',
    min: Date.UTC(2016, 8, 1),
    max: Date.UTC(2019, 5, 1)
  },
  series: [{
    name: 'Some Name',
    type: 'spline',
    data: series
  }]
});
<script src="https://code.highcharts.com/highcharts.js"></script>

<div id="container" style="height: 400px; width: 500px"></div>

Upvotes: 1

Related Questions