增加饼图pie

This commit is contained in:
monitor1394
2018-09-23 08:18:26 +08:00
parent 2e8012c696
commit f99fbf1ed1
8 changed files with 3031 additions and 1229 deletions

View File

@@ -9,7 +9,7 @@ public class Demo : MonoBehaviour
void Awake() void Awake()
{ {
lineChart = transform.Find("line_chart").GetComponent<LineChart>(); lineChart = transform.Find("xchart/line_chart").GetComponent<LineChart>();
} }
void Update() void Update()

View File

@@ -4,7 +4,7 @@ using UnityEngine.UI;
namespace xcharts namespace xcharts
{ {
[System.Serializable] [System.Serializable]
public class BarData public class BarInfo
{ {
public float barWid = 0.7f; public float barWid = 0.7f;
public float space; public float space;
@@ -13,7 +13,7 @@ namespace xcharts
public class BarChart : BaseChart public class BarChart : BaseChart
{ {
[SerializeField] [SerializeField]
private BarData barData; private BarInfo barInfo;
protected override void Awake() protected override void Awake()
{ {
@@ -33,8 +33,8 @@ namespace xcharts
{ {
int seriesCount = seriesList.Count; int seriesCount = seriesList.Count;
float scaleWid = coordinateHig / (yAxis.splitNumber - 1); float scaleWid = coordinateHig / (yAxis.splitNumber - 1);
float barWid = barData.barWid > 1 ? barData.barWid : scaleWid * barData.barWid; float barWid = barInfo.barWid > 1 ? barInfo.barWid : scaleWid * barInfo.barWid;
float offset = (scaleWid - barWid * seriesCount - barData.space * (seriesCount - 1)) / 2; float offset = (scaleWid - barWid * seriesCount - barInfo.space * (seriesCount - 1)) / 2;
float max = GetMaxValue(); float max = GetMaxValue();
for (int j = 0; j < seriesCount; j++) for (int j = 0; j < seriesCount; j++)
{ {
@@ -53,7 +53,7 @@ namespace xcharts
float pY = zeroY + i * coordinateHig / (yAxis.splitNumber - 1); float pY = zeroY + i * coordinateHig / (yAxis.splitNumber - 1);
if (!yAxis.boundaryGap) pY -= scaleWid / 2; if (!yAxis.boundaryGap) pY -= scaleWid / 2;
float barHig = data.value / max * coordinateWid; float barHig = data.value / max * coordinateWid;
float space = offset + j * (barWid + barData.space); float space = offset + j * (barWid + barInfo.space);
Vector3 p1 = new Vector3(pX, pY + space + barWid); Vector3 p1 = new Vector3(pX, pY + space + barWid);
Vector3 p2 = new Vector3(pX + barHig, pY + space + barWid); Vector3 p2 = new Vector3(pX + barHig, pY + space + barWid);
Vector3 p3 = new Vector3(pX + barHig, pY + space); Vector3 p3 = new Vector3(pX + barHig, pY + space);
@@ -66,8 +66,8 @@ namespace xcharts
{ {
int seriesCount = seriesList.Count; int seriesCount = seriesList.Count;
float scaleWid = coordinateWid / (xAxis.splitNumber - 1); float scaleWid = coordinateWid / (xAxis.splitNumber - 1);
float barWid = barData.barWid > 1 ? barData.barWid : scaleWid * barData.barWid; float barWid = barInfo.barWid > 1 ? barInfo.barWid : scaleWid * barInfo.barWid;
float offset = (scaleWid - barWid * seriesCount - barData.space * (seriesCount - 1)) / 2; float offset = (scaleWid - barWid * seriesCount - barInfo.space * (seriesCount - 1)) / 2;
float max = GetMaxValue(); float max = GetMaxValue();
for (int j = 0; j < seriesCount; j++) for (int j = 0; j < seriesCount; j++)
{ {
@@ -86,7 +86,7 @@ namespace xcharts
if (!xAxis.boundaryGap) pX -= scaleWid / 2; if (!xAxis.boundaryGap) pX -= scaleWid / 2;
float pY = zeroY + coordinate.tickness; float pY = zeroY + coordinate.tickness;
float barHig = data.value / max * coordinateHig; float barHig = data.value / max * coordinateHig;
float space = offset + j * (barWid + barData.space); float space = offset + j * (barWid + barInfo.space);
Vector3 p1 = new Vector3(pX + space, pY); Vector3 p1 = new Vector3(pX + space, pY);
Vector3 p2 = new Vector3(pX + space, pY + barHig); Vector3 p2 = new Vector3(pX + space, pY + barHig);
Vector3 p3 = new Vector3(pX + space + barWid, pY + barHig); Vector3 p3 = new Vector3(pX + space + barWid, pY + barHig);

View File

@@ -110,10 +110,10 @@ namespace xcharts
[System.Serializable] [System.Serializable]
public class Legend public class Legend
{ {
public bool show; public bool show = true;
public Location location; public Location location;
public float dataWid; public float dataWid = 50.0f;
public float dataHig; public float dataHig = 20.0f;
public float dataSpace; public float dataSpace;
public float left; public float left;
public float right; public float right;
@@ -222,14 +222,16 @@ namespace xcharts
private float lastCoordinateHig; private float lastCoordinateHig;
private float lastCoordinateScaleLen; private float lastCoordinateScaleLen;
private Align lastTitleAlign; //private Align lastTitleAlign;
private float lastTitleLeft; //private float lastTitleLeft;
private float lastTitleRight; //private float lastTitleRight;
private float lastTitleTop; //private float lastTitleTop;
private float lastXMaxValue; private float lastXMaxValue;
private float lastYMaxValue; private float lastYMaxValue;
private Coordinate checkCoordinate = new Coordinate();
private Title checkTitle = new Title();
private Legend checkLegend = new Legend(); private Legend checkLegend = new Legend();
private XAxis checkXAxis = new XAxis(); private XAxis checkXAxis = new XAxis();
private YAxis checkYAxis = new YAxis(); private YAxis checkYAxis = new YAxis();
@@ -269,7 +271,7 @@ namespace xcharts
CheckYAxisType(); CheckYAxisType();
CheckXAxisType(); CheckXAxisType();
CheckMaxValue(); CheckMaxValue();
CheckCoordinateSizeChange(); CheckCoordinate();
} }
public void AddData(string legend, string key, float value) public void AddData(string legend, string key, float value)
@@ -288,13 +290,13 @@ namespace xcharts
public void AddXAxisCategory(string category) public void AddXAxisCategory(string category)
{ {
xAxis.AddCategory(category); xAxis.AddCategory(category);
OnXAxisType(); OnXAxisChanged();
} }
public void AddYAxisCategory(string category) public void AddYAxisCategory(string category)
{ {
yAxis.AddCategory(category); yAxis.AddCategory(category);
OnYAxisType(); OnYAxisChanged();
} }
private void HideChild(string match = null) private void HideChild(string match = null)
@@ -365,6 +367,7 @@ namespace xcharts
new Vector2(coordinate.left, 20)); new Vector2(coordinate.left, 20));
txt.transform.localPosition = GetYScalePosition(i); txt.transform.localPosition = GetYScalePosition(i);
txt.text = ((int)(max * i / yAxis.splitNumber)).ToString(); txt.text = ((int)(max * i / yAxis.splitNumber)).ToString();
txt.gameObject.SetActive(coordinate.show);
yScaleTextList.Add(txt); yScaleTextList.Add(txt);
} }
} }
@@ -378,6 +381,7 @@ namespace xcharts
new Vector2(coordinate.left, 20)); new Vector2(coordinate.left, 20));
txt.transform.localPosition = GetYScalePosition(i); txt.transform.localPosition = GetYScalePosition(i);
txt.text = yAxis.data[i]; txt.text = yAxis.data[i];
txt.gameObject.SetActive(coordinate.show);
yScaleTextList.Add(txt); yScaleTextList.Add(txt);
} }
} }
@@ -399,6 +403,7 @@ namespace xcharts
new Vector2(scaleWid, 20)); new Vector2(scaleWid, 20));
txt.transform.localPosition = GetXScalePosition(i); txt.transform.localPosition = GetXScalePosition(i);
txt.text = ((int)(max * i / xAxis.splitNumber)).ToString(); txt.text = ((int)(max * i / xAxis.splitNumber)).ToString();
txt.gameObject.SetActive(coordinate.show);
xScaleTextList.Add(txt); xScaleTextList.Add(txt);
} }
} }
@@ -413,6 +418,7 @@ namespace xcharts
new Vector2(scaleWid, 20)); new Vector2(scaleWid, 20));
txt.transform.localPosition = GetXScalePosition(i); txt.transform.localPosition = GetXScalePosition(i);
txt.text = xAxis.data[i]; txt.text = xAxis.data[i];
txt.gameObject.SetActive(coordinate.show);
xScaleTextList.Add(txt); xScaleTextList.Add(txt);
} }
} }
@@ -436,6 +442,7 @@ namespace xcharts
data.show = !data.show; data.show = !data.show;
btn.GetComponent<Image>().color = data.show ? data.color : Color.grey; btn.GetComponent<Image>().color = data.show ? data.color : Color.grey;
OnYMaxValueChanged(); OnYMaxValueChanged();
OnLegendButtonClicked();
RefreshChart(); RefreshChart();
}); });
} }
@@ -520,7 +527,7 @@ namespace xcharts
} }
} }
private void CheckCoordinateSizeChange() private void CheckCoordinate()
{ {
if (lastCoordinateHig != coordinateHig if (lastCoordinateHig != coordinateHig
|| lastCoordinateWid != coordinateWid || lastCoordinateWid != coordinateWid
@@ -531,6 +538,12 @@ namespace xcharts
lastCoordinateScaleLen = coordinate.scaleLen; lastCoordinateScaleLen = coordinate.scaleLen;
OnCoordinateSize(); OnCoordinateSize();
} }
if(checkCoordinate.show != coordinate.show)
{
checkCoordinate.show = coordinate.show;
OnXAxisChanged();
OnYAxisChanged();
}
} }
private void CheckYAxisType() private void CheckYAxisType()
@@ -544,7 +557,7 @@ namespace xcharts
checkYAxis.boundaryGap = yAxis.boundaryGap; checkYAxis.boundaryGap = yAxis.boundaryGap;
checkYAxis.showSplitLine = yAxis.showSplitLine; checkYAxis.showSplitLine = yAxis.showSplitLine;
checkYAxis.splitNumber = yAxis.splitNumber; checkYAxis.splitNumber = yAxis.splitNumber;
OnYAxisType(); OnYAxisChanged();
} }
} }
@@ -559,7 +572,7 @@ namespace xcharts
checkXAxis.boundaryGap = xAxis.boundaryGap; checkXAxis.boundaryGap = xAxis.boundaryGap;
checkXAxis.showSplitLine = xAxis.showSplitLine; checkXAxis.showSplitLine = xAxis.showSplitLine;
checkXAxis.splitNumber = xAxis.splitNumber; checkXAxis.splitNumber = xAxis.splitNumber;
OnXAxisType(); OnXAxisChanged();
} }
} }
@@ -599,15 +612,15 @@ namespace xcharts
private void CheckTile() private void CheckTile()
{ {
if (lastTitleAlign != title.align || if (checkTitle.align != title.align ||
lastTitleLeft != title.left || checkTitle.left != title.left ||
lastTitleRight != title.right || checkTitle.right != title.right ||
lastTitleTop != title.top) checkTitle.top != title.top)
{ {
lastTitleAlign = title.align; checkTitle.align = title.align;
lastTitleLeft = title.left; checkTitle.left = title.left;
lastTitleRight = title.right; checkTitle.right = title.right;
lastTitleTop = title.top; checkTitle.top = title.top;
OnTitleChanged(); OnTitleChanged();
} }
} }
@@ -656,13 +669,13 @@ namespace xcharts
} }
} }
protected virtual void OnYAxisType() protected virtual void OnYAxisChanged()
{ {
HideChild(YSCALE_TEXT_PREFIX); HideChild(YSCALE_TEXT_PREFIX);
InitYScale(); InitYScale();
} }
protected virtual void OnXAxisType() protected virtual void OnXAxisChanged()
{ {
HideChild(XSCALE_TEXT_PREFIX); HideChild(XSCALE_TEXT_PREFIX);
InitXScale(); InitXScale();
@@ -705,6 +718,11 @@ namespace xcharts
} }
} }
protected virtual void OnLegendButtonClicked()
{
}
protected void RefreshChart() protected void RefreshChart()
{ {
int tempWid = (int)chartWid; int tempWid = (int)chartWid;

View File

@@ -62,8 +62,8 @@ namespace xcharts
btnObj.AddComponent<Image>(); btnObj.AddComponent<Image>();
btnObj.AddComponent<Button>(); btnObj.AddComponent<Button>();
Text txt = AddTextObject("Text", btnObj.transform, font, TextAnchor.MiddleCenter, Text txt = AddTextObject("Text", btnObj.transform, font, TextAnchor.MiddleCenter,
Vector2.zero,Vector2.zero, Vector2.zero, sizeDelta); Vector2.zero, Vector2.zero, Vector2.zero, sizeDelta);
txt.text = "Text"; txt.text = "Text";
} }
RectTransform rect = btnObj.GetComponent<RectTransform>(); RectTransform rect = btnObj.GetComponent<RectTransform>();
@@ -111,7 +111,7 @@ namespace xcharts
} }
public static void DrawPolygon(VertexHelper vh, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, public static void DrawPolygon(VertexHelper vh, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4,
Color startColor,Color toColor) Color startColor, Color toColor)
{ {
UIVertex[] vertex = new UIVertex[4]; UIVertex[] vertex = new UIVertex[4];
vertex[0].position = p1; vertex[0].position = p1;
@@ -120,7 +120,7 @@ namespace xcharts
vertex[3].position = p4; vertex[3].position = p4;
for (int j = 0; j < 4; j++) for (int j = 0; j < 4; j++)
{ {
vertex[j].color = j>=2?toColor:startColor; vertex[j].color = j >= 2 ? toColor : startColor;
vertex[j].uv0 = Vector2.zero; vertex[j].uv0 = Vector2.zero;
} }
vh.AddUIVertexQuad(vertex); vh.AddUIVertexQuad(vertex);
@@ -157,32 +157,26 @@ namespace xcharts
public static void DrawCricle(VertexHelper vh, Vector3 p, float radius, Color color, public static void DrawCricle(VertexHelper vh, Vector3 p, float radius, Color color,
int segments) int segments)
{
DrawSector(vh, p, radius, color, segments, 0, 360);
}
public static void DrawSector(VertexHelper vh, Vector3 p, float radius, Color color,
int segments, float startDegree, float toDegree)
{ {
List<UIVertex> vertexs = new List<UIVertex>(); List<UIVertex> vertexs = new List<UIVertex>();
vh.GetUIVertexStream(vertexs); vh.GetUIVertexStream(vertexs);
float angle = 2 * Mathf.PI / segments;
float cos = Mathf.Cos(angle);
float sin = Mathf.Sin(angle);
float cx = radius, cy = 0;
List<UIVertex> vs = new List<UIVertex>();
segments--;
Vector3 p2, p3; Vector3 p2, p3;
for (int i = 0; i < segments; i++) float startAngle = startDegree * Mathf.Deg2Rad;
float angle = (toDegree-startDegree) * Mathf.Deg2Rad / segments;
p2 = new Vector3(p.x + radius * Mathf.Sin(startAngle), p.y + radius * Mathf.Cos(startAngle));
for (int i = 0; i <= segments; i++)
{ {
p2 = new Vector3(p.x + cx, p.y + cy); float currAngle = startAngle + i * angle;
float temp = cx; p3 = new Vector3(p.x + radius * Mathf.Sin(currAngle), p.y + radius * Mathf.Cos(currAngle));
cx = cos * cx - sin * cy;
cy = sin * temp + cos * cy;
p3 = new Vector3(p.x + cx, p.y + cy);
DrawTriangle(vh, vertexs, p, p2, p3, color); DrawTriangle(vh, vertexs, p, p2, p3, color);
p2 = p3;
} }
p2 = new Vector3(p.x + cx, p.y + cy);
cx = radius;
cy = 0;
p3 = new Vector3(p.x + cx, p.y + cy);
DrawTriangle(vh, vertexs, p, p2, p3, color);
} }

View File

@@ -11,9 +11,8 @@ namespace xcharts
} }
[System.Serializable] [System.Serializable]
public class LineData public class LineInfo
{ {
public float tickness = 0.8f; public float tickness = 0.8f;
[Header("Point")] [Header("Point")]
@@ -30,14 +29,13 @@ namespace xcharts
[Header("Area")] [Header("Area")]
public bool area = false; public bool area = false;
public Color areaStartColor; public Color areaColor;
public Color areaToColor;
} }
public class LineChart : BaseChart public class LineChart : BaseChart
{ {
[SerializeField] [SerializeField]
private LineData lineData; private LineInfo lineInfo;
protected override void Awake() protected override void Awake()
{ {
@@ -76,25 +74,25 @@ namespace xcharts
np = new Vector3(startX + i * scaleWid, zeroY + data.value * coordinateHig / max); np = new Vector3(startX + i * scaleWid, zeroY + data.value * coordinateHig / max);
if (i > 0) if (i > 0)
{ {
if (lineData.smooth) if (lineInfo.smooth)
{ {
var list = ChartUtils.GetBezierList(lp, np, lineData.smoothStyle); var list = ChartUtils.GetBezierList(lp, np, lineInfo.smoothStyle);
Vector3 start, to; Vector3 start, to;
start = list[0]; start = list[0];
for (int k = 1; k < list.Count; k++) for (int k = 1; k < list.Count; k++)
{ {
to = list[k]; to = list[k];
ChartUtils.DrawLine(vh, start, to, lineData.tickness, color); ChartUtils.DrawLine(vh, start, to, lineInfo.tickness, color);
start = to; start = to;
} }
} }
else else
{ {
ChartUtils.DrawLine(vh, lp, np, lineData.tickness, color); ChartUtils.DrawLine(vh, lp, np, lineInfo.tickness, color);
if (lineData.area) if (lineInfo.area)
{ {
ChartUtils.DrawPolygon(vh, lp, np, new Vector3(np.x, zeroY), new Vector3(lp.x, zeroY), ChartUtils.DrawPolygon(vh, lp, np, new Vector3(np.x, zeroY),
lineData.areaStartColor, lineData.areaToColor); new Vector3(lp.x, zeroY), lineInfo.areaColor);
} }
} }
@@ -102,20 +100,21 @@ namespace xcharts
lp = np; lp = np;
} }
// draw point // draw point
if (lineData.showPoint) if (lineInfo.showPoint)
{ {
for (int i = 0; i < series.dataList.Count; i++) for (int i = 0; i < series.dataList.Count; i++)
{ {
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);
switch (lineData.pointType) switch (lineInfo.pointType)
{ {
case PointType.square: case PointType.square:
ChartUtils.DrawPolygon(vh, p, lineData.pointWid, lineData.pointColor); ChartUtils.DrawPolygon(vh, p, lineInfo.pointWid, lineInfo.pointColor);
break; break;
case PointType.cicle: case PointType.cicle:
ChartUtils.DrawCricle(vh, p, lineData.pointWid, lineData.pointColor, (int)lineData.pointWid * 5); ChartUtils.DrawCricle(vh, p, lineInfo.pointWid, lineInfo.pointColor,
(int)lineInfo.pointWid * 5);
break; break;
} }
} }

View File

@@ -0,0 +1,116 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace xcharts
{
[System.Serializable]
public class PieData
{
public string text;
public float value;
}
[System.Serializable]
public class PieInfo
{
public float radius = 80f;
public float space;
public float left;
public float right;
public float top;
public float bottom;
public List<PieData> dataList;
public float GetDataTotal()
{
float total = 0;
foreach(var d in dataList)
{
total += d.value;
}
return total;
}
}
public class PieChart : BaseChart
{
[SerializeField]
private PieInfo pieInfo;
private float pieCenterX = 0f;
private float pieCenterY = 0f;
private float pieRadius = 0;
protected override void Awake()
{
base.Awake();
}
protected override void Update()
{
base.Update();
}
protected override void OnPopulateMesh(VertexHelper vh)
{
base.OnPopulateMesh(vh);
UpdatePieCenter();
float totalDegree = 360;
float startDegree = 0;
float dataTotal = GetDataTotal();
for (int i = 0; i < pieInfo.dataList.Count; i++)
{
if (!legend.IsShowSeries(i)) continue;
float value = pieInfo.dataList[i].value;
float degree = totalDegree * value / dataTotal;
float toDegree = startDegree + degree;
ChartUtils.DrawSector(vh, new Vector3(pieCenterX, pieCenterY), pieRadius, legend.GetColor(i), 360,
startDegree, toDegree);
startDegree = toDegree;
}
}
protected override void OnLegendButtonClicked()
{
base.OnLegendButtonClicked();
}
private float GetDataTotal()
{
float total = 0;
for(int i = 0; i < pieInfo.dataList.Count; i++)
{
if (legend.IsShowSeries(i))
{
total += pieInfo.dataList[i].value;
}
}
return total;
}
private void UpdatePieCenter()
{
float diffX = chartWid - pieInfo.left - pieInfo.right;
float diffY = chartHig - pieInfo.top - pieInfo.bottom;
float diff = Mathf.Min(diffX, diffY);
if(pieInfo.radius <= 0)
{
pieRadius = diff / 3 * 2;
pieCenterX = pieInfo.left + pieRadius;
pieCenterY = pieInfo.bottom + pieRadius;
}
else
{
pieRadius = pieInfo.radius;
pieCenterX = chartWid / 2;
pieCenterY = chartHig / 2;
if (pieInfo.left > 0) pieCenterX = pieInfo.left + pieRadius;
if (pieInfo.right > 0) pieCenterX = chartWid - pieInfo.right - pieRadius;
if (pieInfo.top > 0) pieCenterY = chartHig - pieInfo.top - pieRadius;
if (pieInfo.bottom > 0) pieCenterY = pieInfo.bottom + pieRadius;
}
}
}
}

View File

@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: d44276ba809fd92408b296835f6f7658
timeCreated: 1537541828
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff