Skip to content

Commit 3aa9100

Browse files
committed
Fixed #33 + refactoring.
1 parent 5b5a444 commit 3aa9100

File tree

1 file changed

+148
-145
lines changed

1 file changed

+148
-145
lines changed

src/System.Windows.Forms.DataVisualization/Utilities/ValueConverter.cs

+148-145
Original file line numberDiff line numberDiff line change
@@ -12,157 +12,160 @@
1212

1313
using System.Globalization;
1414

15-
namespace System.Windows.Forms.DataVisualization.Charting.Utilities
15+
namespace System.Windows.Forms.DataVisualization.Charting.Utilities;
16+
17+
/// <summary>
18+
/// ValueConverter class is used when numeric or DateTime
19+
/// value needs to be converted to a string using specified format.
20+
/// </summary>
21+
internal static class ValueConverter
1622
{
23+
#region Methods
24+
1725
/// <summary>
18-
/// ValueConverter class is used when numeric or DateTime
19-
/// value needs to be converted to a string using specified format.
26+
/// Converts value to string using specified format.
2027
/// </summary>
21-
internal static class ValueConverter
22-
{
23-
#region Methods
24-
25-
/// <summary>
26-
/// Converts value to string using specified format.
27-
/// </summary>
28-
/// <param name="chart">Reference to the chart object.</param>
29-
/// <param name="obj">Reference to the object being formatted.</param>
30-
/// <param name="objTag">Additional object tag.</param>
31-
/// <param name="value">Value converted to string.</param>
32-
/// <param name="format">Format string.</param>
33-
/// <param name="valueType">Value type.</param>
34-
/// <param name="elementType">Chart element type being formatted.</param>
35-
public static string FormatValue(
36-
Chart chart,
37-
object obj,
38-
object objTag,
39-
double value,
40-
string format,
41-
ChartValueType valueType,
42-
ChartElementType elementType)
43-
{
44-
format ??= string.Empty;
45-
string convertionFormat = format;
46-
string result = string.Empty;
47-
48-
// Make sure value index is part of the format
49-
if(convertionFormat != null && convertionFormat.Length > 0)
50-
{
51-
int bracketIndex = convertionFormat.IndexOf('{', 0);
52-
if(bracketIndex >= 0)
53-
{
54-
while(bracketIndex >= 0)
55-
{
56-
// If format is not followed by the value index
57-
if(!convertionFormat[bracketIndex..].StartsWith("{0:", StringComparison.Ordinal))
58-
{
59-
// Check character prior to the bracket
60-
if(bracketIndex >= 1 && convertionFormat.Substring(bracketIndex - 1, 1) == "{")
61-
{
62-
continue;
63-
}
64-
else
65-
{
66-
// Insert value index in format
67-
convertionFormat = convertionFormat.Insert(bracketIndex + 1, "0:");
68-
}
69-
}
70-
71-
bracketIndex = convertionFormat.IndexOf('{', bracketIndex + 1);
72-
}
73-
}
74-
else
75-
{
76-
convertionFormat = "{0:" + convertionFormat + "}";
77-
}
78-
}
79-
80-
// Date/time formating
81-
if (valueType == ChartValueType.DateTime ||
82-
valueType == ChartValueType.DateTimeOffset ||
83-
valueType == ChartValueType.Date)
84-
{
85-
// Set default format
86-
if(convertionFormat.Length == 0)
87-
{
88-
convertionFormat = "{0:d}";
89-
if (valueType == ChartValueType.DateTimeOffset)
90-
convertionFormat += " +0";
91-
}
92-
93-
// Convert date to string
94-
result = String.Format(CultureInfo.CurrentCulture, convertionFormat, DateTime.FromOADate(value));
95-
}
96-
else if(valueType == ChartValueType.Time)
97-
{
98-
// Set default format
99-
if(convertionFormat.Length == 0)
100-
{
101-
convertionFormat = "{0:t}";
102-
}
103-
104-
// Convert date to string
105-
result = String.Format(CultureInfo.CurrentCulture, convertionFormat, DateTime.FromOADate(value));
106-
}
107-
else
108-
{
109-
bool failedFlag = false;
110-
111-
// Set default format
112-
if(convertionFormat.Length == 0)
113-
{
114-
convertionFormat = "{0:G}";
115-
}
116-
117-
try
118-
{
119-
// Numeric value formatting
120-
result = String.Format(CultureInfo.CurrentCulture,convertionFormat, value);
121-
}
122-
catch(FormatException)
123-
{
124-
failedFlag = true;
125-
}
126-
127-
// If numeric formatting failed try to format using decimal number
128-
if(failedFlag)
129-
{
130-
failedFlag = false;
131-
try
132-
{
133-
// Decimal value formatting
134-
result = String.Format(CultureInfo.CurrentCulture, convertionFormat, (long)value);
135-
}
136-
catch (ArgumentNullException)
137-
{
138-
failedFlag = true;
139-
}
140-
catch (FormatException)
28+
/// <param name="chart">Reference to the chart object.</param>
29+
/// <param name="obj">Reference to the object being formatted.</param>
30+
/// <param name="objTag">Additional object tag.</param>
31+
/// <param name="value">Value converted to string.</param>
32+
/// <param name="format">Format string.</param>
33+
/// <param name="valueType">Value type.</param>
34+
/// <param name="elementType">Chart element type being formatted.</param>
35+
public static string FormatValue(
36+
Chart chart,
37+
object obj,
38+
object objTag,
39+
double value,
40+
string format,
41+
ChartValueType valueType,
42+
ChartElementType elementType)
43+
{
44+
format ??= string.Empty;
45+
string convertionFormat = format;
46+
string result = string.Empty;
47+
48+
// Make sure value index is part of the format
49+
if (convertionFormat != null && convertionFormat.Length > 0)
50+
{
51+
int bracketIndex = convertionFormat.IndexOf('{', 0);
52+
if (bracketIndex >= 0)
53+
{
54+
while (bracketIndex >= 0)
55+
{
56+
// If format is not followed by the value index
57+
if (!convertionFormat[bracketIndex..].StartsWith("{0:", StringComparison.Ordinal))
14158
{
142-
failedFlag = true;
59+
// Check character prior to the bracket
60+
if (bracketIndex >= 1 && convertionFormat.Substring(bracketIndex - 1, 1) == "{")
61+
{
62+
continue;
63+
}
64+
else
65+
{
66+
// Insert value index in format
67+
convertionFormat = convertionFormat.Insert(bracketIndex + 1, "0:");
68+
}
14369
}
144-
}
145-
146-
// Return format string as result (literal) if all formatting methods failed
147-
if(failedFlag)
148-
{
149-
result = format;
150-
}
151-
}
152-
153-
// For the Reporting Services chart a special number formatting
154-
// handler may be set and used for all formatting needs.
155-
if (chart != null)
70+
71+
bracketIndex = convertionFormat.IndexOf('{', bracketIndex + 1);
72+
}
73+
}
74+
else
75+
{
76+
convertionFormat = "{0:" + convertionFormat + "}";
77+
}
78+
}
79+
80+
// Date/time formating
81+
if (valueType == ChartValueType.DateTime ||
82+
valueType == ChartValueType.DateTimeOffset ||
83+
valueType == ChartValueType.Date)
84+
{
85+
// Set default format
86+
if (convertionFormat.Length == 0)
87+
{
88+
convertionFormat = "{0:d}";
89+
if (valueType == ChartValueType.DateTimeOffset)
90+
convertionFormat += " +0";
91+
}
92+
93+
// Convert date to string
94+
result = string.Format(CultureInfo.CurrentCulture, convertionFormat, DateTime.FromOADate(value));
95+
}
96+
else if (valueType == ChartValueType.Time)
97+
{
98+
// Set default format
99+
if (convertionFormat.Length == 0)
156100
{
157-
// Call number formatter
158-
FormatNumberEventArgs eventArguments = new FormatNumberEventArgs(value, format, valueType, result, objTag, elementType);
159-
chart.CallOnFormatNumber(obj, eventArguments);
160-
result = eventArguments.LocalizedValue;
101+
convertionFormat = "{0:t}";
161102
}
162103

163-
return result;
164-
}
165-
166-
#endregion
167-
}
104+
// Convert date to string
105+
result = string.Format(CultureInfo.CurrentCulture, convertionFormat, DateTime.FromOADate(value));
106+
}
107+
else
108+
{
109+
bool failedFlag = false;
110+
111+
// Set default format
112+
if (convertionFormat.Length == 0)
113+
{
114+
convertionFormat = "{0:G}";
115+
}
116+
117+
try
118+
{
119+
// Numeric value formatting
120+
// Workaround for "-0" label due to Floating-Point parsing changes in .NET Core 3. See https://devblogs.microsoft.com/dotnet/floating-point-parsing-and-formatting-improvements-in-net-core-3-0/
121+
if (value == 0d && double.IsNegative(value))
122+
result = string.Format(CultureInfo.CurrentCulture, convertionFormat, 0d);
123+
else
124+
result = string.Format(CultureInfo.CurrentCulture, convertionFormat, value);
125+
}
126+
catch (FormatException)
127+
{
128+
failedFlag = true;
129+
}
130+
131+
// If numeric formatting failed try to format using decimal number
132+
if (failedFlag)
133+
{
134+
failedFlag = false;
135+
try
136+
{
137+
// Decimal value formatting
138+
result = string.Format(CultureInfo.CurrentCulture, convertionFormat, (long)value);
139+
}
140+
catch (ArgumentNullException)
141+
{
142+
failedFlag = true;
143+
}
144+
catch (FormatException)
145+
{
146+
failedFlag = true;
147+
}
148+
}
149+
150+
// Return format string as result (literal) if all formatting methods failed
151+
if (failedFlag)
152+
{
153+
result = format;
154+
}
155+
}
156+
157+
// For the Reporting Services chart a special number formatting
158+
// handler may be set and used for all formatting needs.
159+
if (chart != null)
160+
{
161+
// Call number formatter
162+
FormatNumberEventArgs eventArguments = new FormatNumberEventArgs(value, format, valueType, result, objTag, elementType);
163+
chart.CallOnFormatNumber(obj, eventArguments);
164+
result = eventArguments.LocalizedValue;
165+
}
166+
167+
return result;
168+
}
169+
170+
#endregion
168171
}

0 commit comments

Comments
 (0)