优化坐标系外裁剪

This commit is contained in:
monitor1394
2020-04-19 10:03:45 +08:00
parent b689a18a01
commit 3df6465764
4 changed files with 68 additions and 46 deletions

View File

@@ -267,7 +267,7 @@ namespace XCharts
[SerializeField] [Range(1, 10)] private int m_ShowDataDimension; [SerializeField] [Range(1, 10)] private int m_ShowDataDimension;
[SerializeField] private bool m_ShowDataName; [SerializeField] private bool m_ShowDataName;
[SerializeField] private bool m_ShowDataIcon; [SerializeField] private bool m_ShowDataIcon;
[SerializeField] private bool m_Clip = true; [SerializeField] private bool m_Clip = false;
[SerializeField] private bool m_Ignore = false; [SerializeField] private bool m_Ignore = false;
[SerializeField] private float m_IgnoreValue = 0; [SerializeField] private float m_IgnoreValue = 0;
[SerializeField] private RadarType m_RadarType = RadarType.Multiple; [SerializeField] private RadarType m_RadarType = RadarType.Multiple;

View File

@@ -1728,9 +1728,7 @@ namespace XCharts
protected void CheckClipAndDrawPolygon(VertexHelper vh, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, protected void CheckClipAndDrawPolygon(VertexHelper vh, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4,
Color32 color, bool clip) Color32 color, bool clip)
{ {
if (!IsInChart(p1) || !IsInChart(p2) || !IsInChart(p3) || !IsInChart(p4)) return; CheckClipAndDrawPolygon(vh, p1, p2, p3, p4, color, color, clip);
if (!clip || (clip && (IsInCooridate(p1) || IsInCooridate(p2) || IsInCooridate(p3) || IsInCooridate(p4))))
ChartDrawer.DrawPolygon(vh, p1, p2, p3, p4, color, color);
} }
protected void CheckClipAndDrawPolygon(VertexHelper vh, Vector3 p, float radius, Color32 color, protected void CheckClipAndDrawPolygon(VertexHelper vh, Vector3 p, float radius, Color32 color,
@@ -1744,10 +1742,17 @@ namespace XCharts
protected void CheckClipAndDrawPolygon(VertexHelper vh, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, protected void CheckClipAndDrawPolygon(VertexHelper vh, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4,
Color32 startColor, Color32 toColor, bool clip) Color32 startColor, Color32 toColor, bool clip)
{ {
p1 = ClampInCoordinate(p1); p1 = ClampInChart(p1);
p2 = ClampInCoordinate(p2); p2 = ClampInChart(p2);
p3 = ClampInCoordinate(p3); p3 = ClampInChart(p3);
p4 = ClampInCoordinate(p4); p4 = ClampInChart(p4);
if (clip)
{
p1 = ClampInCoordinate(p1);
p2 = ClampInCoordinate(p2);
p3 = ClampInCoordinate(p3);
p4 = ClampInCoordinate(p4);
}
if (!clip || (clip && (IsInCooridate(p1) && IsInCooridate(p2) && IsInCooridate(p3) && IsInCooridate(p4)))) if (!clip || (clip && (IsInCooridate(p1) && IsInCooridate(p2) && IsInCooridate(p3) && IsInCooridate(p4))))
ChartDrawer.DrawPolygon(vh, p1, p2, p3, p4, startColor, toColor); ChartDrawer.DrawPolygon(vh, p1, p2, p3, p4, startColor, toColor);
} }
@@ -1755,10 +1760,17 @@ namespace XCharts
protected void CheckClipAndDrawPolygon(VertexHelper vh, ref Vector3 p1, ref Vector3 p2, ref Vector3 p3, ref Vector3 p4, protected void CheckClipAndDrawPolygon(VertexHelper vh, ref Vector3 p1, ref Vector3 p2, ref Vector3 p3, ref Vector3 p4,
Color32 startColor, Color32 toColor, bool clip) Color32 startColor, Color32 toColor, bool clip)
{ {
p1 = ClampInCoordinate(p1); p1 = ClampInChart(p1);
p2 = ClampInCoordinate(p2); p2 = ClampInChart(p2);
p3 = ClampInCoordinate(p3); p3 = ClampInChart(p3);
p4 = ClampInCoordinate(p4); p4 = ClampInChart(p4);
if (clip)
{
p1 = ClampInCoordinate(p1);
p2 = ClampInCoordinate(p2);
p3 = ClampInCoordinate(p3);
p4 = ClampInCoordinate(p4);
}
if (!clip || (clip && (IsInCooridate(p1) && IsInCooridate(p2) && IsInCooridate(p3) && IsInCooridate(p4)))) if (!clip || (clip && (IsInCooridate(p1) && IsInCooridate(p2) && IsInCooridate(p3) && IsInCooridate(p4))))
ChartDrawer.DrawPolygon(vh, p1, p2, p3, p4, startColor, toColor); ChartDrawer.DrawPolygon(vh, p1, p2, p3, p4, startColor, toColor);
} }

View File

@@ -70,8 +70,9 @@ namespace XCharts
float value = showData[i].GetCurrData(1, dataChangeDuration, xAxis.inverse); float value = showData[i].GetCurrData(1, dataChangeDuration, xAxis.inverse);
float borderWidth = value == 0 ? 0 : itemStyle.runtimeBorderWidth; float borderWidth = value == 0 ? 0 : itemStyle.runtimeBorderWidth;
if (showData[i].IsDataChanged()) dataChanging = true; if (showData[i].IsDataChanged()) dataChanging = true;
float pX = seriesHig[i] + coordinateX + xAxis.runtimeZeroXOffset + (value < 0 ? -1 : 1) * yAxis.axisLine.width; float axisLineWidth = (value < 0 ? -1 : 1) * yAxis.axisLine.width;
float pY = coordinateY + +i * categoryWidth; float pX = seriesHig[i] + coordinateX + xAxis.runtimeZeroXOffset + axisLineWidth;
float pY = coordinateY + i * categoryWidth;
if (!yAxis.boundaryGap) pY -= categoryWidth / 2; if (!yAxis.boundaryGap) pY -= categoryWidth / 2;
var barHig = 0f; var barHig = 0f;
@@ -97,15 +98,15 @@ namespace XCharts
if (value < 0) if (value < 0)
{ {
plt = new Vector3(pX - borderWidth, pY + space + barWidth - borderWidth); plt = new Vector3(pX - borderWidth, pY + space + barWidth - borderWidth);
prt = new Vector3(pX + currHig + borderWidth, pY + space + barWidth - borderWidth); prt = new Vector3(pX + currHig + borderWidth - axisLineWidth, pY + space + barWidth - borderWidth);
prb = new Vector3(pX + currHig + borderWidth, pY + space + borderWidth); prb = new Vector3(pX + currHig + borderWidth - axisLineWidth, pY + space + borderWidth);
plb = new Vector3(pX - borderWidth, pY + space + borderWidth); plb = new Vector3(pX - borderWidth, pY + space + borderWidth);
} }
else else
{ {
plt = new Vector3(pX + borderWidth, pY + space + barWidth - borderWidth); plt = new Vector3(pX + borderWidth, pY + space + barWidth - borderWidth);
prt = new Vector3(pX + currHig - borderWidth, pY + space + barWidth - borderWidth); prt = new Vector3(pX + currHig - borderWidth - axisLineWidth, pY + space + barWidth - borderWidth);
prb = new Vector3(pX + currHig - borderWidth, pY + space + borderWidth); prb = new Vector3(pX + currHig - borderWidth - axisLineWidth, pY + space + borderWidth);
plb = new Vector3(pX + borderWidth, pY + space + borderWidth); plb = new Vector3(pX + borderWidth, pY + space + borderWidth);
} }
top = new Vector3(pX + currHig - borderWidth, pY + space + barWidth / 2); top = new Vector3(pX + currHig - borderWidth, pY + space + barWidth / 2);
@@ -210,7 +211,8 @@ namespace XCharts
float pX = coordinateX + i * categoryWidth; float pX = coordinateX + i * categoryWidth;
float zeroY = coordinateY + yAxis.runtimeZeroYOffset; float zeroY = coordinateY + yAxis.runtimeZeroYOffset;
if (!xAxis.boundaryGap) pX -= categoryWidth / 2; if (!xAxis.boundaryGap) pX -= categoryWidth / 2;
float pY = seriesHig[i] + zeroY + (value < 0 ? -1 : 1) * xAxis.axisLine.width; float axisLineWidth = (value < 0 ? -1 : 1) * xAxis.axisLine.width;
float pY = seriesHig[i] + zeroY + axisLineWidth;
var barHig = 0f; var barHig = 0f;
var valueTotal = 0f; var valueTotal = 0f;
@@ -233,23 +235,26 @@ namespace XCharts
if (value < 0) if (value < 0)
{ {
plb = new Vector3(pX + space + borderWidth, pY - borderWidth); plb = new Vector3(pX + space + borderWidth, pY - borderWidth);
plt = new Vector3(pX + space + borderWidth, pY + currHig + borderWidth); plt = new Vector3(pX + space + borderWidth, pY + currHig + borderWidth - axisLineWidth);
prt = new Vector3(pX + space + barWidth - borderWidth, pY + currHig + borderWidth); prt = new Vector3(pX + space + barWidth - borderWidth, pY + currHig + borderWidth - axisLineWidth);
prb = new Vector3(pX + space + barWidth - borderWidth, pY - borderWidth); prb = new Vector3(pX + space + barWidth - borderWidth, pY - borderWidth);
} }
else else
{ {
plb = new Vector3(pX + space + borderWidth, pY + borderWidth); plb = new Vector3(pX + space + borderWidth, pY + borderWidth);
plt = new Vector3(pX + space + borderWidth, pY + currHig - borderWidth); plt = new Vector3(pX + space + borderWidth, pY + currHig - borderWidth - axisLineWidth);
prt = new Vector3(pX + space + barWidth - borderWidth, pY + currHig - borderWidth); prt = new Vector3(pX + space + barWidth - borderWidth, pY + currHig - borderWidth - axisLineWidth);
prb = new Vector3(pX + space + barWidth - borderWidth, pY + borderWidth); prb = new Vector3(pX + space + barWidth - borderWidth, pY + borderWidth);
} }
top = new Vector3(pX + space + barWidth / 2, pY + currHig - borderWidth); top = new Vector3(pX + space + barWidth / 2, pY + currHig - borderWidth);
plb = ClampInCoordinate(plb); if (serie.clip)
plt = ClampInCoordinate(plt); {
prt = ClampInCoordinate(prt); plb = ClampInCoordinate(plb);
prb = ClampInCoordinate(prb); plt = ClampInCoordinate(plt);
top = ClampInCoordinate(top); prt = ClampInCoordinate(prt);
prb = ClampInCoordinate(prb);
top = ClampInCoordinate(top);
}
serie.dataPoints.Add(top); serie.dataPoints.Add(top);
if (serie.show) if (serie.show)
{ {

View File

@@ -135,7 +135,7 @@ namespace XCharts
else else
{ {
float yValue = SampleValue(ref showData, serie.sampleType, rate, serie.minShow, maxCount, totalAverage, float yValue = SampleValue(ref showData, serie.sampleType, rate, serie.minShow, maxCount, totalAverage,
i, dataChangeDuration, ref dataChanging,yAxis.inverse); i, dataChangeDuration, ref dataChanging, yAxis.inverse);
seriesHig[i] += GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, seriesHig[i], ref np, seriesHig[i] += GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, seriesHig[i], ref np,
dataChangeDuration); dataChangeDuration);
serie.dataPoints.Add(np); serie.dataPoints.Add(np);
@@ -155,7 +155,7 @@ namespace XCharts
} }
else else
{ {
float yValue = showData[i].GetCurrData(1, dataChangeDuration,yAxis.inverse); float yValue = showData[i].GetCurrData(1, dataChangeDuration, yAxis.inverse);
seriesHig[i] += GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, seriesHig[i], ref np, seriesHig[i] += GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, seriesHig[i], ref np,
dataChangeDuration); dataChangeDuration);
serie.dataPoints.Add(np); serie.dataPoints.Add(np);
@@ -187,7 +187,7 @@ namespace XCharts
} }
else else
{ {
float yValue = showData[i].GetCurrData(1, dataChangeDuration,yAxis.inverse); float yValue = showData[i].GetCurrData(1, dataChangeDuration, yAxis.inverse);
GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, 0, ref firstLastPos, dataChangeDuration); GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, 0, ref firstLastPos, dataChangeDuration);
} }
} }
@@ -204,7 +204,7 @@ namespace XCharts
} }
else else
{ {
float yValue = showData[i].GetCurrData(1, dataChangeDuration,yAxis.inverse); float yValue = showData[i].GetCurrData(1, dataChangeDuration, yAxis.inverse);
GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, 0, ref lastNextPos, dataChangeDuration); GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, 0, ref lastNextPos, dataChangeDuration);
} }
} }
@@ -351,12 +351,12 @@ namespace XCharts
private float SampleValue(ref List<SerieData> showData, SampleType sampleType, int rate, private float SampleValue(ref List<SerieData> showData, SampleType sampleType, int rate,
int minCount, int maxCount, float totalAverage, int index, float dataChangeDuration, int minCount, int maxCount, float totalAverage, int index, float dataChangeDuration,
ref bool dataChanging,bool inverse) ref bool dataChanging, bool inverse)
{ {
if (rate <= 1 || index == minCount) if (rate <= 1 || index == minCount)
{ {
if (showData[index].IsDataChanged()) dataChanging = true; if (showData[index].IsDataChanged()) dataChanging = true;
return showData[index].GetCurrData(1, dataChangeDuration,inverse); return showData[index].GetCurrData(1, dataChangeDuration, inverse);
} }
switch (sampleType) switch (sampleType)
{ {
@@ -365,7 +365,7 @@ namespace XCharts
float total = 0; float total = 0;
for (int i = index; i > index - rate; i--) for (int i = index; i > index - rate; i--)
{ {
total += showData[i].GetCurrData(1, dataChangeDuration,inverse); total += showData[i].GetCurrData(1, dataChangeDuration, inverse);
if (showData[i].IsDataChanged()) dataChanging = true; if (showData[i].IsDataChanged()) dataChanging = true;
} }
if (sampleType == SampleType.Average) return total / rate; if (sampleType == SampleType.Average) return total / rate;
@@ -374,7 +374,7 @@ namespace XCharts
float max = float.MinValue; float max = float.MinValue;
for (int i = index; i > index - rate; i--) for (int i = index; i > index - rate; i--)
{ {
var value = showData[i].GetCurrData(1, dataChangeDuration,inverse); var value = showData[i].GetCurrData(1, dataChangeDuration, inverse);
if (value > max) max = value; if (value > max) max = value;
if (showData[i].IsDataChanged()) dataChanging = true; if (showData[i].IsDataChanged()) dataChanging = true;
} }
@@ -383,7 +383,7 @@ namespace XCharts
float min = float.MaxValue; float min = float.MaxValue;
for (int i = index; i > index - rate; i--) for (int i = index; i > index - rate; i--)
{ {
var value = showData[i].GetCurrData(1, dataChangeDuration,inverse); var value = showData[i].GetCurrData(1, dataChangeDuration, inverse);
if (value < min) min = value; if (value < min) min = value;
if (showData[i].IsDataChanged()) dataChanging = true; if (showData[i].IsDataChanged()) dataChanging = true;
} }
@@ -394,7 +394,7 @@ namespace XCharts
total = 0; total = 0;
for (int i = index; i > index - rate; i--) for (int i = index; i > index - rate; i--)
{ {
var value = showData[i].GetCurrData(1, dataChangeDuration,inverse); var value = showData[i].GetCurrData(1, dataChangeDuration, inverse);
total += value; total += value;
if (value < min) min = value; if (value < min) min = value;
if (value > max) max = value; if (value > max) max = value;
@@ -405,7 +405,7 @@ namespace XCharts
else return min; else return min;
} }
if (showData[index].IsDataChanged()) dataChanging = true; if (showData[index].IsDataChanged()) dataChanging = true;
return showData[index].GetCurrData(1, dataChangeDuration,inverse); return showData[index].GetCurrData(1, dataChangeDuration, inverse);
} }
private float GetDataPoint(Axis xAxis, Axis yAxis, List<SerieData> showData, float yValue, float startX, int i, private float GetDataPoint(Axis xAxis, Axis yAxis, List<SerieData> showData, float yValue, float startX, int i,
@@ -423,7 +423,7 @@ namespace XCharts
float yMaxValue = yAxis.GetCurrMaxValue(duration); float yMaxValue = yAxis.GetCurrMaxValue(duration);
if (xAxis.IsValue() || xAxis.IsLog()) if (xAxis.IsValue() || xAxis.IsLog())
{ {
float xValue = i > showData.Count - 1 ? 0 : showData[i].GetData(0,xAxis.inverse); float xValue = i > showData.Count - 1 ? 0 : showData[i].GetData(0, xAxis.inverse);
float pX = coordinateX + xAxis.axisLine.width; float pX = coordinateX + xAxis.axisLine.width;
float pY = serieHig + coordinateY + xAxis.axisLine.width; float pY = serieHig + coordinateY + xAxis.axisLine.width;
if (xAxis.IsLog()) if (xAxis.IsLog())
@@ -517,7 +517,7 @@ namespace XCharts
{ {
for (int j = 0; j < rate; j++) seriesHig.Add(0); for (int j = 0; j < rate; j++) seriesHig.Add(0);
} }
float value = showData[i].GetCurrData(1, dataChangeDuration,xAxis.inverse); float value = showData[i].GetCurrData(1, dataChangeDuration, xAxis.inverse);
float pY = startY + i * scaleWid; float pY = startY + i * scaleWid;
float pX = seriesHig[i] + coordinateX + yAxis.axisLine.width; float pX = seriesHig[i] + coordinateX + yAxis.axisLine.width;
float dataHig = 0; float dataHig = 0;
@@ -544,7 +544,7 @@ namespace XCharts
{ {
i = maxCount - 1; i = maxCount - 1;
seriesHig.Add(0); seriesHig.Add(0);
float value = showData[i].GetCurrData(1, dataChangeDuration,xAxis.inverse); float value = showData[i].GetCurrData(1, dataChangeDuration, xAxis.inverse);
float pY = startY + i * scaleWid; float pY = startY + i * scaleWid;
float pX = seriesHig[i] + coordinateX + yAxis.axisLine.width; float pX = seriesHig[i] + coordinateX + yAxis.axisLine.width;
float dataHig = 0; float dataHig = 0;
@@ -999,7 +999,7 @@ namespace XCharts
} }
private void DrawPolygonToZero(VertexHelper vh, Vector3 sp, Vector3 ep, Axis axis, Vector3 zeroPos, private void DrawPolygonToZero(VertexHelper vh, Vector3 sp, Vector3 ep, Axis axis, Vector3 zeroPos,
Color areaColor, Color areaToColor, Vector3 areaDiff) Color areaColor, Color areaToColor, Vector3 areaDiff, bool clip = false)
{ {
float diff = 0; float diff = 0;
if (axis is YAxis) if (axis is YAxis)
@@ -1007,7 +1007,8 @@ namespace XCharts
var isLessthan0 = (sp.x < zeroPos.x || ep.x < zeroPos.x); var isLessthan0 = (sp.x < zeroPos.x || ep.x < zeroPos.x);
diff = isLessthan0 ? -axis.axisLine.width : axis.axisLine.width; diff = isLessthan0 ? -axis.axisLine.width : axis.axisLine.width;
if (isLessthan0) areaDiff = -areaDiff; if (isLessthan0) areaDiff = -areaDiff;
ChartDrawer.DrawPolygon(vh, new Vector3(zeroPos.x + diff, sp.y), new Vector3(zeroPos.x + diff, ep.y), ep + areaDiff, sp + areaDiff, areaToColor, areaColor); CheckClipAndDrawPolygon(vh, new Vector3(zeroPos.x + diff, sp.y), new Vector3(zeroPos.x + diff, ep.y),
ep + areaDiff, sp + areaDiff, areaToColor, areaColor, clip);
} }
else else
{ {
@@ -1016,11 +1017,13 @@ namespace XCharts
if (isLessthan0) areaDiff = -areaDiff; if (isLessthan0) areaDiff = -areaDiff;
if (isLessthan0) if (isLessthan0)
{ {
ChartDrawer.DrawPolygon(vh, ep + areaDiff, sp + areaDiff, new Vector3(sp.x, zeroPos.y + diff), new Vector3(ep.x, zeroPos.y + diff), areaColor, areaToColor); CheckClipAndDrawPolygon(vh, ep + areaDiff, sp + areaDiff, new Vector3(sp.x, zeroPos.y + diff),
new Vector3(ep.x, zeroPos.y + diff), areaColor, areaToColor, clip);
} }
else else
{ {
ChartDrawer.DrawPolygon(vh, sp + areaDiff, ep + areaDiff, new Vector3(ep.x, zeroPos.y + diff), new Vector3(sp.x, zeroPos.y + diff), areaColor, areaToColor); CheckClipAndDrawPolygon(vh, sp + areaDiff, ep + areaDiff, new Vector3(ep.x, zeroPos.y + diff),
new Vector3(sp.x, zeroPos.y + diff), areaColor, areaToColor, clip);
} }
} }
} }
@@ -1030,6 +1033,8 @@ namespace XCharts
Vector3 np, int dataIndex, Color lineColor, Color areaColor, Vector3 np, int dataIndex, Color lineColor, Color areaColor,
Color areaToColor, Vector3 zeroPos) Color areaToColor, Vector3 zeroPos)
{ {
//lp = ClampInChart(lp);
//np = ClampInChart(np);
bool isYAxis = axis is YAxis; bool isYAxis = axis is YAxis;
var lineWidth = serie.lineStyle.width; var lineWidth = serie.lineStyle.width;
posList.Clear(); posList.Clear();