2013-10-16 54 views
1

我正在使用ms圖表控件,並且想要執行以下操作: 將chartAreas [0]的Y軸格式化爲特定格式。在這種情況下,它應該是一個沒有小數的數字並且用一個點來分組(每千)。如何在ms圖表控件中格式化軸

嘗試過: chart1.ChartAreas [0] .AxisY.LabelStyle.Format =「{#。###}」;

但這並沒有給我正確的結果。 所以我試圖讓FormNumber事件以及與此測試:

if(e.ElementType == System.Windows.Forms.DataVisualization.Charting.ChartElementType.AxisLabels)// && e.SenderTag!=null) 
{ 
    e.LocalizedValue = e.Value.ToString("#.###", _numberFormatInfo); 
} 

使用:

NumberFormatInfo _numberFormatInfo; 
_numberFormatInfo = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone(); 
_numberFormatInfo.NumberGroupSeparator = "."; 
_numberFormatInfo.NumberDecimalSeparator = ","; 

.ToString("#,0.00", _numberFormatInfo)); 

沒有發揮出來,而一般來說,如果你有這樣的事情:

decimal myDec = 123456.789; 
string test = myDec.ToString("#,0.00", _numberFormatInfo)); 

測試將返回123.456,789(獨立於用戶計算機上的Culture設置)。

但是,這似乎並不適用於ms圖表控件。

有人能向我解釋如何才能夠做到以下幾點:

格式chartArea [0] Y值無小數和一個點爲一組separator.At同一時間格式的x值到dd-MM格式(如16-10 =十月十六日),而該值實際上是一個Uint 20131016. 格式必須爲獨立於文化設置。 希望有人能幫助我。 親切的問候,

Matthijs

+0

工作的東西,可能工作。如果是的話。我將添加代碼 – user369122

+0

您是否嘗試將格式設置爲'#,## 0。##' – V4Vendetta

+0

在這種情況下,它確實有效,但我的客戶希望爲千分組指定一個點。 – user369122

回答

2

我得到它的工作是這樣的:

chart1.ChartAreas[0].AxisY.LabelStyle.Format = "MyAxisYCustomFormat"; 
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "MyAxisXCustomFormat"; 

使用圖表控件的NumberFormat事件:

private void chart1_FormatNumber(object sender, System.Windows.Forms.DataVisualization.Charting.FormatNumberEventArgs e) 
{ 
    if(e.ElementType == System.Windows.Forms.DataVisualization.Charting.ChartElementType.AxisLabels) 
    { 
     switch(e.Format) 
     { 
      case "MyAxisXCustomFormat": 
       e.LocalizedValue = DateTime.ParseExact(e.Value.ToString(), "yyyyMMdd", null).ToString("dd-MM"); 
       break; 
      case "MyAxisYCustomFormat": 
       e.LocalizedValue = e.Value.ToString("#,###", _numberFormatInfoNLV); 
       break; 
      default: 
       break; 
     } 
    } 
} 

所有作品就像我想它; - )