2012-12-12 43 views
0

我米使用dotnetHightchartsHighcharts每個系列的tooltipformat?

BasicColumnChart

public static Highcharts BasicColumnChart(Series[] Series, string[] Categories, string Title = "", string SubTitle = "", string XAxisTitle = "", string YAxisTitle = "", string ToolTipFormat = "", string YAxisLabel = "") 
{ 
    Highcharts chart = new Highcharts("chart") 
     .InitChart(new Chart { DefaultSeriesType = ChartTypes.Column, Height = 300 }) 
     .SetTitle(new Title { Text = Title }) 
     .SetSubtitle(new Subtitle { Text = SubTitle }) 
     .SetXAxis(new XAxis { Categories = Categories }) 
     .SetYAxis(new YAxis 
     { 
      Min = 0, 
      Title = new YAxisTitle { Text = YAxisTitle }, 
      Labels = new YAxisLabels 
      { 
       Formatter = @"function() { return this.value +' " + YAxisLabel + "';}" 
      } 
     }) 
     .SetLegend(new Legend 
     { 
      Layout = Layouts.Vertical, 
      VerticalAlign = VerticalAligns.Top, 
      Align = HorizontalAligns.Right, 
      Shadow = true, 
      BackgroundColor = ColorTranslator.FromHtml("#FFFFFF"), 
      Floating = true 
     }) 
     .SetTooltip(new Tooltip { Formatter = @"function() { return ''+ this.x +' - '+ this.y +' " + ToolTipFormat + "'; }" }) 
     .SetPlotOptions(new PlotOptions 
     { 
      Column = new PlotOptionsColumn 
      { 
       PointPadding = 0.2, 
       BorderWidth = 0 
      } 
     }) 
     .SetSeries(Series); 

    return chart; 
} 

我可以設置相同tooltipFormat爲每個系列下面一行:

.SetTooltip(new Tooltip { Formatter = @"function() { return ''+ this.x +' - '+ this.y +' " + ToolTipFormat + "'; }" }) 

以上代碼的輸出:date - value tooltipFormat

但我想爲每個系列顯示不同的格式。爲什麼我要這樣,因爲我要證明有不同的單元,例如不同的數據:

value[0] = 220 V //Volt 
value[1] = 5 A  //Ampere 
... 
value[15] = 1200 kW //Kilowatt 

有很多例子關於這個話題,但是,這些例子只是更改文本。我的數據意見是這樣的:

NAME  VALUE UNIT 

Volt  220  V 
Ampere  5  A 
.... 

我想動態改變每個系列的tooltipFormat。我想動態地添加UNIT字段。我怎樣才能做到這一點?

謝謝。

回答

3

可以在一系列對象添加自定義屬性:

series:{name:'series1',unit:'%'} 

,可以在工具提示格式化函數訪問:

http://api.highcharts.com/highcharts#plotOptions.series.tooltip.valueSuffix

this.series.options.unit 
+0

我創建了這樣的系列C#端:'系列localSeries =新系列{名稱=項目名稱,數據=新數據(數據)};'所以,我該如何添加新的屬性? –

+0

你的建議後,我發現這樣的:'系列localSeries =新系列{名稱= item.Name,數據=新的數據(數據),PlotOptionsSpline =新的PlotOptionsSpline {Tooltip = item.Unit}};但這段代碼不工作也是如此。 :) –

+0

Series對象中有Stack屬性(不知道它是什麼),您可以使用它。 –

2

您可以直接在一系列做到這一點例如,這是一個系列:

series: [{ 
     data: [10, 20, 30], 
     type: "line", 
     name: "Percentage", 
     tooltip: { valueSuffix: "%", } 
    },]