[英]How to pass highchart series to highchart title?
我已經使用Highcharts
創建了一個Pie Chart
。 我有一個title
,我需要在標題內插入數據系列的名稱。 就像這樣:
title: {
text: 'Data name: {name}',
align: 'center',
useHTML: true,
verticalAlign: 'middle',
y: -70
},
series: [{
type: 'pie',
name: 'Some name to be injected inside the title',
innerSize: '70%',
data: []
}]
有可能這樣做嗎?
為此,我將首先標識或檢索系列名稱,然后將其分配給要在圖表配置選項中使用的變量。
無論您使用哪種方法來檢索或處理圖表系列對象的數據,都應能夠為您提供獨立使用的名稱。
喜歡:
//process data...
var seriesData = [['Group A', 1598],['Group B', 872],['Group C', 321]];
var seriesName = 'Chart Series Name';
$(function() {
$('#container').highcharts({
chart : { type : 'pie' },
title : { text : seriesName },
subtitle : { text : null },
tooltip : { },
plotOptions : { },
series : [{
name : seriesName,
data : seriesData
}]
});
})
例:
沒有直接的方法可以做到這一點。 我建議使用highcharts的load事件:
chart: {
/* some other code */
events: {
load: function () {
//collect information
var title_old = this.title.textStr,
title_new,
substitute= this.series[0].name,
needle = '{name}';
//replaceing 'needle' with 'substitute'
title_new = title_old.replace(needle,substitute);
//set title
this.setTitle({ text: title_new});
}
}
}
提示:
[1]: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replace
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.