mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-18 06:20:15 +00:00
3.0
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using UnityEngine;
|
||||
using System.Linq;
|
||||
|
||||
namespace XCharts.Runtime
|
||||
{
|
||||
public static class FormatterHelper
|
||||
{
|
||||
public const string PH_NN = "\n";
|
||||
private static Regex s_Regex = new Regex(@"{([a-e|.]\d*)(:\d+(-\d+)?)?(:[c-g|x|p|r]\d*|:0\.#*)?}", RegexOptions.IgnoreCase);
|
||||
private static Regex s_Regex = new Regex(@"{([a-g|.]\d*)(:\d+(-\d+)?)?(:[c-g|x|p|r]\d*|:0\.#*)?}", RegexOptions.IgnoreCase);
|
||||
private static Regex s_RegexSub = new Regex(@"(0\.#*)|(\d+-\d+)|(\w+)|(\.)", RegexOptions.IgnoreCase);
|
||||
private static Regex s_RegexN = new Regex(@"^\d+", RegexOptions.IgnoreCase);
|
||||
private static Regex s_RegexN_N = new Regex(@"\d+-\d+", RegexOptions.IgnoreCase);
|
||||
@@ -17,11 +16,16 @@ namespace XCharts.Runtime
|
||||
private static Regex s_RegexNewLine = new Regex(@"[\\|/]+n|</br>|<br>|<br/>", RegexOptions.IgnoreCase);
|
||||
private static Regex s_RegexForAxisLabel = new Regex(@"{value(:[c-g|x|p|r]\d*)?}", RegexOptions.IgnoreCase);
|
||||
private static Regex s_RegexSubForAxisLabel = new Regex(@"(value)|([c-g|x|p|r]\d*)", RegexOptions.IgnoreCase);
|
||||
private static Regex s_RegexForSerieLabel = new Regex(@"{[a-e|\.](:[c-g|x|p|r]\d*)?}", RegexOptions.IgnoreCase);
|
||||
private static Regex s_RegexSubForSerieLabel = new Regex(@"(\.)|([a-e])|([c-g|x|p|r]\d*)", RegexOptions.IgnoreCase);
|
||||
private static Regex s_RegexForSerieLabel = new Regex(@"{[a-g|\.](:[c-g|x|p|r]\d*)?}", RegexOptions.IgnoreCase);
|
||||
private static Regex s_RegexSubForSerieLabel = new Regex(@"(\.)|([a-g])|([c-g|x|p|r]\d*)", RegexOptions.IgnoreCase);
|
||||
|
||||
public static bool NeedFormat(string content)
|
||||
{
|
||||
return content.IndexOf('{') >= 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 替换字符串中的通配符,支持的通配符有{.}、{a}、{b}、{c}、{d}、{e}。
|
||||
/// 替换字符串中的通配符,支持的通配符有{.}、{a}、{b}、{c}、{d}、{e}、{f}、{g}。
|
||||
/// </summary>
|
||||
/// <param name="content">要替换的字符串</param>
|
||||
/// <param name="dataIndex">选中的数据项serieData索引</param>
|
||||
@@ -98,9 +102,14 @@ namespace XCharts.Runtime
|
||||
content = content.Replace(old, serieData.name);
|
||||
}
|
||||
}
|
||||
else if (p == 'c' || p == 'C' || p == 'd' || p == 'D')
|
||||
else if (p == 'g' || p == 'G')
|
||||
{
|
||||
content = content.Replace(old, ChartCached.NumberToStr(serie.dataCount, ""));
|
||||
}
|
||||
else if (p == 'c' || p == 'C' || p == 'd' || p == 'D' || p == 'f' || p == 'f')
|
||||
{
|
||||
var isPercent = p == 'd' || p == 'D';
|
||||
var isTotal = p == 'f' || p == 'f';
|
||||
var bIndex = dataIndex;
|
||||
var dimensionIndex = -1;
|
||||
if (argsCount >= 2)
|
||||
@@ -133,7 +142,7 @@ namespace XCharts.Runtime
|
||||
if (dimensionIndex == -1) dimensionIndex = 1;
|
||||
if (numericFormatter == string.Empty)
|
||||
{
|
||||
numericFormatter = SerieHelper.GetNumericFormatter(serie, serie.GetSerieData(bIndex));
|
||||
numericFormatter = SerieHelper.GetNumericFormatter(serie, serie.GetSerieData(bIndex), "");
|
||||
}
|
||||
var value = serie.GetData(bIndex, dimensionIndex, dataZoom);
|
||||
if (isPercent)
|
||||
@@ -142,6 +151,11 @@ namespace XCharts.Runtime
|
||||
var percent = total == 0 ? 0 : value / serie.yTotal * 100;
|
||||
content = content.Replace(old, ChartCached.FloatToStr(percent, numericFormatter));
|
||||
}
|
||||
else if (isTotal)
|
||||
{
|
||||
var total = serie.GetDataTotal(dimensionIndex, serie.GetSerieData(bIndex));
|
||||
content = content.Replace(old, ChartCached.FloatToStr(total, numericFormatter));
|
||||
}
|
||||
else
|
||||
{
|
||||
content = content.Replace(old, ChartCached.FloatToStr(value, numericFormatter));
|
||||
@@ -152,6 +166,58 @@ namespace XCharts.Runtime
|
||||
return foundDot;
|
||||
}
|
||||
|
||||
public static void ReplaceSerieLabelContent(ref string content, string numericFormatter, int dataCount, double value, double total,
|
||||
string serieName, string category, string dataName, Color color)
|
||||
{
|
||||
var mc = s_RegexForSerieLabel.Matches(content);
|
||||
foreach (var m in mc)
|
||||
{
|
||||
var old = m.ToString();
|
||||
var args = s_RegexSubForSerieLabel.Matches(old);
|
||||
var argsCount = args.Count;
|
||||
if (argsCount <= 0) continue;
|
||||
var p = args[0].ToString().ElementAt(0);
|
||||
if (argsCount >= 2)
|
||||
{
|
||||
numericFormatter = args[1].ToString();
|
||||
}
|
||||
if (p == '.')
|
||||
{
|
||||
content = content.Replace(old, ChartCached.ColorToDotStr(color));
|
||||
}
|
||||
else if (p == 'a' || p == 'A')
|
||||
{
|
||||
content = content.Replace(old, serieName);
|
||||
}
|
||||
else if (p == 'b' || p == 'B')
|
||||
{
|
||||
content = content.Replace(old, category);
|
||||
}
|
||||
else if (p == 'e' || p == 'E')
|
||||
{
|
||||
content = content.Replace(old, dataName);
|
||||
}
|
||||
else if (p == 'd' || p == 'D')
|
||||
{
|
||||
var rate = total == 0 ? 0 : value / total * 100;
|
||||
content = content.Replace(old, ChartCached.NumberToStr(rate, numericFormatter));
|
||||
}
|
||||
else if (p == 'c' || p == 'C')
|
||||
{
|
||||
content = content.Replace(old, ChartCached.NumberToStr(value, numericFormatter));
|
||||
}
|
||||
else if (p == 'f' || p == 'f')
|
||||
{
|
||||
content = content.Replace(old, ChartCached.NumberToStr(total, numericFormatter));
|
||||
}
|
||||
else if (p == 'g' || p == 'G')
|
||||
{
|
||||
content = content.Replace(old, ChartCached.NumberToStr(dataCount, numericFormatter));
|
||||
}
|
||||
}
|
||||
content = TrimAndReplaceLine(content);
|
||||
}
|
||||
|
||||
private static char GetSerieIndex(string strType, ref int index)
|
||||
{
|
||||
index = -1;
|
||||
@@ -207,48 +273,5 @@ namespace XCharts.Runtime
|
||||
content = TrimAndReplaceLine(content);
|
||||
}
|
||||
|
||||
public static void ReplaceSerieLabelContent(ref string content, string numericFormatter, double value, double total,
|
||||
string serieName, string category, string dataName, Color color)
|
||||
{
|
||||
var mc = s_RegexForSerieLabel.Matches(content);
|
||||
foreach (var m in mc)
|
||||
{
|
||||
var old = m.ToString();
|
||||
var args = s_RegexSubForSerieLabel.Matches(old);
|
||||
var argsCount = args.Count;
|
||||
if (argsCount <= 0) continue;
|
||||
var p = args[0].ToString().ElementAt(0);
|
||||
if (argsCount >= 2)
|
||||
{
|
||||
numericFormatter = args[1].ToString();
|
||||
}
|
||||
if (p == '.')
|
||||
{
|
||||
content = content.Replace(old, ChartCached.ColorToDotStr(color));
|
||||
}
|
||||
else if (p == 'a' || p == 'A')
|
||||
{
|
||||
content = content.Replace(old, serieName);
|
||||
}
|
||||
else if (p == 'b' || p == 'B')
|
||||
{
|
||||
content = content.Replace(old, category);
|
||||
}
|
||||
else if (p == 'e' || p == 'E')
|
||||
{
|
||||
content = content.Replace(old, dataName);
|
||||
}
|
||||
else if (p == 'd' || p == 'D')
|
||||
{
|
||||
var rate = total == 0 ? 0 : value / total * 100;
|
||||
content = content.Replace(old, ChartCached.FloatToStr(rate, numericFormatter));
|
||||
}
|
||||
else if (p == 'c' || p == 'C')
|
||||
{
|
||||
content = content.Replace(old, ChartCached.FloatToStr(value, numericFormatter));
|
||||
}
|
||||
}
|
||||
content = TrimAndReplaceLine(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user