The Wayback Machine - https://web.archive.org/web/20250602045249/https://github.com/chartjs/Chart.js/pull/7742
Skip to content

Added gridlines between ticks #7742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

JoostBaars
Copy link

Adds the possibility to draw gridlines without a label (between labeled gridlines).

A badly working hack for this is posted on stackoverflow: https://stackoverflow.com/questions/42487438/different-gridline-steps-on-chart-js-line-chart. But the scaling does not work properly anymore with this hack.

Therefore, this PR adds the possibility to configure these gridlines from the gridline options. The number of gridlines can be configured as well as the width of the gridlines.

A working fiddle is shown here: https://jsfiddle.net/L4b7sq9u/4/.

@@ -24,6 +24,9 @@ The grid line configuration is nested under the scale configuration in the `grid
| `tickMarkLength` | `number` | | | `10` | Length in pixels that the grid lines will draw into the axis area.
| `offsetGridLines` | `boolean` | | | `false` | If true, grid lines will be shifted to be between labels. This is set to `true` for a bar chart by default.
| `z` | `number` | | | `0` | z-index of gridline layer. Values <= 0 are drawn under datasets, > 0 on top.
| `linesBetweenTicks` | `number` | | | `0` | Number of lines between the labeled gridlines
| `linesBetweenTicksWidth` | `number` | | | `0.5` | Stroke width of the `linesBetweenTicks` gridlines
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to have a full set of config options for these (color, dash, etc). Alternatively, have these lines appear in the scriptable gridline callbacks.

ty1 = ty2 = y1 = y2 = alignedLineValue;
let lineDistance = 0;
if (gridLines.linesBetweenTicks !== 0) {
lineDistance = (lineValue - previousLineValue) / (gridLines.linesBetweenTicks + 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding these into the ticks array when it is built? It would require ensuring the label does not show, but would mean that the extra config options wouldn't be needed. @kurkle thoughts?

Copy link
Author

@JoostBaars JoostBaars Sep 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etimberg Sure, but this would mean that these lines cannot be configured on their own. I think it would be useful to have config options for these lines (at least the width, but dash and color would also be useful to add as well).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etimberg do you think this feature could be useful for ChartJS? and what would you like to have changed compared to this pull request? More configurations (width, dash, and color?)? And do you want to access this feature in a different way, for example adding it directly in the ticks array or in a callback? Then I could look at it again and implement it that way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JoostBaars sorry for the delay. I was hoping @kurkle could provide some input before I requested changes.

At minimum, I think the configuration should include the same options as grid lines (width, dash, colour). In terms of accessing these values through the tick array, I'm torn. On one hand, I think from a modelling perspective they are better put there. The time scale already has the concept of major/minor ticks and there is configuration overrides for the major ticks. https://www.chartjs.org/docs/master/axes/styling#major-tick-configuration

V2 used to have a minor tick override, but it was removed as it turned out to not be very useful. Perhaps this could be leveraged to format these intermediate grid lines?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etimberg Alright, I'll add the other options as the gridlines. I will look more into the location where to access these values (the minor tick override of V2 or the tick array).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I've been busy the last time... I have added the other settings but did not look for a better place yet to store these parameters.

@chartjs chartjs deleted a comment from adryanfe0302 Oct 29, 2020
@kurkle
Copy link
Member

kurkle commented Feb 5, 2021

Sorry for the late comment.

Some generic thoughts:

  • Some use cases might want more than one line between the ticks.
  • This can already be achieved with some tricks: https://codepen.io/kurkle/pen/ExNPewz
  • (The scriptable context for gridline border color looks incorrect, see the pen above. This is unrelated)
  • This part of the code is not very well structured (ticks / gridlines). Generally thinking, adding to it makes it harder to refactor. I haven't taken a good look at it (yet). That's also why I haven't commented on this earlier.

And some specific thoughts:

  • I think it would be a good addition to have the intermediate gridlines configurable separately to ticks.
  • Adding to ticks array feels a bit hacky and would need to be considered everywhere that array is used (including extensions).

Would this make any sense:

gridLines: {
  intermediate: {
    count: 1,
    borderColor: 'green' //same names of options with `gridLines` scope, and fallback to there
  },
  borderColor: 'red',
  borderDash: [1, 2]
}

@kurkle
Copy link
Member

kurkle commented Sep 5, 2021

Does not look like this is going anywhere.

@kurkle kurkle closed this Sep 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants