【问题标题】:how can I format the axis in ms chart control如何在 ms 图表控件中格式化轴
【发布时间】:2013-10-16 10:31:04
【问题描述】:

我正在使用 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);
}

使用:

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));

test 将返回 123.456,789(与用户计算机上的文化设置无关)。

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

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

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

马蒂斯

【问题讨论】:

  • 做一些可能有用的事情。如果是这样。我会添加代码
  • 您是否尝试将格式设置为#,##0.##
  • 在这种情况下它确实有效,但我的客户希望对千人分组有所了解。

标签: c# format axis mschart invariantculture


【解决方案1】:

我是这样工作的:

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;
        }
    }
}

一切都如我所愿;-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    相关资源
    最近更新 更多