Chart JS:始终在多数据集折线图中显示工具提示

时间:2015-11-19 01:09:37

标签: javascript charts chart.js

我试图始终在ChartJS中显示多数据集折线图的工具提示

现有解决方案仅适用于单个数据集(例如Chart.js - Doughnut show tooltips always?),如下所示:

var options = 
{
    tooltipTemplate: "<%= value %>",

    onAnimationComplete: function()
    {
        this.showTooltip(this.segments, true);

        //Show tooltips in bar chart (issue: multiple datasets doesnt work http://jsfiddle.net/5gyfykka/14/)
        //this.showTooltip(this.datasets[0].bars, true);

        //Show tooltips in line chart (issue: multiple datasets doesnt work http://jsfiddle.net/5gyfykka/14/)
        //this.showTooltip(this.datasets[0].points, true);  
    },

    tooltipEvents: [],

    showTooltips: true
}

var context = $('#chart').get(0).getContext('2d');
var chart = new Chart(context).Pie(data, options);  

用于处理单一数据集解决方案的现有JS小提琴:http://jsfiddle.net/5gyfykka/14/

我设法让How to display Line Chart dataset point labels with Chart.js?使用不同的onAnimationComplete函数

 onAnimationComplete: function () {
       var ctx = document.getElementById("LineWithLine").getContext("2d");
       ctx.font = '.8rem "Gotham Book",sans-serif';
                ctx.fontWeight = 'bold';
                ctx.fillStyle = '#000';
                ctx.textAlign="right";
            var self = this;
                $(self.datasets).each(function(idx,dataset){
                    $(dataset.points).each(function(pdx,pointinfo){
                        if ( pointinfo.value !== null ) {
                            ctx.fillText(pointinfo.value,pointinfo.x,pointinfo.y - 18);
                        }
                    });
                });         
    },

虽然这有效,但它并不像Chart JS提供的多工具提示那么好。

JS小提琴:https://jsfiddle.net/hdnu4bpa/

1 个答案:

答案 0 :(得分:4)

您需要控制tootip生成过程(即从Chart.js库中复制粘贴代码的相关部分: - )

自动显示多个系列折线图的工具提示(无悬停)

以下是它的外观

enter image description here

只需更改此选项

即可
var options = {
    showTooltips: true,
    onAnimationComplete: function () {
        for (var dataIndex = 0; dataIndex < this.datasets[0].points.length; dataIndex++) {
            // the following is pretty much a copy-paste from the Chart.js library
            var tooltipLabels = [],
                tooltipColors = [],
                medianPosition = (function (index) {

                    // Get all the points at that particular index
                    var Elements = [],
                        dataCollection,
                        xPositions = [],
                        yPositions = [],
                        xMax,
                        yMax,
                        xMin,
                        yMin;
                    Chart.helpers.each(this.datasets, function (dataset) {
                        dataCollection = dataset.points;
                        if (dataCollection[dataIndex] && dataCollection[dataIndex].hasValue()) {
                            Elements.push(dataCollection[dataIndex]);
                        }
                    });

                    Chart.helpers.each(Elements, function (element) {
                        xPositions.push(element.x);
                        yPositions.push(element.y);

                        //Include any colour information about the element
                        tooltipLabels.push(Chart.helpers.template(this.options.multiTooltipTemplate, element));
                        tooltipColors.push({
                            fill: element._saved.fillColor || element.fillColor,
                            stroke: element._saved.strokeColor || element.strokeColor
                        });

                    }, this);

                    yMin = Chart.helpers.min(yPositions);
                    yMax = Chart.helpers.max(yPositions);

                    xMin = Chart.helpers.min(xPositions);
                    xMax = Chart.helpers.max(xPositions);

                    return {
                        x: (xMin > this.chart.width / 2) ? xMin : xMax,
                        y: (yMin + yMax) / 2
                    };
                }).call(this, dataIndex);

            new Chart.MultiTooltip({
                x: medianPosition.x,
                y: medianPosition.y,
                xPadding: this.options.tooltipXPadding,
                yPadding: this.options.tooltipYPadding,
                xOffset: this.options.tooltipXOffset,
                fillColor: this.options.tooltipFillColor,
                textColor: this.options.tooltipFontColor,
                fontFamily: this.options.tooltipFontFamily,
                fontStyle: this.options.tooltipFontStyle,
                fontSize: this.options.tooltipFontSize,
                titleTextColor: this.options.tooltipTitleFontColor,
                titleFontFamily: this.options.tooltipTitleFontFamily,
                titleFontStyle: this.options.tooltipTitleFontStyle,
                titleFontSize: this.options.tooltipTitleFontSize,
                cornerRadius: this.options.tooltipCornerRadius,
                labels: tooltipLabels,
                legendColors: tooltipColors,
                legendColorBackground: this.options.multiTooltipKeyBackground,
                title: this.scale.xLabels[dataIndex],
                chart: this.chart,
                ctx: this.chart.ctx,
                custom: this.options.customTooltips
            }).draw();
        }
    },
    tooltipEvents: [],
    datasetFill: true,
}

小提琴 - https://jsfiddle.net/racqd639/