折线图优化

This commit is contained in:
monitor1394
2019-05-25 00:39:33 +08:00
parent 187c54b5fa
commit 4093756afc
4 changed files with 4688 additions and 2345 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -54,14 +54,14 @@ namespace XCharts
{
var seriesCurrHig = new Dictionary<int, float>();
var serieList = stackSeries[j];
for (int n = 0; n < serieList.Count; n++)
{
Serie serie = serieList[n];
if (!IsActive(serie.name)) continue;
List<Vector3> lastPoints = new List<Vector3>();
List<Vector3> lastSmoothPoints = new List<Vector3>();
Color color = m_ThemeInfo.GetColor(serieCount);
Vector3 lp = Vector3.zero;
Vector3 np = Vector3.zero;
@@ -72,16 +72,16 @@ namespace XCharts
dataCount = (maxCount - minShowDataNumber);
if (m_Line.area && points.Count > 0)
{
if(!m_Line.smooth && points.Count > 0)
if (!m_Line.smooth && points.Count > 0)
{
for (int m = points.Count - dataCount; m < points.Count; m++)
{
lastPoints.Add(points[m]);
}
}
else if(m_Line.smooth && smoothPoints.Count > 0)
else if (m_Line.smooth && smoothPoints.Count > 0)
{
for(int m = 0; m < smoothPoints.Count; m++)
for (int m = 0; m < smoothPoints.Count; m++)
{
lastSmoothPoints.Add(smoothPoints[m]);
}
@@ -99,9 +99,9 @@ namespace XCharts
float pX = startX + i * scaleWid;
float pY = seriesCurrHig[i] + zeroY + m_Coordinate.tickness;
float dataHig = value / (maxValue - minValue) * coordinateHig;
np = new Vector3(pX, pY+ dataHig);
np = new Vector3(pX, pY + dataHig);
if (i > 0)
{
if (m_Line.smooth)
@@ -120,14 +120,18 @@ namespace XCharts
Vector3 alp = new Vector3(start.x, start.y - m_Line.tickness);
Vector3 anp = new Vector3(to.x, to.y - m_Line.tickness);
Vector3 tnp = serieCount > 0 ?
(smoothPointCount>lastSmoothPoints.Count-1?
new Vector3(lastSmoothPoints[lastSmoothPoints.Count - 1].x, lastSmoothPoints[lastSmoothPoints.Count - 1].y + m_Line.tickness):
new Vector3(lastSmoothPoints[smoothPointCount].x, lastSmoothPoints[smoothPointCount].y + m_Line.tickness)) :
(smoothPointCount > lastSmoothPoints.Count - 1 ?
new Vector3(lastSmoothPoints[lastSmoothPoints.Count - 1].x,
lastSmoothPoints[lastSmoothPoints.Count - 1].y + m_Line.tickness) :
new Vector3(lastSmoothPoints[smoothPointCount].x,
lastSmoothPoints[smoothPointCount].y + m_Line.tickness)) :
new Vector3(to.x, zeroY + m_Coordinate.tickness);
Vector3 tlp = serieCount > 0 ?
(smoothPointCount>lastSmoothPoints.Count-1?
new Vector3(lastSmoothPoints[lastSmoothPoints.Count - 2].x, lastSmoothPoints[lastSmoothPoints.Count - 2].y + m_Line.tickness):
new Vector3(lastSmoothPoints[smoothPointCount - 1].x, lastSmoothPoints[smoothPointCount - 1].y + m_Line.tickness)) :
(smoothPointCount > lastSmoothPoints.Count - 1 ?
new Vector3(lastSmoothPoints[lastSmoothPoints.Count - 2].x,
lastSmoothPoints[lastSmoothPoints.Count - 2].y + m_Line.tickness) :
new Vector3(lastSmoothPoints[smoothPointCount - 1].x,
lastSmoothPoints[smoothPointCount - 1].y + m_Line.tickness)) :
new Vector3(start.x, zeroY + m_Coordinate.tickness);
Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f);
ChartHelper.DrawPolygon(vh, alp, anp, tnp, tlp, areaColor);
@@ -141,16 +145,30 @@ namespace XCharts
ChartHelper.DrawLine(vh, lp, np, m_Line.tickness, color);
if (m_Line.area)
{
Vector3 alp = new Vector3(lp.x,lp.y-m_Line.tickness);
Vector3 anp = new Vector3(np.x,np.y-m_Line.tickness);
Vector3 tnp = serieCount > 0 ?
new Vector3(lastPoints[i].x, lastPoints[i].y + m_Line.tickness) :
new Vector3(np.x, zeroY + m_Coordinate.tickness);
Vector3 tlp = serieCount > 0 ?
new Vector3(lastPoints[i-1].x, lastPoints[i-1].y + m_Line.tickness) :
new Vector3(lp.x, zeroY + m_Coordinate.tickness);
Vector3 alp = new Vector3(lp.x, lp.y - m_Line.tickness);
Vector3 anp = new Vector3(np.x, np.y - m_Line.tickness);
Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f);
ChartHelper.DrawPolygon(vh, alp, anp, tnp, tlp, areaColor);
var cross = ChartHelper.GetIntersection(lp, np, new Vector3(zeroX, zeroY),
new Vector3(zeroX + coordinateWid, zeroY));
if (cross == Vector3.zero)
{
Vector3 tnp = serieCount > 0 ?
new Vector3(lastPoints[i].x, lastPoints[i].y + m_Line.tickness) :
new Vector3(np.x, zeroY + m_Coordinate.tickness);
Vector3 tlp = serieCount > 0 ?
new Vector3(lastPoints[i - 1].x, lastPoints[i - 1].y + m_Line.tickness) :
new Vector3(lp.x, zeroY + m_Coordinate.tickness);
ChartHelper.DrawPolygon(vh, alp, anp, tnp, tlp, areaColor);
}
else
{
Vector3 cross1 = new Vector3(cross.x, cross.y + (alp.y > zeroY ? m_Coordinate.tickness : -m_Coordinate.tickness));
Vector3 cross2 = new Vector3(cross.x, cross.y + (anp.y > zeroY ? m_Coordinate.tickness : -m_Coordinate.tickness));
Vector3 xp1 = new Vector3(alp.x, zeroY + (alp.y > zeroY ? m_Coordinate.tickness : -m_Coordinate.tickness));
Vector3 xp2 = new Vector3(anp.x, zeroY + (anp.y > zeroY ? m_Coordinate.tickness : -m_Coordinate.tickness));
ChartHelper.DrawTriangle(vh, alp, cross1, xp1, areaColor);
ChartHelper.DrawTriangle(vh, anp, cross2, xp2, areaColor);
}
}
}
}
@@ -174,7 +192,7 @@ namespace XCharts
{
Vector3 p = points[i];
float pointWid = m_Line.pointWidth;
if (m_Tooltip.show && i% dataCount == m_Tooltip.dataIndex - 1)
if (m_Tooltip.show && i % dataCount == m_Tooltip.dataIndex - 1)
{
pointWid = pointWid * 1.8f;
}
@@ -198,7 +216,8 @@ namespace XCharts
if (m_Tooltip.show && m_Tooltip.dataIndex > 0)
{
float splitWidth = m_XAxis.GetSplitWidth(coordinateWid);
float px = zeroX + (m_Tooltip.dataIndex - 1) * splitWidth + (m_XAxis.boundaryGap ? splitWidth / 2 : 0);
float px = zeroX + (m_Tooltip.dataIndex - 1) * splitWidth
+ (m_XAxis.boundaryGap ? splitWidth / 2 : 0);
Vector2 sp = new Vector2(px, coordinateY);
Vector2 ep = new Vector2(px, coordinateY + coordinateHig);
ChartHelper.DrawLine(vh, sp, ep, m_Coordinate.tickness, m_ThemeInfo.tooltipFlagAreaColor);
@@ -211,16 +230,22 @@ namespace XCharts
int seriesCount = stackSeries.Count;
float scaleWid = m_YAxis.GetDataWidth(coordinateHig);
int serieCount = 0;
List<Vector3> pointList = new List<Vector3>();
List<Color> pointColorList = new List<Color>();
List<Vector3> points = new List<Vector3>();
List<Vector3> smoothPoints = new List<Vector3>();
List<Color> colorList = new List<Color>();
int dataCount = 0;
for (int j = 0; j < seriesCount; j++)
{
var seriesCurrHig = new Dictionary<int, float>();
var serieList = stackSeries[j];
for (int n = 0; n < serieList.Count; n++)
{
Serie serie = serieList[n];
if (!m_Legend.IsActive(serie.name)) continue;
if (!IsActive(serie.name)) continue;
List<Vector3> lastPoints = new List<Vector3>();
List<Vector3> lastSmoothPoints = new List<Vector3>();
Color color = m_ThemeInfo.GetColor(serieCount);
Vector3 lp = Vector3.zero;
Vector3 np = Vector3.zero;
@@ -228,6 +253,26 @@ namespace XCharts
int maxCount = maxShowDataNumber > 0 ?
(maxShowDataNumber > serie.data.Count ? serie.data.Count : maxShowDataNumber)
: serie.data.Count;
dataCount = (maxCount - minShowDataNumber);
if (m_Line.area && points.Count > 0)
{
if (!m_Line.smooth && points.Count > 0)
{
for (int m = points.Count - dataCount; m < points.Count; m++)
{
lastPoints.Add(points[m]);
}
}
else if (m_Line.smooth && smoothPoints.Count > 0)
{
for (int m = 0; m < smoothPoints.Count; m++)
{
lastSmoothPoints.Add(smoothPoints[m]);
}
smoothPoints.Clear();
}
}
int smoothPointCount = 1;
for (int i = minShowDataNumber; i < maxCount; i++)
{
if (!seriesCurrHig.ContainsKey(i))
@@ -238,24 +283,43 @@ namespace XCharts
float pY = startY + i * scaleWid;
float pX = seriesCurrHig[i] + zeroY + m_Coordinate.tickness;
float dataHig = value / (maxValue - minValue) * coordinateWid;
seriesCurrHig[i] += dataHig;
np = new Vector3(pX+ dataHig, pY);
if (m_Line.point)
{
pointList.Add(np);
pointColorList.Add(color);
}
np = new Vector3(pX + dataHig, pY);
if (i > 0)
{
if (m_Line.smooth)
{
var list = ChartHelper.GetBezierList(lp, np, m_Line.smoothStyle);
var list = ChartHelper.GetBezierListVertical(lp, np, m_Line.smoothStyle);
Vector3 start, to;
start = list[0];
for (int k = 1; k < list.Length; k++)
{
smoothPoints.Add(list[k]);
to = list[k];
ChartHelper.DrawLine(vh, start, to, m_Line.tickness, color);
if (m_Line.area)
{
Vector3 alp = new Vector3(start.x, start.y - m_Line.tickness);
Vector3 anp = new Vector3(to.x, to.y - m_Line.tickness);
Vector3 tnp = serieCount > 0 ?
(smoothPointCount > lastSmoothPoints.Count - 1 ?
new Vector3(lastSmoothPoints[lastSmoothPoints.Count - 1].x,
lastSmoothPoints[lastSmoothPoints.Count - 1].y + m_Line.tickness) :
new Vector3(lastSmoothPoints[smoothPointCount].x,
lastSmoothPoints[smoothPointCount].y + m_Line.tickness)) :
new Vector3(zeroX + m_Coordinate.tickness, to.y);
Vector3 tlp = serieCount > 0 ?
(smoothPointCount > lastSmoothPoints.Count - 1 ?
new Vector3(lastSmoothPoints[lastSmoothPoints.Count - 2].x,
lastSmoothPoints[lastSmoothPoints.Count - 2].y + m_Line.tickness) :
new Vector3(lastSmoothPoints[smoothPointCount - 1].x,
lastSmoothPoints[smoothPointCount - 1].y + m_Line.tickness)) :
new Vector3(zeroX + m_Coordinate.tickness, start.y);
Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f);
ChartHelper.DrawPolygon(vh, alp, anp, tnp, tlp, areaColor);
}
smoothPointCount++;
start = to;
}
}
@@ -264,14 +328,41 @@ namespace XCharts
ChartHelper.DrawLine(vh, lp, np, m_Line.tickness, color);
if (m_Line.area)
{
ChartHelper.DrawPolygon(vh, lp, np, new Vector3(np.x, zeroY),
new Vector3(lp.x, zeroY), color);
Vector3 alp = new Vector3(lp.x, lp.y - m_Line.tickness);
Vector3 anp = new Vector3(np.x, np.y - m_Line.tickness);
Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f);
var cross = ChartHelper.GetIntersection(lp, np, new Vector3(zeroX, zeroY),
new Vector3(zeroX, zeroY + coordinateHig));
if (cross == Vector3.zero)
{
Vector3 tnp = serieCount > 0 ?
new Vector3(lastPoints[i].x, lastPoints[i].y + m_Line.tickness) :
new Vector3(zeroX + m_Coordinate.tickness, np.y);
Vector3 tlp = serieCount > 0 ?
new Vector3(lastPoints[i - 1].x, lastPoints[i - 1].y + m_Line.tickness) :
new Vector3(zeroX + m_Coordinate.tickness, lp.y);
ChartHelper.DrawPolygon(vh, alp, anp, tnp, tlp, areaColor);
}
else
{
Vector3 cross1 = new Vector3(cross.x + (alp.x > zeroX ? m_Coordinate.tickness : -m_Coordinate.tickness), cross.y);
Vector3 cross2 = new Vector3(cross.x + (anp.x > zeroX ? m_Coordinate.tickness : -m_Coordinate.tickness), cross.y);
Vector3 xp1 = new Vector3(zeroX + (alp.x > zeroX ? m_Coordinate.tickness : -m_Coordinate.tickness), alp.y);
Vector3 xp2 = new Vector3(zeroX + (anp.x > zeroX ? m_Coordinate.tickness : -m_Coordinate.tickness), anp.y);
ChartHelper.DrawTriangle(vh, alp, cross1, xp1, areaColor);
ChartHelper.DrawTriangle(vh, anp, cross2, xp2, areaColor);
}
}
}
}
if (m_Line.point)
{
points.Add(np);
colorList.Add(color);
}
seriesCurrHig[i] += dataHig;
lp = np;
}
if (serie.show)
{
serieCount++;
@@ -280,112 +371,35 @@ namespace XCharts
// draw point
if (m_Line.point)
{
for (int i = 0; i < pointList.Count; i++)
for (int i = 0; i < points.Count; i++)
{
Vector3 p = pointList[i];
Color color = pointColorList[i];
Vector3 p = points[i];
float pointWid = m_Line.pointWidth;
if (m_Tooltip.show && i == m_Tooltip.dataIndex - 1)
if (m_Tooltip.show && i % dataCount == m_Tooltip.dataIndex - 1)
{
pointWid = pointWid * 1.8f;
}
if (m_Theme == Theme.Dark)
{
ChartHelper.DrawCricle(vh, p, pointWid, color,
ChartHelper.DrawCricle(vh, p, pointWid, colorList[i],
(int)m_Line.pointWidth * 5);
}
else
{
ChartHelper.DrawCricle(vh, p, pointWid, Color.white);
ChartHelper.DrawDoughnut(vh, p, pointWid - m_Line.tickness,
pointWid, 0, 360, color);
pointWid, 0, 360, colorList[i]);
}
}
}
}
//int seriesCount = m_Series.Count;
//float scaleWid = m_YAxis.GetDataWidth(coordinateHig);
//for (int j = 0; j < seriesCount; j++)
//{
// if (!IsActive(j)) continue;
// Serie serie = m_Series.series[j];
// Color32 color = m_ThemeInfo.GetColor(j);
// Vector3 lp = Vector3.zero;
// Vector3 np = Vector3.zero;
// float startY = coordinateY + (m_YAxis.boundaryGap ? scaleWid / 2 : 0);
// int maxCount = maxShowDataNumber > 0 ?
// (maxShowDataNumber > serie.data.Count ? serie.data.Count : maxShowDataNumber)
// : serie.data.Count;
// for (int i = minShowDataNumber; i < maxCount; i++)
// {
// float value = serie.data[i];
// float dataHig = coordinateX + (value - minValue) / (maxValue - minValue) * coordinateWid;
// np = new Vector3(dataHig,startY + i * scaleWid);
// if (i > 0)
// {
// if (m_Line.smooth)
// {
// var list = ChartHelper.GetBezierList(lp, np, m_Line.smoothStyle);
// Vector3 start, to;
// start = list[0];
// for (int k = 1; k < list.Length; k++)
// {
// to = list[k];
// ChartHelper.DrawLine(vh, start, to, m_Line.tickness, color);
// start = to;
// }
// }
// else
// {
// ChartHelper.DrawLine(vh, lp, np, m_Line.tickness, color);
// if (m_Line.area)
// {
// ChartHelper.DrawPolygon(vh, lp, np, new Vector3(np.x, zeroY),
// new Vector3(lp.x, zeroY), color);
// }
// }
// }
// lp = np;
// }
// // draw point
// if (m_Line.point)
// {
// for (int i = 0; i < serie.data.Count; i++)
// {
// float value = serie.data[i];
// float dataHig = coordinateX + (value - minValue) / (maxValue - minValue) * coordinateWid;
// Vector3 p = new Vector3(dataHig,startY + i * scaleWid);
// float pointWid = m_Line.pointWidth;
// if (m_Tooltip.show && i == m_Tooltip.dataIndex - 1)
// {
// pointWid = pointWid * 1.8f;
// }
// if (m_Theme == Theme.Dark)
// {
// ChartHelper.DrawCricle(vh, p, pointWid, color,
// (int)m_Line.pointWidth * 5);
// }
// else
// {
// ChartHelper.DrawCricle(vh, p, pointWid, Color.white);
// ChartHelper.DrawDoughnut(vh, p, pointWid - m_Line.tickness,
// pointWid, 0, 360, color);
// }
// }
// }
//}
//draw tooltip line
if (m_Tooltip.show && m_Tooltip.dataIndex > 0)
{
float splitWidth = m_YAxis.GetSplitWidth(coordinateHig);
float pY = zeroY + (m_Tooltip.dataIndex - 1) * splitWidth + (m_YAxis.boundaryGap ? splitWidth / 2 : 0);
Vector2 sp = new Vector2(coordinateX,pY);
Vector2 sp = new Vector2(coordinateX, pY);
Vector2 ep = new Vector2(coordinateX + coordinateWid, pY);
ChartHelper.DrawLine(vh, sp, ep, m_Coordinate.tickness, m_ThemeInfo.tooltipFlagAreaColor);
}

View File

@@ -318,6 +318,18 @@ namespace XCharts
return GetBezierList2(sp, ep, segment, cp1, cp2);
}
public static Vector3[] GetBezierListVertical(Vector3 sp, Vector3 ep, float k = 2.0f)
{
Vector3 dir = (ep - sp).normalized;
float dist = Vector3.Distance(sp, ep);
Vector3 cp1 = sp + dist / k * dir * 1;
Vector3 cp2 = sp + dist / k * dir * (k - 1);
cp1.y = sp.y;
cp2.y = ep.y;
int segment = (int)(dist / 0.3f);
return GetBezierList2(sp, ep, segment, cp2, cp1);
}
public static List<Vector3> GetBezierList(Vector3 sp, Vector3 ep, int segment, Vector3 cp)
{
List<Vector3> list = new List<Vector3>();
@@ -463,7 +475,7 @@ namespace XCharts
if (mm > 10)
{
mm = bigger - bigger % (Mathf.Pow(10, n));
mm += max >0 ? Mathf.Pow(10, n) : -Mathf.Pow(10, n);
mm += max > 0 ? Mathf.Pow(10, n) : -Mathf.Pow(10, n);
}
if (max < 0) return (int)-mm;
else return (int)mm;
@@ -502,5 +514,79 @@ namespace XCharts
trigger.triggers.Clear();
trigger.triggers.Add(entry1);
}
//获取两直线交点
public static Vector3 GetIntersection(Vector3 lineAStart, Vector3 lineAEnd, Vector3 lineBStart, Vector3 lineBEnd)
{
float x1 = lineAStart.x, y1 = lineAStart.y;
float x2 = lineAEnd.x, y2 = lineAEnd.y;
float x3 = lineBStart.x, y3 = lineBStart.y;
float x4 = lineBEnd.x, y4 = lineBEnd.y;
//equations of the form x=c (two vertical lines)
if (x1 == x2 && x3 == x4 && x1 == x3)
{
return Vector3.zero;
}
//equations of the form y=c (two horizontal lines)
if (y1 == y2 && y3 == y4 && y1 == y3)
{
return Vector3.zero;
}
//equations of the form x=c (two vertical lines)
if (x1 == x2 && x3 == x4)
{
return Vector3.zero;
}
//equations of the form y=c (two horizontal lines)
if (y1 == y2 && y3 == y4)
{
return Vector3.zero;
}
float x, y;
if (x1 == x2)
{
float m2 = (y4 - y3) / (x4 - x3);
float c2 = -m2 * x3 + y3;
x = x1;
y = c2 + m2 * x1;
}
else if (x3 == x4)
{
float m1 = (y2 - y1) / (x2 - x1);
float c1 = -m1 * x1 + y1;
x = x3;
y = c1 + m1 * x3;
}
else
{
//compute slope of line 1 (m1) and c2
float m1 = (y2 - y1) / (x2 - x1);
float c1 = -m1 * x1 + y1;
//compute slope of line 2 (m2) and c2
float m2 = (y4 - y3) / (x4 - x3);
float c2 = -m2 * x3 + y3;
//solving equations (3) & (4) => x = (c1-c2)/(m2-m1)
//plugging x value in equation (4) => y = c2 + m2 * x
x = (c1 - c2) / (m2 - m1);
y = c2 + m2 * x;
}
if (IsInsideLine(lineAStart, lineAEnd, x, y) &&
IsInsideLine(lineBStart, lineBEnd, x, y))
{
return new Vector3(x, y, 0);
}
return Vector3.zero;
}
private static bool IsInsideLine(Vector3 start, Vector3 end, float x, float y)
{
return ((x >= start.x && x <= end.x)
|| (x >= end.x && x <= start.x))
&& ((y >= start.y && y <= end.y)
|| (y >= end.y && y <= start.y));
}
}
}

View File

@@ -16,12 +16,13 @@ UGUI图表库风格参考了[`ECharts`](https://www.echartsjs.com/examples/#c
1. 基础折线图
2. 负数数值轴+自定义最大最小刻度
3. XY轴互换
4. 贝塞尔曲线平滑
5. 折线图堆叠+图例
6. 堆叠区域图
7. 面积
8. 动态数据
9. 大数据量面积图
4. XY轴互换+区域堆叠
5. 贝塞尔曲线平滑
6. 折线图堆叠+图例
7. 堆叠区域
8. 面积图
9. 动态数据
10. 大数据量面积图
* 柱状图
1. 基础柱状图
2. 负数数值轴+自定义最大最小刻度