gpt4 book ai didi

javascript - 图表 JS : Always show tooltips in a multi dataset line chart

转载 作者:行者123 更新时间:2023-11-30 09:56:19 25 4
gpt4 key购买 nike

我试图始终在 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 Fiddle: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 fiddle :https://jsfiddle.net/hdnu4bpa/

最佳答案

您需要控制 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,
}

fiddle - https://jsfiddle.net/racqd639/

关于javascript - 图表 JS : Always show tooltips in a multi dataset line chart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33793540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com