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

[BUG] Line chart with 1 label and 1 data point does not correctly render in v2.5

Open jordanwillis opened this issue 9 years ago • 3 comments

Expected Behavior

A line chart (using a category scale for x-axis) with a single label and a single point should render the point inline with the x-axis tick label (typically right on top of the y-axis).

Current Behavior

Currently, when you create a line chart with 1 point (and only 1 label), the scale tick (and label) is positioned correctly, however the point in the chart is rendered in the middle of the x-axis

Possible Solution

It looks like this has been fixed for other scales (e.g. #2354) but not category scale. After looking through the code I think logic needs to either be added in core.scale.js in the draw() method to determine if includeOffset need to be passed to me.getPixelForTick(). I'm referring to here .

I'm not sure what side effects this could have on other scales, so it might make more sense to add some logic to scale.category.js in getPixelForValue() to determine if the widthOffset needs to be adjusted by half the value. I am referring to here. Currently, the category scale's getPixelForValue() function is never being passed includeOffset = true for line charts.

I tried attempting both approaches with partial success.

Steps to Reproduce (for bugs)

  1. Create line chart with default options that contains a single 1 label and 1 data point.

Working example

Context

I am often using chart.js for demonstration purposes especially feeding in real time data (using the .update() prorotype method). This issue causes these demonstrations to look strange until at least 2 points and labels are on the chart.

Environment

  • Chart.js version: 2.5.0
  • Browser name and version: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
  • Link to your project: http://codepen.io/jordanwillis/pen/jBayrZ

jordanwillis avatar Mar 16 '17 20:03 jordanwillis

@jordanwillis thanks for reporting this. I believe this is a duplicate of #3579 so I'm closing in favour of the older issue.

If you get something working, I am happy to look at a PR for this. I should note that it would be a good idea to have a setting on the axis to toggle this behaviour because I think the current behaviour was requested specifically by a user.

If you're running into implementation challenges, feel free to post some comments either here or in #3579 and we can iterate 😄

etimberg avatar Mar 16 '17 22:03 etimberg

Sounds good! I start working on a PR for this and will comment if I have any questions.

jordanwillis avatar Mar 21 '17 18:03 jordanwillis

Perfect, sounds good 😄

etimberg avatar Mar 21 '17 22:03 etimberg