gpt4 book ai didi

jquery - 想要对 Highcharts 工具提示结果进行排序

转载 作者:行者123 更新时间:2023-12-03 22:44:02 25 4
gpt4 key购买 nike

我的 highcharts 工具提示中有 4 个结果,它们没有排序,看起来像这样:

somerhing: 10 $
something: 18 $
something: 2 $
something: 8 $

我想将它们从最低的 $ 到最高的 $ 从 2$ 到 18$ 排序,如下所示:

somerhing: 2 $
something: 8 $
something: 10 $
something: 18 $

这是工具提示结果的 highcharts 循环:

      tooltip: {
formatter: function() {
var s = '<strong>something: '+ this.x +'</strong>';

$.each(this.points, function(i, point) {
s += '<br/>'+ point.series.name +': '+ point.y +currency;
});

return s;
},

有什么想法如何排序吗?

谢谢。

最佳答案

补充上述答案。

我也想要工具提示的默认格式。所以我在 formatter 中调用 tooltip.defaultFormatter.call(this, tooltip)回调。

请注意,它是按降序排列的。如果您想要升序,只需删除 items.reverse() 即可。

HighCharts v4.0.4

JSFiddle

function isArray(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
}

function splat(obj) {
return isArray(obj) ? obj : [obj];
}

$(function () {
$('#container').highcharts({
tooltip: {
formatter: function (tooltip) {
var items = this.points || splat(this),
series = items[0].series,
s;

// sort the values
items.sort(function(a, b){
return ((a.y < b.y) ? -1 : ((a.y > b.y) ? 1 : 0));
});
items.reverse();

return tooltip.defaultFormatter.call(this, tooltip);
},
shared: true
},
});
});

HighCharts v3.0.2

基于defaultFormatter应用上述内容的函数。

tooltip: {
formatter: function (tooltip) {
var items = this.points || splat(this),
series = items[0].series,
s;

// build the header
s = [series.tooltipHeaderFormatter(items[0])];

// sort the values
items.sort(function(a, b){
return ((a.y < b.y) ? -1 : ((a.y > b.y) ? 1 : 0));
});
items.reverse();

// build the values
$.each(items, function (i, item) {
series = item.series;
s.push((series.tooltipFormatter && series.tooltipFormatter(item)) ||
item.point.tooltipFormatter(series.tooltipOptions.pointFormat));
});

// footer
s.push(tooltip.options.footerFormat || '');

return s.join('');
},
shared: true
},

关于jquery - 想要对 Highcharts 工具提示结果进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6867607/

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