如何在 ms 图表控件中格式化轴

use*_*122 2 c# format axis mschart invariantculture

我正在使用 ms 图表控件并希望执行以下操作:将 chartAreas[0] 的 Y 轴格式化为特定格式。在这种情况下,它应该是一个没有小数的数字,并用一个点分组(每千分之一)。

尝试使用: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);
}
Run Code Online (Sandbox Code Playgroud)

使用:

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

.ToString("#,0.00", _numberFormatInfo));
Run Code Online (Sandbox Code Playgroud)

没有工作,而通常如果你有这样的事情:

decimal myDec = 123456.789;
string test = myDec.ToString("#,0.00", _numberFormatInfo));
Run Code Online (Sandbox Code Playgroud)

测试将返回 123.456,789(独立于用户计算机上的文化设置)。

但这似乎不适用于 ms 图表控件。

有人可以向我解释如何能够做到以下几点:

格式化chartArea[0] 中的Y 值,不带小数点,并用一个点作为组分隔符。同时将x 值格式化为dd-MM 格式(如16-10 => 10 月16 日),而值为实际上是一个 Uint 20131016。格式必须与文化设置无关。希望有人可以帮助我。亲切的问候,

马蒂斯

use*_*122 5

我让它像这样工作:

chart1.ChartAreas[0].AxisY.LabelStyle.Format = "MyAxisYCustomFormat";
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "MyAxisXCustomFormat";
Run Code Online (Sandbox Code Playgroud)

使用图表控件的 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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

一切都像我想要的那样工作 ;-)