优化PieChart在数据全为0时的显示为等份的效果

This commit is contained in:
monitor1394
2020-06-07 07:16:24 +08:00
parent 2353b24aa6
commit 9e16e72c51
8 changed files with 248 additions and 180 deletions

View File

@@ -4,23 +4,14 @@
/* https://github.com/monitor1394 */
/* */
/******************************************/
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
using System.Linq;
namespace XCharts
{
internal static class TooltipHelper
{
private const string PH_NN = "\n";
private static Regex s_Regex = new Regex(@"{([a-d|.]\d*)(:\d+(-\d+)?)?(:[c-g|x|p|r]\d*)?}", RegexOptions.IgnoreCase);
private static Regex s_RegexSub = new Regex(@"(\w?-?\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);
private static Regex s_RegexFn = new Regex(@"[c-g|x|p|r]\d*", RegexOptions.IgnoreCase);
private static Regex s_RegexNewLine = new Regex(@"[\\|/]+n", RegexOptions.IgnoreCase);
private static void InitScatterTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
ThemeInfo themeInfo)
{
@@ -28,7 +19,7 @@ namespace XCharts
var dataIndexList = tooltip.runtimeSerieDataIndex[serie.index];
if (!string.IsNullOrEmpty(serie.name))
{
sb.Append(serie.name).Append(PH_NN);
sb.Append(serie.name).Append(FormatterHelper.PH_NN);
}
for (int i = 0; i < dataIndexList.Count; i++)
{
@@ -62,7 +53,7 @@ namespace XCharts
sb.Length = 0;
if (!string.IsNullOrEmpty(serie.name))
{
sb.Append(serie.name).Append(PH_NN);
sb.Append(serie.name).Append(FormatterHelper.PH_NN);
}
sb.Append("<color=#").Append(themeInfo.GetColorStr(index)).Append(">● </color>");
if (!string.IsNullOrEmpty(key))
@@ -102,7 +93,7 @@ namespace XCharts
{
string key = radar.indicatorList[i].name;
float value = serieData.GetData(i);
if ((i == 0 && !string.IsNullOrEmpty(serieData.name)) || i > 0) sb.Append(PH_NN);
if ((i == 0 && !string.IsNullOrEmpty(serieData.name)) || i > 0) sb.Append(FormatterHelper.PH_NN);
sb.AppendFormat("{0}: {1}", key, ChartCached.FloatToStr(value, numericFormatter));
}
break;
@@ -213,27 +204,27 @@ namespace XCharts
var numericFormatter = GetItemNumericFormatter(tooltip, serie, null);
if (string.IsNullOrEmpty(itemFormatter))
{
if (!first) sb.Append(PH_NN);
InitDefaultContent(ref sb, tooltip, serie, dataIndex, category, themeInfo, dataZoom, isCartesian);
if (!first) sb.Append(FormatterHelper.PH_NN);
InitDefaultContent(ref sb, tooltip, serie, dataIndex, category, themeInfo, dataZoom);
first = false;
continue;
}
var itemTitle = title;
if (!string.IsNullOrEmpty(itemTitle))
{
ReplaceContent(ref itemTitle, dataIndex, tooltip, serie, null, themeInfo, category, dataZoom, isCartesian);
sb.Append(itemTitle).Append(PH_NN);
FormatterHelper.ReplaceContent(ref itemTitle, dataIndex, tooltip.numericFormatter, serie, null, themeInfo, category, dataZoom);
sb.Append(itemTitle).Append(FormatterHelper.PH_NN);
}
var dataIndexList = tooltip.runtimeSerieDataIndex[serie.index];
foreach (var tempIndex in dataIndexList)
{
string content = itemFormatter;
var foundDot = ReplaceContent(ref content, tempIndex, tooltip, serie, null, themeInfo, category, dataZoom, isCartesian);
var foundDot = FormatterHelper.ReplaceContent(ref content, tempIndex, tooltip.numericFormatter, serie, null, themeInfo, category, dataZoom);
if (!foundDot)
{
sb.Append(ChartCached.ColorToDotStr(themeInfo.GetColor(serie.index)));
}
sb.Append(content).Append(PH_NN);
sb.Append(content).Append(FormatterHelper.PH_NN);
}
}
}
@@ -245,20 +236,20 @@ namespace XCharts
needCategory = needCategory || (serie.type == SerieType.Line || serie.type == SerieType.Bar);
if (formatTitle)
{
ReplaceContent(ref title, dataIndex, tooltip, null, series, themeInfo, category, dataZoom, isCartesian);
FormatterHelper.ReplaceContent(ref title, dataIndex, tooltip.numericFormatter, null, series, themeInfo, category, dataZoom);
}
if (serie.show)
{
if (string.IsNullOrEmpty(itemFormatter))
{
if (!first) sb.Append(PH_NN);
InitDefaultContent(ref sb, tooltip, serie, dataIndex, category, themeInfo, dataZoom, isCartesian);
if (!first) sb.Append(FormatterHelper.PH_NN);
InitDefaultContent(ref sb, tooltip, serie, dataIndex, category, themeInfo, dataZoom);
first = false;
continue;
}
string content = itemFormatter;
ReplaceContent(ref content, dataIndex, tooltip, serie, null, themeInfo, category, dataZoom, isCartesian);
if (!first) sb.Append(PH_NN);
FormatterHelper.ReplaceContent(ref content, dataIndex, tooltip.numericFormatter, serie, null, themeInfo, category, dataZoom);
if (!first) sb.Append(FormatterHelper.PH_NN);
sb.Append(ChartCached.ColorToDotStr(themeInfo.GetColor(i)));
sb.Append(content);
first = false;
@@ -267,159 +258,27 @@ namespace XCharts
}
if (isScatter)
{
return TrimAndReplaceLine(sb);
return FormatterHelper.TrimAndReplaceLine(sb);
}
else if (string.IsNullOrEmpty(title))
{
if (needCategory) return category + PH_NN + TrimAndReplaceLine(sb);
else return TrimAndReplaceLine(sb);
if (needCategory) return category + FormatterHelper.PH_NN + FormatterHelper.TrimAndReplaceLine(sb);
else return FormatterHelper.TrimAndReplaceLine(sb);
}
else
{
title = s_RegexNewLine.Replace(title, PH_NN);
return title + PH_NN + TrimAndReplaceLine(sb);
title = FormatterHelper.TrimAndReplaceLine(title);
return title + FormatterHelper.PH_NN + FormatterHelper.TrimAndReplaceLine(sb);
}
}
else
{
string content = tooltip.formatter;
ReplaceContent(ref content, dataIndex, tooltip, null, series, themeInfo, category, dataZoom, isCartesian);
FormatterHelper.ReplaceContent(ref content, dataIndex, tooltip.numericFormatter, null, series, themeInfo, category, dataZoom);
return content;
}
}
public static bool ReplaceContent(ref string content, int dataIndex, Tooltip tooltip, Serie serie, Series series,
ThemeInfo themeInfo, string category = null, DataZoom dataZoom = null, bool isCartesian = false)
{
var foundDot = false;
var mc = s_Regex.Matches(content);
foreach (var m in mc)
{
var old = m.ToString();
var args = s_RegexSub.Matches(m.ToString());
var argsCount = args.Count;
if (argsCount <= 0) continue;
int targetIndex = 0;
char p = GetSerieIndex(args[0].ToString(), ref targetIndex);
if (serie == null)
{
if (targetIndex == -1) continue;
serie = series.GetSerie(targetIndex);
if (serie == null) continue;
}
else
{
targetIndex = serie.index;
}
if (p == '.')
{
var bIndex = targetIndex;
if (argsCount >= 2)
{
var args1Str = args[1].ToString();
if (s_RegexN.IsMatch(args1Str)) bIndex = int.Parse(args1Str);
}
content = content.Replace(old, ChartCached.ColorToDotStr(themeInfo.GetColor(bIndex)));
foundDot = true;
}
else if (p == 'a' || p == 'A')
{
if (argsCount == 1)
{
content = content.Replace(old, serie.name);
}
}
else if (p == 'b' || p == 'B')
{
var bIndex = dataIndex;
if (argsCount >= 2)
{
var args1Str = args[1].ToString();
if (s_RegexN.IsMatch(args1Str)) bIndex = int.Parse(args1Str);
}
var needCategory = serie.type == SerieType.Line || serie.type == SerieType.Bar;
if (needCategory)
{
content = content.Replace(old, category);
}
else
{
var serieData = serie.GetSerieData(bIndex, dataZoom);
content = content.Replace(old, serieData.name);
}
}
else if (p == 'c' || p == 'C' || p == 'd' || p == 'D')
{
var isPercent = p == 'd' || p == 'D';
var bIndex = dataIndex;
var dimensionIndex = -1;
var numericFormatter = string.Empty;
if (argsCount >= 2)
{
var args1Str = args[1].ToString();
if (s_RegexFn.IsMatch(args1Str))
{
numericFormatter = args1Str;
}
else if (s_RegexN_N.IsMatch(args1Str))
{
var temp = args1Str.Split('-');
bIndex = int.Parse(temp[0]);
dimensionIndex = int.Parse(temp[1]);
}
else if (s_RegexN.IsMatch(args1Str))
{
dimensionIndex = int.Parse(args1Str);
}
else
{
Debug.LogError("unmatch:" + args1Str);
continue;
}
}
if (argsCount >= 3)
{
numericFormatter = args[2].ToString();
}
if (dimensionIndex == -1) dimensionIndex = 1;
if (numericFormatter == string.Empty)
{
numericFormatter = GetItemNumericFormatter(tooltip, serie, serie.GetSerieData(bIndex));
}
var value = serie.GetData(bIndex, dimensionIndex, dataZoom);
if (isPercent)
{
var percent = value / serie.yTotal * 100;
content = content.Replace(old, ChartCached.FloatToStr(percent, numericFormatter));
}
else
{
content = content.Replace(old, ChartCached.FloatToStr(value, numericFormatter));
}
}
}
content = s_RegexNewLine.Replace(content, PH_NN);
return foundDot;
}
private static char GetSerieIndex(string strType, ref int index)
{
index = 0;
if (strType.Length > 1)
{
if (!int.TryParse(strType.Substring(1), out index))
{
index = -1;
}
}
return strType.ElementAt(0);
}
private static string TrimAndReplaceLine(StringBuilder sb)
{
return s_RegexNewLine.Replace(sb.ToString().Trim(), PH_NN);
}
private static bool IsSelectedSerie(Tooltip tooltip, int serieIndex)
{
if (tooltip.runtimeSerieDataIndex.ContainsKey(serieIndex))