]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fixed misplaced data points on category scale
authorMartin Zürn <martin@weltundall.de>
Mon, 27 Mar 2017 22:02:56 +0000 (00:02 +0200)
committerEvert Timberg <evert.timberg+github@gmail.com>
Mon, 27 Mar 2017 22:02:56 +0000 (18:02 -0400)
* Fixed issue, that category scale shows data points misplaced. See #4060
* Cleaned code
* Fixed Irregular whitespace
* Fixed error in case value is undefined
* Verified that no error is thrown in case value === null

src/scales/scale.category.js

index 3cee4ebf71fc10187f1d47e2ada82ae07c692814..0388baa00deeb4abcffc80a851aae5127764104f 100644 (file)
@@ -66,8 +66,15 @@ module.exports = function(Chart) {
                        // 1 is added because we need the length but we have the indexes
                        var offsetAmt = Math.max((me.maxIndex + 1 - me.minIndex - ((me.options.gridLines.offsetGridLines) ? 0 : 1)), 1);
 
-                       if (value !== undefined && isNaN(index)) {
+                       // If value is a data object, then index is the index in the data array,
+                       // not the index of the scale. We need to change that.
+                       var valueCategory;
+                       if (value !== undefined && value !== null) {
+                               valueCategory = me.isHorizontal() ? value.x : value.y;
+                       }
+                       if (valueCategory !== undefined || (value !== undefined && isNaN(index))) {
                                var labels = me.getLabels();
+                               value = valueCategory || value;
                                var idx = labels.indexOf(value);
                                index = idx !== -1 ? idx : index;
                        }