]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Tooltip's label mode now uses average position of all points
authorTanner Linsley <tannerlinsley@gmail.com>
Mon, 2 Nov 2015 05:43:44 +0000 (22:43 -0700)
committerTanner Linsley <tannerlinsley@gmail.com>
Mon, 2 Nov 2015 05:43:44 +0000 (22:43 -0700)
src/core/core.tooltip.js

index b93bbe17b01e86cd12974ee70ba18ad814f73395..03b833dff107b7a50325de6e932ac077e6badb4f 100644 (file)
                        return lines;
                },
 
+               getAveragePosition: function(elements){
+
+                       if(!elements.length){
+                               return false;
+                       }
+
+                       var xPositions = [];
+                       var yPositions = [];
+
+                       helpers.each(elements, function(el){
+                               var pos = el.tooltipPosition();
+                               xPositions.push(pos.x);
+                               yPositions.push(pos.y);
+                       });
+
+                       var x = 0, y = 0;
+                       for (var i = 0; i < xPositions.length; i++) {
+                               x += xPositions[i];
+                               y += yPositions[i];
+                       }
+
+                       return {
+                               x: Math.round(x / xPositions.length),
+                               y: Math.round(y / xPositions.length)
+                       };
+
+               },
+
                update: function(changed) {
 
                        var ctx = this._chart.ctx;
                                                index: element._index,
                                                datasetIndex: element._datasetIndex,
                                        });
-                                       tooltipPosition = this._active[0].tooltipPosition();
+                                       tooltipPosition = this.getAveragePosition(this._active);
                                } else {
                                        helpers.each(this._data.datasets, function(dataset, datasetIndex) {
                                                if (!helpers.isDatasetVisible(dataset)) {
                                                });
                                        }, this);
 
-                                       tooltipPosition = this._active[0].tooltipPosition();
+                                       tooltipPosition = this.getAveragePosition(this._active);
                                        tooltipPosition.y = this._active[0]._yScale.getPixelForDecimal(0.5);
                                }