Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Web.DataVisualization / Common / Utilities / ValueConverter.cs
1 //-------------------------------------------------------------
2 // <copyright company=\92Microsoft Corporation\92>
3 //   Copyright © Microsoft Corporation. All Rights Reserved.
4 // </copyright>
5 //-------------------------------------------------------------
6 // @owner=alexgor, deliant
7 //=================================================================
8 //  File:               ValueConverter.cs
9 //
10 //  Namespace:  System.Web.UI.WebControls[Windows.Forms].Charting.Utilities
11 //
12 //      Classes:        ValueConverter
13 //
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.
17 //
18 //      Reviewed:       AG - August 7, 2002
19 //              AG - Microsoft 5, 2007
20 //
21 //===================================================================
22
23
24 #region Used Namespaces
25
26 using System;
27 using System.Globalization;
28
29 #if Microsoft_CONTROL
30         using System.Windows.Forms.DataVisualization.Charting;
31 #else
32         using System.Web.UI.DataVisualization.Charting;
33 #endif
34
35 #endregion
36
37 #if Microsoft_CONTROL
38         namespace System.Windows.Forms.DataVisualization.Charting.Utilities
39 #else
40         namespace System.Web.UI.DataVisualization.Charting.Utilities
41 #endif
42 {
43         /// <summary>
44     /// ValueConverter class is used when numeric or DateTime 
45     /// value needs to be converted to a string using specified format.
46         /// </summary>
47         internal static class ValueConverter
48         {
49                 #region Methods
50
51         /// <summary>
52         /// Converts value to string using specified format.
53         /// </summary>
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(
62                         Chart chart,
63                         object obj,
64             object objTag,
65                         double value, 
66                         string format, 
67                         ChartValueType valueType,
68                         ChartElementType elementType)
69                 {
70             format = format ?? String.Empty;
71             string      convertionFormat = format;
72                         string  result = "";
73
74                         // Make sure value index is part of the format
75                         if(convertionFormat != null && convertionFormat.Length > 0)
76                         {
77                                 int     bracketIndex = convertionFormat.IndexOf('{', 0);
78                                 if(bracketIndex >= 0)
79                                 {
80                                         while(bracketIndex >= 0)
81                                         {
82                                                 // If format is not followed by the value index
83                                                 if(!convertionFormat.Substring(bracketIndex).StartsWith("{0:", StringComparison.Ordinal))
84                                                 {
85                                                         // Check charcter prior to the bracket
86                                                         if(bracketIndex >= 1 && convertionFormat.Substring(bracketIndex - 1, 1) == "{")
87                                                         {
88                                                                 continue;
89                                                         }
90                                                         else
91                                                         {
92                                                                 // Insert value index in format
93                                                                 convertionFormat = convertionFormat.Insert(bracketIndex + 1, "0:");
94                                                         }
95                                                 }
96
97                                                 bracketIndex = convertionFormat.IndexOf('{', bracketIndex + 1);
98                                         }
99                                 }
100                                 else
101                                 {
102                                         convertionFormat = "{0:" + convertionFormat + "}";
103                                 }
104                         }
105
106                         // Date/time formating
107             if (valueType == ChartValueType.DateTime || 
108                 valueType == ChartValueType.DateTimeOffset || 
109                 valueType == ChartValueType.Date)
110                         {
111                                 // Set default format
112                                 if(convertionFormat.Length == 0)
113                                 {
114                                         convertionFormat = "{0:d}";
115                     if (valueType == ChartValueType.DateTimeOffset)
116                         convertionFormat += " +0";
117                                 }
118
119                                 // Convert date to string
120                 result = String.Format(CultureInfo.CurrentCulture, convertionFormat, DateTime.FromOADate(value));
121                         }
122                         else if(valueType == ChartValueType.Time)
123                         {
124                                 // Set default format
125                                 if(convertionFormat.Length == 0)
126                                 {
127                                         convertionFormat = "{0:t}";
128                                 }
129
130                                 // Convert date to string
131                 result = String.Format(CultureInfo.CurrentCulture, convertionFormat, DateTime.FromOADate(value));
132                         }
133                         else
134                         {
135                                 bool    failedFlag = false;
136
137                                 // Set default format
138                                 if(convertionFormat.Length == 0)
139                                 {
140                                         convertionFormat = "{0:G}";
141                                 }
142
143                                 try
144                                 {
145                                         // Numeric value formatting
146                     result = String.Format(CultureInfo.CurrentCulture,convertionFormat, value);
147                                 }
148                                 catch(FormatException)
149                                 {
150                                         failedFlag = true;
151                                 }
152
153                                 // If numeric formatting failed try to format using decimal number
154                                 if(failedFlag)
155                                 {
156                                         failedFlag = false;
157                     try
158                     {
159                         // Decimal value formatting
160                         result = String.Format(CultureInfo.CurrentCulture, convertionFormat, (long)value);
161                     }
162                     catch (ArgumentNullException)
163                     {
164                         failedFlag = true;
165                     }
166                     catch (FormatException)
167                     {
168                         failedFlag = true;
169                     }
170                                 }
171
172                                 // Return format string as result (literal) if all formatting methods failed
173                                 if(failedFlag)
174                                 {
175                     result = format;
176                                 }
177                         }
178
179             // For the Reporting Services chart a special number formatting
180             // handler may be set and used for all formatting needs.
181             if (chart != null)
182             {
183                 // Call number formatter
184                 FormatNumberEventArgs eventArguments = new FormatNumberEventArgs(value, format, valueType, result, objTag, elementType);
185                 chart.CallOnFormatNumber(obj, eventArguments);
186                 result = eventArguments.LocalizedValue;
187             }
188
189                         return result;
190                 }
191         
192                 #endregion
193         }
194 }