private void DrawSmoothLine(VertexHelper vh, int serieIndex, Serie serie, Axis xAxis, Vector3 lp, Vector3 np, int dataIndex, Color lineColor, Color areaColor, Color areaToColor, bool isStack, Vector3 zeroPos) { bool isYAxis = xAxis is YAxis; var lineWidth = serie.lineStyle.width; var smoothPoints = serie.GetUpSmoothList(dataIndex); var smoothDownPoints = serie.GetDownSmoothList(dataIndex); var fine = isStack && m_Series.IsAnyGradientSerie(serie.stack); if (isYAxis) { ChartHelper.GetBezierListVertical(ref bezierPoints, lp, np, fine, lineSmoothStyle); } else { ChartHelper.GetBezierList(ref bezierPoints, lp, np, fine, lineSmoothStyle); } Vector3 start, to; start = bezierPoints[0]; var dir = bezierPoints[1] - start; var dir1v = Vector3.Cross(dir, Vector3.forward).normalized; var startUp = start + (isYAxis ? Vector3.right : Vector3.up) * lineWidth; var startDn = start - (isYAxis ? Vector3.right : Vector3.up) * lineWidth; Vector3 toUp, toDn, tnp, tlp; smoothPoints.Add(startUp); smoothDownPoints.Add(startDn); for (int k = 1; k < bezierPoints.Count; k++) { to = bezierPoints[k]; dir = to - start; if (k < bezierPoints.Count - 1) { dir1v = Vector3.Cross(dir, Vector3.forward).normalized *(isYAxis ? -1 : 1); var diff = dir1v * lineWidth; toUp = to - diff; toDn = to + diff; if (isYAxis) { ChartHelper.DrawPolygon(vh, startDn, toDn, toUp, startUp, lineColor); } else { ChartHelper.DrawPolygon(vh, startUp, toUp, toDn, startDn, lineColor); } } else { toUp = to + (isYAxis ? Vector3.right : Vector3.up) * lineWidth; toDn = to - (isYAxis ? Vector3.right : Vector3.up) * lineWidth; if (isYAxis) { ChartHelper.DrawPolygon(vh, toDn, toUp, startUp, startDn, lineColor); } else { ChartHelper.DrawPolygon(vh, startUp, toUp, toDn, startDn, lineColor); } } smoothPoints.Add(toUp); smoothDownPoints.Add(toDn); if (serie.areaStyle.show && (serieIndex == 0 || !isStack)) { if (isYAxis) { if (start.x > zeroPos.x && to.x > zeroPos.x) { tnp = new Vector3(zeroPos.x + xAxis.axisLine.width, toDn.y); tlp = new Vector3(zeroPos.x + xAxis.axisLine.width, startDn.y); ChartHelper.DrawPolygon(vh, startDn, toDn, tnp, tlp, areaColor, areaToColor); } else if (start.x < zeroPos.x && to.x < zeroPos.x) { tnp = new Vector3(zeroPos.x - xAxis.axisLine.width, toUp.y); tlp = new Vector3(zeroPos.x - xAxis.axisLine.width, startUp.y); ChartHelper.DrawPolygon(vh, tnp, tlp, startUp, toUp, areaToColor, areaColor); } } else { if (start.y > zeroPos.y && to.y > zeroPos.y) { tnp = new Vector3(toDn.x, zeroPos.y + xAxis.axisLine.width); tlp = new Vector3(startDn.x, zeroPos.y + xAxis.axisLine.width); ChartHelper.DrawPolygon(vh, startDn, toDn, tnp, tlp, areaColor, areaToColor); } else if (start.y < zeroPos.y && to.y < zeroPos.y) { tnp = new Vector3(toUp.x, zeroPos.y - xAxis.axisLine.width); tlp = new Vector3(startUp.x, zeroPos.y - xAxis.axisLine.width); ChartHelper.DrawPolygon(vh, tlp, tnp, toUp, startUp, areaToColor, areaColor); } } } start = to; startUp = toUp; startDn = toDn; } if (serie.areaStyle.show) { var lastSerie = m_Series.GetLastStackSerie(serie); if (lastSerie != null) { var lastSmoothPoints = lastSerie.GetUpSmoothList(dataIndex); DrawStackArea(vh, serie, xAxis, smoothDownPoints, lastSmoothPoints, lineColor, areaColor, areaToColor); } } }
private void DrawNormalLine(VertexHelper vh, int serieIndex, Serie serie, Axis axis, Vector3 lp, Vector3 np, Vector3 nnp, int dataIndex, Color lineColor, Color areaColor, Color areaToColor, Vector3 zeroPos) { bool isYAxis = axis is YAxis; var lastSerie = m_Series.GetLastStackSerie(serie); Vector3 dnPos, upPos1, upPos2, dir1v, dir2v; bool isDown; var dir1 = (np - lp).normalized; dir1v = Vector3.Cross(dir1, Vector3.forward).normalized *(isYAxis ? -1 : 1); float cut = 0; if (np != nnp) { var dir2 = (nnp - np).normalized; var dir3 = (dir1 + dir2).normalized; var normal = Vector3.Cross(dir1, dir2); isDown = isYAxis ? normal.z >= 0 : normal.z <= 0; var angle = (180 - Vector3.Angle(dir1, dir2)) * Mathf.Deg2Rad / 2; var diff = serie.lineStyle.width / Mathf.Sin(angle); cut = serie.lineStyle.width / Mathf.Tan(angle); var dirDp = Vector3.Cross(dir3, Vector3.forward).normalized *(isYAxis ? -1 : 1); dir2v = Vector3.Cross(dir2, Vector3.forward).normalized *(isYAxis ? -1 : 1); dnPos = np + (isDown ? dirDp : -dirDp) * diff; upPos1 = np + (isDown ? -dir1v : dir1v) * serie.lineStyle.width; upPos2 = np + (isDown ? -dir2v : dir2v) * serie.lineStyle.width; if (dataIndex == 1) { stPos1 = lp - dir1v * serie.lineStyle.width; stPos2 = lp + dir1v * serie.lineStyle.width; } if (isDown) { ChartHelper.DrawPolygon(vh, stPos1, upPos1, dnPos, stPos2, lineColor); ChartHelper.DrawTriangle(vh, upPos1, upPos2, dnPos, lineColor); } else { ChartHelper.DrawPolygon(vh, stPos1, dnPos, upPos1, stPos2, lineColor); ChartHelper.DrawTriangle(vh, dnPos, upPos2, upPos1, lineColor); } lastDir = dir1; } else { isDown = Vector3.Cross(dir1, lastDir).z <= 0; if (isYAxis) { isDown = !isDown; } dir1v = Vector3.Cross(dir1, Vector3.forward).normalized *(isYAxis ? -1 : 1); upPos1 = np - dir1v * serie.lineStyle.width; upPos2 = np + dir1v * serie.lineStyle.width; dnPos = isDown ? upPos2 : upPos1; ChartHelper.DrawPolygon(vh, stPos1, upPos1, upPos2, stPos2, lineColor); } var smoothPoints = serie.GetUpSmoothList(dataIndex); var smoothDownPoints = serie.GetDownSmoothList(dataIndex); var dist = Vector3.Distance(lp, np); var fine = m_Series.IsAnyGradientSerie(serie.stack); var tick = fine ? 3f : 30f; int segment = (int)(dist / tick); smoothPoints.Clear(); smoothDownPoints.Clear(); smoothPoints.Add(stPos1); smoothDownPoints.Add(stPos2); for (int i = 1; i < segment; i++) { var cp = lp + dir1 * dist * i / segment; var tp1 = cp - dir1v * serie.lineStyle.width; var tp2 = cp + dir1v * serie.lineStyle.width; if (isDown) { if (isYAxis) { if (tp1.y > lastDnPos.y || dataIndex == 1) { smoothPoints.Add(tp1); } if (tp2.y < dnPos.y) { smoothDownPoints.Add(tp2); } } else { if (tp1.x > lastDnPos.x || dataIndex == 1) { smoothPoints.Add(tp1); } if (tp2.x < dnPos.x || dataIndex == 1) { smoothDownPoints.Add(tp2); } } } else { if (isYAxis) { if (tp1.y < dnPos.y) { smoothPoints.Add(tp1); } if (tp2.y > lastDnPos.y || dataIndex == 1) { smoothDownPoints.Add(tp2); } } else { if (tp1.x < dnPos.x) { smoothPoints.Add(tp1); } if (tp2.x > lastDnPos.x || dataIndex == 1) { smoothDownPoints.Add(tp2); } } } } if (isDown) { smoothPoints.Add(upPos1); smoothPoints.Add(upPos2); smoothDownPoints.Add(dnPos); } else { smoothPoints.Add(dnPos); if (isYAxis) { smoothDownPoints.Add(np != nnp ? upPos1 : upPos2); smoothDownPoints.Add(np != nnp ? upPos2 : upPos1); } else { smoothDownPoints.Add(np != nnp ? upPos1 : upPos2); smoothDownPoints.Add(np != nnp ? upPos2 : upPos1); } } if (serie.areaStyle.show) { if (lastSerie != null) { var lastSmoothPoints = lastSerie.GetUpSmoothList(dataIndex); DrawStackArea(vh, serie, axis, smoothDownPoints, lastSmoothPoints, lineColor, areaColor, areaToColor); } else { Vector3 alp = new Vector3(lp.x, lp.y - serie.lineStyle.width); Vector3 anp = new Vector3(np.x, np.y - serie.lineStyle.width); Vector3 aep = isYAxis ? new Vector3(zeroPos.x, zeroPos.y + coordinateHig) : new Vector3(zeroPos.x + coordinateWid, zeroPos.y); var cross = ChartHelper.GetIntersection(lp, np, zeroPos, aep); if (cross == Vector3.zero) { var points = ((isYAxis && lp.x < zeroPos.x) || (!isYAxis && lp.y < zeroPos.y)) ? smoothPoints : smoothDownPoints; Vector3 sp = points[0]; Vector3 ep; for (int i = 1; i < points.Count; i++) { ep = points[i]; DrawPolygonToZero(vh, sp, ep, axis, zeroPos, areaColor, areaToColor); sp = ep; } } else { var sp1 = smoothDownPoints[1]; var ep1 = smoothDownPoints[smoothDownPoints.Count - 2]; var axisUpStart = zeroPos + (isYAxis ? Vector3.right : Vector3.up) * axis.axisLine.width; var axisUpEnd = axisUpStart + (isYAxis ? Vector3.up * coordinateHig : Vector3.right * coordinateWid); var axisDownStart = zeroPos - (isYAxis ? Vector3.right : Vector3.up) * axis.axisLine.width; var axisDownEnd = axisDownStart + (isYAxis ? Vector3.up * coordinateHig : Vector3.right * coordinateWid); var luPos = ChartHelper.GetIntersection(sp1, ep1, axisUpStart, axisUpEnd); var ldPos = ChartHelper.GetIntersection(sp1, ep1, axisDownStart, axisDownEnd); sp1 = smoothPoints[1]; ep1 = smoothPoints[smoothPoints.Count - 2]; var ruPos = ChartHelper.GetIntersection(sp1, ep1, axisUpStart, axisUpEnd); var rdPos = ChartHelper.GetIntersection(sp1, ep1, axisDownStart, axisDownEnd); Vector3 sp, ep; if ((isYAxis && lp.x > zeroPos.x) || (!isYAxis && lp.y > zeroPos.y)) { sp = smoothDownPoints[0]; for (int i = 1; i < smoothDownPoints.Count; i++) { ep = smoothDownPoints[i]; if ((isYAxis && ep.y > luPos.y) || (!isYAxis && ep.x > luPos.x)) { var tp = isYAxis ? new Vector3(luPos.x, sp.y) : new Vector3(sp.x, luPos.y); ChartHelper.DrawTriangle(vh, sp, luPos, tp, areaColor, areaToColor, areaToColor); break; } DrawPolygonToZero(vh, sp, ep, axis, zeroPos, areaColor, areaToColor); sp = ep; } sp = smoothPoints[smoothPoints.Count - 1]; for (int i = smoothPoints.Count - 2; i >= 0; i--) { ep = smoothPoints[i]; if ((isYAxis && ep.y > rdPos.y) || (!isYAxis && ep.x > rdPos.x)) { DrawPolygonToZero(vh, sp, ep, axis, zeroPos, areaColor, areaToColor); } else { var tp = isYAxis ? new Vector3(rdPos.x, sp.y) : new Vector3(sp.x, rdPos.y); ChartHelper.DrawTriangle(vh, sp, rdPos, tp, areaColor, areaToColor, areaToColor); break; } sp = ep; } } else { sp = smoothPoints[0]; for (int i = 1; i < smoothPoints.Count; i++) { ep = smoothPoints[i]; if ((isYAxis && ep.y > rdPos.y) || (!isYAxis && ep.x > rdPos.x)) { var tp = isYAxis ? new Vector3(rdPos.x, sp.y) : new Vector3(sp.x, rdPos.y); ChartHelper.DrawTriangle(vh, sp, rdPos, tp, areaColor, areaToColor, areaToColor); break; } DrawPolygonToZero(vh, sp, ep, axis, zeroPos, areaColor, areaToColor); sp = ep; } sp = smoothDownPoints[smoothDownPoints.Count - 1]; for (int i = smoothDownPoints.Count - 2; i >= 0; i--) { ep = smoothDownPoints[i]; if ((isYAxis && ep.y > luPos.y) || (!isYAxis && ep.x > luPos.x)) { DrawPolygonToZero(vh, sp, ep, axis, zeroPos, areaColor, areaToColor); } else { var tp = isYAxis ? new Vector3(luPos.x, sp.y) : new Vector3(sp.x, luPos.y); ChartHelper.DrawTriangle(vh, sp, luPos, tp, areaColor, areaToColor, areaToColor); break; } sp = ep; } } } } } stPos1 = isDown ? upPos2 : dnPos; stPos2 = isDown ? dnPos : upPos2; lastDnPos = dnPos; }