增加饼图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()
{
lineChart = transform.Find("line_chart").GetComponent<LineChart>();
lineChart = transform.Find("xchart/line_chart").GetComponent<LineChart>();
}
void Update()

View File

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

View File

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

View File

@@ -62,8 +62,8 @@ namespace xcharts
btnObj.AddComponent<Image>();
btnObj.AddComponent<Button>();
Text txt = AddTextObject("Text", btnObj.transform, font, TextAnchor.MiddleCenter,
Vector2.zero,Vector2.zero, Vector2.zero, sizeDelta);
Text txt = AddTextObject("Text", btnObj.transform, font, TextAnchor.MiddleCenter,
Vector2.zero, Vector2.zero, Vector2.zero, sizeDelta);
txt.text = "Text";
}
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,
Color startColor,Color toColor)
Color startColor, Color toColor)
{
UIVertex[] vertex = new UIVertex[4];
vertex[0].position = p1;
@@ -120,7 +120,7 @@ namespace xcharts
vertex[3].position = p4;
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;
}
vh.AddUIVertexQuad(vertex);
@@ -157,32 +157,26 @@ namespace xcharts
public static void DrawCricle(VertexHelper vh, Vector3 p, float radius, Color color,
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>();
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;
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 temp = cx;
cx = cos * cx - sin * cy;
cy = sin * temp + cos * cy;
p3 = new Vector3(p.x + cx, p.y + cy);
float currAngle = startAngle + i * angle;
p3 = new Vector3(p.x + radius * Mathf.Sin(currAngle), p.y + radius * Mathf.Cos(currAngle));
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]
public class LineData
public class LineInfo
{
public float tickness = 0.8f;
[Header("Point")]
@@ -30,14 +29,13 @@ namespace xcharts
[Header("Area")]
public bool area = false;
public Color areaStartColor;
public Color areaToColor;
public Color areaColor;
}
public class LineChart : BaseChart
{
[SerializeField]
private LineData lineData;
private LineInfo lineInfo;
protected override void Awake()
{
@@ -76,25 +74,25 @@ namespace xcharts
np = new Vector3(startX + i * scaleWid, zeroY + data.value * coordinateHig / max);
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;
start = list[0];
for (int k = 1; k < list.Count; k++)
{
to = list[k];
ChartUtils.DrawLine(vh, start, to, lineData.tickness, color);
ChartUtils.DrawLine(vh, start, to, lineInfo.tickness, color);
start = to;
}
}
else
{
ChartUtils.DrawLine(vh, lp, np, lineData.tickness, color);
if (lineData.area)
ChartUtils.DrawLine(vh, lp, np, lineInfo.tickness, color);
if (lineInfo.area)
{
ChartUtils.DrawPolygon(vh, lp, np, new Vector3(np.x, zeroY), new Vector3(lp.x, zeroY),
lineData.areaStartColor, lineData.areaToColor);
ChartUtils.DrawPolygon(vh, lp, np, new Vector3(np.x, zeroY),
new Vector3(lp.x, zeroY), lineInfo.areaColor);
}
}
@@ -102,20 +100,21 @@ namespace xcharts
lp = np;
}
// draw point
if (lineData.showPoint)
if (lineInfo.showPoint)
{
for (int i = 0; i < series.dataList.Count; i++)
{
SeriesData data = series.dataList[i];
Vector3 p = new Vector3(startX + i * scaleWid, zeroY + data.value * coordinateHig / max);
switch (lineData.pointType)
switch (lineInfo.pointType)
{
case PointType.square:
ChartUtils.DrawPolygon(vh, p, lineData.pointWid, lineData.pointColor);
ChartUtils.DrawPolygon(vh, p, lineInfo.pointWid, lineInfo.pointColor);
break;
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;
}
}

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