簡體   English   中英

如何將highchart系列轉換為highchart標題?

[英]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});
            }
        }
    }

提示:

  • 僅當您有一個系列時才有效(請參見代碼: series [0]
  • 已通過Highcharts JS v4.1.8測試(2015-08-20)
  • 有關String.prototype.replace()的信息,請看這里[1]

[1]: https//developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replace

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM