折线图增加更多参数配置

This commit is contained in:
monitor1394
2018-09-20 07:10:49 +08:00
parent c5395041b1
commit d0f0da9c83
5 changed files with 157 additions and 94 deletions

View File

@@ -33,7 +33,7 @@ namespace xcharts
if(yAxis.type == AxisType.category)
{
int seriesCount = seriesList.Count;
float scaleWid = coordinateHig / (yAxis.scaleNum - 1);
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 max = GetMaxValue();
@@ -46,7 +46,7 @@ namespace xcharts
{
SeriesData data = series.dataList[i];
float pX = zeroX + coordinate.tickness;
float pY = zeroY + i * coordinateHig / (yAxis.scaleNum - 1);
float pY = zeroY + i * coordinateHig / (yAxis.splitNumber - 1);
float barHig = data.value / max * coordinateWid;
float space = offset + j * (barWid + barData.space);
Vector3 p1 = new Vector3(pX, pY + space + barWid);
@@ -60,7 +60,7 @@ namespace xcharts
else
{
int seriesCount = seriesList.Count;
float scaleWid = coordinateWid / (xAxis.scaleNum - 1);
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 max = GetMaxValue();
@@ -72,7 +72,7 @@ namespace xcharts
for (int i = 0; i < series.dataList.Count; i++)
{
SeriesData data = series.dataList[i];
float pX = zeroX + i * coordinateWid / (xAxis.scaleNum - 1);
float pX = zeroX + i * coordinateWid / (xAxis.splitNumber - 1);
float pY = zeroY + coordinate.tickness;
float barHig = data.value / max * coordinateHig;
float space = offset + j * (barWid + barData.space);

View File

@@ -56,19 +56,23 @@ namespace xcharts
}
[System.Serializable]
public enum Layout
public enum Location
{
left,
right,
top,
bottom
bottom,
start,
middle,
center,
end,
}
[System.Serializable]
public class Axis
{
public AxisType type;
public int scaleNum = 5;
public int splitNumber = 5;
public bool showSplitLine;
public List<string> data;
}
@@ -98,7 +102,7 @@ namespace xcharts
public class Legend
{
public bool show;
public Layout layout;
public Location location;
public float dataWid;
public float dataHig;
public float dataSpace;
@@ -304,8 +308,8 @@ namespace xcharts
yScaleTextList.Clear();
if (yAxis.type == AxisType.value)
{
yAxis.scaleNum = DEFAULT_YSACLE_NUM;
for (int i = 0; i < yAxis.scaleNum; i++)
yAxis.splitNumber = DEFAULT_YSACLE_NUM;
for (int i = 0; i < yAxis.splitNumber; i++)
{
Text txt = ChartUtils.AddTextObject(YSCALE_TEXT_PREFIX + i, transform, font,
TextAnchor.MiddleRight, Vector2.zero, Vector2.zero, new Vector2(1, 0.5f),
@@ -317,8 +321,8 @@ namespace xcharts
}
else
{
yAxis.scaleNum = yAxis.data.Count + 1;
for (int i = 0; i < yAxis.scaleNum - 1; i++)
yAxis.splitNumber = yAxis.data.Count + 1;
for (int i = 0; i < yAxis.splitNumber - 1; i++)
{
Text txt = ChartUtils.AddTextObject(YSCALE_TEXT_PREFIX + i, transform, font,
TextAnchor.MiddleRight, Vector2.zero, Vector2.zero, new Vector2(1, 0.5f),
@@ -335,9 +339,9 @@ namespace xcharts
xScaleTextList.Clear();
if (xAxis.type == AxisType.value)
{
xAxis.scaleNum = DEFAULT_YSACLE_NUM;
float scaleWid = coordinateWid / (xAxis.scaleNum - 1);
for (int i = 0; i < xAxis.scaleNum; i++)
xAxis.splitNumber = DEFAULT_YSACLE_NUM;
float scaleWid = coordinateWid / (xAxis.splitNumber - 1);
for (int i = 0; i < xAxis.splitNumber; i++)
{
Text txt = ChartUtils.AddTextObject(XSCALE_TEXT_PREFIX + i, transform, font,
TextAnchor.MiddleCenter, Vector2.zero, Vector2.zero, new Vector2(1, 0.5f),
@@ -349,9 +353,9 @@ namespace xcharts
}
else
{
xAxis.scaleNum = xAxis.data.Count + 1;
float scaleWid = coordinateWid / (xAxis.scaleNum - 1);
for (int i = 0; i < xAxis.scaleNum - 1; i++)
xAxis.splitNumber = xAxis.data.Count + 1;
float scaleWid = coordinateWid / (xAxis.splitNumber - 1);
for (int i = 0; i < xAxis.splitNumber - 1; i++)
{
Text txt = ChartUtils.AddTextObject(XSCALE_TEXT_PREFIX + i, transform, font,
TextAnchor.MiddleCenter, Vector2.zero, Vector2.zero, new Vector2(1, 0.5f),
@@ -389,19 +393,19 @@ namespace xcharts
private Vector3 GetLegendPosition(int i)
{
int legendCount = legend.dataList.Count;
switch (legend.layout)
switch (legend.location)
{
case Layout.bottom:
case Layout.top:
case Location.bottom:
case Location.top:
float startX = legend.left;
if (startX <= 0)
{
startX = (chartWid -(legendCount *legend.dataWid - (legendCount-1) * legend.dataSpace)) / 2;
}
float posY = legend.layout == Layout.bottom ? legend.bottom : chartHig - legend.top - legend.dataHig;
float posY = legend.location == Location.bottom ? legend.bottom : chartHig - legend.top - legend.dataHig;
return new Vector3(startX + i * (legend.dataWid+legend.dataSpace),posY,0);
case Layout.left:
case Layout.right:
case Location.left:
case Location.right:
float startY =0;
if (legend.top > 0)
{
@@ -411,7 +415,7 @@ namespace xcharts
{
startY = chartHig - (chartHig - (legendCount * legend.dataHig - (legendCount - 1) * legend.dataSpace)) / 2 - legend.dataHig;
}
float posX = legend.layout == Layout.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);
default:break;
}
@@ -420,7 +424,7 @@ namespace xcharts
private Vector3 GetYScalePosition(int i)
{
float scaleWid = coordinateHig / (yAxis.scaleNum - 1);
float scaleWid = coordinateHig / (yAxis.splitNumber - 1);
if (yAxis.type == AxisType.value)
{
return new Vector3(zeroX - coordinate.scaleLen - 2f,
@@ -435,7 +439,7 @@ namespace xcharts
private Vector3 GetXScalePosition(int i)
{
float scaleWid = coordinateWid / (xAxis.scaleNum - 1);
float scaleWid = coordinateWid / (xAxis.splitNumber - 1);
if (xAxis.type == AxisType.value)
{
return new Vector3(zeroX + (i + 1 - 0.5f) * scaleWid, zeroY - coordinate.scaleLen - 10, 0);
@@ -533,7 +537,7 @@ namespace xcharts
checkLegend.right != legend.right ||
checkLegend.bottom != legend.bottom ||
checkLegend.top != legend.top ||
checkLegend.layout != legend.layout ||
checkLegend.location != legend.location ||
checkLegend.show != legend.show)
{
checkLegend.dataWid = legend.dataWid;
@@ -543,7 +547,7 @@ namespace xcharts
checkLegend.right = legend.right;
checkLegend.bottom = legend.bottom;
checkLegend.top = legend.top;
checkLegend.layout = legend.layout;
checkLegend.location = legend.location;
checkLegend.show = legend.show;
OnLegendChanged();
}
@@ -552,14 +556,14 @@ namespace xcharts
protected virtual void OnCoordinateSize()
{
//update yScale pos
for (int i = 0; i < yAxis.scaleNum; i++)
for (int i = 0; i < yAxis.splitNumber; i++)
{
if (i < yScaleTextList.Count && yScaleTextList[i])
{
yScaleTextList[i].transform.localPosition = GetYScalePosition(i);
}
}
for (int i = 0; i < xAxis.scaleNum; i++)
for (int i = 0; i < xAxis.splitNumber; i++)
{
if (i < xScaleTextList.Count && xScaleTextList[i])
{
@@ -643,19 +647,19 @@ namespace xcharts
{
if (!coordinate.show) return;
// draw scale
for (int i = 1; i < yAxis.scaleNum; i++)
for (int i = 1; i < yAxis.splitNumber; i++)
{
float pX = zeroX - coordinate.scaleLen;
float pY = zeroY + i * coordinateHig / (yAxis.scaleNum - 1);
float pY = zeroY + i * coordinateHig / (yAxis.splitNumber - 1);
ChartUtils.DrawLine(vh, new Vector3(pX, pY), new Vector3(zeroX, pY), coordinate.tickness, Color.white);
if (yAxis.showSplitLine)
{
ChartUtils.DrawLine(vh, new Vector3(zeroX, pY), new Vector3(zeroX + coordinateWid, pY), coordinate.tickness, Color.grey);
}
}
for (int i = 1; i < xAxis.scaleNum; i++)
for (int i = 1; i < xAxis.splitNumber; i++)
{
float pX = zeroX + i * coordinateWid / (xAxis.scaleNum - 1);
float pX = zeroX + i * coordinateWid / (xAxis.splitNumber - 1);
float pY = zeroY - coordinate.scaleLen - 2;
ChartUtils.DrawLine(vh, new Vector3(pX, zeroY), new Vector3(pX, pY), coordinate.tickness, Color.white);
if (xAxis.showSplitLine)

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>();
@@ -101,11 +101,17 @@ namespace xcharts
Vector3 p2 = new Vector3(p.x + size, p.y - size);
Vector3 p3 = new Vector3(p.x + size, p.y + size);
Vector3 p4 = new Vector3(p.x - size, p.y + size);
DrawPolygon(vh, p1, p2, p3, p4, color);
DrawPolygon(vh, p1, p2, p3, p4, color, color);
}
public static void DrawPolygon(VertexHelper vh, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4,
Color color)
{
DrawPolygon(vh, p1, p2, p3, p4, color, color);
}
public static void DrawPolygon(VertexHelper vh, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4,
Color startColor,Color toColor)
{
UIVertex[] vertex = new UIVertex[4];
vertex[0].position = p1;
@@ -114,7 +120,7 @@ namespace xcharts
vertex[3].position = p4;
for (int j = 0; j < 4; j++)
{
vertex[j].color = color;
vertex[j].color = j>=2?toColor:startColor;
vertex[j].uv0 = Vector2.zero;
}
vh.AddUIVertexQuad(vertex);
@@ -180,7 +186,7 @@ namespace xcharts
}
public static List<Vector3> GetBezierList(Vector3 sp, Vector3 ep, int k = 2)
public static List<Vector3> GetBezierList(Vector3 sp, Vector3 ep, float k = 2.0f)
{
Vector3 dir = (ep - sp).normalized;
float dist = Vector3.Distance(sp, ep);
@@ -203,7 +209,8 @@ namespace xcharts
return list;
}
public static List<Vector3> GetBezierList2(Vector3 sp, Vector3 ep, int segment, Vector3 cp, Vector3 cp2)
public static List<Vector3> GetBezierList2(Vector3 sp, Vector3 ep, int segment, Vector3 cp,
Vector3 cp2)
{
List<Vector3> list = new List<Vector3>();
for (int i = 0; i < segment; i++)

View File

@@ -5,13 +5,35 @@ using UnityEngine.UI;
namespace xcharts
{
[System.Serializable]
public enum PointType
{
square,
cicle
}
[System.Serializable]
public class LineData
{
public bool smooth = false;
public bool area = false;
public float pointWid = 1;
public float tickness = 0.8f;
[Header("Point")]
public bool showPoint = true;
public PointType pointType = PointType.square;
public float pointWid = 1.0f;
public Color pointColor = Color.white;
[Header("Smooth")]
public bool smooth = false;
[Range(1f, 10f)]
public float smoothStyle = 2f;
[Header("Area")]
public bool area = false;
public Color areaStartColor;
public Color areaToColor;
}
public class LineChart : BaseChart
@@ -34,7 +56,7 @@ namespace xcharts
base.OnPopulateMesh(vh);
int seriesCount = seriesList.Count;
float max = GetMaxValue();
float scaleWid = coordinateWid / (xAxis.scaleNum - 1);
float scaleWid = coordinateWid / (xAxis.splitNumber - 1);
for (int j = 0; j < seriesCount; j++)
{
if (!legend.IsShowSeries(j)) continue;
@@ -52,10 +74,10 @@ namespace xcharts
{
if (lineData.smooth)
{
var list = ChartUtils.GetBezierList(lp, np);
var list = ChartUtils.GetBezierList(lp, np, lineData.smoothStyle);
Vector3 start, to;
start = list[0];
for(int k = 1; k < list.Count; k++)
for (int k = 1; k < list.Count; k++)
{
to = list[k];
ChartUtils.DrawLine(vh, start, to, lineData.tickness, color);
@@ -67,20 +89,32 @@ namespace xcharts
ChartUtils.DrawLine(vh, lp, np, lineData.tickness, color);
if (lineData.area)
{
ChartUtils.DrawPolygon(vh, lp, np, new Vector3(np.x, zeroY), new Vector3(lp.x, zeroY), color);
ChartUtils.DrawPolygon(vh, lp, np, new Vector3(np.x, zeroY), new Vector3(lp.x, zeroY),
lineData.areaStartColor,lineData.areaToColor);
}
}
}
lp = np;
}
// draw point
for (int i = 0; i < series.dataList.Count; i++)
if (lineData.showPoint)
{
SeriesData data = series.dataList[i];
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);
ChartUtils.DrawPolygon(vh, p, lineData.pointWid, Color.white);
Vector3 p = new Vector3(startX + i * scaleWid, zeroY + data.value * coordinateHig / max);
switch (lineData.pointType)
{
case PointType.square:
ChartUtils.DrawPolygon(vh, p, lineData.pointWid, lineData.pointColor);
break;
case PointType.cicle:
ChartUtils.DrawCricle(vh, p, lineData.pointWid, lineData.pointColor, (int)lineData.pointWid * 5);
break;
}
}
}
}