增加Legend几种内置图标的支持 #90

This commit is contained in:
monitor1394
2021-03-05 08:56:55 +08:00
parent ef3817d231
commit 642dde3c27
9 changed files with 210 additions and 23 deletions

View File

@@ -815,6 +815,7 @@ namespace XCharts
vh.Clear();
DrawBackground(vh);
DrawPainterBase(vh);
DrawLegend(vh);
foreach (var drawSerie in m_DrawSeries) drawSerie.DrawBase(vh);
}
@@ -868,6 +869,81 @@ namespace XCharts
UGL.DrawQuadrilateral(vh, p1, p2, p3, p4, backgroundColor);
}
protected virtual void DrawLegend(VertexHelper vh)
{
if (m_Series.Count == 0) return;
foreach (var legend in m_Legends)
{
if (!legend.show) continue;
if (legend.iconType == Legend.Type.Custom) continue;
foreach (var kv in legend.buttonList)
{
var item = kv.Value;
var rect = item.GetIconRect();
var radius = Mathf.Min(rect.width, rect.height) / 2;
var color = item.GetIconColor();
var iconType = legend.iconType;
if (legend.iconType == Legend.Type.Auto)
{
var serie = m_Series.GetSerie(item.legendName);
if (serie != null && serie.type == SerieType.Line)
{
var sp = new Vector3(rect.center.x - rect.width / 2, rect.center.y);
var ep = new Vector3(rect.center.x + rect.width / 2, rect.center.y);
UGL.DrawLine(vh, sp, ep, m_Settings.legendIconLineWidth, color);
if (!serie.symbol.show) continue;
switch (serie.symbol.type)
{
case SerieSymbolType.None:
continue;
case SerieSymbolType.Circle:
iconType = Legend.Type.Circle;
break;
case SerieSymbolType.Diamond:
iconType = Legend.Type.Diamond;
break;
case SerieSymbolType.EmptyCircle:
iconType = Legend.Type.EmptyCircle;
break;
case SerieSymbolType.Rect:
iconType = Legend.Type.Rect;
break;
case SerieSymbolType.Triangle:
iconType = Legend.Type.Triangle;
break;
}
}
else
{
iconType = Legend.Type.Rect;
}
}
switch (iconType)
{
case Legend.Type.Rect:
var cornerRadius = m_Settings.legendIconCornerRadius;
UGL.DrawRoundRectangle(vh, rect.center, rect.width, rect.height, color, color,
0, cornerRadius, false, 0.5f);
break;
case Legend.Type.Circle:
UGL.DrawCricle(vh, rect.center, radius, color);
break;
case Legend.Type.Diamond:
UGL.DrawDiamond(vh, rect.center, radius, color);
break;
case Legend.Type.EmptyCircle:
var backgroundColor = ThemeHelper.GetBackgroundColor(m_Theme, m_Background);
UGL.DrawEmptyCricle(vh, rect.center, radius, 2 * m_Settings.legendIconLineWidth,
color, color, backgroundColor, 1f);
break;
case Legend.Type.Triangle:
UGL.DrawTriangle(vh, rect.center, 1.2f * radius, color);
break;
}
}
}
}
public void DrawSymbol(VertexHelper vh, SerieSymbolType type, float symbolSize,
float tickness, Vector3 pos, Color32 color, Color32 toColor, float gap, float[] cornerRadius)
{

View File

@@ -105,6 +105,28 @@ namespace XCharts
}
}
public Rect GetIconRect()
{
if (m_GameObject && m_IconRect)
{
var pos = m_GameObject.transform.localPosition;
var sizeDelta = m_IconRect.sizeDelta;
var y = pos.y - (m_Rect.sizeDelta.y - sizeDelta.y) / 2 - sizeDelta.y;
return new Rect(pos.x, y, m_IconRect.sizeDelta.x, m_IconRect.sizeDelta.y);
}
else
{
return Rect.zero;
}
}
public Color GetIconColor()
{
if (m_Icon) return m_Icon.color;
else return Color.clear;
}
public void SetIconColor(Color color)
{
if (m_Icon)
@@ -121,6 +143,14 @@ namespace XCharts
}
}
public void SetIconActive(bool active)
{
if (m_Icon)
{
m_Icon.gameObject.SetActive(active);
}
}
public void SetContentColor(Color color)
{
if (m_Text != null)

View File

@@ -52,7 +52,6 @@ namespace XCharts
}
else
{
//UGL.DrawPolygon(vh, pos, symbolSize, color, toColor);
UGL.DrawRoundRectangle(vh, pos, symbolSize, symbolSize, color, color, 0, cornerRadius, true);
}
break;

View File

@@ -621,6 +621,16 @@ namespace XCharts
foreach (var item in fromList) toList.Add(item);
return true;
}
public static bool CopyArray<T>(T[] toList, T[] fromList)
{
if (toList == null || fromList == null) return false;
if (toList.Length != fromList.Length)
{
toList = new T[fromList.Length];
}
for (int i = 0; i < fromList.Length; i++) toList[i] = fromList[i];
return true;
}
public static List<float> ParseFloatFromString(string jsonData)
{