]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Time scales now support passing in data as points. Added this to the time scale sampl...
authoretimberg <evert.timberg@gmail.com>
Sun, 18 Oct 2015 22:00:46 +0000 (18:00 -0400)
committeretimberg <evert.timberg@gmail.com>
Sun, 18 Oct 2015 22:00:46 +0000 (18:00 -0400)
samples/line-time-scale.html
src/core/core.scale.js
src/scales/scale.category.js
src/scales/scale.linear.js
src/scales/scale.logarithmic.js
src/scales/scale.time.js

index 327054703e4606ebb6cc7a0bbbd0e795d3945c85..224fb392b95c924a404360618b952404334137f9 100644 (file)
                 }, {
                     label: "My Second dataset",
                     data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
+                }, {
+                    label: "Dataset with point data",
+                    data: [{
+                        x: "12/31/2014 06:00",
+                        y: randomScalingFactor()
+                    }, {
+                        x: "01/04/2015 13:00",
+                        y: randomScalingFactor()
+                    }, {
+                        x: "01/07/2015 01:15",
+                        y: randomScalingFactor()
+                    }, {
+                        x: "01/15/2015 01:15",
+                        y: randomScalingFactor()
+                    }],
+                    fill: false
                 }]
             },
             options: {
index a2e96eee912707d1780e828266d9f79827d1867b..c9f8423399c2cd6b67f2fd2efce22cff424900ff 100644 (file)
                },
                afterFit: helpers.noop,
 
-
-
-
-
                // Shared Methods
                isHorizontal: function() {
                        return this.options.position == "top" || this.options.position == "bottom";
                },
+               
+               // Get the correct value. If the value type is object get the x or y based on whether we are horizontal or not
+               getRightValue: function(rawValue) {
+                       return (typeof(rawValue) === "object" && rawValue !== null) ? (this.isHorizontal() ? rawValue.x : rawValue.y) : rawValue;
+               },
 
                // Used to get the value to display in the tooltip for the data at the given index
                // function getLabelForIndex(index, datasetIndex)
index 7c7656bcb56bf1d134cc7825c3a7b27fbcdd1657..e85ba04ea0c071ebc1f033173275c82b871e8229 100644 (file)
@@ -21,7 +21,6 @@
 
         // Used to get data value locations.  Value can either be an index or a numerical value
         getPixelForValue: function(value, index, datasetIndex, includeOffset) {
-
             if (this.isHorizontal()) {
                 var innerWidth = this.width - (this.paddingLeft + this.paddingRight);
                 var valueWidth = innerWidth / Math.max((this.data.labels.length - ((this.options.gridLines.offsetGridLines) ? 0 : 1)), 1);
index 9b36820d8beb53b766d3d6cf029088a74ffc5b5b..194e6da813c723e16facf6b2d7c9b998add4a404 100644 (file)
                                return Math.round(pixel);
                        }
                },
-
-               // Get the correct value. If the value type is object get the x or y based on whether we are horizontal or not
-               getRightValue: function(rawValue) {
-                       return (typeof(rawValue) === "object" && rawValue !== null) ? (this.isHorizontal() ? rawValue.x : rawValue.y) : rawValue;
-               },
-
        });
        Chart.scaleService.registerScaleType("linear", LinearScale, defaultConfig);
 
index c8ce9bbbaf9ec6ed0c45b1932ea5b85f5bdb3def..53ef000ce6b1f52741d07808dcef43064342c0cb 100644 (file)
                getLabelForIndex: function(index, datasetIndex) {
                        return this.getRightValue(this.data.datasets[datasetIndex].data[index]);
                },
-               // Get the correct value. If the value type is object get the x or y based on whether we are horizontal or not
-               getRightValue: function(rawValue) {
-                       return typeof rawValue === "object" ? (this.isHorizontal() ? rawValue.x : rawValue.y) : rawValue;
-               },
                getPixelForTick: function(index, includeOffset) {
                        return this.getPixelForValue(this.tickValues[index], null, null, includeOffset);
                },
index 87b54ca84487c99ae1b121b8d22b08c60cccfb2d..2ed211528b7fc5fe0c0e314cf3b6dc04cbfef47d 100644 (file)
        };
 
        var TimeScale = Chart.Scale.extend({
-               buildTicks: function(index) {
-
-                       this.ticks = [];
-                       this.labelMoments = [];
+               getLabelMoment: function(datasetIndex, index) {
+                       return this.labelMoments[datasetIndex][index];
+               },
 
-                       // Parse each label into a moment
-                       this.data.labels.forEach(function(label, index) {
+               buildLabelMoments: function() {
+                       // Only parse these once. If the dataset does not have data as x,y pairs, we will use
+                       // these 
+                       var scaleLabelMoments = [];
+                       helpers.each(this.data.labels, function(label, index) {
                                var labelMoment = this.parseTime(label);
                                if (this.options.time.round) {
                                        labelMoment.startOf(this.options.time.round);
                                }
-                               this.labelMoments.push(labelMoment);
+                               scaleLabelMoments.push(labelMoment);
+                       }, this);
+
+                       this.firstTick = moment.min.call(this, scaleLabelMoments);
+                       this.lastTick = moment.max.call(this, scaleLabelMoments);
+
+                       helpers.each(this.data.datasets, function(dataset, datasetIndex) {
+                               var momentsForDataset = [];
+
+                               if (typeof dataset.data[0] === 'object') {
+                                       helpers.each(dataset.data, function(value, index) {
+                                               var labelMoment = this.parseTime(this.getRightValue(value));
+                                               if (this.options.time.round) {
+                                                       labelMoment.startOf(this.options.time.round);
+                                               }
+                                               momentsForDataset.push(labelMoment);
+
+                                               // May have gone outside the scale ranges, make sure we keep the first and last ticks updated
+                                               this.firstTick = moment.min(this.firstTick, labelMoment);
+                                               this.lastTick = moment.max(this.lastTick, labelMoment);
+                                       }, this);
+                               } else {
+                                       // We have no labels. Use the ones from the scale
+                                       momentsForDataset = scaleLabelMoments;
+                               }
+
+                               this.labelMoments.push(momentsForDataset);
                        }, this);
 
-                       // Find the first and last moments, and range
-                       this.firstTick = moment.min.call(this, this.labelMoments).clone();
-                       this.lastTick = moment.max.call(this, this.labelMoments).clone();
+                       // We will modify these, so clone for later
+                       this.firstTick = this.firstTick.clone();
+                       this.lastTick = this.lastTick.clone();
+               },
+
+               buildTicks: function(index) {
+
+                       this.ticks = [];
+                       this.labelMoments = [];
+
+                       this.buildLabelMoments();
 
                        // Set unit override if applicable
                        if (this.options.time.unit) {
                        this.lastTick.endOf(this.tickUnit);
                        this.smallestLabelSeparation = this.width;
 
-                       var i = 0;
-
-                       for (i = 1; i < this.labelMoments.length; i++) {
-                               this.smallestLabelSeparation = Math.min(this.smallestLabelSeparation, this.labelMoments[i].diff(this.labelMoments[i - 1], this.tickUnit, true));
-                       }
+                       helpers.each(this.data.datasets, function(dataset, datasetIndex) {
+                               for (var i = 1; i < this.labelMoments[datasetIndex].length; i++) {
+                                       this.smallestLabelSeparation = Math.min(this.smallestLabelSeparation, this.labelMoments[datasetIndex][i].diff(this.labelMoments[datasetIndex][i - 1], this.tickUnit, true));
+                               }
+                       }, this);
 
                        // Tick displayFormat override
                        if (this.options.time.displayFormat) {
                        }
 
                        // For every unit in between the first and last moment, create a moment and add it to the ticks tick
-                       for (i = 0; i <= this.tickRange; ++i) {
+                       for (var i = 0; i <= this.tickRange; ++i) {
                                this.ticks.push(this.firstTick.clone().add(i, this.tickUnit));
                        }
                },
                // Get tooltip label
                getLabelForIndex: function(index, datasetIndex) {
-                       return this.data.labels[index];
+                       var label = this.data.labels[index];
+
+                       if (typeof this.data.datasets[datasetIndex].data[0] === 'object') {
+                               label = this.getRightValue(this.data.datasets[datasetIndex].data[index]);
+                       }
+
+                       return label;
                },
                convertTicksToLabels: function() {
                        this.ticks = this.ticks.map(function(tick, index, ticks) {
                        }, this);
                },
                getPixelForValue: function(value, index, datasetIndex, includeOffset) {
-
-                       var offset = this.labelMoments[index].diff(this.firstTick, this.tickUnit, true);
+                       var labelMoment = this.getLabelMoment(datasetIndex, index);
+                       var offset = labelMoment.diff(this.firstTick, this.tickUnit, true);
 
                        var decimal = offset / this.tickRange;