public static Color32 GetLineGradientColor(VisualMap visualMap, Vector3 pos, CoordinateChart chart, Axis axis, Color32 defaultColor) { float value = 0; switch (visualMap.direction) { case VisualMap.Direction.Default: case VisualMap.Direction.X: var min = axis.runtimeMinValue; var max = axis.runtimeMaxValue; var grid = chart.GetAxisGridOrDefault(axis); value = min + (pos.x - grid.runtimeX) / grid.runtimeWidth * (max - min); break; case VisualMap.Direction.Y: if (axis is YAxis) { var yAxis = chart.xAxes[axis.index]; min = yAxis.runtimeMinValue; max = yAxis.runtimeMaxValue; } else { var yAxis = chart.yAxes[axis.index]; min = yAxis.runtimeMinValue; max = yAxis.runtimeMaxValue; } grid = chart.GetAxisGridOrDefault(axis); value = min + (pos.y - grid.runtimeY) / grid.runtimeHeight * (max - min); break; } var color = visualMap.GetColor(value); if (ChartHelper.IsClearColor(color)) { return(defaultColor); } else { return(color); } }
private void DrawSmoothAreaPoints(VertexHelper vh, int serieIndex, Serie serie, Axis xAxis, Vector3 lp, Vector3 np, int dataIndex, Color lineColor, Color areaColor, bool isStack) { bool isYAxis = xAxis is YAxis; if (isYAxis) { ChartHelper.GetBezierListVertical(ref smoothSegmentPoints, lp, np, m_Line.smoothStyle); } else { ChartHelper.GetBezierList(ref smoothSegmentPoints, lp, np, m_Line.smoothStyle); } Vector3 start, to; start = smoothSegmentPoints[0]; var smoothPoints = serie.GetSmoothList(dataIndex, smoothSegmentPoints.Count); smoothPoints.Clear(); var lastSerie = m_Series.GetSerie(serie.index - 1); var lastSmoothPoints = lastSerie != null?lastSerie.GetSmoothList(dataIndex, smoothSegmentPoints.Count) : new List <Vector3>(); smoothPoints.Add(start); var lastCount = 1; for (int k = 1; k < smoothSegmentPoints.Count; k++) { smoothPoints.Add(smoothSegmentPoints[k]); to = smoothSegmentPoints[k]; ChartHelper.DrawLine(vh, start, to, m_Line.tickness, lineColor); if (m_Line.area) { Vector3 alp, anp, tnp, tlp; if (isYAxis) { alp = new Vector3(start.x - m_Line.tickness, start.y); anp = new Vector3(to.x - m_Line.tickness, to.y); } else { alp = new Vector3(start.x, start.y - m_Line.tickness); anp = new Vector3(to.x, to.y - m_Line.tickness); } if (serieIndex > 0 && isStack) { if (k == smoothSegmentPoints.Count - 1) { if (k < lastSmoothPoints.Count - 1) { tnp = lastSmoothPoints[lastCount - 1]; if (isYAxis) { tnp.x += m_Line.tickness; } else { tnp.y += m_Line.tickness; } ChartHelper.DrawTriangle(vh, alp, anp, tnp, areaColor); while (lastCount < lastSmoothPoints.Count) { tlp = lastSmoothPoints[lastCount]; if (isYAxis) { tlp.x += m_Line.tickness; } else { tlp.y += m_Line.tickness; } ChartHelper.DrawTriangle(vh, tnp, anp, tlp, areaColor); lastCount++; tnp = tlp; } start = to; continue; } } if (lastCount >= lastSmoothPoints.Count) { tlp = lastSmoothPoints[lastSmoothPoints.Count - 1]; if (isYAxis) { tlp.x += m_Line.tickness; } else { tlp.y += m_Line.tickness; } ChartHelper.DrawTriangle(vh, anp, alp, tlp, areaColor); start = to; continue; } if (isYAxis) { tnp = new Vector3(lastSmoothPoints[lastCount].x + m_Line.tickness, lastSmoothPoints[lastCount].y); } else { tnp = new Vector3(lastSmoothPoints[lastCount].x, lastSmoothPoints[lastCount].y + m_Line.tickness); } var diff = isYAxis ? tnp.y - to.y : tnp.x - to.x; if (Math.Abs(diff) < 1) { if (isYAxis) { tlp = new Vector3(lastSmoothPoints[lastCount - 1].x + m_Line.tickness, lastSmoothPoints[lastCount - 1].y); } else { tlp = new Vector3(lastSmoothPoints[lastCount - 1].x, lastSmoothPoints[lastCount - 1].y + m_Line.tickness); } ChartHelper.DrawPolygon(vh, alp, anp, tnp, tlp, areaColor); lastCount++; } else { if (diff < 0) { if (isYAxis) { tnp = new Vector3(lastSmoothPoints[lastCount - 1].x + m_Line.tickness, lastSmoothPoints[lastCount - 1].y); } else { tnp = new Vector3(lastSmoothPoints[lastCount - 1].x, lastSmoothPoints[lastCount - 1].y + m_Line.tickness); } ChartHelper.DrawTriangle(vh, alp, anp, tnp, areaColor); while (diff < 0 && lastCount < lastSmoothPoints.Count) { if (isYAxis) { tlp = new Vector3(lastSmoothPoints[lastCount].x + m_Line.tickness, lastSmoothPoints[lastCount].y); } else { tlp = new Vector3(lastSmoothPoints[lastCount].x, lastSmoothPoints[lastCount].y + m_Line.tickness); } ChartHelper.DrawTriangle(vh, tnp, anp, tlp, areaColor); lastCount++; diff = isYAxis ? tlp.y - to.y : tlp.x - to.x; tnp = tlp; } } else { if (isYAxis) { tlp = new Vector3(lastSmoothPoints[lastCount - 1].x + m_Line.tickness, lastSmoothPoints[lastCount - 1].y); } else { tlp = new Vector3(lastSmoothPoints[lastCount - 1].x, lastSmoothPoints[lastCount - 1].y + m_Line.tickness); } ChartHelper.DrawTriangle(vh, alp, anp, tlp, areaColor); } } } else { if (isYAxis) { tnp = new Vector3(coordinateX + xAxis.axisLine.width, to.y); tlp = new Vector3(coordinateX + xAxis.axisLine.width, start.y); } else { tnp = new Vector3(to.x, coordinateY + xAxis.axisLine.width); tlp = new Vector3(start.x, coordinateY + xAxis.axisLine.width); } ChartHelper.DrawPolygon(vh, alp, anp, tnp, tlp, areaColor); } } start = to; } }
public static Color32 GetLineStyleGradientColor(LineStyle lineStyle, Vector3 pos, CoordinateChart chart, Axis axis, Color32 defaultColor) { var min = axis.runtimeMinValue; var max = axis.runtimeMaxValue; var grid = chart.GetAxisGridOrDefault(axis); var value = min + (pos.x - grid.runtimeX) / grid.runtimeWidth * (max - min); var rate = (value - min) / (max - min); var color = lineStyle.GetGradientColor(rate, defaultColor); if (ChartHelper.IsClearColor(color)) { return(defaultColor); } else { return(color); } }
/// <summary> /// 调整最大最小值 /// </summary> /// <param name="minValue"></param> /// <param name="maxValue"></param> internal static void AdjustMinMaxValue(Axis axis, ref float minValue, ref float maxValue, bool needFormat) { if (axis.type == Axis.AxisType.Log) { int minSplit = 0; int maxSplit = 0; maxValue = ChartHelper.GetMaxLogValue(maxValue, axis.logBase, axis.logBaseE, out maxSplit); minValue = ChartHelper.GetMinLogValue(minValue, axis.logBase, axis.logBaseE, out minSplit); axis.splitNumber = (minSplit > 0 && maxSplit > 0) ? (maxSplit + minSplit - 1) : (maxSplit + minSplit); return; } if (axis.minMaxType == Axis.AxisMinMaxType.Custom) { if (axis.min != 0 || axis.max != 0) { if (axis.inverse) { minValue = -axis.max; maxValue = -axis.min; } else { minValue = axis.min; maxValue = axis.max; } } } else { switch (axis.minMaxType) { case Axis.AxisMinMaxType.Default: if (minValue == 0 && maxValue == 0) { } else if (minValue > 0 && maxValue > 0) { minValue = 0; maxValue = needFormat ? ChartHelper.GetMaxDivisibleValue(maxValue, axis.ceilRate) : maxValue; } else if (minValue < 0 && maxValue < 0) { minValue = needFormat ? ChartHelper.GetMinDivisibleValue(minValue, axis.ceilRate) : minValue; maxValue = 0; } else { minValue = needFormat ? ChartHelper.GetMinDivisibleValue(minValue, axis.ceilRate) : minValue; maxValue = needFormat ? ChartHelper.GetMaxDivisibleValue(maxValue, axis.ceilRate) : maxValue; } break; case Axis.AxisMinMaxType.MinMax: minValue = needFormat ? ChartHelper.GetMinDivisibleValue(minValue, axis.ceilRate) : minValue; maxValue = needFormat ? ChartHelper.GetMaxDivisibleValue(maxValue, axis.ceilRate) : maxValue; break; } } var tempRange = maxValue - minValue; if (axis.runtimeMinMaxRange != tempRange) { axis.runtimeMinMaxRange = tempRange; if (axis.type == Axis.AxisType.Value && axis.interval > 0) { axis.SetComponentDirty(); } } }
/// <summary> /// 获得分割段宽度 /// </summary> /// <param name="coordinateWidth"></param> /// <param name="dataZoom"></param> /// <returns></returns> internal static float GetScaleWidth(Axis axis, float coordinateWidth, int index, DataZoom dataZoom = null) { if (index < 0) { return(0); } int num = GetScaleNumber(axis, coordinateWidth, dataZoom); int splitNum = GetSplitNumber(axis, coordinateWidth, dataZoom); if (num <= 0) { num = 1; } if (axis.type == Axis.AxisType.Value && axis.interval > 0) { if (axis.runtimeMinMaxRange <= 0) { return(0); } if (index >= splitNum) { return(coordinateWidth - (index - 1) * axis.interval * coordinateWidth / axis.runtimeMinMaxRange); } else { return(axis.interval * coordinateWidth / axis.runtimeMinMaxRange); } } else { if (axis.IsCategory() && axis.data.Count > 0) { int tick = Mathf.RoundToInt(axis.data.Count * 1f / splitNum); var count = axis.boundaryGap ? axis.data.Count : axis.data.Count - 1; if (count <= 0) { return(0); } var each = coordinateWidth / count; if (index >= num - 1) { if (axis.axisTick.alignWithLabel) { return(each * tick); } else { return(coordinateWidth - each * tick * (index - 1)); } } else { return(each * tick); } } else { if (splitNum <= 0) { return(0); } else { return(coordinateWidth / splitNum); } } } }
/// <summary> /// 获得标签显示的名称 /// </summary> /// <param name="index"></param> /// <param name="minValue"></param> /// <param name="maxValue"></param> /// <param name="dataZoom"></param> /// <returns></returns> internal static string GetLabelName(Axis axis, float coordinateWidth, int index, float minValue, float maxValue, DataZoom dataZoom, bool forcePercent) { int split = GetSplitNumber(axis, coordinateWidth, dataZoom); if (axis.type == Axis.AxisType.Value) { if (minValue == 0 && maxValue == 0) { return(string.Empty); } var value = 0f; if (forcePercent) { maxValue = 100; } if (axis.interval > 0) { if (index == split) { value = maxValue; } else { value = minValue + index * axis.interval; } } else { value = minValue + (maxValue - minValue) * index / split; if (!axis.clockwise && value != minValue) { value = maxValue - value; } } if (axis.inverse) { value = -value; minValue = -minValue; maxValue = -maxValue; } if (forcePercent) { return(string.Format("{0}%", (int)value)); } else { return(axis.axisLabel.GetFormatterContent(value, minValue, maxValue)); } } else if (axis.type == Axis.AxisType.Log) { float value = axis.logBaseE ? Mathf.Exp(axis.runtimeMinLogIndex + index) : Mathf.Pow(axis.logBase, axis.runtimeMinLogIndex + index); if (axis.inverse) { value = -value; minValue = -minValue; maxValue = -maxValue; } return(axis.axisLabel.GetFormatterContent(value, minValue, maxValue, true)); } var showData = axis.GetDataList(dataZoom); int dataCount = showData.Count; if (dataCount <= 0) { return(""); } int rate = Mathf.RoundToInt(dataCount * 1f / split); int newIndex = index * rate; if (newIndex <= dataCount - 1) { return(axis.axisLabel.GetFormatterContent(showData[newIndex])); } else { if (rate == 1) { return(string.Empty); } else if (axis.boundaryGap && coordinateWidth / dataCount > 10) { return(string.Empty); } else { if ((index - 1) * rate > dataCount - 1) { return(string.Empty); } else { return(axis.axisLabel.GetFormatterContent(showData[dataCount - 1])); } } } }
/// <summary> /// 获得分割段数 /// </summary> /// <param name="dataZoom"></param> /// <returns></returns> public static int GetSplitNumber(Axis axis, float coordinateWid, DataZoom dataZoom) { if (axis.type == Axis.AxisType.Value) { if (axis.interval > 0) { if (coordinateWid <= 0) { return(0); } int num = Mathf.CeilToInt(axis.runtimeMinMaxRange / axis.interval); int maxNum = Mathf.CeilToInt(coordinateWid / 15); if (num > maxNum) { axis.interval *= 2; num = Mathf.CeilToInt(axis.runtimeMinMaxRange / axis.interval); } return(num); } else { return(axis.splitNumber > 0 ? axis.splitNumber : 4); } } else if (axis.type == Axis.AxisType.Time) { if (axis.interval > 0) { if (coordinateWid <= 0) { return(0); } int num = Mathf.CeilToInt(axis.runtimeMinMaxRange / axis.interval); int maxNum = Mathf.CeilToInt(coordinateWid / 15); if (num > maxNum) { axis.interval *= 2; num = Mathf.CeilToInt(axis.runtimeMinMaxRange / axis.interval); } return(num); } else { return(axis.splitNumber > 0 ? axis.splitNumber : 4); } } else if (axis.type == Axis.AxisType.Log) { return(axis.splitNumber > 0 ? axis.splitNumber : 4); } else if (axis.type == Axis.AxisType.Category) { int dataCount = axis.GetDataList(dataZoom).Count; if (axis.splitNumber <= 0 || axis.splitNumber > dataCount) { return(dataCount); } if (dataCount >= axis.splitNumber * 2) { return(axis.splitNumber); } else { return(dataCount); } } return(0); }