]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add chart data property setter and unit tests
authorSimon Brunel <simonbrunel@users.noreply.github.com>
Sat, 11 Feb 2017 15:02:15 +0000 (16:02 +0100)
committerEvert Timberg <evert.timberg+github@gmail.com>
Sat, 18 Feb 2017 16:53:39 +0000 (11:53 -0500)
Chart data can now be entirely replaced using `chart.data = {...}` thanks to the new property setter (instead of using `chart.config.data = {}`). Also update the documentation, as suggested by @ldaguise and @kennethkalmer, with a note about versions prior 2.6.

docs/09-Advanced.md
src/core/core.controller.js
test/controller.bar.tests.js
test/core.controller.tests.js
test/core.tooltip.tests.js

index bdf02e7044bacf9dc9ecab89d5fab35df8d4be0c..9d1f28341370374f5bfeb67c78dd678cc604e52d 100644 (file)
@@ -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
@@ -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: [...]}`.
+
 #### .reset()
 
 Reset the chart to it's state before the initial animation. A new animation can then be triggered using `update`.
index 02433ce5292d9c69480d5c63f0d484d6f171a917..fc71401fa026019328e2b56837476e2fa6f22615 100644 (file)
@@ -98,9 +98,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;
                                }
                        });
 
index 1a08376704674dc0d3faef433298cbebdf8c064c..db3cf07460e03e166209f3aad07f9b888412fb12 100644 (file)
@@ -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',
index 7b5e4933216e233f616cd2c1617a9743b79eb6cc..68535eb88dfd543db144865ed0bba08ce18ae01a 100644 (file)
@@ -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() {};
 
index e6e3960779f5657e1f162a98e1f3581e2c83ed4d..93cfb3bea360d4719a2c1a0f138e2b1c14463e4d 100755 (executable)
@@ -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);