重构代码

This commit is contained in:
monitor1394
2020-05-17 20:36:14 +08:00
parent bc5bd1214e
commit f23347a86e
62 changed files with 1021 additions and 1030 deletions

View File

@@ -0,0 +1,275 @@
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
using System.Text;
using UnityEngine;
namespace XCharts
{
public static class AxisHelper
{
public static float GetTickWidth(Axis axis)
{
return axis.axisTick.width != 0 ? axis.axisTick.width : axis.axisLine.width;
}
/// <summary>
/// 获得分割段数
/// </summary>
/// <param name="dataZoom"></param>
/// <returns></returns>
public static int GetSplitNumber(Axis axis, float coordinateWid, DataZoom dataZoom)
{
if (axis.type == Axis.AxisType.Value)
{
if (axis.interval > 0)
{
if (coordinateWid <= 0) return 0;
int num = Mathf.CeilToInt(axis.runtimeMinMaxRange / axis.interval) + 1;
int maxNum = Mathf.CeilToInt(coordinateWid / 15);
if (num > maxNum)
{
axis.interval *= 2;
num = Mathf.CeilToInt(axis.runtimeMinMaxRange / axis.interval) + 1;
}
return num;
}
else return axis.splitNumber;
}
else if (axis.type == Axis.AxisType.Log)
{
return axis.splitNumber;
}
int dataCount = axis.GetDataList(dataZoom).Count;
if (axis.splitNumber <= 0) return dataCount;
if (dataCount > 2 * axis.splitNumber || dataCount <= 0)
return axis.splitNumber;
else
return dataCount;
}
/// <summary>
/// 获得分割段的宽度
/// </summary>
/// <param name="coordinateWidth"></param>
/// <param name="dataZoom"></param>
/// <returns></returns>
public static float GetSplitWidth(Axis axis, float coordinateWidth, DataZoom dataZoom)
{
int split = GetSplitNumber(axis, coordinateWidth, dataZoom);
int segment = (axis.boundaryGap ? split : split - 1);
segment = segment <= 0 ? 1 : segment;
return coordinateWidth / segment;
}
/// <summary>
/// 获得一个类目数据在坐标系中代表的宽度
/// </summary>
/// <param name="coordinateWidth"></param>
/// <param name="dataZoom"></param>
/// <returns></returns>
public static float GetDataWidth(Axis axis, float coordinateWidth, int dataCount, DataZoom dataZoom)
{
if (dataCount < 1) dataCount = 1;
var categoryCount = axis.GetDataNumber(dataZoom);
int segment = (axis.boundaryGap ? categoryCount : categoryCount - 1);
segment = segment <= 0 ? dataCount : segment;
return coordinateWidth / segment;
}
/// <summary>
/// 获得标签显示的名称
/// </summary>
/// <param name="index"></param>
/// <param name="minValue"></param>
/// <param name="maxValue"></param>
/// <param name="dataZoom"></param>
/// <returns></returns>
internal static string GetLabelName(Axis axis, float coordinateWidth, int index, float minValue, float maxValue,
DataZoom dataZoom, bool forcePercent)
{
int split = GetSplitNumber(axis, coordinateWidth, dataZoom);
if (axis.type == Axis.AxisType.Value)
{
if (minValue == 0 && maxValue == 0) return string.Empty;
float value = 0;
if (forcePercent) maxValue = 100;
if (axis.interval > 0)
{
if (index == split - 1) value = maxValue;
else value = minValue + index * axis.interval;
}
else
{
value = (minValue + (maxValue - minValue) * index / (split - 1));
}
if (axis.inverse)
{
value = -value;
minValue = -minValue;
maxValue = -maxValue;
}
if (forcePercent) return string.Format("{0}%", (int)value);
else return axis.axisLabel.GetFormatterContent(value, minValue, maxValue);
}
else if (axis.type == Axis.AxisType.Log)
{
float value = axis.logBaseE ? Mathf.Exp(axis.runtimeMinLogIndex + index) :
Mathf.Pow(axis.logBase, axis.runtimeMinLogIndex + index);
if (axis.inverse)
{
value = -value;
minValue = -minValue;
maxValue = -maxValue;
}
return axis.axisLabel.GetFormatterContent(value, minValue, maxValue, true);
}
var showData = axis.GetDataList(dataZoom);
int dataCount = showData.Count;
if (dataCount <= 0) return "";
if (index == split - 1 && !axis.boundaryGap)
{
return axis.axisLabel.GetFormatterContent(showData[dataCount - 1]);
}
else
{
float rate = dataCount / split;
if (rate < 1) rate = 1;
int offset = axis.boundaryGap ? (int)(rate / 2) : 0;
int newIndex = (int)(index * rate >= dataCount - 1 ?
dataCount - 1 : offset + index * rate);
return axis.axisLabel.GetFormatterContent(showData[newIndex]);
}
}
/// <summary>
/// 获得分割线条数
/// </summary>
/// <param name="dataZoom"></param>
/// <returns></returns>
internal static int GetScaleNumber(Axis axis, float coordinateWidth, DataZoom dataZoom)
{
if (axis.type == Axis.AxisType.Value || axis.type == Axis.AxisType.Log)
{
int splitNum = GetSplitNumber(axis, coordinateWidth, dataZoom);
return axis.boundaryGap ? splitNum + 1 : splitNum;
}
else
{
var showData = axis.GetDataList(dataZoom);
int dataCount = showData.Count;
if (axis.splitNumber <= 0) return axis.boundaryGap ? dataCount + 1 : dataCount;
if (dataCount > 2 * axis.splitNumber || dataCount <= 0)
return axis.boundaryGap ? axis.splitNumber + 1 : axis.splitNumber;
else
return axis.boundaryGap ? dataCount + 1 : dataCount;
}
}
/// <summary>
/// 获得分割段宽度
/// </summary>
/// <param name="coordinateWidth"></param>
/// <param name="dataZoom"></param>
/// <returns></returns>
internal static float GetScaleWidth(Axis axis, float coordinateWidth, int index, DataZoom dataZoom)
{
int num = GetScaleNumber(axis, coordinateWidth, dataZoom) - 1;
if (num <= 0) num = 1;
if (axis.type == Axis.AxisType.Value && axis.interval > 0)
{
if (index == num - 1) return coordinateWidth - (num - 1) * axis.interval * coordinateWidth / axis.runtimeMinMaxRange;
else return axis.interval * coordinateWidth / axis.runtimeMinMaxRange;
}
else
{
return coordinateWidth / num;
}
}
/// <summary>
/// 调整最大最小值
/// </summary>
/// <param name="minValue"></param>
/// <param name="maxValue"></param>
internal static void AdjustMinMaxValue(Axis axis, ref float minValue, ref float maxValue, bool needFormat)
{
if (axis.type == Axis.AxisType.Log)
{
int minSplit = 0;
int maxSplit = 0;
maxValue = ChartHelper.GetMaxLogValue(maxValue, axis.logBase, axis.logBaseE, out maxSplit);
minValue = ChartHelper.GetMinLogValue(minValue, axis.logBase, axis.logBaseE, out minSplit);
axis.splitNumber = (minSplit > 0 && maxSplit > 0) ? (maxSplit + minSplit - 1) : (maxSplit + minSplit);
return;
}
if (axis.minMaxType == Axis.AxisMinMaxType.Custom)
{
if (axis.min != 0 || axis.max != 0)
{
if (axis.inverse)
{
minValue = -axis.max;
maxValue = -axis.min;
}
else
{
minValue = axis.min;
maxValue = axis.max;
}
}
}
else
{
switch (axis.minMaxType)
{
case Axis.AxisMinMaxType.Default:
if (minValue == 0 && maxValue == 0)
{
}
else if (minValue > 0 && maxValue > 0)
{
minValue = 0;
maxValue = needFormat ? ChartHelper.GetMaxDivisibleValue(maxValue, axis.ceilRate) : maxValue;
}
else if (minValue < 0 && maxValue < 0)
{
minValue = needFormat ? ChartHelper.GetMinDivisibleValue(minValue, axis.ceilRate) : minValue;
maxValue = 0;
}
else
{
minValue = needFormat ? ChartHelper.GetMinDivisibleValue(minValue, axis.ceilRate) : minValue;
maxValue = needFormat ? ChartHelper.GetMaxDivisibleValue(maxValue, axis.ceilRate) : maxValue;
}
break;
case Axis.AxisMinMaxType.MinMax:
minValue = needFormat ? ChartHelper.GetMinDivisibleValue(minValue, axis.ceilRate) : minValue;
maxValue = needFormat ? ChartHelper.GetMaxDivisibleValue(maxValue, axis.ceilRate) : maxValue;
break;
}
}
var tempRange = maxValue - minValue;
if (axis.runtimeMinMaxRange != tempRange)
{
axis.runtimeMinMaxRange = tempRange;
if (axis.type == Axis.AxisType.Value && axis.interval > 0)
{
axis.SetComponentDirty();
}
}
}
internal static bool NeedShowSplit(Axis axis)
{
if (!axis.show) return false;
if (axis.IsCategory() && axis.data.Count <= 0) return false;
else if (axis.IsValue() && axis.runtimeMinValue == 0 && axis.runtimeMaxValue == 0) return false;
else return true;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 502c2be6d197b40f59ae65d9c659700f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,24 @@
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
using UnityEngine;
using UnityEngine.UI;
namespace XCharts
{
internal static class ItemStyleHelper
{
public static bool IsNeedCorner(ItemStyle itemStyle)
{
if (itemStyle.cornerRadius == null) return false;
foreach (var value in itemStyle.cornerRadius)
{
if (value != 0) return true;
}
return false;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 14c20b85c2c6d44b08f57c928307080d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,268 @@
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
using UnityEngine;
using UnityEngine.UI;
namespace XCharts
{
internal static class LegendHelper
{
public static Color GetContentColor(Legend legend, ThemeInfo themeInfo, bool active)
{
var textStyle = legend.textStyle;
if (active) return !ChartHelper.IsClearColor(textStyle.color) ? textStyle.color : (Color)themeInfo.legendTextColor;
else return (Color)themeInfo.legendUnableColor;
}
public static Color GetIconColor(Legend legend, int readIndex, ThemeInfo themeInfo, bool active)
{
if (active)
{
if (legend.itemAutoColor || legend.GetIcon(readIndex) == null)
return (Color)themeInfo.GetColor(readIndex);
else
return Color.white;
}
else return (Color)themeInfo.legendUnableColor;
}
public static LegendItem AddLegendItem(Legend legend, int i, string legendName, Transform parent, ThemeInfo themeInfo,
string content, Color itemColor, bool active)
{
var objName = i + "_" + legendName;
var anchorMin = new Vector2(0, 0.5f);
var anchorMax = new Vector2(0, 0.5f);
var pivot = new Vector2(0, 0.5f);
var sizeDelta = new Vector2(100, 30);
var iconSizeDelta = new Vector2(legend.itemWidth, legend.itemHeight);
var textStyle = legend.textStyle;
var font = textStyle.font ? textStyle.font : themeInfo.font;
var contentColor = GetContentColor(legend, themeInfo, active);
var objAnchorMin = legend.location.runtimeAnchorMin;
var objAnchorMax = legend.location.runtimeAnchorMax;
var objPivot = legend.location.runtimePivot;
var btnObj = ChartHelper.AddObject(objName, parent, objAnchorMin, objAnchorMax, objPivot, sizeDelta, i);
var iconObj = ChartHelper.AddObject("icon", btnObj.transform, anchorMin, anchorMax, pivot, iconSizeDelta);
var contentObj = ChartHelper.AddObject("content", btnObj.transform, anchorMin, anchorMax, pivot, sizeDelta);
var img = ChartHelper.GetOrAddComponent<Image>(btnObj);
img.color = Color.clear;
ChartHelper.GetOrAddComponent<Button>(btnObj);
ChartHelper.GetOrAddComponent<Image>(iconObj);
ChartHelper.GetOrAddComponent<Image>(contentObj);
ChartHelper.AddTextObject("Text", contentObj.transform, font, contentColor,
TextAnchor.MiddleLeft, anchorMin, anchorMax, pivot, sizeDelta, textStyle.fontSize,
textStyle.rotate, textStyle.fontStyle, textStyle.lineSpacing);
var item = new LegendItem();
item.index = i;
item.name = objName;
item.legendName = legendName;
item.SetObject(btnObj);
item.SetIconSize(legend.itemWidth, legend.itemHeight);
item.SetIconColor(itemColor);
item.SetIconImage(legend.GetIcon(i));
item.SetContentPosition(textStyle.offsetv3);
item.SetContent(content);
item.SetContentBackgroundColor(textStyle.backgroundColor);
return item;
}
public static void ResetItemPosition(Legend legend)
{
var startX = 0f;
var currWidth = 0f;
var currHeight = 0f;
switch (legend.orient)
{
case Orient.Vertical:
switch (legend.location.align)
{
case Location.Align.TopCenter:
startX = legend.runtimeWidth / 2;
currHeight = 0f;
foreach (var kv in legend.buttonList)
{
var item = kv.Value;
item.SetPosition(new Vector3(-startX + item.width / 2, -currHeight));
currHeight += item.height + legend.itemGap;
}
break;
case Location.Align.TopLeft:
currHeight = 0f;
foreach (var kv in legend.buttonList)
{
var item = kv.Value;
item.SetPosition(new Vector3(0, -currHeight));
currHeight += item.height + legend.itemGap;
}
break;
case Location.Align.TopRight:
currHeight = 0f;
foreach (var kv in legend.buttonList)
{
var item = kv.Value;
item.SetPosition(new Vector3(item.width - legend.runtimeWidth, -currHeight));
currHeight += item.height + legend.itemGap;
}
break;
case Location.Align.Center:
startX = legend.runtimeWidth / 2;
currHeight = legend.runtimeHeight;
foreach (var kv in legend.buttonList)
{
var item = kv.Value;
item.SetPosition(new Vector3(-startX + item.width / 2, currHeight - item.height));
currHeight -= item.height + legend.itemGap;
}
break;
case Location.Align.CenterLeft:
currHeight = legend.runtimeHeight;
foreach (var kv in legend.buttonList)
{
var item = kv.Value;
item.SetPosition(new Vector3(0, currHeight - item.height));
currHeight -= item.height + legend.itemGap;
}
break;
case Location.Align.CenterRight:
currHeight = legend.runtimeHeight;
foreach (var kv in legend.buttonList)
{
var item = kv.Value;
item.SetPosition(new Vector3(item.width - legend.runtimeWidth, currHeight - item.height));
currHeight -= item.height + legend.itemGap;
}
break;
case Location.Align.BottomCenter:
startX = legend.runtimeWidth / 2;
currHeight = legend.runtimeHeight;
foreach (var kv in legend.buttonList)
{
var item = kv.Value;
item.SetPosition(new Vector3(-startX + item.width / 2, currHeight - item.height));
currHeight -= item.height + legend.itemGap;
}
break;
case Location.Align.BottomLeft:
currHeight = legend.runtimeHeight;
foreach (var kv in legend.buttonList)
{
var item = kv.Value;
item.SetPosition(new Vector3(0, currHeight - item.height));
currHeight -= item.height + legend.itemGap;
}
break;
case Location.Align.BottomRight:
currHeight = legend.runtimeHeight;
foreach (var kv in legend.buttonList)
{
var item = kv.Value;
item.SetPosition(new Vector3(item.width - legend.runtimeWidth, currHeight - item.height));
currHeight -= item.height + legend.itemGap;
}
break;
}
break;
case Orient.Horizonal:
switch (legend.location.align)
{
case Location.Align.TopLeft:
case Location.Align.CenterLeft:
case Location.Align.BottomLeft:
currWidth = 0f;
foreach (var kv in legend.buttonList)
{
var item = kv.Value;
item.SetPosition(new Vector3(currWidth, 0));
currWidth += item.width + legend.itemGap;
}
break;
case Location.Align.TopCenter:
case Location.Align.Center:
case Location.Align.BottomCenter:
startX = legend.runtimeWidth / 2;
currWidth = 0f;
foreach (var kv in legend.buttonList)
{
var item = kv.Value;
item.SetPosition(new Vector3(-startX + item.width / 2 + currWidth, 0));
currWidth += item.width + legend.itemGap;
}
break;
case Location.Align.TopRight:
case Location.Align.CenterRight:
case Location.Align.BottomRight:
startX = -legend.runtimeWidth;
currWidth = 0f;
foreach (var kv in legend.buttonList)
{
var item = kv.Value;
item.SetPosition(new Vector3(startX + currWidth + item.width, 0));
currWidth += item.width + legend.itemGap;
}
break;
}
break;
}
}
public static bool CheckDataShow(Series series, string legendName, bool show)
{
bool needShow = false;
foreach (var serie in series.list)
{
if (legendName.Equals(serie.name))
{
serie.show = show;
serie.highlighted = false;
if (serie.show) needShow = true;
}
else
{
foreach (var data in serie.data)
{
if (legendName.Equals(data.name))
{
data.show = show;
data.highlighted = false;
if (data.show) needShow = true;
}
}
}
}
return needShow;
}
public static bool CheckDataHighlighted(Series series, string legendName, bool heighlight)
{
bool show = false;
foreach (var serie in series.list)
{
if (legendName.Equals(serie.name))
{
serie.highlighted = heighlight;
}
else
{
foreach (var data in serie.data)
{
if (legendName.Equals(data.name))
{
data.highlighted = heighlight;
if (data.highlighted) show = true;
}
}
}
}
return show;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d03ce7a11ecde41b6930883612bf8f05
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,15 @@
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
using UnityEngine;
using UnityEngine.UI;
namespace XCharts
{
internal static class SerieDataHelper
{
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e78d1b28fc61d4b1b995a1eca16cf3b6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,262 @@
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
using UnityEngine;
using UnityEngine.UI;
namespace XCharts
{
internal static class SerieHelper
{
internal static Color GetItemBackgroundColor(Serie serie, SerieData serieData, ThemeInfo theme, int index, bool highlight, bool useDefault = true)
{
var color = Color.clear;
if (highlight)
{
var itemStyleEmphasis = GetItemStyleEmphasis(serie, serieData);
if (itemStyleEmphasis != null && !ChartHelper.IsClearColor(itemStyleEmphasis.backgroundColor))
{
color = itemStyleEmphasis.backgroundColor;
color.a *= itemStyleEmphasis.opacity;
return color;
}
}
var itemStyle = GetItemStyle(serie, serieData);
if (!ChartHelper.IsClearColor(itemStyle.backgroundColor))
{
color = itemStyle.backgroundColor;
if (highlight) color *= color;
color.a *= itemStyle.opacity;
return color;
}
else if (useDefault)
{
color = (Color)theme.GetColor(index);
if (highlight) color *= color;
color.a = 0.2f;
return color;
}
return color;
}
internal static Color GetItemColor(Serie serie, SerieData serieData, ThemeInfo theme, int index, bool highlight)
{
if (highlight)
{
var itemStyleEmphasis = GetItemStyleEmphasis(serie, serieData);
if (itemStyleEmphasis != null && !ChartHelper.IsClearColor(itemStyleEmphasis.color))
{
var color = itemStyleEmphasis.color;
color.a *= itemStyleEmphasis.opacity;
return color;
}
}
var itemStyle = GetItemStyle(serie, serieData);
if (!ChartHelper.IsClearColor(itemStyle.color))
{
var color = itemStyle.color;
if (highlight) color *= color;
color.a *= itemStyle.opacity;
return color;
}
else
{
var color = (Color)theme.GetColor(index);
if (highlight) color *= color;
color.a *= itemStyle.opacity;
return color;
}
}
internal static Color GetItemToColor(Serie serie, SerieData serieData, ThemeInfo theme, int index, bool highlight)
{
if (highlight)
{
var itemStyleEmphasis = GetItemStyleEmphasis(serie, serieData);
if (itemStyleEmphasis != null && !ChartHelper.IsClearColor(itemStyleEmphasis.toColor))
{
var color = itemStyleEmphasis.toColor;
color.a *= itemStyleEmphasis.opacity;
return color;
}
}
var itemStyle = GetItemStyle(serie, serieData, highlight);
if (itemStyle == null) itemStyle = serieData.itemStyle;
if (!ChartHelper.IsClearColor(itemStyle.toColor))
{
var color = itemStyle.toColor;
if (highlight) color *= color;
color.a *= itemStyle.opacity;
return color;
}
if (!ChartHelper.IsClearColor(itemStyle.color))
{
var color = itemStyle.color;
if (highlight) color *= color;
color.a *= itemStyle.opacity;
return color;
}
else
{
var color = (Color)theme.GetColor(index);
if (highlight) color *= color;
color.a *= itemStyle.opacity;
return color;
}
}
public static bool IsDownPoint(Serie serie, int index)
{
var dataPoints = serie.dataPoints;
if (dataPoints.Count < 2) return false;
else if (index > 0 && index < dataPoints.Count - 1)
{
var lp = dataPoints[index - 1];
var np = dataPoints[index + 1];
var cp = dataPoints[index];
var dot = Vector3.Cross(np - lp, cp - np);
return dot.z < 0;
}
else if (index == 0)
{
return dataPoints[0].y < dataPoints[1].y;
}
else if (index == dataPoints.Count - 1)
{
return dataPoints[index].y < dataPoints[index - 1].y;
}
else
{
return false;
}
}
public static ItemStyle GetItemStyle(Serie serie, SerieData serieData, bool highlight = false)
{
if (highlight)
{
var style = GetItemStyleEmphasis(serie, serieData);
if (style == null) return GetItemStyle(serie, serieData, false);
else return style;
}
else if (serie.IsPerformanceMode()) return serie.itemStyle;
else if (serieData != null && serieData.enableItemStyle) return serieData.itemStyle;
else return serie.itemStyle;
}
public static ItemStyle GetItemStyleEmphasis(Serie serie, SerieData serieData)
{
if (!serie.IsPerformanceMode() && serieData != null && serieData.enableEmphasis && serieData.emphasis.show)
return serieData.emphasis.itemStyle;
else if (serie.emphasis.show) return serie.emphasis.itemStyle;
else return null;
}
public static SerieLabel GetSerieLabel(Serie serie, SerieData serieData, bool highlight = false)
{
if (highlight)
{
if (!serie.IsPerformanceMode() && serieData.enableEmphasis && serieData.emphasis.show)
return serieData.emphasis.label;
else if (serie.emphasis.show) return serie.emphasis.label;
else return serie.label;
}
else
{
if (!serie.IsPerformanceMode() && serieData.enableLabel) return serieData.label;
else return serie.label;
}
}
public static Color GetAreaColor(Serie serie, ThemeInfo theme, int index, bool highlight)
{
var areaStyle = serie.areaStyle;
var color = !ChartHelper.IsClearColor(areaStyle.color) ? areaStyle.color : (Color)theme.GetColor(index);
if (highlight)
{
if (!ChartHelper.IsClearColor(areaStyle.highlightColor)) color = areaStyle.highlightColor;
else color *= color;
}
color.a *= areaStyle.opacity;
return color;
}
public static Color GetAreaToColor(Serie serie, ThemeInfo theme, int index, bool highlight)
{
var areaStyle = serie.areaStyle;
if (!ChartHelper.IsClearColor(areaStyle.toColor))
{
var color = areaStyle.toColor;
if (highlight)
{
if (!ChartHelper.IsClearColor(areaStyle.highlightToColor)) color = areaStyle.highlightToColor;
else color *= color;
}
color.a *= areaStyle.opacity;
return color;
}
else
{
return GetAreaColor(serie, theme, index, highlight);
}
}
public static Color GetLineColor(Serie serie, ThemeInfo theme, int index, bool highlight)
{
var color = Color.clear;
if (highlight)
{
var itemStyleEmphasis = GetItemStyleEmphasis(serie, null);
if (itemStyleEmphasis != null && !ChartHelper.IsClearColor(itemStyleEmphasis.color))
{
color = itemStyleEmphasis.color;
color.a *= itemStyleEmphasis.opacity;
return color;
}
}
if (!ChartHelper.IsClearColor(serie.lineStyle.color)) color = serie.lineStyle.GetColor();
else if (!ChartHelper.IsClearColor(serie.itemStyle.color)) color = serie.itemStyle.GetColor();
if (ChartHelper.IsClearColor(color))
{
color = (Color)theme.GetColor(index);
color.a = serie.lineStyle.opacity;
}
if (highlight) color *= color;
return color;
}
public static float GetSymbolBorder(Serie serie, SerieData serieData, bool highlight, bool useLineWidth = true)
{
var itemStyle = GetItemStyle(serie, serieData, highlight);
if (itemStyle != null && itemStyle.borderWidth != 0) return itemStyle.borderWidth;
else if (serie.lineStyle.width != 0 && useLineWidth) return serie.lineStyle.width;
else return 0;
}
public static float[] GetSymbolCornerRadius(Serie serie, SerieData serieData, bool highlight)
{
var itemStyle = GetItemStyle(serie, serieData, highlight);
if (itemStyle != null) return itemStyle.cornerRadius;
else return null;
}
/// <summary>
/// 更新运行时中心点和半径
/// </summary>
/// <param name="chartWidth"></param>
/// <param name="chartHeight"></param>
internal static void UpdateCenter(Serie serie, Vector3 chartPosition, float chartWidth, float chartHeight)
{
if (serie.center.Length < 2) return;
var centerX = serie.center[0] <= 1 ? chartWidth * serie.center[0] : serie.center[0];
var centerY = serie.center[1] <= 1 ? chartHeight * serie.center[1] : serie.center[1];
serie.runtimeCenterPos = chartPosition + new Vector3(centerX, centerY);
var minWidth = Mathf.Min(chartWidth, chartHeight);
serie.runtimeInsideRadius = serie.radius[0] <= 1 ? minWidth * serie.radius[0] : serie.radius[0];
serie.runtimeOutsideRadius = serie.radius[1] <= 1 ? minWidth * serie.radius[1] : serie.radius[1];
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e5c421b17fb6f45a283f4f57efce686f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,177 @@
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
using UnityEngine;
using UnityEngine.UI;
namespace XCharts
{
internal static class SerieLabelHelper
{
public static void CheckLabel(Serie serie, ref bool m_ReinitLabel, ref bool m_UpdateLabelText)
{
switch (serie.type)
{
case SerieType.Gauge:
case SerieType.Ring:
var serieData = serie.GetSerieData(0);
if (serieData != null)
{
if (serie.label.show && serie.show)
{
if (serieData.labelObject != null)
{
serieData.SetLabelActive(true);
m_UpdateLabelText = true;
}
else
{
m_ReinitLabel = true;
}
}
else if (serieData.labelObject != null)
{
serieData.SetLabelActive(false);
}
}
break;
}
}
public static void UpdateLabelText(Series series, ThemeInfo themeInfo)
{
foreach (var serie in series.list)
{
if (!serie.label.show) continue;
switch (serie.type)
{
case SerieType.Gauge:
SetGaugeLabelText(serie);
break;
case SerieType.Ring:
SetRingLabelText(serie, themeInfo);
break;
}
}
}
public static Color GetLabelColor(Serie serie, ThemeInfo themeInfo, int index)
{
if (!ChartHelper.IsClearColor(serie.label.color))
{
return serie.label.color;
}
else
{
return themeInfo.GetColor(index);
}
}
public static void ResetLabel(SerieData serieData, SerieLabel label, ThemeInfo themeInfo, int colorIndex)
{
if (serieData.labelObject == null) return;
if (serieData.labelObject.label == null) return;
serieData.labelObject.label.color = !ChartHelper.IsClearColor(label.color) ? label.color :
(Color)themeInfo.GetColor(colorIndex);
serieData.labelObject.label.fontSize = label.fontSize;
serieData.labelObject.label.fontStyle = label.fontStyle;
}
public static string GetFormatterContent(Serie serie, SerieData serieData,
float dataValue, float dataTotal, SerieLabel serieLabel = null)
{
if (serieLabel == null)
{
serieLabel = SerieHelper.GetSerieLabel(serie, serieData);
}
var numericFormatter = GetLabelNumericFormatter(serie, serieData);
var serieName = serie.name;
var dataName = serieData != null ? serieData.name : null;
if (string.IsNullOrEmpty(serieLabel.formatter))
return ChartCached.NumberToStr(dataValue, numericFormatter);
else
{
var content = serieLabel.formatter.Replace("{a}", serieName);
content = content.Replace("{b}", dataName);
content = content.Replace("{c}", ChartCached.NumberToStr(dataValue, numericFormatter));
content = content.Replace("{c:f0}", ChartCached.IntToStr((int)Mathf.Round(dataValue)));
content = content.Replace("{c:f1}", ChartCached.FloatToStr(dataValue, string.Empty, 1));
content = content.Replace("{c:f2}", ChartCached.FloatToStr(dataValue, string.Empty, 2));
if (dataTotal > 0)
{
var percent = dataValue / dataTotal * 100;
content = content.Replace("{d}", ChartCached.NumberToStr(percent, numericFormatter));
content = content.Replace("{d:f0}", ChartCached.IntToStr((int)Mathf.Round(percent)));
content = content.Replace("{d:f1}", ChartCached.FloatToStr(percent, string.Empty, 1));
content = content.Replace("{d:f2}", ChartCached.FloatToStr(percent, string.Empty, 2));
}
content = content.Replace("\\n", "\n");
content = content.Replace("<br/>", "\n");
return content;
}
}
private static string GetLabelNumericFormatter(Serie serie, SerieData serieData)
{
var itemStyle = SerieHelper.GetItemStyle(serie, serieData);
if (!string.IsNullOrEmpty(itemStyle.numericFormatter)) return itemStyle.numericFormatter;
else return serie.label.numericFormatter;
}
private static void SetGaugeLabelText(Serie serie)
{
var serieData = serie.GetSerieData(0);
if (serieData == null) return;
if (serieData.labelObject == null) return;
var value = serieData.GetData(1);
var total = serie.max;
var content = SerieLabelHelper.GetFormatterContent(serie, serieData, value, total);
serieData.labelObject.SetText(content);
serieData.labelObject.SetLabelPosition(serie.runtimeCenterPos + serie.label.offset);
if (!ChartHelper.IsClearColor(serie.label.color))
{
serieData.labelObject.label.color = serie.label.color;
}
}
private static void SetRingLabelText(Serie serie, ThemeInfo themeInfo)
{
for (int i = 0; i < serie.dataCount; i++)
{
var serieData = serie.data[i];
var serieLabel = SerieHelper.GetSerieLabel(serie, serieData, serieData.highlighted);
if (serieLabel.show && serieData.labelObject != null)
{
if (!serie.show || !serieData.show)
{
serieData.SetLabelActive(false);
continue;
}
var value = serieData.GetData(0);
var total = serieData.GetData(1);
var content = SerieLabelHelper.GetFormatterContent(serie, serieData, value, total);
serieData.SetLabelActive(true);
serieData.labelObject.SetText(content);
serieData.labelObject.SetLabelColor(GetLabelColor(serie, themeInfo, i));
if (serie.label.position == SerieLabel.Position.Bottom)
{
var labelWidth = serieData.GetLabelWidth();
if (serie.clockwise)
serieData.labelObject.SetLabelPosition(serieData.labelPosition - new Vector3(labelWidth / 2, 0));
else
serieData.labelObject.SetLabelPosition(serieData.labelPosition + new Vector3(labelWidth / 2, 0));
}
else
{
serieData.labelObject.SetLabelPosition(serieData.labelPosition);
}
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bbd57df98f6f145f299cf942b04229a4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,447 @@
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
using System.Collections.Generic;
using UnityEngine;
namespace XCharts
{
internal static class SeriesHelper
{
public static bool IsNeedLabelUpdate(Series series)
{
foreach (var serie in series.list)
{
if (serie.label.vertsDirty) return true;
}
return false;
}
public static bool IsLabelDirty(Series series)
{
if (series.labelDirty) return true;
foreach (var serie in series.list)
{
if (serie.label.componentDirty) return true;
}
return false;
}
public static bool IsLegalLegendName(string name)
{
int numName = -1;
if (int.TryParse(name, out numName))
{
if (numName >= 0 && numName < 100) return false;
}
return true;
}
public static List<string> GetLegalSerieNameList(Series series)
{
var list = new List<string>();
for (int n = 0; n < series.list.Count; n++)
{
var serie = series.GetSerie(n);
switch (serie.type)
{
case SerieType.Pie:
case SerieType.Radar:
case SerieType.Ring:
for (int i = 0; i < serie.data.Count; i++)
{
var dataName = serie.data[i].name;
if (!string.IsNullOrEmpty(dataName) && IsLegalLegendName(dataName) && !list.Contains(dataName))
list.Add(dataName);
}
break;
default:
if (!string.IsNullOrEmpty(serie.name) && !list.Contains(serie.name) && IsLegalLegendName(serie.name))
list.Add(serie.name);
break;
}
}
return list;
}
/// <summary>
/// 获得所有系列名,不包含空名字。
/// </summary>
/// <returns></returns>
internal static void UpdateSerieNameList(Series series, ref List<string> serieNameList)
{
serieNameList.Clear();
for (int n = 0; n < series.list.Count; n++)
{
var serie = series.GetSerie(n);
switch (serie.type)
{
case SerieType.Pie:
case SerieType.Radar:
case SerieType.Ring:
for (int i = 0; i < serie.data.Count; i++)
{
if (string.IsNullOrEmpty(serie.data[i].name))
serieNameList.Add(ChartCached.IntToStr(i));
else if (!serieNameList.Contains(serie.data[i].name))
serieNameList.Add(serie.data[i].name);
}
break;
default:
if (string.IsNullOrEmpty(serie.name))
serieNameList.Add(ChartCached.IntToStr(n));
else if (!serieNameList.Contains(serie.name))
serieNameList.Add(serie.name);
break;
}
}
}
/// <summary>
/// 同堆叠的serie是否有渐变色的。
/// </summary>
/// <param name="stack"></param>
/// <returns></returns>
internal static bool IsAnyGradientSerie(Series series, string stack)
{
if (string.IsNullOrEmpty(stack)) return false;
foreach (var serie in series.list)
{
if (serie.show && serie.areaStyle.show && stack.Equals(serie.stack))
{
if (serie.areaStyle.color != serie.areaStyle.toColor
&& !ChartHelper.IsClearColor(serie.areaStyle.toColor))
return true;
}
}
return false;
}
/// <summary>
/// 是否有需裁剪的serie。
/// </summary>
/// <returns></returns>
internal static bool IsAnyClipSerie(Series series)
{
foreach (var serie in series.list)
{
if (serie.clip) return true;
}
return false;
}
internal static bool IsAnyUpdateAnimationSerie(Series series)
{
foreach (var serie in series.list)
{
if (serie.animation.enable && serie.animation.dataChangeEnable)
{
return true;
}
}
return false;
}
/// <summary>
/// 获得上一个同堆叠且显示的serie。
/// </summary>
/// <param name="serie"></param>
/// <returns></returns>
internal static Serie GetLastStackSerie(Series series, Serie serie)
{
if (serie == null || string.IsNullOrEmpty(serie.stack)) return null;
for (int i = serie.index - 1; i >= 0; i--)
{
var temp = series.list[i];
if (temp.show && serie.stack.Equals(temp.stack)) return temp;
}
return null;
}
/// <summary>
/// 获得上一个同堆叠且显示的serie。
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
internal static Serie GetLastStackSerie(Series series, int index)
{
var serie = series.GetSerie(index);
return GetLastStackSerie(series, serie);
}
/// <summary>
/// 是否由系列在用指定索引的axis
/// </summary>
/// <param name="axisIndex"></param>
/// <returns></returns>
internal static bool IsUsedAxisIndex(Series series, int axisIndex)
{
foreach (var serie in series.list)
{
if (serie.axisIndex == axisIndex) return true;
}
return false;
}
private static HashSet<string> _setForStack = new HashSet<string>();
/// <summary>
/// 是否由数据堆叠
/// </summary>
/// <returns></returns>
internal static bool IsStack(Series series)
{
_setForStack.Clear();
foreach (var serie in series.list)
{
if (string.IsNullOrEmpty(serie.stack)) continue;
if (_setForStack.Contains(serie.stack)) return true;
else
{
_setForStack.Add(serie.stack);
}
}
return false;
}
/// <summary>
/// 是否堆叠
/// </summary>
/// <param name="stackName"></param>
/// <param name="type"></param>
/// <returns></returns>
internal static bool IsStack(Series series, string stackName, SerieType type)
{
if (string.IsNullOrEmpty(stackName)) return false;
int count = 0;
foreach (var serie in series.list)
{
if (serie.show && serie.type == type)
{
if (stackName.Equals(serie.stack)) count++;
if (count >= 2) return true;
}
}
return false;
}
/// <summary>
/// 是否时百分比堆叠
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
internal static bool IsPercentStack(Series series, SerieType type)
{
int count = 0;
bool isPercentStack = false;
foreach (var serie in series.list)
{
if (serie.show && serie.type == type)
{
if (!string.IsNullOrEmpty(serie.stack))
{
count++;
if (serie.barPercentStack) isPercentStack = true;
}
if (count >= 2 && isPercentStack) return true;
}
}
return false;
}
/// <summary>
/// 是否时百分比堆叠
/// </summary>
/// <param name="stackName"></param>
/// <param name="type"></param>
/// <returns></returns>
internal static bool IsPercentStack(Series series, string stackName, SerieType type)
{
if (string.IsNullOrEmpty(stackName)) return false;
int count = 0;
bool isPercentStack = false;
foreach (var serie in series.list)
{
if (serie.show && serie.type == type)
{
if (stackName.Equals(serie.stack))
{
count++;
if (serie.barPercentStack) isPercentStack = true;
}
if (count >= 2 && isPercentStack) return true;
}
}
return false;
}
private static Dictionary<string, int> sets = new Dictionary<string, int>();
/// <summary>
/// 获得堆叠系列列表
/// </summary>
/// <param name="Dictionary<int"></param>
/// <param name="stackSeries"></param>
internal static void GetStackSeries(Series series, ref Dictionary<int, List<Serie>> stackSeries)
{
int count = 0;
var serieCount = series.list.Count;
sets.Clear();
if (stackSeries == null)
{
stackSeries = new Dictionary<int, List<Serie>>(serieCount);
}
else
{
foreach (var kv in stackSeries)
{
kv.Value.Clear();
}
}
for (int i = 0; i < serieCount; i++)
{
var serie = series.GetSerie(i);
serie.index = i;
if (string.IsNullOrEmpty(serie.stack))
{
if (!stackSeries.ContainsKey(count))
stackSeries[count] = new List<Serie>(serieCount);
stackSeries[count].Add(serie);
count++;
}
else
{
if (!sets.ContainsKey(serie.stack))
{
sets.Add(serie.stack, count);
if (!stackSeries.ContainsKey(count))
stackSeries[count] = new List<Serie>(serieCount);
stackSeries[count].Add(serie);
count++;
}
else
{
int stackIndex = sets[serie.stack];
stackSeries[stackIndex].Add(serie);
}
}
}
}
/// <summary>
/// 获得维度X的最大最小值
/// </summary>
/// <param name="dataZoom"></param>
/// <param name="axisIndex"></param>
/// <param name="minVaule"></param>
/// <param name="maxValue"></param>
internal static void GetXMinMaxValue(Series series, DataZoom dataZoom, int axisIndex, bool isValueAxis,
bool inverse, out float minVaule, out float maxValue)
{
GetMinMaxValue(series, dataZoom, axisIndex, isValueAxis, inverse, false, out minVaule, out maxValue);
}
/// <summary>
/// 获得维度Y的最大最小值
/// </summary>
/// <param name="dataZoom"></param>
/// <param name="axisIndex"></param>
/// <param name="minVaule"></param>
/// <param name="maxValue"></param>
internal static void GetYMinMaxValue(Series series, DataZoom dataZoom, int axisIndex, bool isValueAxis,
bool inverse, out float minVaule, out float maxValue)
{
GetMinMaxValue(series, dataZoom, axisIndex, isValueAxis, inverse, true, out minVaule, out maxValue);
}
private static Dictionary<int, List<Serie>> _stackSeriesForMinMax = new Dictionary<int, List<Serie>>();
private static Dictionary<int, float> _serieTotalValueForMinMax = new Dictionary<int, float>();
internal static void GetMinMaxValue(Series series, DataZoom dataZoom, int axisIndex, bool isValueAxis,
bool inverse, bool yValue, out float minVaule, out float maxValue)
{
float min = int.MaxValue;
float max = int.MinValue;
var isPercentStack = SeriesHelper.IsPercentStack(series, SerieType.Bar);
if (!SeriesHelper.IsStack(series) || (isValueAxis && !yValue))
{
for (int i = 0; i < series.list.Count; i++)
{
var serie = series.GetSerie(i);
if (serie.axisIndex != axisIndex) continue;
if (series.IsActive(i))
{
if (isPercentStack && SeriesHelper.IsPercentStack(series, serie.name, SerieType.Bar))
{
if (100 > max) max = 100;
if (0 < min) min = 0;
}
else
{
var showData = serie.GetDataList(dataZoom);
foreach (var data in showData)
{
var currData = data.GetData(yValue ? 1 : 0, inverse);
if (currData > max) max = currData;
if (currData < min) min = currData;
}
}
}
}
}
else
{
SeriesHelper.GetStackSeries(series, ref _stackSeriesForMinMax);
foreach (var ss in _stackSeriesForMinMax)
{
_serieTotalValueForMinMax.Clear();
for (int i = 0; i < ss.Value.Count; i++)
{
var serie = ss.Value[i];
if (serie.axisIndex != axisIndex || !series.IsActive(i)) continue;
var showData = serie.GetDataList(dataZoom);
if (SeriesHelper.IsPercentStack(series, serie.stack, SerieType.Bar))
{
for (int j = 0; j < showData.Count; j++)
{
_serieTotalValueForMinMax[j] = 100;
}
}
else
{
for (int j = 0; j < showData.Count; j++)
{
if (!_serieTotalValueForMinMax.ContainsKey(j))
_serieTotalValueForMinMax[j] = 0;
var currData = (yValue ? showData[j].GetData(1) : showData[j].GetData(0));
if (inverse) currData = -currData;
_serieTotalValueForMinMax[j] = _serieTotalValueForMinMax[j] + currData;
}
}
}
float tmax = int.MinValue;
float tmin = int.MaxValue;
foreach (var tt in _serieTotalValueForMinMax)
{
if (tt.Value > tmax) tmax = tt.Value;
if (tt.Value < tmin) tmin = tt.Value;
}
if (tmax > max) max = tmax;
if (tmin < min) min = tmin;
}
}
if (max == int.MinValue && min == int.MaxValue)
{
minVaule = 0;
maxValue = 0;
}
else
{
minVaule = min > 1 ? Mathf.FloorToInt(min) : min;
maxValue = max > 1 ? Mathf.CeilToInt(max) : max;
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e7411a13172764cf89b10e643089c832
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,34 @@
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
using UnityEngine;
using UnityEngine.UI;
namespace XCharts
{
internal static class TitleHelper
{
public static Font GetTextFont(Title title, ThemeInfo themeInfo)
{
return (title.textStyle.font != null) ? title.textStyle.font : themeInfo.font;
}
public static Color GetTextColor(Title title, ThemeInfo themeInfo)
{
return !ChartHelper.IsClearColor(title.textStyle.color) ? title.textStyle.color : (Color)themeInfo.titleTextColor;
}
public static Font GetSubTextFont(Title title, ThemeInfo themeInfo)
{
return (title.subTextStyle.font != null) ? title.subTextStyle.font : themeInfo.font;
}
public static Color GetSubTextColor(Title title, ThemeInfo themeInfo)
{
return !ChartHelper.IsClearColor(title.subTextStyle.color) ? title.subTextStyle.color : (Color)themeInfo.titleSubTextColor;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e22859d8021f6491f8ee08339b71e577
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,43 @@
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
using UnityEngine;
using UnityEngine.UI;
namespace XCharts
{
internal static class TitleStyleHelper
{
public static void CheckTitle(Serie serie, ref bool m_ReinitTitle, ref bool m_UpdateTitleText)
{
if (serie.titleStyle.show)
{
if (serie.titleStyle.IsInited())
{
serie.titleStyle.UpdatePosition(serie.runtimeCenterPos);
m_UpdateTitleText = true;
}
else
{
m_ReinitTitle = true;
}
}
}
public static void UpdateTitleText(Series series)
{
foreach (var serie in series.list) UpdateTitleText(serie);
}
public static void UpdateTitleText(Serie serie)
{
if (serie.titleStyle.show)
{
serie.titleStyle.SetText(serie.name);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 35fa58aecc0f4414e8a2f03195b66175
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,438 @@
using System;
using System.Collections.Generic;
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
using System.Text;
using UnityEngine;
using UnityEngine.UI;
namespace XCharts
{
internal static class TooltipHelper
{
private const string PH_A = "{a}";
private const string PH_B = "{b}";
private const string PH_C = "{c}";
private const string PH_D = "{d}";
private const string PH_I = "{.}";
private const string PH_Y = "{j}";
private const string PH_ON = "\\n";
private const string PH_NN = "\n";
private const string PH_NN_BBB = "\n";
private const string PH_BR = "<br/>";
private static Dictionary<string, Dictionary<int, string>> s_PHDic = new Dictionary<string, Dictionary<int, string>>();
private static Dictionary<int, string> s_PHCCDic = new Dictionary<int, string>();
private static Dictionary<int, Dictionary<int, string>> s_PHSerieCCDic = new Dictionary<int, Dictionary<int, string>>();
private static void InitScatterTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
ThemeInfo themeInfo)
{
if (!tooltip.runtimeSerieDataIndex.ContainsKey(serie.index)) return;
var dataIndexList = tooltip.runtimeSerieDataIndex[serie.index];
if (!string.IsNullOrEmpty(serie.name))
{
sb.Append(serie.name).Append(PH_NN);
}
for (int i = 0; i < dataIndexList.Count; i++)
{
var dataIndex = dataIndexList[i];
var serieData = serie.GetSerieData(dataIndex);
var numericFormatter = GetItemNumericFormatter(tooltip, serie, serieData);
float xValue, yValue;
serie.GetXYData(dataIndex, null, out xValue, out yValue);
sb.Append("<color=#").Append(themeInfo.GetColorStr(serie.index)).Append(">● </color>");
if (!string.IsNullOrEmpty(serieData.name))
sb.Append(serieData.name).Append(": ");
sb.AppendFormat("({0},{1})", ChartCached.FloatToStr(xValue, numericFormatter),
ChartCached.FloatToStr(yValue, numericFormatter));
if (i != dataIndexList.Count - 1)
{
sb.Append("\n");
}
}
}
private static void InitPieTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
ThemeInfo themeInfo)
{
string key = serie.data[index].name;
var serieData = serie.GetSerieData(index);
var numericFormatter = GetItemNumericFormatter(tooltip, serie, serieData);
float value = serieData.GetData(1);
sb.Length = 0;
if (!string.IsNullOrEmpty(serie.name))
{
sb.Append(serie.name).Append(PH_NN);
}
sb.Append("<color=#").Append(themeInfo.GetColorStr(index)).Append(">● </color>");
if (!string.IsNullOrEmpty(key))
sb.Append(key).Append(": ");
sb.Append(ChartCached.FloatToStr(value, numericFormatter));
}
private static void InitRingTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
ThemeInfo themeInfo)
{
var serieData = serie.GetSerieData(index);
var numericFormatter = GetItemNumericFormatter(tooltip, serie, serieData);
float value = serieData.GetFirstData();
sb.Length = 0;
if (!string.IsNullOrEmpty(serieData.name))
{
sb.Append("<color=#").Append(themeInfo.GetColorStr(index)).Append(">● </color>")
.Append(serieData.name).Append(": ").Append(ChartCached.FloatToStr(value, numericFormatter));
}
else
{
sb.Append(ChartCached.FloatToStr(value, numericFormatter));
}
}
public static void InitRadarTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, Radar radar,
ThemeInfo themeInfo)
{
var dataIndex = tooltip.runtimeDataIndex[1];
var serieData = serie.GetSerieData(dataIndex);
var numericFormatter = GetItemNumericFormatter(tooltip, serie, serieData);
switch (serie.radarType)
{
case RadarType.Multiple:
sb.Append(serieData.name);
for (int i = 0; i < radar.indicatorList.Count; i++)
{
string key = radar.indicatorList[i].name;
float value = serieData.GetData(i);
if ((i == 0 && !string.IsNullOrEmpty(serieData.name)) || i > 0) sb.Append(PH_NN);
sb.AppendFormat("{0}: {1}", key, ChartCached.FloatToStr(value, numericFormatter));
}
break;
case RadarType.Single:
string key2 = serieData.name;
float value2 = serieData.GetData(1);
if (string.IsNullOrEmpty(key2))
{
key2 = radar.indicatorList[dataIndex].name;
}
sb.AppendFormat("{0}: {1}", key2, ChartCached.FloatToStr(value2, numericFormatter));
break;
}
}
private static void InitCoordinateTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
ThemeInfo themeInfo, bool isCartesian, DataZoom dataZoom = null)
{
string key = serie.name;
float xValue, yValue;
serie.GetXYData(index, dataZoom, out xValue, out yValue);
var isIngore = serie.IsIgnorePoint(index);
var serieData = serie.GetSerieData(index, dataZoom);
var numericFormatter = GetItemNumericFormatter(tooltip, serie, serieData);
if (isCartesian)
{
if (serieData != null && serieData.highlighted)
{
sb.Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : "");
sb.Append("[").Append(ChartCached.FloatToStr(xValue, numericFormatter)).Append(",")
.Append(ChartCached.FloatToStr(yValue, numericFormatter)).Append("]");
}
}
else
{
var valueTxt = isIngore ? tooltip.ignoreDataDefaultContent :
ChartCached.FloatToStr(yValue, numericFormatter);
sb.Append("<color=#").Append(themeInfo.GetColorStr(serie.index)).Append(">● </color>")
.Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : "")
.Append(valueTxt);
}
}
private static void InitDefaultContent(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
string category, ThemeInfo themeInfo = null, DataZoom dataZoom = null, bool isCartesian = false)
{
switch (serie.type)
{
case SerieType.Line:
case SerieType.Bar:
InitCoordinateTooltip(ref sb, tooltip, serie, index, themeInfo, isCartesian, dataZoom);
break;
case SerieType.Scatter:
case SerieType.EffectScatter:
InitScatterTooltip(ref sb, tooltip, serie, index, themeInfo);
break;
case SerieType.Radar:
break;
case SerieType.Pie:
InitPieTooltip(ref sb, tooltip, serie, index, themeInfo);
break;
case SerieType.Ring:
InitRingTooltip(ref sb, tooltip, serie, index, themeInfo);
break;
case SerieType.Heatmap:
break;
case SerieType.Gauge:
break;
}
}
public static void SetContentAndPosition(Tooltip tooltip, string content, Rect chartRect)
{
tooltip.UpdateContentText(content);
var pos = tooltip.GetContentPos();
if (pos.x + tooltip.runtimeWidth > chartRect.x + chartRect.width)
{
pos.x = chartRect.x + chartRect.width - tooltip.runtimeWidth;
}
if (pos.y - tooltip.runtimeHeight < chartRect.y)
{
pos.y = chartRect.y + tooltip.runtimeHeight;
}
tooltip.UpdateContentPos(pos);
}
public static string GetFormatterContent(Tooltip tooltip, int dataIndex, Series series, ThemeInfo themeInfo,
string category = null, DataZoom dataZoom = null, bool isCartesian = false)
{
if (string.IsNullOrEmpty(tooltip.formatter))
{
var sb = ChartHelper.sb;
var title = tooltip.titleFormatter;
var formatTitle = !string.IsNullOrEmpty(title);
var needCategory = false;
var first = true;
var isScatter = false;
sb.Length = 0;
for (int i = 0; i < series.Count; i++)
{
var serie = series.GetSerie(i);
if (serie.type == SerieType.Scatter || serie.type == SerieType.EffectScatter)
{
if (serie.show && IsSelectedSerie(tooltip, serie.index))
{
isScatter = true;
var itemFormatter = GetItemFormatter(tooltip, serie, null);
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);
first = false;
continue;
}
var itemTitle = title;
if (!string.IsNullOrEmpty(itemTitle))
{
Replace(ref itemTitle, PH_A, i, serie.name, true);
sb.Append(itemTitle).Append(PH_NN);
}
var dataIndexList = tooltip.runtimeSerieDataIndex[serie.index];
foreach (var tempIndex in dataIndexList)
{
var foundDot = false;
var serieData = serie.GetSerieData(tempIndex);
string content = itemFormatter;
Replace(ref content, PH_A, i, serie.name, true);
Replace(ref content, PH_B, i, needCategory ? category : serieData.name, true);
if (itemFormatter.IndexOf(PH_I) >= 0)
{
foundDot = true;
Replace(ref content, PH_I, i, ChartCached.ColorToDotStr(themeInfo.GetColor(serie.index)), true);
}
for (int n = 0; n < serieData.data.Count; n++)
{
var valueStr = ChartCached.FloatToStr(serieData.GetData(n), numericFormatter);
Replace(ref content, GetPHCC(n), i, valueStr, true);
}
if (!foundDot)
{
sb.Append(ChartCached.ColorToDotStr(themeInfo.GetColor(serie.index)));
}
sb.Append(content).Append(PH_NN);
}
}
}
else
{
var serieData = serie.GetSerieData(dataIndex, dataZoom);
if (serieData == null) continue;
var itemFormatter = GetItemFormatter(tooltip, serie, serieData);
var numericFormatter = GetItemNumericFormatter(tooltip, serie, serieData);
var percent = serieData.GetData(1) / serie.yTotal * 100;
needCategory = needCategory || (serie.type == SerieType.Line || serie.type == SerieType.Bar);
if (formatTitle)
{
var valueStr = ChartCached.FloatToStr(serieData.GetData(1), numericFormatter);
Replace(ref title, PH_A, i, serie.name, true);
Replace(ref title, PH_B, i, needCategory ? category : serieData.name, true);
Replace(ref title, PH_C, i, valueStr, true);
Replace(ref title, PH_D, i, ChartCached.FloatToStr(percent, string.Empty, 1), true);
}
if (serie.show)
{
if (string.IsNullOrEmpty(itemFormatter))
{
if (!first) sb.Append(PH_NN);
InitDefaultContent(ref sb, tooltip, serie, dataIndex, category, themeInfo, dataZoom, isCartesian);
first = false;
continue;
}
string content = itemFormatter;
var valueStr = ChartCached.FloatToStr(serieData.GetData(1), numericFormatter);
Replace(ref content, PH_A, i, serie.name, true);
Replace(ref content, PH_B, i, needCategory ? category : serieData.name, true);
Replace(ref content, PH_C, i, valueStr, true);
Replace(ref content, PH_D, i, ChartCached.FloatToStr(percent, string.Empty, 1), true);
for (int n = 0; n < serieData.data.Count; n++)
{
valueStr = ChartCached.FloatToStr(serieData.GetData(n), numericFormatter);
Replace(ref content, GetPHCC(n), i, valueStr, true);
}
if (!first) sb.Append(PH_NN);
sb.Append(ChartCached.ColorToDotStr(themeInfo.GetColor(i)));
sb.Append(content);
first = false;
}
}
}
if (isScatter)
{
return TrimAndReplaceLine(sb);
}
else if (string.IsNullOrEmpty(title))
{
if (needCategory) return category + PH_NN + TrimAndReplaceLine(sb);
else return TrimAndReplaceLine(sb);
}
else
{
title = title.Replace(PH_ON, PH_NN);
title = title.Replace(PH_BR, PH_NN);
return title + PH_NN + TrimAndReplaceLine(sb);
}
}
else
{
string content = tooltip.formatter;
for (int i = 0; i < series.Count; i++)
{
var serie = series.GetSerie(i);
if (serie.show)
{
var needCategory = serie.type == SerieType.Line || serie.type == SerieType.Bar;
var serieData = serie.GetSerieData(dataIndex, dataZoom);
var numericFormatter = GetItemNumericFormatter(tooltip, serie, serieData);
var percent = serieData.GetData(1) / serie.yTotal * 100;
Replace(ref content, PH_A, i, serie.name);
Replace(ref content, PH_B, i, needCategory ? category : serieData.name);
Replace(ref content, PH_C, i, ChartCached.FloatToStr(serieData.GetData(1), numericFormatter));
Replace(ref content, PH_D, i, ChartCached.FloatToStr(percent, string.Empty, 1));
Replace(ref content, PH_I, i, ChartCached.ColorToDotStr(themeInfo.GetColor(i)));
for (int n = 0; n < serieData.data.Count; n++)
{
var valueStr = ChartCached.FloatToStr(serieData.GetData(n), numericFormatter);
if (i == 0) Replace(ref content, GetPHCC(n), i, valueStr, true);
Replace(ref content, GetPHCC(i, n), i, valueStr, true);
}
}
}
content = content.Replace(PH_ON, PH_NN);
content = content.Replace(PH_BR, PH_NN);
return content;
}
}
private static string TrimAndReplaceLine(StringBuilder sb)
{
return sb.ToString().Trim().Replace(PH_ON, PH_NN_BBB).Replace(PH_BR, PH_NN_BBB);
}
private static bool IsSelectedSerie(Tooltip tooltip, int serieIndex)
{
if (tooltip.runtimeSerieDataIndex.ContainsKey(serieIndex))
{
return tooltip.runtimeSerieDataIndex[serieIndex].Count > 0;
}
return false;
}
private static void Replace(ref string content, string placeHolder, int index, string newStr, bool all = false)
{
if ((all || index == 0) && content.IndexOf(placeHolder) >= 0)
{
content = content.Replace(placeHolder, newStr);
}
if (!s_PHDic.ContainsKey(placeHolder))
{
s_PHDic[placeHolder] = new Dictionary<int, string>();
}
if (!s_PHDic[placeHolder].ContainsKey(index))
{
s_PHDic[placeHolder][index] = placeHolder.Insert(2, index.ToString());
}
var holder = s_PHDic[placeHolder][index];
if (content.IndexOf(holder) >= 0)
{
content = content.Replace(holder, newStr);
}
}
private static string GetPHCC(int index)
{
if (!s_PHCCDic.ContainsKey(index))
{
s_PHCCDic[index] = "{c:" + index + "}";
}
return s_PHCCDic[index];
}
private static string GetPHCC(int serieIndex, int dataIndex)
{
if (!s_PHSerieCCDic.ContainsKey(serieIndex))
{
s_PHSerieCCDic[serieIndex] = new Dictionary<int, string>();
}
if (!s_PHSerieCCDic[serieIndex].ContainsKey(dataIndex))
{
s_PHSerieCCDic[serieIndex][dataIndex] = "{c" + serieIndex + ":" + dataIndex + "}";
}
return s_PHSerieCCDic[serieIndex][dataIndex];
}
private static string GetItemFormatter(Tooltip tooltip, Serie serie, SerieData serieData)
{
var itemStyle = SerieHelper.GetItemStyle(serie, serieData);
if (!string.IsNullOrEmpty(itemStyle.tooltipFormatter)) return itemStyle.tooltipFormatter;
else return tooltip.itemFormatter;
}
private static string GetItemNumericFormatter(Tooltip tooltip, Serie serie, SerieData serieData)
{
var itemStyle = SerieHelper.GetItemStyle(serie, serieData);
if (!string.IsNullOrEmpty(itemStyle.numericFormatter)) return itemStyle.numericFormatter;
else return tooltip.numericFormatter;
}
public static Color GetLineColor(Tooltip tooltip, ThemeInfo theme)
{
var lineStyle = tooltip.lineStyle;
if (!ChartHelper.IsClearColor(lineStyle.color))
{
var color = lineStyle.color;
color.a *= lineStyle.opacity;
return color;
}
else
{
var color = (Color)theme.tooltipLineColor;
color.a *= lineStyle.opacity;
return color;
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f0fa1b19683a8424bb335802de5c9726
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,14 @@
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
namespace XCharts
{
public static class VisualMapHelper
{
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 15e5106bd46f5484596429d512f6af5d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: