123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- //
- // Purpose: Helper class which converts DateTime or numeric
- // values to string. It used to display data point
- // values as labels, tooltips and axis labels.
- //
- using System;
- using System.Globalization;
- namespace FastReport.DataVisualization.Charting.Utilities
- {
- /// <summary>
- /// ValueConverter class is used when numeric or DateTime
- /// value needs to be converted to a string using specified format.
- /// </summary>
- internal static class ValueConverter
- {
- #region Methods
- /// <summary>
- /// Converts value to string using specified format.
- /// </summary>
- /// <param name="chart">Reference to the chart object.</param>
- /// <param name="obj">Reference to the object being formatted.</param>
- /// <param name="objTag">Additional object tag.</param>
- /// <param name="value">Value converted to string.</param>
- /// <param name="format">Format string.</param>
- /// <param name="valueType">Value type.</param>
- /// <param name="elementType">Chart element type being formatted.</param>
- public static string FormatValue(
- Chart chart,
- object obj,
- object objTag,
- double value,
- string format,
- ChartValueType valueType,
- ChartElementType elementType)
- {
- format = format ?? String.Empty;
- string convertionFormat = format;
- string result = "";
- // Make sure value index is part of the format
- if(convertionFormat != null && convertionFormat.Length > 0)
- {
- int bracketIndex = convertionFormat.IndexOf('{', 0);
- if(bracketIndex >= 0)
- {
- while(bracketIndex >= 0)
- {
- // If format is not followed by the value index
- if(!convertionFormat.Substring(bracketIndex).StartsWith("{0:", StringComparison.Ordinal))
- {
- // Check charcter prior to the bracket
- if(bracketIndex >= 1 && convertionFormat.Substring(bracketIndex - 1, 1) == "{")
- {
- continue;
- }
- else
- {
- // Insert value index in format
- convertionFormat = convertionFormat.Insert(bracketIndex + 1, "0:");
- }
- }
- bracketIndex = convertionFormat.IndexOf('{', bracketIndex + 1);
- }
- }
- else
- {
- convertionFormat = "{0:" + convertionFormat + "}";
- }
- }
- // Date/time formating
- if (valueType == ChartValueType.DateTime ||
- valueType == ChartValueType.DateTimeOffset ||
- valueType == ChartValueType.Date)
- {
- // Set default format
- if(convertionFormat.Length == 0)
- {
- convertionFormat = "{0:d}";
- if (valueType == ChartValueType.DateTimeOffset)
- convertionFormat += " +0";
- }
- // Convert date to string
- result = String.Format(CultureInfo.CurrentCulture, convertionFormat, DateTime.FromOADate(value));
- }
- else if(valueType == ChartValueType.Time)
- {
- // Set default format
- if(convertionFormat.Length == 0)
- {
- convertionFormat = "{0:t}";
- }
- // Convert date to string
- result = String.Format(CultureInfo.CurrentCulture, convertionFormat, DateTime.FromOADate(value));
- }
- else
- {
- bool failedFlag = false;
- // Set default format
- if(convertionFormat.Length == 0)
- {
- convertionFormat = "{0:G}";
- }
- try
- {
- // Numeric value formatting
- result = String.Format(CultureInfo.CurrentCulture,convertionFormat, value);
- }
- catch(FormatException)
- {
- failedFlag = true;
- }
- // If numeric formatting failed try to format using decimal number
- if(failedFlag)
- {
- failedFlag = false;
- try
- {
- // Decimal value formatting
- result = String.Format(CultureInfo.CurrentCulture, convertionFormat, (long)value);
- }
- catch (ArgumentNullException)
- {
- failedFlag = true;
- }
- catch (FormatException)
- {
- failedFlag = true;
- }
- }
- // Return format string as result (literal) if all formatting methods failed
- if(failedFlag)
- {
- result = format;
- }
- }
- // For the Reporting Services chart a special number formatting
- // handler may be set and used for all formatting needs.
- if (chart != null)
- {
- // Call number formatter
- FormatNumberEventArgs eventArguments = new FormatNumberEventArgs(value, format, valueType, result, objTag, elementType);
- chart.CallOnFormatNumber(obj, eventArguments);
- result = eventArguments.LocalizedValue;
- }
- return result;
- }
-
- #endregion
- }
- }
|