优化代码

This commit is contained in:
monitor1394
2018-10-03 22:30:25 +08:00
parent 16754d1a49
commit 481cf9ae68
6 changed files with 81 additions and 36 deletions

View File

@@ -244,7 +244,8 @@ namespace xcharts
float scaleWid = coordinateWid / (xAxis.splitNumber - 1); float scaleWid = coordinateWid / (xAxis.splitNumber - 1);
if (xAxis.type == AxisType.value) if (xAxis.type == AxisType.value)
{ {
return new Vector3(zeroX + (i + 1 - 0.5f) * scaleWid, zeroY - coordinate.scaleLen - 10, 0); return new Vector3(zeroX + (i + 1 - 0.5f) * scaleWid,
zeroY - coordinate.scaleLen - 10, 0);
} }
else else
{ {
@@ -254,7 +255,8 @@ namespace xcharts
} }
else else
{ {
return new Vector3(zeroX + (i + 1 - 0.5f) * scaleWid, zeroY - coordinate.scaleLen - 5, 0); return new Vector3(zeroX + (i + 1 - 0.5f) * scaleWid,
zeroY - coordinate.scaleLen - 5, 0);
} }
} }
} }
@@ -389,7 +391,7 @@ namespace xcharts
float pX = zeroX - coordinate.scaleLen; float pX = zeroX - coordinate.scaleLen;
float pY = zeroY + i * coordinateHig / (yAxis.splitNumber - 1); float pY = zeroY + i * coordinateHig / (yAxis.splitNumber - 1);
ChartUtils.DrawLine(vh, new Vector3(pX, pY), new Vector3(zeroX, pY), coordinate.tickness, ChartUtils.DrawLine(vh, new Vector3(pX, pY), new Vector3(zeroX, pY), coordinate.tickness,
Color.white); themeInfo.axisLineColor);
if (yAxis.showSplitLine) if (yAxis.showSplitLine)
{ {
DrawSplitLine(vh, true,yAxis.splitLineType, new Vector3(zeroX, pY), DrawSplitLine(vh, true,yAxis.splitLineType, new Vector3(zeroX, pY),
@@ -401,7 +403,7 @@ namespace xcharts
float pX = zeroX + i * coordinateWid / (xAxis.splitNumber - 1); float pX = zeroX + i * coordinateWid / (xAxis.splitNumber - 1);
float pY = zeroY - coordinate.scaleLen - 2; float pY = zeroY - coordinate.scaleLen - 2;
ChartUtils.DrawLine(vh, new Vector3(pX, zeroY), new Vector3(pX, pY), coordinate.tickness, ChartUtils.DrawLine(vh, new Vector3(pX, zeroY), new Vector3(pX, pY), coordinate.tickness,
Color.white); themeInfo.axisLineColor);
if (xAxis.showSplitLine) if (xAxis.showSplitLine)
{ {
DrawSplitLine(vh, false,xAxis.splitLineType, new Vector3(pX, zeroY), DrawSplitLine(vh, false,xAxis.splitLineType, new Vector3(pX, zeroY),
@@ -410,12 +412,15 @@ namespace xcharts
} }
//draw x,y axis //draw x,y axis
ChartUtils.DrawLine(vh, new Vector3(zeroX, zeroY - coordinate.scaleLen), ChartUtils.DrawLine(vh, new Vector3(zeroX, zeroY - coordinate.scaleLen),
new Vector3(zeroX, zeroY + coordinateHig + 2), coordinate.tickness, themeInfo.axisLineColor); new Vector3(zeroX, zeroY + coordinateHig + 2), coordinate.tickness,
themeInfo.axisLineColor);
ChartUtils.DrawLine(vh, new Vector3(zeroX - coordinate.scaleLen, zeroY), ChartUtils.DrawLine(vh, new Vector3(zeroX - coordinate.scaleLen, zeroY),
new Vector3(zeroX + coordinateWid + 2, zeroY), coordinate.tickness, themeInfo.axisLineColor); new Vector3(zeroX + coordinateWid + 2, zeroY), coordinate.tickness,
themeInfo.axisLineColor);
} }
private void DrawSplitLine(VertexHelper vh,bool isYAxis,SplitLineType type,Vector3 startPos,Vector3 endPos) private void DrawSplitLine(VertexHelper vh,bool isYAxis,SplitLineType type,Vector3 startPos,
Vector3 endPos)
{ {
switch (type) switch (type)
{ {

View File

@@ -67,10 +67,12 @@ namespace xcharts
public float top; public float top;
public float bottom; public float bottom;
public List<LegendData> dataList = new List<LegendData>(); public List<LegendData> dataList = new List<LegendData>();
public int checkDataListCount { get; set; }
public bool IsShowSeries(int seriesIndex) public bool IsShowSeries(int seriesIndex)
{ {
if (seriesIndex < 0 || seriesIndex >= dataList.Count) seriesIndex = 0; if (seriesIndex < 0 || seriesIndex >= dataList.Count) seriesIndex = 0;
if (seriesIndex >= dataList.Count) return false;
return dataList[seriesIndex].show; return dataList[seriesIndex].show;
} }
} }
@@ -236,7 +238,8 @@ namespace xcharts
break; break;
case Align.right: case Align.right:
anchor = TextAnchor.MiddleRight; anchor = TextAnchor.MiddleRight;
titlePosition = new Vector3(chartWid - title.right - titleWid, chartHig - title.top, 0); titlePosition = new Vector3(chartWid - title.right - titleWid,
chartHig - title.top, 0);
break; break;
case Align.center: case Align.center:
anchor = TextAnchor.MiddleCenter; anchor = TextAnchor.MiddleCenter;
@@ -265,7 +268,7 @@ namespace xcharts
themeInfo.textColor, Vector2.zero,Vector2.zero, Vector2.zero, themeInfo.textColor, Vector2.zero,Vector2.zero, Vector2.zero,
new Vector2(legend.dataWid, legend.dataHig)); new Vector2(legend.dataWid, legend.dataHig));
legend.dataList[i].button = btn; legend.dataList[i].button = btn;
Color bcolor = data.show ? themeInfo.GetColor(i) : Color.grey; Color bcolor = data.show ? themeInfo.GetColor(i) : themeInfo.unableColor;
btn.gameObject.SetActive(legend.show); btn.gameObject.SetActive(legend.show);
btn.transform.localPosition = GetLegendPosition(i); btn.transform.localPosition = GetLegendPosition(i);
btn.GetComponent<Image>().color = bcolor; btn.GetComponent<Image>().color = bcolor;
@@ -273,7 +276,7 @@ namespace xcharts
btn.onClick.AddListener(delegate () btn.onClick.AddListener(delegate ()
{ {
data.show = !data.show; data.show = !data.show;
btn.GetComponent<Image>().color = data.show ? themeInfo.GetColor(i) : Color.grey; btn.GetComponent<Image>().color = data.show ? themeInfo.GetColor(i) : themeInfo.unableColor;
OnYMaxValueChanged(); OnYMaxValueChanged();
OnLegendButtonClicked(); OnLegendButtonClicked();
RefreshChart(); RefreshChart();
@@ -306,10 +309,13 @@ namespace xcharts
} }
else if (startY <= 0) else if (startY <= 0)
{ {
float offset = (chartHig - (legendCount * legend.dataHig - (legendCount - 1) * legend.dataSpace)) / 2; float legendHig = legendCount * legend.dataHig - (legendCount - 1) * legend.dataSpace;
float offset = (chartHig - legendHig) / 2;
startY = chartHig - offset - legend.dataHig; startY = chartHig - offset - legend.dataHig;
} }
float posX = legend.location == Location.left ? legend.left : chartWid - legend.right - legend.dataWid; float posX = legend.location == Location.left ?
legend.left :
chartWid - legend.right - legend.dataWid;
return new Vector3(posX, startY - i * (legend.dataHig + legend.dataSpace), 0); return new Vector3(posX, startY - i * (legend.dataHig + legend.dataSpace), 0);
default: break; default: break;
} }
@@ -353,6 +359,12 @@ namespace xcharts
private void CheckLegend() private void CheckLegend()
{ {
if (checkLegend.checkDataListCount != legend.dataList.Count)
{
checkLegend.checkDataListCount = legend.dataList.Count;
OnLegendDataListChanged();
}
if (checkLegend.dataWid != legend.dataWid || if (checkLegend.dataWid != legend.dataWid ||
checkLegend.dataHig != legend.dataHig || checkLegend.dataHig != legend.dataHig ||
checkLegend.dataSpace != legend.dataSpace || checkLegend.dataSpace != legend.dataSpace ||
@@ -404,15 +416,22 @@ namespace xcharts
for (int i = 0; i < legend.dataList.Count; i++) for (int i = 0; i < legend.dataList.Count; i++)
{ {
Button btn = legend.dataList[i].button; Button btn = legend.dataList[i].button;
btn.GetComponent<RectTransform>().sizeDelta = new Vector2(legend.dataWid, legend.dataHig); btn.GetComponent<RectTransform>().sizeDelta =
new Vector2(legend.dataWid, legend.dataHig);
Text txt = btn.GetComponentInChildren<Text>(); Text txt = btn.GetComponentInChildren<Text>();
txt.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(legend.dataWid, legend.dataHig); txt.transform.GetComponent<RectTransform>().sizeDelta =
new Vector2(legend.dataWid, legend.dataHig);
txt.transform.localPosition = Vector3.zero; txt.transform.localPosition = Vector3.zero;
btn.transform.localPosition = GetLegendPosition(i); btn.transform.localPosition = GetLegendPosition(i);
btn.gameObject.SetActive(legend.show); btn.gameObject.SetActive(legend.show);
} }
} }
protected virtual void OnLegendDataListChanged()
{
InitLegend();
}
protected virtual void OnYMaxValueChanged() protected virtual void OnYMaxValueChanged()
{ {
} }

View File

@@ -19,6 +19,7 @@ namespace xcharts
public Color contrastColor; public Color contrastColor;
public Color textColor; public Color textColor;
public Color subTextColor; public Color subTextColor;
public Color unableColor;
public Color axisLineColor; public Color axisLineColor;
public Color axisSplitLineColor; public Color axisSplitLineColor;
@@ -40,6 +41,7 @@ namespace xcharts
font = theme.font; font = theme.font;
backgroundColor = theme.backgroundColor; backgroundColor = theme.backgroundColor;
contrastColor = theme.contrastColor; contrastColor = theme.contrastColor;
unableColor = theme.unableColor;
textColor = theme.textColor; textColor = theme.textColor;
subTextColor = theme.subTextColor; subTextColor = theme.subTextColor;
axisLineColor = theme.axisLineColor; axisLineColor = theme.axisLineColor;
@@ -60,6 +62,7 @@ namespace xcharts
font = Resources.GetBuiltinResource<Font>("Arial.ttf"), font = Resources.GetBuiltinResource<Font>("Arial.ttf"),
backgroundColor = new Color32(255, 255, 255, 255), backgroundColor = new Color32(255, 255, 255, 255),
contrastColor = GetColor("#514D4D"), contrastColor = GetColor("#514D4D"),
unableColor = GetColor("#cccccc"),
textColor = GetColor("#514D4D"), textColor = GetColor("#514D4D"),
subTextColor = GetColor("#514D4D"), subTextColor = GetColor("#514D4D"),
axisLineColor = GetColor("#514D4D"), axisLineColor = GetColor("#514D4D"),
@@ -91,6 +94,7 @@ namespace xcharts
font = Resources.GetBuiltinResource<Font>("Arial.ttf"), font = Resources.GetBuiltinResource<Font>("Arial.ttf"),
backgroundColor = new Color32(255, 255, 255, 255), backgroundColor = new Color32(255, 255, 255, 255),
contrastColor = GetColor("#514D4D"), contrastColor = GetColor("#514D4D"),
unableColor = GetColor("#cccccc"),
textColor = GetColor("#514D4D"), textColor = GetColor("#514D4D"),
subTextColor = GetColor("#514D4D"), subTextColor = GetColor("#514D4D"),
axisLineColor = GetColor("#514D4D"), axisLineColor = GetColor("#514D4D"),
@@ -98,14 +102,14 @@ namespace xcharts
colorPalette = new Color[] colorPalette = new Color[]
{ {
new Color32(55, 162, 218, 255), new Color32(55, 162, 218, 255),
new Color32(50, 197, 233, 255),
new Color32(103, 224, 227, 255),
new Color32(159, 230, 184, 255),
new Color32(255, 219, 92, 255),
new Color32(255, 159, 127, 255), new Color32(255, 159, 127, 255),
new Color32(50, 197, 233, 255),
new Color32(251, 114, 147, 255), new Color32(251, 114, 147, 255),
new Color32(103, 224, 227, 255),
new Color32(224, 98, 174, 255), new Color32(224, 98, 174, 255),
new Color32(159, 230, 184, 255),
new Color32(230, 144, 209, 255), new Color32(230, 144, 209, 255),
new Color32(255, 219, 92, 255),
new Color32(230, 188, 243, 255), new Color32(230, 188, 243, 255),
new Color32(157, 150, 245, 255), new Color32(157, 150, 245, 255),
new Color32(131, 120, 234, 255), new Color32(131, 120, 234, 255),
@@ -122,6 +126,7 @@ namespace xcharts
return new ThemeInfo() return new ThemeInfo()
{ {
font = Resources.GetBuiltinResource<Font>("Arial.ttf"), font = Resources.GetBuiltinResource<Font>("Arial.ttf"),
unableColor = GetColor("#cccccc"),
backgroundColor = new Color32(34, 34, 34, 255), backgroundColor = new Color32(34, 34, 34, 255),
contrastColor = GetColor("#eee"), contrastColor = GetColor("#eee"),
textColor = GetColor("#eee"), textColor = GetColor("#eee"),

View File

@@ -7,8 +7,9 @@ namespace xcharts
public static class ChartUtils public static class ChartUtils
{ {
private static float CRICLE_SMOOTHNESS = 1f; private static float CRICLE_SMOOTHNESS = 1f;
public static Text AddTextObject(string name, Transform parent, Font font, Color color,TextAnchor anchor, public static Text AddTextObject(string name, Transform parent, Font font, Color color,
Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 sizeDelta, int fontSize = 14) TextAnchor anchor,Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 sizeDelta,
int fontSize = 14)
{ {
GameObject txtObj; GameObject txtObj;
if (parent.Find(name)) if (parent.Find(name))
@@ -73,6 +74,7 @@ namespace xcharts
{ {
rect = btnObj.AddComponent<RectTransform>(); rect = btnObj.AddComponent<RectTransform>();
} }
btnObj.GetComponentInChildren<Text>().color = color;
rect.anchorMax = anchorMax; rect.anchorMax = anchorMax;
rect.anchorMin = anchorMin; rect.anchorMin = anchorMin;
rect.pivot = pivot; rect.pivot = pivot;
@@ -167,8 +169,8 @@ namespace xcharts
DrawSector(vh, p, radius, color, 0, 360, segments); DrawSector(vh, p, radius, color, 0, 360, segments);
} }
public static void DrawCicleNotFill(VertexHelper vh, Vector3 p, float radius, float tickness, Color color, public static void DrawCicleNotFill(VertexHelper vh, Vector3 p, float radius, float tickness,
int segments = 0) Color color, int segments = 0)
{ {
if (segments <= 0) if (segments <= 0)
{ {
@@ -206,7 +208,8 @@ namespace xcharts
for (int i = 0; i <= segments; i++) for (int i = 0; i <= segments; i++)
{ {
float currAngle = startAngle + i * angle; float currAngle = startAngle + i * angle;
p3 = new Vector3(p.x + radius * Mathf.Sin(currAngle), p.y + radius * Mathf.Cos(currAngle)); p3 = new Vector3(p.x + radius * Mathf.Sin(currAngle),
p.y + radius * Mathf.Cos(currAngle));
DrawTriangle(vh, vertexs, p, p2, p3, color); DrawTriangle(vh, vertexs, p, p2, p3, color);
p2 = p3; p2 = p3;
} }
@@ -227,14 +230,17 @@ namespace xcharts
Vector3 p1, p2, p3, p4; Vector3 p1, p2, p3, p4;
float startAngle = startDegree * Mathf.Deg2Rad; float startAngle = startDegree * Mathf.Deg2Rad;
float angle = (toDegree - startDegree) * Mathf.Deg2Rad / segments; float angle = (toDegree - startDegree) * Mathf.Deg2Rad / segments;
p1 = new Vector3(p.x + insideRadius * Mathf.Sin(startAngle), p.y + insideRadius * Mathf.Cos(startAngle)); p1 = new Vector3(p.x + insideRadius * Mathf.Sin(startAngle),
p2 = new Vector3(p.x + outsideRadius * Mathf.Sin(startAngle), p.y + outsideRadius * Mathf.Cos(startAngle)); p.y + insideRadius * Mathf.Cos(startAngle));
p2 = new Vector3(p.x + outsideRadius * Mathf.Sin(startAngle),
p.y + outsideRadius * Mathf.Cos(startAngle));
for (int i = 0; i <= segments; i++) for (int i = 0; i <= segments; i++)
{ {
float currAngle = startAngle + i * angle; float currAngle = startAngle + i * angle;
p3 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle), p.y + outsideRadius * Mathf.Cos(currAngle)); p3 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle),
p4 = new Vector3(p.x + insideRadius * Mathf.Sin(currAngle), p.y + insideRadius * Mathf.Cos(currAngle)); p.y + outsideRadius * Mathf.Cos(currAngle));
p4 = new Vector3(p.x + insideRadius * Mathf.Sin(currAngle),
p.y + insideRadius * Mathf.Cos(currAngle));
DrawPolygon(vh, p1, p2, p3, p4, color); DrawPolygon(vh, p1, p2, p3, p4, color);
p1 = p4; p1 = p4;
p2 = p3; p2 = p3;

View File

@@ -97,10 +97,12 @@ namespace xcharts
{ {
SeriesData data = series.dataList[i]; SeriesData data = series.dataList[i];
Vector3 p = new Vector3(startX + i * scaleWid, zeroY + data.value * coordinateHig / max); Vector3 p = new Vector3(startX + i * scaleWid,
zeroY + data.value * coordinateHig / max);
if(theme == Theme.Dark) if(theme == Theme.Dark)
{ {
ChartUtils.DrawCricle(vh, p, lineInfo.pointWid, color, (int)lineInfo.pointWid * 5); ChartUtils.DrawCricle(vh, p, lineInfo.pointWid, color,
(int)lineInfo.pointWid * 5);
} }
else else
{ {

View File

@@ -210,7 +210,10 @@ namespace xcharts
{ {
toPoint = new Vector3(p.x + radius * Mathf.Sin(currAngle), toPoint = new Vector3(p.x + radius * Mathf.Sin(currAngle),
p.y + radius * Mathf.Cos(currAngle)); p.y + radius * Mathf.Cos(currAngle));
if (radarInfo.area) ChartUtils.DrawTriangle(vh, p, startPoint, toPoint, areaColor); if (radarInfo.area)
{
ChartUtils.DrawTriangle(vh, p, startPoint, toPoint, areaColor);
}
ChartUtils.DrawLine(vh, startPoint, toPoint, radarInfo.lineTickness, color); ChartUtils.DrawLine(vh, startPoint, toPoint, radarInfo.lineTickness, color);
startPoint = toPoint; startPoint = toPoint;
} }
@@ -245,8 +248,10 @@ namespace xcharts
for (int j = 0; j <= indicatorNum; j++) for (int j = 0; j <= indicatorNum; j++)
{ {
float currAngle = j * angle; float currAngle = j * angle;
p3 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle), p.y + outsideRadius * Mathf.Cos(currAngle)); p3 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle),
p4 = new Vector3(p.x + insideRadius * Mathf.Sin(currAngle), p.y + insideRadius * Mathf.Cos(currAngle)); p.y + outsideRadius * Mathf.Cos(currAngle));
p4 = new Vector3(p.x + insideRadius * Mathf.Sin(currAngle),
p.y + insideRadius * Mathf.Cos(currAngle));
ChartUtils.DrawPolygon(vh, p1, p2, p3, p4, color); ChartUtils.DrawPolygon(vh, p1, p2, p3, p4, color);
ChartUtils.DrawLine(vh, p2, p3, radarInfo.lineTickness, radarInfo.lineColor); ChartUtils.DrawLine(vh, p2, p3, radarInfo.lineTickness, radarInfo.lineColor);
@@ -258,7 +263,8 @@ namespace xcharts
for (int j = 0; j <= indicatorNum; j++) for (int j = 0; j <= indicatorNum; j++)
{ {
float currAngle = j * angle; float currAngle = j * angle;
p3 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle), p.y + outsideRadius * Mathf.Cos(currAngle)); p3 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle),
p.y + outsideRadius * Mathf.Cos(currAngle));
ChartUtils.DrawLine(vh, p, p3, radarInfo.lineTickness/2, radarInfo.lineColor); ChartUtils.DrawLine(vh, p, p3, radarInfo.lineTickness/2, radarInfo.lineColor);
} }
} }
@@ -276,13 +282,15 @@ namespace xcharts
Color color = radarInfo.backgroundColorList[i % radarInfo.backgroundColorList.Count]; Color color = radarInfo.backgroundColorList[i % radarInfo.backgroundColorList.Count];
outsideRadius = insideRadius + block; outsideRadius = insideRadius + block;
ChartUtils.DrawDoughnut(vh, p, insideRadius, outsideRadius, 0, 360, color); ChartUtils.DrawDoughnut(vh, p, insideRadius, outsideRadius, 0, 360, color);
ChartUtils.DrawCicleNotFill(vh, p, outsideRadius, radarInfo.lineTickness, radarInfo.lineColor); ChartUtils.DrawCicleNotFill(vh, p, outsideRadius, radarInfo.lineTickness,
radarInfo.lineColor);
insideRadius = outsideRadius; insideRadius = outsideRadius;
} }
for (int j = 0; j <= indicatorNum; j++) for (int j = 0; j <= indicatorNum; j++)
{ {
float currAngle = j * angle; float currAngle = j * angle;
p1 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle), p.y + outsideRadius * Mathf.Cos(currAngle)); p1 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle),
p.y + outsideRadius * Mathf.Cos(currAngle));
ChartUtils.DrawLine(vh, p, p1, radarInfo.lineTickness / 2, radarInfo.lineColor); ChartUtils.DrawLine(vh, p, p1, radarInfo.lineTickness / 2, radarInfo.lineColor);
} }
} }