简体   繁体   English

如何在DevExpress XtraCharts中通过CultureInfo格式化货币?

[英]How to format currency by CultureInfo in DevExpress XtraCharts?

I am creating charts programmatically in a manner similiar to this example on the DevExpress website. 我正在以编程方式创建图表,其方式类似于DevExpress网站上的此示例 I see where you can identify series data to be formatted as currency, but the reporting tool is globally based and needs to be able to format the various chart labels using a passed in currency. 我看到您可以在哪里识别要格式化为货币的系列数据,但是报告工具是基于全局的,因此需要能够使用传入的货币对各种图表标签进行格式化。

Here is an example of code used to generate a typical SeriesView: 这是用于生成典型SeriesView的代码示例:

private void FormatBarSeries (SideBySideBarSeriesView bar)
{
    bar.AxisY.NumericOptions.Format = NumericFormat.Currency;
    bar.AxisY.NumericOptions.Precision = 0;
    bar.AxisY.Title.Text = "Sample Bar";
    bar.AxisY.Title.Font = new Font(GetChartFontFamily(), 10.0f, FontStyle.Regular);
    bar.AxisY.Title.Visible = true;
    bar.AxisY.Tickmarks.Visible = false;
    bar.AxisY.Tickmarks.MinorVisible = false;
    bar.AxisX.Tickmarks.MinorVisible = false;
}

I cannot seem to find any manner of defining the culture for the specific chart. 我似乎找不到任何方法来定义特定图表的区域性。 Each report may contain multiple charts, but all the charts on a report would be the same currency format. 每个报告可能包含多个图表,但是报告上的所有图表将使用相同的货币格式。

Can this perhaps be set at the report level and have all charts inherit it? 可以在报表级别设置它,并让所有图表继承它吗? Or how can I set this on a chart by chart basis? 或者如何在逐个图表的基础上进行设置?

Found the answer, as demonstrated here : 找到了答案,这表现在这里

You have to hook into a CustomDrawAxisLabel event: 您必须加入CustomDrawAxisLabel事件:

Chart.CustomDrawAxisLabel += Chart_CustomDrawAxisLabel;

where you can override the code with a function like this: 您可以使用以下函数覆盖代码:

void Chart_CustomDrawAxisLabel (object sender, CustomDrawAxisLabelEventArgs e)
  {
    AxisBase axis = e.Item.Axis;
    if(axis.NumericOptions.Format == NumericFormat.Currency)
      {
        decimal value = 0.00M;
        e.Item.Text = decimal.TryParse(e.Item.AxisValue.ToString(),
                                        out value)
                         ? value.ToString("C0", _culture)
                         : e.Item.AxisValue.ToString();
      }
  }

using a culture variable defined elsewhere in the class. 使用班级其他地方定义的文化变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM