Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Axis displaying incorrect tick values for some data point combination

Open dragos-triteanu opened this issue 7 years ago • 4 comments

I've noticed that when using chart.js to plot date related data points, there are times when the graph displays incorrect axis ticks. This happens only for specific data points.

Expected Behavior

The expected behaviour is that the chart correctly generates axis ticks when using a date as an axis based on the provided data set. If the difference between the data points is in the magnitude of days, the chart's axis should reflect that.

Current Behavior

For a multi-day plot, instead of displaying dates such as "20 Feb" the chart displays hours such as "1PM" although the difference of points is in the magnitude of days. Weird thing is that this happens only for specific data.

Possible Solution

I think that there is logic in the chart.js implementation for calculating gaps between data points that are of type "date". I would start investigating how these points are calculated when using the data from Steps To Reproduce (for bugs)

Steps to Reproduce (for bugs)

  1. Setup a chart.js environment. I've tested this out with chart.js 2.8.0 as part of an angular 6 application.
  2. Add a canvas element having the following configuration:
In markup:
<canvas 
[options]="options" 
[colors]="colors" 
[legend]="true" 
[type]="line" 
[datasets]="dataThatWorks">

Please play around with the data provided on **step 3**

</canvas>
// In js / ts file:
public options: any = {
		responsive: true,
		maintainAspectRatio: true,
		aspectRatio: 3.7,
		scales: {
			xAxes: [{
				type: 'time',
				time: {
					format: this.timeFormat,
					tooltipFormat: 'll'
				},
				scaleLabel: {
					display: true,
					labelString: 'Date'
				},
				ticks: {
					source: 'auto'
				},
				bounds: 'data'


			}],
			yAxes: [{
				scaleLabel: {
					display: true,
					labelString: ''
				},
				ticks: {
					beginAtZero: true
				},

				bounds: 'data'

			}]
		},
		legend: {
			position: 'bottom'
		}
	};

lineChartColors = // Irrelevant at the moment


  1. Switch between the two data sets that are provided. They both try to represent values on an Y-axis with dates on the X-axis. One of them generates the ticks correctly while the other generates ticks in the magnitude of hours.
dataThatDoesNotWork = [{
label: 'Mock'
data: [
{x: 1552868040000, y: 190000},
{x: 1553040840000, y: 195000},
{x: 1553213640000, y: 200000},
{x: 1553472840000, y: 205000}
}]
];

dataThatWorks= [{
label: 'Mock'
data: [
{x: 1550880840000, y: 190000},
{x: 1551053640000, y: 195000},
{x: 1553300040000, y: 200000},
{x: 1553472840000, y: 205000}
}]
];

Context

As our project is intended to present various networking metrics to our customers, the date ranges for these points are important.

Environment

Chart.js version 2.8.0 being used as part of an Angular 6 application Browser used for testing is Chrome Version 70.0.3538.77 (Official Build) (64-bit) running on top of Lubuntu.

dragos-triteanu avatar Mar 26 '19 08:03 dragos-triteanu

@dragos-triteanu can you please put the code into an interactive example. E.g. https://codepen.io/pen?template=JXVYzq or https://jsfiddle.net/. That will make it easier for the devs to see exactly what you're seeing and to be able to step through with a debugger. Unfortunately, we don't have the bandwidth to setup examples for every bug report and it scales much better if the bug reporter is able to take this step

benmccann avatar Mar 26 '19 13:03 benmccann

You want to make the times MMM/DD right? Doesnt look like your 'time' is correct:

options: {
  scales: {
    xAxes: [{
      type: 'time',
      time: {
        displayFormats: {
           'millisecond': 'MMM DD',
           'second': 'MMM DD',
           'minute': 'MMM DD',
           'hour': 'MMM DD',
           'day': 'MMM DD',
           'week': 'MMM DD',
           'month': 'MMM DD',
           'quarter': 'MMM DD',
           'year': 'MMM DD',
        }

spaulsteinberg avatar Apr 01 '19 14:04 spaulsteinberg

Closing since this issue doesn't follow the issue guidelines and lacks an interactive example. Please feel free to reopen with an interactive example

benmccann avatar Apr 01 '19 14:04 benmccann

I've created a pen with our data and it seems to work fine : https://codepen.io/anon/pen/KYazaR

We're using charts.js via ng2-charts abstraction which seems to be causing an issue.

Thank you guys!

dragos-triteanu avatar Apr 09 '19 12:04 dragos-triteanu