20 December, 2011

Localization in Chart (Microsoft Chart Control), Localizing DataPoint

I had a scenario where need to localize the data points that are displayed on the chart, that the decimal needs to be localized as comma (,) in Russian and Spanish., Thanks to my friend Gagan for this
E.g.:

We can assign an EventHandler to FormatNumber Property as Below

private MemoryStream RenderGraph()
{
Chart barchart = new Chart();
//many number codes are removed
//Removed other formatting lines
barchart.FormatNumber += new EventHandler<FormatNumberEventArgs>(ConvertChartDecimalFormat);
            barchart.DataSource = dt;   
barchart.Series[0].XValueMember = "XProp";
            barchart.Series[0].YValueMembers = "YProp";
            barchart.DataBind();

//return the memory stream image
}

Followed the event method will look as below. 
void ConvertChartDecimalFormat(object sender, FormatNumberEventArgs e)
{
if (sender.GetType() == typeof(DataPoint))
       {
                try
                {
                    e.LocalizedValue = Double.Parse(e.LocalizedValue).ToString(currentCulture);
                }
                catch (Exception)
                {
                    //do if anything required.
                }
        }

}




Enjoy.!!! :-)

0 comments:

Post a Comment