1 //-------------------------------------------------------------
2 // <copyright company=
\92Microsoft Corporation
\92>
3 // Copyright © Microsoft Corporation. All Rights Reserved.
5 //-------------------------------------------------------------
6 // @owner=alexgor, deliant
7 //=================================================================
8 // File: ValueConverter.cs
10 // Namespace: System.Web.UI.WebControls[Windows.Forms].Charting.Utilities
12 // Classes: ValueConverter
14 // Purpose: Helper class which converts DateTime or numeric
15 // values to string. It used to display data point
16 // values as labels, tooltips and axis labels.
18 // Reviewed: AG - August 7, 2002
19 // AG - Microsoft 5, 2007
21 //===================================================================
24 #region Used Namespaces
27 using System.Globalization;
30 using System.Windows.Forms.DataVisualization.Charting;
32 using System.Web.UI.DataVisualization.Charting;
38 namespace System.Windows.Forms.DataVisualization.Charting.Utilities
40 namespace System.Web.UI.DataVisualization.Charting.Utilities
44 /// ValueConverter class is used when numeric or DateTime
45 /// value needs to be converted to a string using specified format.
47 internal static class ValueConverter
52 /// Converts value to string using specified format.
54 /// <param name="chart">Reference to the chart object.</param>
55 /// <param name="obj">Reference to the object being formatted.</param>
56 /// <param name="objTag">Additional object tag.</param>
57 /// <param name="value">Value converted to string.</param>
58 /// <param name="format">Format string.</param>
59 /// <param name="valueType">Value type.</param>
60 /// <param name="elementType">Chart element type being formatted.</param>
61 public static string FormatValue(
67 ChartValueType valueType,
68 ChartElementType elementType)
70 format = format ?? String.Empty;
71 string convertionFormat = format;
74 // Make sure value index is part of the format
75 if(convertionFormat != null && convertionFormat.Length > 0)
77 int bracketIndex = convertionFormat.IndexOf('{', 0);
80 while(bracketIndex >= 0)
82 // If format is not followed by the value index
83 if(!convertionFormat.Substring(bracketIndex).StartsWith("{0:", StringComparison.Ordinal))
85 // Check charcter prior to the bracket
86 if(bracketIndex >= 1 && convertionFormat.Substring(bracketIndex - 1, 1) == "{")
92 // Insert value index in format
93 convertionFormat = convertionFormat.Insert(bracketIndex + 1, "0:");
97 bracketIndex = convertionFormat.IndexOf('{', bracketIndex + 1);
102 convertionFormat = "{0:" + convertionFormat + "}";
106 // Date/time formating
107 if (valueType == ChartValueType.DateTime ||
108 valueType == ChartValueType.DateTimeOffset ||
109 valueType == ChartValueType.Date)
111 // Set default format
112 if(convertionFormat.Length == 0)
114 convertionFormat = "{0:d}";
115 if (valueType == ChartValueType.DateTimeOffset)
116 convertionFormat += " +0";
119 // Convert date to string
120 result = String.Format(CultureInfo.CurrentCulture, convertionFormat, DateTime.FromOADate(value));
122 else if(valueType == ChartValueType.Time)
124 // Set default format
125 if(convertionFormat.Length == 0)
127 convertionFormat = "{0:t}";
130 // Convert date to string
131 result = String.Format(CultureInfo.CurrentCulture, convertionFormat, DateTime.FromOADate(value));
135 bool failedFlag = false;
137 // Set default format
138 if(convertionFormat.Length == 0)
140 convertionFormat = "{0:G}";
145 // Numeric value formatting
146 result = String.Format(CultureInfo.CurrentCulture,convertionFormat, value);
148 catch(FormatException)
153 // If numeric formatting failed try to format using decimal number
159 // Decimal value formatting
160 result = String.Format(CultureInfo.CurrentCulture, convertionFormat, (long)value);
162 catch (ArgumentNullException)
166 catch (FormatException)
172 // Return format string as result (literal) if all formatting methods failed
179 // For the Reporting Services chart a special number formatting
180 // handler may be set and used for all formatting needs.
183 // Call number formatter
184 FormatNumberEventArgs eventArguments = new FormatNumberEventArgs(value, format, valueType, result, objTag, elementType);
185 chart.CallOnFormatNumber(obj, eventArguments);
186 result = eventArguments.LocalizedValue;