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 chart data property setter and unit tests #3897

Merged
merged 1 commit into from
Feb 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/09-Advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ myLineChart.destroy();

#### .update(duration, lazy)

Triggers an update of the chart. This can be safely called after replacing the entire data object. This will update all scales, legends, and then re-render the chart.
Triggers an update of the chart. This can be safely called after updating the data object. This will update all scales, legends, and then re-render the chart.

```javascript
// duration is the time for the animation of the redraw in milliseconds
Expand All @@ -34,6 +34,8 @@ myLineChart.data.datasets[0].data[2] = 50; // Would update the first dataset's v
myLineChart.update(); // Calling update now animates the position of March from 90 to 50.
```

> **Note:** replacing the data reference (e.g. `myLineChart.data = {datasets: [...]}` only works starting **version 2.6**. Prior that, replacing the entire data object could be achieved with the following workaround: `myLineChart.config.data = {datasets: [...]}`.

Choose a reason for hiding this comment

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

👏


#### .reset()

Reset the chart to it's state before the initial animation. A new animation can then be triggered using `update`.
Expand Down
4 changes: 4 additions & 0 deletions src/core/core.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,13 @@ module.exports = function(Chart) {
// Add the chart instance to the global namespace
Chart.instances[me.id] = me;

// Define alias to the config data: `chart.data === chart.config.data`
Object.defineProperty(me, 'data', {
get: function() {
return me.config.data;
},
set: function(value) {
me.config.data = value;
}
});

Expand Down
4 changes: 2 additions & 2 deletions test/controller.bar.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,8 +738,8 @@ describe('Bar controller tests', function() {
expect(meta.data[i]._model.base).toBeCloseToPixel(484);
expect(meta.data[i]._model.width).toBeCloseToPixel(40);
expect(meta.data[i]._model).toEqual(jasmine.objectContaining({
datasetLabel: chart.config.data.datasets[1].label,
label: chart.config.data.labels[i],
datasetLabel: chart.data.datasets[1].label,
label: chart.data.labels[i],
backgroundColor: 'red',
borderSkipped: 'top',
borderColor: 'blue',
Expand Down
19 changes: 19 additions & 0 deletions test/core.controller.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ describe('Chart', function() {
expect(chart.data.datasets[1].data).toBe(ds1.data);
});

it('should define chart.data as an alias for config.data', function() {
var config = {data: {labels: [], datasets: []}};
var chart = acquireChart(config);

expect(chart.data).toBe(config.data);

chart.data = {labels: [1, 2, 3], datasets: [{data: [4, 5, 6]}]};

expect(config.data).toBe(chart.data);
expect(config.data.labels).toEqual([1, 2, 3]);
expect(config.data.datasets[0].data).toEqual([4, 5, 6]);

config.data = {labels: [7, 8, 9], datasets: [{data: [10, 11, 12]}]};

expect(chart.data).toBe(config.data);
expect(chart.data.labels).toEqual([7, 8, 9]);
expect(chart.data.datasets[0].data).toEqual([10, 11, 12]);
});

it('should initialize config with default options', function() {
var callback = function() {};

Expand Down
4 changes: 2 additions & 2 deletions test/core.tooltip.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,10 +692,10 @@ describe('Core.Tooltip', function() {
expect(tooltip._view.dataPoints[0].index).toEqual(pointIndex);
expect(tooltip._view.dataPoints[0].datasetIndex).toEqual(datasetIndex);
expect(tooltip._view.dataPoints[0].xLabel).toEqual(
chart.config.data.labels[pointIndex]
chart.data.labels[pointIndex]
);
expect(tooltip._view.dataPoints[0].yLabel).toEqual(
chart.config.data.datasets[datasetIndex].data[pointIndex]
chart.data.datasets[datasetIndex].data[pointIndex]
);
expect(tooltip._view.dataPoints[0].x).toBeCloseToPixel(point._model.x);
expect(tooltip._view.dataPoints[0].y).toBeCloseToPixel(point._model.y);
Expand Down