]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Category scale now has defaults. Updated the bar & line charts to use this new default. 1206/head
authorEvert Timberg <evert.timberg@gmail.com>
Sat, 13 Jun 2015 17:25:41 +0000 (13:25 -0400)
committerEvert Timberg <evert.timberg@gmail.com>
Sat, 13 Jun 2015 17:25:41 +0000 (13:25 -0400)
src/charts/chart.bar.js
src/charts/chart.line.js
src/core/core.js
src/core/core.scale.js
src/scales/scale.category.js

index b77fe97fe836068a96f25db5d1cab47f2e557dcb..d83b25b44a32b7adc6b99eaed6a3ccc11598fd4d 100644 (file)
 
                scales: {
                        xAxes: [{
-                               type: "category", // scatter should not use a dataset axis
-                               display: true,
-                               position: "bottom",
-                               id: "x-axis-1", // need an ID so datasets can reference the scale
-
+                               type: "category",
                                categorySpacing: 10,
                                spacing: 1,
 
                                // grid line settings
                                gridLines: {
-                                       show: true,
-                                       color: "rgba(0, 0, 0, 0.05)",
-                                       lineWidth: 1,
-                                       drawOnChartArea: true,
-                                       drawTicks: true,
-                                       zeroLineWidth: 1,
-                                       zeroLineColor: "rgba(0,0,0,0.25)",
                                        offsetGridLines: true,
                                },
-
-                               // label settings
-                               labels: {
-                                       show: true,
-                                       template: "<%=value%>",
-                                       fontSize: 12,
-                                       fontStyle: "normal",
-                                       fontColor: "#666",
-                                       fontFamily: "Helvetica Neue",
-                               },
                        }],
                        yAxes: [{
-                               type: "linear", // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
+                               type: "linear",
                                display: true,
                                position: "left",
                                id: "y-axis-1",
index 779f78e3f66b4174b0fb6e666dafd71e1cb7c44a..97b3e1ef208cd0775a005ed670f8c547e42d039b 100644 (file)
                scales: {
                        xAxes: [{
                                type: "category", // scatter should not use a dataset axis
-                               display: true,
-                               position: "bottom",
-                               id: "x-axis-1", // need an ID so datasets can reference the scale
-
-                               // grid line settings
-                               gridLines: {
-                                       show: true,
-                                       color: "rgba(0, 0, 0, 0.05)",
-                                       lineWidth: 1,
-                                       drawOnChartArea: true,
-                                       drawTicks: true,
-                                       zeroLineWidth: 1,
-                                       zeroLineColor: "rgba(0,0,0,0.25)",
-                                       offsetGridLines: false,
-                               },
-
-                               // label settings
-                               labels: {
-                                       show: true,
-                                       template: "<%=value%>",
-                                       fontSize: 12,
-                                       fontStyle: "normal",
-                                       fontColor: "#666",
-                                       fontFamily: "Helvetica Neue",
-                               },
                        }],
                        yAxes: [{
                                type: "linear", // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
index b4af0a10b0bdf2385bcbb446cd6573d4190b9ce3..1cb6fafc7b9c79de353b61d3c1e5d52af41f2265 100755 (executable)
                        helpers.each(Array.prototype.slice.call(arguments, 1), function(extension) {
                                helpers.each(extension, function(value, key) {
                                        if (extension.hasOwnProperty(key)) {
-                                               if (base.hasOwnProperty(key) && helpers.isArray(base[key]) && helpers.isArray(value)) {
+                                               if (key === 'scales') {
+                                                       // Scale config merging is complex. Add out own function here for that
+                                                       base[key] = helpers.scaleMerge(base.hasOwnProperty(key) ? base[key] : {}, value);
+
+                                               } else if (base.hasOwnProperty(key) && helpers.isArray(base[key]) && helpers.isArray(value)) {
                                                        // In this case we have an array of objects replacing another array. Rather than doing a strict replace,
                                                        // merge. This allows easy scale option merging
                                                        var baseArray = base[key];
 
                                                        helpers.each(value, function(valueObj, index) {
+                                                               
                                                                if (index < baseArray.length) {
                                                                        baseArray[index] = helpers.configMerge(baseArray[index], valueObj);
                                                                } else {
                                                                        baseArray.push(valueObj); // nothing to merge
                                                                }
                                                        });
+
                                                } else if (base.hasOwnProperty(key) && typeof base[key] == "object" && base[key] !== null && typeof value == "object") {
                                                        // If we are overwriting an object with an object, do a merge of the properties.
                                                        base[key] = helpers.configMerge(base[key], value);
+
                                                } else {
                                                        // can just overwrite the value in this case
                                                        base[key] = value;
 
                        return base;
                },
+               scaleMerge = helpers.scaleMerge = function(_base, extension) {
+                       var base = clone(_base);
+
+                       helpers.each(extension, function(value, key) {
+                               if (extension.hasOwnProperty(key)) {
+                                       if (key === 'xAxes' || key === 'yAxes') {
+                                               // These properties are arrays of items
+                                               if (base.hasOwnProperty(key)) {
+                                                       helpers.each(value, function(valueObj, index) {
+                                                               if (index >= base[key].length || !base[key][index].type) {
+                                                                       base[key].push(helpers.configMerge(valueObj.type ? Chart.scaleService.getScaleDefaults(valueObj.type) : {}, valueObj));
+                                                               } else  if (valueObj.type !== base[key][index].type) {
+                                                                       // Type changed. Bring in the new defaults before we bring in valueObj so that valueObj can override the correct scale defaults
+                                                                       base[key][index] = helpers.configMerge(base[key][index], valueObj.type ? Chart.scaleService.getScaleDefaults(valueObj.type) : {}, valueObj)
+                                                               } else {
+                                                                       // Type is the same
+                                                                       base[key][index] = helpers.configMerge(base[key][index], valueObj);
+                                                               }
+                                                       });
+                                               } else {
+                                                       base[key] = [];
+                                                       helpers.each(value, function(valueObj) {
+                                                               base[key].push(helpers.configMerge(valueObj.type ? Chart.scaleService.getScaleDefaults(valueObj.type) : {}, valueObj));
+                                                       });
+                                               }
+                                       } else if (base.hasOwnProperty(key) && typeof base[key] == "object" && base[key] !== null && typeof value == "object") {
+                                               // If we are overwriting an object with an object, do a merge of the properties.
+                                               base[key] = helpers.configMerge(base[key], value);
+
+                                       } else {
+                                               // can just overwrite the value in this case
+                                               base[key] = value;
+                                       }
+                               }
+                       });
+
+                       return base;
+               },
                getValueAtIndexOrDefault = helpers.getValueAtIndexOrDefault = function(value, index, defaultValue) {
                        if (!value) {
                                return defaultValue;
index 7494d14c865cd930229b3e97ee85eb1c46a227a2..c738f1f5853a72e21cf6b78cadd8aa60e934ea54 100644 (file)
                constructors: {},
                // Use a registration function so that we can move to an ES6 map when we no longer need to support
                // old browsers
-               registerScaleType: function(type, scaleConstructor) {
+               // Scale config defaults
+               defaults: {},
+               registerScaleType: function(type, scaleConstructor, defaults) {
                        this.constructors[type] = scaleConstructor;
+                       this.defaults[type] = defaults;
                },
                getScaleConstructor: function(type) {
                        return this.constructors.hasOwnProperty(type) ? this.constructors[type] : undefined;
                },
-
+               getScaleDefaults: function(type) {
+                       return this.defaults.hasOwnProperty(type) ? this.defaults[type] : {};
+               },
                // The interesting function
                fitScalesForChart: function(chartInstance, width, height) {
                        var xPadding = width > 30 ? 5 : 2;
index 7ed54613ec918ad3b8899116b6307fa838d37a83..3577315784012e64335441b1a4cb5673553a506c 100644 (file)
@@ -5,6 +5,35 @@
                Chart = root.Chart,
                helpers = Chart.helpers;
 
+       // Default config for a category scale
+       var defaultConfig = {
+               display: true,
+               position: "bottom",
+               id: "x-axis-1", // need an ID so datasets can reference the scale
+
+               // grid line settings
+               gridLines: {
+                       show: true,
+                       color: "rgba(0, 0, 0, 0.1)",
+                       lineWidth: 1,
+                       drawOnChartArea: true,
+                       drawTicks: true,
+                       zeroLineWidth: 1,
+                       zeroLineColor: "rgba(0,0,0,0.25)",
+                       offsetGridLines: false,
+               },
+
+               // label settings
+               labels: {
+                       show: true,
+                       template: "<%=value%>",
+                       fontSize: 12,
+                       fontStyle: "normal",
+                       fontColor: "#666",
+                       fontFamily: "Helvetica Neue",
+               },
+       };
+
        var DatasetScale = Chart.Element.extend({
                isHorizontal: function() {
                        return this.options.position == "top" || this.options.position == "bottom";
                        }
                }
        });
-       Chart.scaleService.registerScaleType("category", DatasetScale);
+       Chart.scaleService.registerScaleType("category", DatasetScale, defaultConfig);
 }).call(this);