]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Data editable from chart config
authorTanner Linsley <tannerlinsley@gmail.com>
Thu, 8 Jan 2015 04:45:50 +0000 (21:45 -0700)
committerTanner Linsley <tannerlinsley@gmail.com>
Thu, 8 Jan 2015 04:45:50 +0000 (21:45 -0700)
src/Chart.Bar.js
src/Chart.Doughnut.js
src/Chart.Line.js
src/Chart.PolarArea.js
src/Chart.Radar.js

index 87fb26ddc405267419abcaa749b76435ee3340cc..86d5fc989fbfd213ce1dee9f2a212c3a812e2969 100644 (file)
@@ -48,6 +48,9 @@
                defaults : defaultConfig,
                initialize:  function(data){
 
+                       // Save data as a source for updating of values & methods
+                       this.data = data;
+
                        //Expose options as a scope variable here so we can access it in the ScaleClass
                        var options = this.options;
 
                        this.render();
                },
                update : function(){
+                       //Iterate through each of the datasets, and build this into a property of the chart
+                       helpers.each(this.data.datasets,function(dataset,datasetIndex){
+
+                               helpers.extend(this.datasets[datasetIndex], {
+                                       label : dataset.label || null,
+                                       fillColor : dataset.fillColor,
+                                       strokeColor : dataset.strokeColor,
+                               });
+
+                               helpers.each(dataset.data,function(dataPoint,index){
+                                       helpers.extend(this.datasets[datasetIndex].bars[index], {
+                                               value : dataPoint,
+                                               label : this.data.labels[index],
+                                               datasetLabel: dataset.label,
+                                               strokeColor : dataset.strokeColor,
+                                               fillColor : dataset.fillColor,
+                                               highlightFill : dataset.highlightFill || dataset.fillColor,
+                                               highlightStroke : dataset.highlightStroke || dataset.strokeColor
+                                       });
+                               },this);
+
+                       },this);
+
                        this.scale.update();
                        // Reset any highlight colours before updating.
                        helpers.each(this.activeElements, function(activeElement){
index 35de93c7fd606ad844f0ffc9f1c436f29868b546..d3786f3fbe812242983ec465093080892dd45082 100644 (file)
@@ -46,6 +46,9 @@
                //Config is automatically merged by the core of Chart.js, and is available at this.options
                initialize:  function(data){
 
+                       // Save data as a source for updating of values & methods
+                       this.data = data;
+
                        //Declare segments as a static property to prevent inheriting across the Chart type prototype
                        this.segments = [];
                        this.outerRadius = (helpers.min([this.chart.width,this.chart.height]) - this.options.segmentStrokeWidth/2)/2;
                        },this);
                },
                update : function(){
+
+                       // Map new data to data points
+                       if(this.data.length == this.segments.length){
+                               helpers.each(this.data, function(segment, i){
+                                       helpers.extend(this.segments[i], {
+                                               value : segment.value,
+                                               fillColor : segment.color,
+                                               highlightColor : segment.highlight || segment.color,
+                                               showStroke : this.options.segmentShowStroke,
+                                               strokeWidth : this.options.segmentStrokeWidth,
+                                               strokeColor : this.options.segmentStrokeColor,
+                                               label : segment.label
+                                       });
+                               }, this);
+                               console.log(this.data, this.segments);
+                       } else{
+                               // Data size changed without properly inserting, just redraw the chart
+                               this.initialize(this.data);
+                       }
+
                        this.calculateTotal(this.segments);
 
                        // Reset any highlight colours before updating.
index 34ad85b4d0ae1ae96b40b506385ae42547ef6063..a3acb6a5d25030ef5ac934265cf2eea2ad97a98b 100644 (file)
@@ -59,6 +59,9 @@
                name: "Line",
                defaults : defaultConfig,
                initialize:  function(data){
+                       // Save data as a source for updating of values & methods
+                       this.data = data;
+
                        //Declare the extension of the default point, to cater for the options passed in to the constructor
                        this.PointClass = Chart.Point.extend({
                                strokeWidth : this.options.pointDotStrokeWidth,
                        this.render();
                },
                update : function(){
+                       //Iterate through each of the datasets, and build this into a property of the chart
+                       helpers.each(this.data.datasets,function(dataset,datasetIndex){
+
+                               helpers.extend(this.datasets[datasetIndex], {
+                                       label : dataset.label || null,
+                                       fillColor : dataset.fillColor,
+                                       strokeColor : dataset.strokeColor,
+                                       pointColor : dataset.pointColor,
+                                       pointStrokeColor : dataset.pointStrokeColor,
+                               });
+
+                               helpers.each(dataset.data,function(dataPoint,index){
+                                       helpers.extend(this.datasets[datasetIndex].points[index], {
+                                               value : dataPoint,
+                                               label : this.data.labels[index],
+                                               datasetLabel: dataset.label,
+                                               strokeColor : dataset.pointStrokeColor,
+                                               fillColor : dataset.pointColor,
+                                               highlightFill : dataset.pointHighlightFill || dataset.pointColor,
+                                               highlightStroke : dataset.pointHighlightStroke || dataset.pointStrokeColor
+                                       });
+                               },this);
+
+                       },this);
+
                        this.scale.update();
                        // Reset any highlight colours before updating.
                        helpers.each(this.activeElements, function(activeElement){
index 4dbf2eb92a08681ba8933f1ec97500d8e94f7c3c..326b5a373fd292e4b70351d832891ce19320404b 100644 (file)
@@ -59,6 +59,9 @@
                //Initialize is fired when the chart is initialized - Data is passed in as a parameter
                //Config is automatically merged by the core of Chart.js, and is available at this.options
                initialize:  function(data){
+                       // Save data as a source for updating of values & methods
+                       this.data = data;
+
                        this.segments = [];
                        //Declare segment class as a chart instance specific class, so it can share props for this instance
                        this.SegmentArc = Chart.Arc.extend({
 
                },
                update : function(){
+
+                       // Map new data to data points
+                       if(this.data.length == this.segments.length){
+                               helpers.each(this.data, function(segment, i){
+                                       helpers.extend(this.segments[i], {
+                                               fillColor: segment.color,
+                                               highlightColor: segment.highlight || segment.color,
+                                               label: segment.label,
+                                               value: segment.value,
+                                       });
+                               },this);
+                               console.log(this.data, this.segments);
+                       } else{
+                               // Data size changed without properly inserting, just redraw the chart
+                               this.initialize(this.data);
+                       }
+
                        this.calculateTotal(this.segments);
 
                        helpers.each(this.segments,function(segment){
index 134fd2d021866ccf25cdc4902b3d1f8de59b36ba..4fb593ca6cccdb6b0005a3d36d4366146c654d56 100644 (file)
@@ -67,6 +67,9 @@
                },
 
                initialize: function(data){
+                       // Save data as a source for updating of values & methods
+                       this.data = data;
+
                        this.PointClass = Chart.Point.extend({
                                strokeWidth : this.options.pointDotStrokeWidth,
                                radius : this.options.pointDotRadius,
                        this.update();
                },
                update : function(){
+                       //Iterate through each of the datasets, and build this into a property of the chart
+                       helpers.each(this.data.datasets,function(dataset,datasetIndex){
+
+                               helpers.extend(this.datasets[datasetIndex], {
+                                       label : dataset.label || null,
+                                       fillColor : dataset.fillColor,
+                                       strokeColor : dataset.strokeColor,
+                                       pointColor : dataset.pointColor,
+                                       pointStrokeColor : dataset.pointStrokeColor,
+                               });
+
+                               helpers.each(dataset.data,function(dataPoint,index){
+                                       helpers.extend(this.datasets[datasetIndex].points[index], {
+                                               value : dataPoint,
+                                               label : this.data.labels[index],
+                                               datasetLabel: dataset.label,
+                                               strokeColor : dataset.pointStrokeColor,
+                                               fillColor : dataset.pointColor,
+                                               highlightFill : dataset.pointHighlightFill || dataset.pointColor,
+                                               highlightStroke : dataset.pointHighlightStroke || dataset.pointStrokeColor
+                                       });
+                               },this);
+
+                       },this);
+                       
                        this.eachPoints(function(point){
                                point.save();
                        });