gpt4 book ai didi

javascript - Highchart Js 中的工具提示修改

转载 作者:行者123 更新时间:2023-12-03 01:08:55 26 4
gpt4 key购买 nike

我正在修改图表中Highchart JS的工具提示。但产出并没有像我预期的那样。我想在工具提示的右侧打印百分比。

示例:满意到忠诚:4 (20%)

我添加了两个数组,根据一系列图表在工具提示右侧添加 valueSuffix。但它在单个工具提示上打印所有数组值。

我尝试了下面的代码来修改 Highchart。

$(function() {
$('#container').highcharts({
title: {
text: ''
},
charts: {
zoomType: 'xy',
},
exporting: {
enabled: false
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
],
},
yAxis: {
title: '',
},
labels: {
items: [{
style: {
left: '50px',
top: '18px',
}
}]
},
tooltip: {
shared: true,
},
credits: {
enabled: false
},
plotOptions: {
series: {
label: {
enabled: false
},
},
column: {
states: {
hover: {
color: '#90D0FF'
}
}
}
},
series: [{
type: 'column',
name: 'Provide Feedback',
data: [10, 4, 5, 6, 8, 9, 2, 3, 4, 5, 6, 9],
color: 'yellow'
},
{
name: 'Satisfied to Loyal',
type: 'spline',
data: [2, 5, 4, 3, 2, 3, 7, 8, 9, 5, 4, 3],
color: '#55BF3B',
dataLabels: {
enabled: false
},
tooltip: {
valueSuffix: [' (1%)', ' (2%)', ' (18%)', ' (10%)', ' (3%)', ' (1%)', ' (1%)', ' (6%)', ' (4%)', ' (1%)', ' (8%)', ' (70%)'],
// valueSuffix: ' (val %)',
},
marker: {
lineWidth: 4,
fillColor: 'white',
width: 16,
height: 16
}
},
{
name: 'Unhappy to Satisfied',
type: 'spline',
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 5, 4],
color: '#FFC800',
tooltip: {
valueSuffix: [' (10%)', ' (12%)', ' (1%)', ' (100%)', ' (30%)', ' (10%)', ' (10%)', ' (60%)', ' (34%)', ' (10%)', ' (98%)', ' (40%)'],
// valueSuffix: ' (val %)',
},
marker: {
lineWidth: 4,
fillColor: 'white',
width: 16,
height: 16
}
}
]
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>

非常感谢任何帮助。

最佳答案

您可以在pointFormatter函数中从图表之外的数组中获取后缀:

var valueSuffix1 = [' (1%)', ' (2%)', ' (18%)', ' (10%)', ' (3%)', ' (1%)', ' (1%)', ' (6%)', ' (4%)', ' (1%)', ' (8%)', ' (70%)'];

var valueSuffix2 = [' (10%)', ' (12%)', ' (1%)', ' (100%)', ' (30%)', ' (10%)', ' (10%)', ' (60%)', ' (34%)', ' (10%)', ' (98%)', ' (40%)'];

$('#container').highcharts({

...

tooltip: {
shared: true,
pointFormatter: function() {
var suffix = '';

if (this.series.index === 1) {
suffix = valueSuffix1[this.index]
} else if (this.series.index === 2) {
suffix = valueSuffix2[this.index]
}

return '<span style="color:' + this.color + '">\u25CF</span> ' + this.series.name + ': <b>' + this.y + '</b>' + suffix + '<br/>'
}
}
});

现场演示:http://jsfiddle.net/BlackLabel/8L5q7h4d/

API引用:https://api.highcharts.com/highcharts/tooltip.pointFormatter

关于javascript - Highchart Js 中的工具提示修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52279007/

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