ValueConverter.cs 5.0 KB

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