user15157403
user15157403

Reputation:

Amcharts 4 bug on date axis

I am making a two dimensional amchart chart. If the date is before 1945 the shown time is displayed 1 hour forward.

On the image below the cursor is on time 02:00 and the tool-tip shows 03:00. chart image

// Use themes
am4core.useTheme(am4themes_animated);

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

// Add data
chart.data = [{
  "date": new Date(1818, 3, 2, 0, 0, 0),
  "value": 90
}, {
  "date": new Date(1818, 3, 2, 1, 0, 0),
  "value": 102
}, {
  "date": new Date(1818, 3, 2, 2, 0, 0),
  "value": 65
}, {
  "date": new Date(1818, 3, 2, 3, 0, 0),
  "value": 125
}, {
  "date": new Date(1818, 3, 2, 4, 0, 0),
  "value": 55
}];

// Create axes
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.minGridDistance = 50;
dateAxis.renderer.grid.template.location = 0.5;
dateAxis.startLocation = 0.5;
dateAxis.endLocation = 0.5;
chart.dateFormatter.dateFormat = "yyyy-MM-dd HH:mm";

// Create value axis
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());

// Create series
var lineSeries = chart.series.push(new am4charts.LineSeries());
lineSeries.dataFields.valueY = "value";
lineSeries.dataFields.dateX = "date";
lineSeries.strokeWidth = 3;
lineSeries.showOnInit = false;
lineSeries.tooltipText = "{date}: [b]{valueY}[/]";

chart.cursor = new am4charts.XYCursor();
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: 300px;
}
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<script src="//www.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>

I tried changing date format or date timeZone but that didn't work.

Upvotes: 2

Views: 983

Answers (2)

wuerfelfreak
wuerfelfreak

Reputation: 2439

Took me some time to figure it out:

The solution is to change the dates given to chart.data from

new Date(1818, 3, 2, 0, 0, 0)

to

new Date(Date.UTC(1818, 3, 2, 0, 0, 0))

while also setting

chart.dateFormatter.utc = true

Now the axis as well as the data points start at 00:00 in sync.
I tested my solution in multiple timezones, with the same result.

// Use themes
am4core.useTheme(am4themes_animated);

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

// Add data
chart.data = [{
  "date": new Date(Date.UTC(1818, 3, 2, 0, 0, 0)),
  "value": 90
}, {
  "date": new Date(Date.UTC(1818, 3, 2, 1, 0, 0)),
  "value": 102
}, {
  "date": new Date(Date.UTC(1818, 3, 2, 2, 0, 0)),
  "value": 65
}, {
  "date": new Date(Date.UTC(1818, 3, 2, 3, 0, 0)),
  "value": 125
}, {
  "date": new Date(Date.UTC(1818, 3, 2, 4, 0, 0)),
  "value": 55
}];

// Create axes
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.minGridDistance = 50;
dateAxis.renderer.grid.template.location = 0.5;
dateAxis.startLocation = 0.5;
dateAxis.endLocation = 0.5;
chart.dateFormatter.dateFormat = "yyyy-MM-dd HH:mm";
chart.dateFormatter.utc = true

// Create value axis
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());

// Create series
var lineSeries = chart.series.push(new am4charts.LineSeries());
lineSeries.dataFields.valueY = "value";
lineSeries.dataFields.dateX = "date";
lineSeries.strokeWidth = 3;
lineSeries.showOnInit = false;
lineSeries.tooltipText = "{date}: [b]{valueY}[/]";

chart.cursor = new am4charts.XYCursor();
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: 300px;
}
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<script src="//www.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>

Upvotes: 1

hamed dehghan
hamed dehghan

Reputation: 595

have you tried these?

chart.dateFormatter.timezoneOffset = 300;

or

chart.dateFormatter.utc = true;

Upvotes: 0

Related Questions