]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
All tooltip callbacks support arrays for lines, and no drawing invisible tooltips.
authorTanner Linsley <tannerlinsley@gmail.com>
Sat, 17 Oct 2015 21:53:33 +0000 (15:53 -0600)
committerTanner Linsley <tannerlinsley@gmail.com>
Sat, 17 Oct 2015 21:53:33 +0000 (15:53 -0600)
Simply return a string for a single line tooltip, or return an array to
create multiple lines.

samples/tooltip-hooks.html
src/core/core.tooltip.js

index 1fbe8d2018353ace87c1dfd297fb9c1e1e0bcd81..5b8ef3fa8aa15b48ea3e818a5131ec53d1767e06 100644 (file)
             dataset.pointBorderWidth = 1;
         });
 
-        console.log(config.data);
-
         window.onload = function() {
             var ctx = document.getElementById("canvas").getContext("2d");
             window.myLine = new Chart(ctx, config);
index 2dc4dfd451124dc8ad9c80edfb801ae296dabf0e..a1a94a1ee22e66cb846ce382ed7e4081376a494c 100644 (file)
                        var lines = [];
 
                        if (beforeTitle) {
-                               lines.push(beforeTitle);
+                               if (helpers.isArray(beforeTitle)) {
+                                       lines = lines.concat(beforeTitle);
+                               } else {
+                                       lines.push(beforeTitle);
+                               }
                        }
                        if (title) {
-                               lines.push(title);
+                               if (helpers.isArray(title)) {
+                                       lines = lines.concat(title);
+                               } else {
+                                       lines.push(title);
+                               }
                        }
                        if (afterTitle) {
-                               lines.push(afterTitle);
+                               if (helpers.isArray(afterTitle)) {
+                                       lines = lines.concat(afterTitle);
+                               } else {
+                                       lines.push(afterTitle);
+                               }
                        }
                        return lines;
                },
 
+               getBeforeBody: function(xLabel, yLabel, index, datasetIndex, data) {
+                       var lines = this._options.tooltips.callbacks.beforeBody.call(this, xLabel, yLabel, index, datasetIndex, data);
+                       return helpers.isArray(lines) ? lines : [lines];
+               },
+
                getBody: function(xLabel, yLabel, index, datasetIndex) {
 
                        var lines = [];
                        return lines;
                },
 
+               getAfterBody: function(xLabel, yLabel, index, datasetIndex, data) {
+                       var lines = this._options.tooltips.callbacks.afterBody.call(this, xLabel, yLabel, index, datasetIndex, data);
+                       return helpers.isArray(lines) ? lines : [lines];
+               },
+
                getFooter: function() {
                        var beforeFooter = this._options.tooltips.callbacks.beforeFooter.apply(this, arguments),
                                footer = this._options.tooltips.callbacks.footer.apply(this, arguments),
                        var lines = [];
 
                        if (beforeFooter) {
-                               lines.push(beforeFooter);
+                               if (helpers.isArray(beforeFooter)) {
+                                       lines = lines.concat(beforeFooter);
+                               } else {
+                                       lines.push(beforeFooter);
+                               }
                        }
                        if (footer) {
-                               lines.push(footer);
+                               if (helpers.isArray(footer)) {
+                                       lines = lines.concat(footer);
+                               } else {
+                                       lines.push(footer);
+                               }
                        }
                        if (afterFooter) {
-                               lines.push(afterFooter);
+                               if (helpers.isArray(afterFooter)) {
+                                       lines = lines.concat(afterFooter);
+                               } else {
+                                       lines.push(afterFooter);
+                               }
                        }
 
                        return lines;
                                xLabel = [];
                                yLabel = [];
 
-                               console.log(this._active);
-
                                helpers.each(this._data.datasets, function(dataset, datasetIndex) {
 
                                        xLabel.push(element._xScale.getLabelForIndex(element._index, datasetIndex));
                        // Build the Text Lines
                        helpers.extend(this._model, {
                                title: this.getTitle(xLabel, yLabel, element._index, element._datasetIndex, this._data),
-                               beforeBody: this._options.tooltips.callbacks.beforeBody.call(this, xLabel, yLabel, element._index, element._datasetIndex, this._data),
+                               beforeBody: this.getBeforeBody(xLabel, yLabel, element._index, element._datasetIndex, this._data),
                                body: this.getBody(xLabel, yLabel, element._index, element._datasetIndex, this._data),
-                               afterBody: this._options.tooltips.callbacks.afterBody.call(this, xLabel, yLabel, element._index, element._datasetIndex, this._data),
+                               afterBody: this.getBeforeBody(xLabel, yLabel, element._index, element._datasetIndex, this._data),
                                footer: this.getFooter(xLabel, yLabel, element._index, element._datasetIndex, this._data),
                        });
 
                        var ctx = this._chart.ctx;
                        var vm = this._view;
 
+                       if (this._view.opacity === 0) {
+                               return;
+                       }
+
                        // Get Dimensions
 
                        vm.position = "top";
 
                        var caretPadding = vm.caretPadding || 2;
-                       var combinedBodyLength = vm.body.length + (vm.beforeBody ? 1 : 0) + (vm.afterBody ? 1 : 0);
+
+                       var combinedBodyLength = vm.body.length + vm.beforeBody.length + vm.afterBody.length;
 
                        // Height
                        var tooltipHeight = vm.yPadding * 2; // Tooltip Padding
                                ctx.font = helpers.fontString(vm.bodyFontSize, vm._bodyFontStyle, vm._bodyFontFamily);
 
                                // Before Body
-                               if (vm.beforeBody) {
+                               helpers.each(vm.beforeBody, function(beforeBody, i) {
                                        ctx.fillText(vm.beforeBody, xBase, yBase);
                                        yBase += vm.bodyFontSize + vm.bodySpacing;
-                               }
+                               });
 
                                helpers.each(vm.body, function(body, i) {
 
                                }, this);
 
                                // After Body
-                               if (vm.afterBody) {
+                               helpers.each(vm.afterBody, function(afterBody, i) {
                                        ctx.fillText(vm.afterBody, xBase, yBase);
                                        yBase += vm.bodyFontSize;
-                               } else {
-                                       yBase -= vm.bodySpacing; // Remove last body spacing
-                               }
+                               });
+
+                               yBase -= vm.bodySpacing; // Remove last body spacing
 
 
                                // Footer