Skip to content
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

Add support for ticks.source: 'data' to category scale #4776

Closed
ortonomy opened this issue Sep 19, 2017 · 9 comments
Closed

Add support for ticks.source: 'data' to category scale #4776

ortonomy opened this issue Sep 19, 2017 · 9 comments

Comments

@ortonomy
Copy link

ortonomy commented Sep 19, 2017

Expected Behavior

I expect that using the new 2.7 feature of defining a bar chart using arrays of objects with x/y values e.g. [{x: ..., y: ...}, ...] to generate the x labels and y values on a barchart as per the docs here: http://www.chartjs.org/docs/latest/charts/bar.html

You can also specify the dataset as x/y coordinates.

data: [{x:'2016-12-25', y:20}, {x:'2016-12-26', y:10}]

Current Behavior

See interactive pen here: https://codepen.io/ortonomy/pen/NaxVeZ

Providing a data.dataset with an array of x/y objects (e.g. data: [{x: ..., y: ...}, ...]) does not render all data points. It only renders a single bar, using the first value of the data set.

The only way that I can get it to display multiple values, is by providing the labels: [...] property to the data object. And even then, the number of bars displayed is only equal to the length of labels: [...].

I may be wrong here, but from what I can tell, the reason for providing the array of x/y objects is so the data itself provides the x label.

Steps to Reproduce (for bugs)

code

data: {
        // labels: ['x','x'],
        datasets: [{
            label: '# of Votes',
            data: [{x: 'label', y: 12}, {x: 'label', y: 19}, {x: 'label', y: 3}, {x: 'label', y: 5}, {x: 'label', y: 2}, {x: 'label', y: 3}]
        }]
    },

link
https://codepen.io/ortonomy/pen/NaxVeZ

Context

The data coming back from my API is already in an object: {x: ..., y: ...} format so you can imagine my joy at seeing this being provided as an option. Now I have to split the data to use x: ... for labels: [...], and then y: ... for data: [...].

Environment

  • Chart.js version:
    2.7.0
  • Browser name and version:
    Chrome 61 Mac OS X
  • Link to your project:
    Can't link -- is private.
@ortonomy
Copy link
Author

ortonomy commented Sep 19, 2017

This feature is referenced here: https://github.com/chartjs/Chart.js/releases/tag/v2.7.0

#4456 Allow specifying bar chart via {x, y} data points. Thanks @benmccann

@SanoLitch
Copy link

SanoLitch commented Sep 19, 2017

+1 Also found this bug today

@benmccann
Copy link
Contributor

benmccann commented Sep 19, 2017

@nagix worked on this as well in #4673

@benmccann
Copy link
Contributor

Yes, you still need to specify the labels. It's at least a bit easier for you because you now only need to create the labels instead of creating the labels and transforming the data from your format to array format.

One solution to this would be to add a ticks.source: 'data' just like @simonbrunel added in the time scale in #4507. And it should default to this if the user doesn't specify any labels.

@ortonomy
Copy link
Author

ortonomy commented Sep 20, 2017

Hmmm,

@benmccann -- but why then specify [{x:...,y:...},...] objects if the x:... part is never used. You're right it saves a little bit of time, but I still have to extract the x part into an array of labels.

Moreover, if you provide labels: [...] it overrides any x-value, making it even more pointless.

To actually make use of this feature, the label resolution should be

for type bar chart

  1. if data: { datasets[{data: [{x:...,...},...],...],...},...} else
  2. if data: { labels: ['label',...],... } else
  3. error

I might be missing something of course.

@benmccann
Copy link
Contributor

Yes, I agree with you. It depends on the type of scale that you use. It works the way you'd expect with the time scale. It doesn't work well with the category scale, which is the default scale used by the bar chart.

@tchan
Copy link

tchan commented Oct 5, 2017

@ortonomy Your codepen link does not work for me, it just opens up an new untitled pen on my account over at codepen.

@ortonomy
Copy link
Author

ortonomy commented Oct 6, 2017

@tchan -- thanks for pointing that out. I've updated the link

@benmccann benmccann changed the title [2.7] barchart data: [{x: '2016-01', y: 999}, ... ] does not work as expected Add support for ticks.source: 'data' to category scale Oct 8, 2017
@benmccann benmccann removed this from the Version 2.7.1 milestone Oct 8, 2017
@etimberg
Copy link
Member

etimberg commented Mar 6, 2021

Renders all points in v3.0.0-beta.12 https://codepen.io/etimberg/pen/zYoJPxE

@etimberg etimberg closed this as completed Mar 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants