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

Shared data between charts #2346

Merged
merged 10 commits into from
Apr 27, 2016
Merged

Shared data between charts #2346

merged 10 commits into from
Apr 27, 2016

Conversation

simonbrunel
Copy link
Member

@simonbrunel simonbrunel commented Apr 24, 2016

Important: This PR breaks all tests because now it requires to work on a real chart instance (not a fake chart/context). Modifying all tests will require an important effort, so I prefer to be sure we want to merge these changes before rewriting all tests.

Note: Since this PR is pretty big and touches almost all files, it would be great to consider merging it as soon as possible to avoid too many conflicts with upcoming contributions.


This PR allows sharing config.data between multiple chart instances (charts can be of different types). For details, please review commit messages which should be enough to understand changes. But basically, I moved all meta info under dataset._meta, which is a map from chart id to meta info (chart id is now an integer to speed up lookups).

image

left: master | right: PR

Also changed (fixed) the polar show/hide animations as talk on Slack:

polar-chart-v2

@coveralls
Copy link

Coverage Status

Coverage decreased (-30.3%) to 41.652% when pulling 4935990 on simonbrunel:shared-data into c1d7725 on nnnick:master.

addElementAndReset: function(index) {
this.getDataset().metaData = this.getDataset().metaData || [];
Copy link
Member

Choose a reason for hiding this comment

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

Is the meta.data array defined somewhere else?

Copy link
Member Author

@simonbrunel simonbrunel Apr 24, 2016

Choose a reason for hiding this comment

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

Accessing meta info requires to use the getDatasetMeta() (or getMeta() on a dataset controller), which will build the meta object (so the meta.data array).

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, we should never access the _meta object directly, but always rely on the get(Dataset)Meta() accessors. This to ensure that meta are initialized, but also in the case we decide to change where we store these meta info.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, perfect. I haven't gotten that far down yet so that jumped out at me.

@etimberg
Copy link
Member

The data_label_combo-bar-line.html sample will need an update. Other than that, the samples work for adding data, removing data, adding datasets, and removing datasets. All of the legends work as expected. Tooltips still work for all charts

@simonbrunel
Copy link
Member Author

Yes, I plan to fix this sample while working on the tests

@coveralls
Copy link

Coverage Status

Coverage decreased (-30.3%) to 41.632% when pulling 517d9a9 on simonbrunel:shared-data into ae33b1e on nnnick:master.

@simonbrunel
Copy link
Member Author

@etimberg, can you please review my fix in the last commit?

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling c2c09b6 on simonbrunel:shared-data into * on chartjs:master*.

@etimberg
Copy link
Member

@simonbrunel looks good

@tannerlinsley
Copy link
Contributor

This is great!

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling ac7e267 on simonbrunel:shared-data into * on chartjs:master*.

@simonbrunel
Copy link
Member Author

@etimberg @tannerlinsley @zachpanz88: commit ac7e267 is a rewrite of some tests using the new meta data structure. It would be nice to review it because I had to change a lot of code and might have change the tests logic.

@etimberg
Copy link
Member

@simonbrunel I'll have a detailed look tonight.

@etimberg
Copy link
Member

At first glance the test changes look good 👍

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling d74e97c on simonbrunel:shared-data into * on chartjs:master*.

Meta info are now scoped by chart and moved under the dataset._meta map { chart.id -> meta }. Meta for a specific chart (and dataset) can be accessed using chart.getDatasetMeta(datasetIndex) or from the dataset controller using getMeta(). Note that helpers.uid() now generates an int (instead of a string) to make lookups in the _meta map faster.
Dataset effective type is now stored under meta.type, allowing many charts to share the same dataset but with different types. Also move dataset.bar flag to meta.bar.
Introduced a new meta.hidden 3 states flag (null|true|false) to be able to override dataset.hidden when interacting with the chart (i.e., true or false to ignore the dataset.hidden value). This is required in order to be able to correctly share dataset.hidden between multiple charts.

For example: 2 charts are sharing the same data and dataset.hidden is initially false: the dataset will be displayed on both charts because meta.hidden is null. If the user clicks the legend of the first chart, meta.hidden is changed to true and the dataset is only hidden on the first chart. If dataset.hidden changes, only the second chart will have the dataset visibility updated and that until the user click again on the first chart legend, switching the meta.hidden to null.
New Chart.Element.hidden bool flag storing the visibility state of its associated data. Since elements belong to a specific chart, this change allows to manage data visibility per chart (e.g. when clicking the legend of some charts).

This commit also changes (fixes?) the polar chart animation when data visibility changes. Previous implementation was affected by an edge effect due to the use of NaN as hidden implementation.
Fix access of uninitialized meta data while calculating circumference in the polar area chart by caching the number of visible elements in the update() method. Also make the calculateTotal() of the doughnut chart tolerant of uninitialized meta data.
Since we changed the way how meta data are stores, now unit tests need to work on real Chart instances. This commit brings some helpers to inject/cleanup HTML canvas and it's wrapper into/from the DOM.
Because of differences between testing platforms, introduce a new matcher for (floating) pixel values comparison (currently 2 pixels tolerance).
Also replace the 2 spaces indentation in controller.bar.tests.js by tabs to match the overall code style.
@coveralls
Copy link

Coverage Status

Coverage decreased (-1.7%) to 70.295% when pulling e0353da on simonbrunel:shared-data into b615fe8 on chartjs:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+1.8%) to 73.776% when pulling 83b98dd on simonbrunel:shared-data into b615fe8 on chartjs:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.9%) to 71.032% when pulling eb14481 on simonbrunel:shared-data into cbb88be on chartjs:master.

@simonbrunel
Copy link
Member Author

simonbrunel commented Apr 27, 2016

As agreed with @etimberg, I disabled failing tests to be able to merge that PR. The list includes the following suites (can be updated in gulpfile.js):

  • controller.line.tests.js (@zachpanz88)
  • controller.radar.tests.js (@etimberg)
  • core.layoutService.tests.js
  • defaultConfig.tests.js
  • scale.linear.tests.js (@etimberg)
  • scale.radialLinear.tests.js (@etimberg)
  • scale.time.tests.js (@etimberg)

Note: core.layoutService.tests.js has been rewritten, but is failing only in Chrome on the test server. No idea how to fix it since the difference with expected value is 10px.

@etimberg etimberg merged commit 7c6a14c into chartjs:master Apr 27, 2016
@simonbrunel simonbrunel deleted the shared-data branch April 27, 2016 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants