Files
XCharts/Runtime/Serie/Bar/BarHandler.cs

660 lines
33 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
2021-01-11 08:54:28 +08:00
using XUGL;
2022-02-19 22:37:57 +08:00
namespace XCharts.Runtime
{
2021-11-23 13:20:07 +08:00
[UnityEngine.Scripting.Preserve]
internal sealed class BarHandler : SerieHandler<Bar>
{
2021-11-23 13:20:07 +08:00
List<List<SerieData>> m_StackSerieData = new List<List<SerieData>>();
2021-12-23 13:23:18 +08:00
private GridCoord m_SerieGrid;
2021-11-23 13:20:07 +08:00
public override void Update()
{
2021-11-23 13:20:07 +08:00
base.Update();
UpdateSerieContext();
}
2021-12-19 20:53:55 +08:00
public override void UpdateTooltipSerieParams(int dataIndex, bool showCategory, string category,
string marker, string itemFormatter, string numericFormatter,
ref List<SerieParams> paramList, ref string title)
2021-11-23 13:20:07 +08:00
{
2021-12-19 20:53:55 +08:00
UpdateCoordSerieParams(ref paramList, ref title, dataIndex, showCategory, category,
marker, itemFormatter, numericFormatter);
2021-11-23 13:20:07 +08:00
}
public override void DrawSerie(VertexHelper vh)
{
2021-12-12 18:05:26 +08:00
DrawBarSerie(vh, serie, serie.context.colorIndex);
2021-11-23 13:20:07 +08:00
}
2022-01-13 21:45:59 +08:00
public override Vector3 GetSerieDataLabelPosition(SerieData serieData, LabelStyle label)
{
switch (label.position)
{
case LabelStyle.Position.Bottom:
var center = serieData.context.rect.center;
return new Vector3(center.x, center.y - serieData.context.rect.height / 2);
case LabelStyle.Position.Center:
case LabelStyle.Position.Inside:
return serieData.context.rect.center;
default:
return serieData.context.position;
}
}
2021-11-23 13:20:07 +08:00
private void UpdateSerieContext()
{
2021-12-23 13:23:18 +08:00
if (m_SerieGrid == null)
return;
2021-12-24 07:38:10 +08:00
var needCheck = (chart.isPointerInChart && m_SerieGrid.IsPointerEnter()) || m_LegendEnter;
2021-12-23 13:23:18 +08:00
var needInteract = false;
if (!needCheck)
2021-11-23 13:20:07 +08:00
{
2021-12-23 13:23:18 +08:00
if (m_LastCheckContextFlag != needCheck)
2021-11-23 13:20:07 +08:00
{
2021-12-23 13:23:18 +08:00
m_LastCheckContextFlag = needCheck;
serie.context.pointerItemDataIndex = -1;
serie.context.pointerEnter = false;
foreach (var serieData in serie.data)
{
var barColor = SerieHelper.GetItemColor(serie, serieData, chart.theme, serie.context.colorIndex, false);
var barToColor = SerieHelper.GetItemToColor(serie, serieData, chart.theme, serie.context.colorIndex, false);
serieData.interact.SetColor(ref needInteract, barColor, barToColor);
}
if (needInteract)
{
chart.RefreshPainter(serie);
}
2021-11-23 13:20:07 +08:00
}
2021-12-23 13:23:18 +08:00
return;
}
m_LastCheckContextFlag = needCheck;
2021-12-24 07:38:10 +08:00
if (m_LegendEnter)
2021-12-23 13:23:18 +08:00
{
serie.context.pointerEnter = true;
foreach (var serieData in serie.data)
2021-11-23 13:20:07 +08:00
{
2021-12-23 13:23:18 +08:00
var barColor = SerieHelper.GetItemColor(serie, serieData, chart.theme, serie.context.colorIndex, true);
var barToColor = SerieHelper.GetItemToColor(serie, serieData, chart.theme, serie.context.colorIndex, true);
serieData.interact.SetColor(ref needInteract, barColor, barToColor);
2021-11-23 13:20:07 +08:00
}
}
2021-12-23 13:23:18 +08:00
else
{
serie.context.pointerItemDataIndex = -1;
serie.context.pointerEnter = false;
foreach (var serieData in serie.data)
{
if (serieData.context.rect.Contains(chart.pointerPos))
{
serie.context.pointerItemDataIndex = serieData.index;
serie.context.pointerEnter = true;
serieData.context.highlight = true;
var barColor = SerieHelper.GetItemColor(serie, serieData, chart.theme, serie.context.colorIndex, true);
var barToColor = SerieHelper.GetItemToColor(serie, serieData, chart.theme, serie.context.colorIndex, true);
serieData.interact.SetColor(ref needInteract, barColor, barToColor);
}
else
{
serieData.context.highlight = false;
var barColor = SerieHelper.GetItemColor(serie, serieData, chart.theme, serie.context.colorIndex, false);
var barToColor = SerieHelper.GetItemToColor(serie, serieData, chart.theme, serie.context.colorIndex, false);
serieData.interact.SetColor(ref needInteract, barColor, barToColor);
}
}
}
if (needInteract)
{
chart.RefreshPainter(serie);
}
2021-11-23 13:20:07 +08:00
}
2021-12-12 18:05:26 +08:00
private void DrawBarSerie(VertexHelper vh, Bar serie, int colorIndex)
{
2021-12-11 18:26:28 +08:00
if (!serie.show || serie.animation.HasFadeOut())
return;
var isY = ComponentHelper.IsAnyCategoryOfYAxis(chart.components);
Axis axis;
Axis relativedAxis;
if (isY)
{
axis = chart.GetChartComponent<YAxis>(serie.yAxisIndex);
relativedAxis = chart.GetChartComponent<XAxis>(serie.xAxisIndex);
}
else
{
axis = chart.GetChartComponent<XAxis>(serie.xAxisIndex);
relativedAxis = chart.GetChartComponent<YAxis>(serie.yAxisIndex);
}
2021-12-23 13:23:18 +08:00
m_SerieGrid = chart.GetChartComponent<GridCoord>(axis.gridIndex);
2021-12-11 18:26:28 +08:00
if (axis == null)
return;
if (relativedAxis == null)
return;
2021-12-23 13:23:18 +08:00
if (m_SerieGrid == null)
2021-12-11 18:26:28 +08:00
return;
var dataZoom = chart.GetDataZoomOfAxis(axis);
var showData = serie.GetDataList(dataZoom);
2021-12-11 18:26:28 +08:00
if (showData.Count <= 0)
return;
2021-12-23 13:23:18 +08:00
var axisLength = isY ? m_SerieGrid.context.height : m_SerieGrid.context.width;
2022-01-13 21:45:59 +08:00
var relativedAxisLength = isY ? m_SerieGrid.context.width : m_SerieGrid.context.height;
2021-12-23 13:23:18 +08:00
var axisXY = isY ? m_SerieGrid.context.y : m_SerieGrid.context.x;
2021-12-11 18:26:28 +08:00
2021-11-23 13:20:07 +08:00
var isStack = SeriesHelper.IsStack<Bar>(chart.series, serie.stack);
2021-12-11 18:26:28 +08:00
if (isStack)
SeriesHelper.UpdateStackDataList(chart.series, serie, dataZoom, m_StackSerieData);
float categoryWidth = AxisHelper.GetDataWidth(axis, axisLength, showData.Count, dataZoom);
2021-11-23 13:20:07 +08:00
float barGap = chart.GetSerieBarGap<Bar>();
float totalBarWidth = chart.GetSerieTotalWidth<Bar>(categoryWidth, barGap);
float barWidth = serie.GetBarWidth(categoryWidth);
2021-12-11 18:26:28 +08:00
float offset = (categoryWidth - totalBarWidth) * 0.5f;
float barGapWidth = barWidth + barWidth * barGap;
2022-03-04 22:17:32 +08:00
float gap = serie.barGap == -1 ? offset : offset + chart.GetSerieIndexIfStack<Bar>(serie) * barGapWidth;
2021-01-11 08:54:28 +08:00
int maxCount = serie.maxShow > 0
? (serie.maxShow > showData.Count ? showData.Count : serie.maxShow)
: showData.Count;
2021-11-23 13:20:07 +08:00
var isPercentStack = SeriesHelper.IsPercentStack<Bar>(chart.series, serie.stack);
2019-11-30 21:24:04 +08:00
bool dataChanging = false;
float dataChangeDuration = serie.animation.GetUpdateAnimationDuration();
2021-12-11 18:26:28 +08:00
double yMinValue = relativedAxis.context.minValue;
double yMaxValue = relativedAxis.context.maxValue;
2021-12-23 13:23:18 +08:00
var areaColor = ColorUtil.clearColor32;
var areaToColor = ColorUtil.clearColor32;
var interacting = false;
serie.containerIndex = m_SerieGrid.index;
serie.containterInstanceId = m_SerieGrid.instanceId;
2021-12-12 18:05:26 +08:00
serie.animation.InitProgress(axisXY, axisXY + axisLength);
for (int i = serie.minShow; i < maxCount; i++)
{
var serieData = showData[i];
if (!serieData.show || serie.IsIgnoreValue(serieData))
{
2021-12-08 13:36:02 +08:00
serie.context.dataPoints.Add(Vector3.zero);
continue;
}
2021-12-12 18:05:26 +08:00
2021-12-11 18:26:28 +08:00
if (serieData.IsDataChanged())
dataChanging = true;
var highlight = serieData.context.highlight || serie.highlight;
var itemStyle = SerieHelper.GetItemStyle(serie, serieData, highlight);
2021-12-11 18:26:28 +08:00
var value = axis.IsCategory() ? i : serieData.GetData(0, axis.inverse);
var relativedValue = serieData.GetCurrData(1, dataChangeDuration, relativedAxis.inverse, yMinValue, yMaxValue);
var borderWidth = relativedValue == 0 ? 0 : itemStyle.runtimeBorderWidth;
2021-12-23 13:23:18 +08:00
if (!serieData.interact.TryGetColor(ref areaColor, ref areaToColor, ref interacting))
{
areaColor = SerieHelper.GetItemColor(serie, serieData, chart.theme, colorIndex, highlight);
areaToColor = SerieHelper.GetItemToColor(serie, serieData, chart.theme, colorIndex, highlight);
serieData.interact.SetColor(ref interacting, areaColor, areaToColor);
}
2021-12-11 18:26:28 +08:00
var pX = 0f;
var pY = 0f;
2021-12-23 13:23:18 +08:00
UpdateXYPosition(m_SerieGrid, isY, axis, relativedAxis, i, categoryWidth, barWidth, isStack, value, ref pX, ref pY);
var barHig = 0f;
if (isPercentStack)
{
2021-12-11 18:26:28 +08:00
var valueTotal = chart.GetSerieSameStackTotalValue<Bar>(serie.stack, i);
2022-01-13 21:45:59 +08:00
barHig = valueTotal != 0 ? (float)(relativedValue / valueTotal * relativedAxisLength) : 0;
}
else
{
2021-12-23 13:23:18 +08:00
barHig = AxisHelper.GetAxisValueLength(m_SerieGrid, relativedAxis, categoryWidth, relativedValue);
}
2021-12-11 18:26:28 +08:00
2021-12-12 18:05:26 +08:00
float currHig = AnimationStyleHelper.CheckDataAnimation(chart, serie, i, barHig);
2020-04-18 23:38:42 +08:00
Vector3 plb, plt, prt, prb, top;
2022-03-04 22:17:32 +08:00
UpdateRectPosition(m_SerieGrid, isY, relativedValue, pX, pY, gap, borderWidth, barWidth, currHig,
2021-12-11 18:26:28 +08:00
out plb, out plt, out prt, out prb, out top);
serieData.context.stackHeight = barHig;
serieData.context.position = top;
2021-12-09 07:12:15 +08:00
serieData.context.rect = Rect.MinMaxRect(plb.x, plb.y, prb.x, prt.y);
2022-01-26 20:47:14 +08:00
if (!serie.clip || (serie.clip && m_SerieGrid.Contains(top)))
serie.context.dataPoints.Add(top);
else
continue;
2022-01-13 21:45:59 +08:00
if (serie.show && currHig != 0 && !serie.placeHolder)
{
switch (serie.barType)
{
case BarType.Normal:
2022-03-04 22:17:32 +08:00
DrawNormalBar(vh, serie, serieData, itemStyle, colorIndex, highlight, gap, barWidth,
2022-01-13 21:45:59 +08:00
pX, pY, plb, plt, prt, prb, isY, m_SerieGrid, axis, areaColor, areaToColor);
break;
case BarType.Zebra:
2022-03-04 22:17:32 +08:00
DrawZebraBar(vh, serie, serieData, itemStyle, colorIndex, highlight, gap, barWidth,
2022-01-13 21:45:59 +08:00
pX, pY, plb, plt, prt, prb, isY, m_SerieGrid, axis, areaColor, areaToColor);
break;
case BarType.Capsule:
2022-03-04 22:17:32 +08:00
DrawCapsuleBar(vh, serie, serieData, itemStyle, colorIndex, highlight, gap, barWidth,
2022-01-13 21:45:59 +08:00
pX, pY, plb, plt, prt, prb, isY, m_SerieGrid, axis, areaColor, areaToColor);
break;
}
}
2021-12-12 18:05:26 +08:00
if (serie.animation.CheckDetailBreak(top, isY))
{
break;
}
}
2021-12-12 18:05:26 +08:00
if (!serie.animation.IsFinish())
2020-08-18 09:29:23 +08:00
{
2021-12-12 18:05:26 +08:00
serie.animation.CheckProgress();
chart.RefreshPainter(serie);
2020-08-18 09:29:23 +08:00
}
2021-12-23 13:23:18 +08:00
if (dataChanging || interacting)
2019-11-30 21:24:04 +08:00
{
2021-11-23 13:20:07 +08:00
chart.RefreshPainter(serie);
}
}
2021-12-11 18:26:28 +08:00
private void UpdateXYPosition(GridCoord grid, bool isY, Axis axis, Axis relativedAxis, int i, float categoryWidth, float barWidth, bool isStack,
double value, ref float pX, ref float pY)
{
if (isY)
{
if (axis.IsCategory())
{
pY = grid.context.y + i * categoryWidth + (axis.boundaryGap ? 0 : -categoryWidth * 0.5f);
}
else
{
if (axis.context.minMaxRange <= 0) pY = grid.context.y;
2022-03-04 22:17:32 +08:00
else
{
var valueLen = (float)((value - axis.context.minValue) / axis.context.minMaxRange) * grid.context.height;
pY = grid.context.y + valueLen - categoryWidth * 0.5f;
}
2021-12-11 18:26:28 +08:00
}
2022-01-13 21:45:59 +08:00
pX = AxisHelper.GetAxisValuePosition(grid, relativedAxis, categoryWidth, 0);
2021-12-11 18:26:28 +08:00
if (isStack)
{
for (int n = 0; n < m_StackSerieData.Count - 1; n++)
pX += m_StackSerieData[n][i].context.stackHeight;
}
}
else
{
if (axis.IsCategory())
{
pX = grid.context.x + i * categoryWidth + (axis.boundaryGap ? 0 : -categoryWidth * 0.5f);
}
else
{
if (axis.context.minMaxRange <= 0) pX = grid.context.x;
2022-03-04 22:17:32 +08:00
else
{
var valueLen = (float)((value - axis.context.minValue) / axis.context.minMaxRange) * grid.context.width;
pX = grid.context.x + valueLen - categoryWidth * 0.5f;
}
2021-12-11 18:26:28 +08:00
}
2022-01-13 21:45:59 +08:00
pY = AxisHelper.GetAxisValuePosition(grid, relativedAxis, categoryWidth, 0);
2021-12-11 18:26:28 +08:00
if (isStack)
{
for (int n = 0; n < m_StackSerieData.Count - 1; n++)
pY += m_StackSerieData[n][i].context.stackHeight;
}
}
}
2022-03-04 22:17:32 +08:00
private void UpdateRectPosition(GridCoord grid, bool isY, double yValue, float pX, float pY, float gap, float borderWidth,
2021-12-11 18:26:28 +08:00
float barWidth, float currHig,
out Vector3 plb, out Vector3 plt, out Vector3 prt, out Vector3 prb, out Vector3 top)
{
if (isY)
{
if (yValue < 0)
{
2022-03-04 22:17:32 +08:00
plt = new Vector3(pX - borderWidth, pY + gap + barWidth - borderWidth);
prt = new Vector3(pX + currHig + borderWidth, pY + gap + barWidth - borderWidth);
prb = new Vector3(pX + currHig + borderWidth, pY + gap + borderWidth);
plb = new Vector3(pX - borderWidth, pY + gap + borderWidth);
2021-12-11 18:26:28 +08:00
}
else
{
2022-03-04 22:17:32 +08:00
plt = new Vector3(pX + borderWidth, pY + gap + barWidth - borderWidth);
prt = new Vector3(pX + currHig - borderWidth, pY + gap + barWidth - borderWidth);
prb = new Vector3(pX + currHig - borderWidth, pY + gap + borderWidth);
plb = new Vector3(pX + borderWidth, pY + gap + borderWidth);
2021-12-11 18:26:28 +08:00
}
2022-03-04 22:17:32 +08:00
top = new Vector3(pX + currHig - borderWidth, pY + gap + barWidth / 2);
2021-12-11 18:26:28 +08:00
}
else
{
if (yValue < 0)
{
2022-03-04 22:17:32 +08:00
plb = new Vector3(pX + gap + borderWidth, pY - borderWidth);
plt = new Vector3(pX + gap + borderWidth, pY + currHig - borderWidth);
prt = new Vector3(pX + gap + barWidth - borderWidth, pY + currHig - borderWidth);
prb = new Vector3(pX + gap + barWidth - borderWidth, pY - borderWidth);
2021-12-11 18:26:28 +08:00
}
else
{
2022-03-04 22:17:32 +08:00
plb = new Vector3(pX + gap + borderWidth, pY + borderWidth);
plt = new Vector3(pX + gap + borderWidth, pY + currHig - borderWidth);
prt = new Vector3(pX + gap + barWidth - borderWidth, pY + currHig - borderWidth);
prb = new Vector3(pX + gap + barWidth - borderWidth, pY + borderWidth);
2021-12-11 18:26:28 +08:00
}
2022-03-04 22:17:32 +08:00
top = new Vector3(pX + gap + barWidth / 2, pY + currHig - borderWidth);
2021-12-11 18:26:28 +08:00
}
if (serie.clip)
{
plb = chart.ClampInGrid(grid, plb);
plt = chart.ClampInGrid(grid, plt);
prt = chart.ClampInGrid(grid, prt);
prb = chart.ClampInGrid(grid, prb);
top = chart.ClampInGrid(grid, top);
}
}
private void DrawNormalBar(VertexHelper vh, Serie serie, SerieData serieData, ItemStyle itemStyle, int colorIndex,
2022-03-04 22:17:32 +08:00
bool highlight, float gap, float barWidth, float pX, float pY, Vector3 plb, Vector3 plt, Vector3 prt,
2022-01-13 21:45:59 +08:00
Vector3 prb, bool isYAxis, GridCoord grid, Axis axis, Color32 areaColor, Color32 areaToColor)
{
2022-03-04 22:17:32 +08:00
DrawBarBackground(vh, serie, serieData, itemStyle, colorIndex, highlight, pX, pY, gap, barWidth, isYAxis, grid, axis);
var borderWidth = itemStyle.runtimeBorderWidth;
if (isYAxis)
{
if (serie.clip)
{
2021-11-23 13:20:07 +08:00
prb = chart.ClampInGrid(grid, prb);
plb = chart.ClampInGrid(grid, plb);
plt = chart.ClampInGrid(grid, plt);
prt = chart.ClampInGrid(grid, prt);
}
var itemWidth = Mathf.Abs(prb.x - plt.x);
var itemHeight = Mathf.Abs(prt.y - plb.y);
var center = new Vector3((plt.x + prb.x) / 2, (prt.y + plb.y) / 2);
if (itemWidth > 0 && itemHeight > 0)
{
var invert = center.x < plb.x;
if (ItemStyleHelper.IsNeedCorner(itemStyle))
{
2021-01-11 08:54:28 +08:00
UGL.DrawRoundRectangle(vh, center, itemWidth, itemHeight, areaColor, areaToColor, 0,
2021-11-23 13:20:07 +08:00
itemStyle.cornerRadius, isYAxis, chart.settings.cicleSmoothness, invert);
}
else
{
2021-11-23 13:20:07 +08:00
chart.DrawClipPolygon(vh, plb, plt, prt, prb, areaColor, areaToColor, serie.clip, grid);
}
UGL.DrawBorder(vh, center, itemWidth, itemHeight, borderWidth, itemStyle.borderColor,
2021-11-23 13:20:07 +08:00
itemStyle.borderToColor, 0, itemStyle.cornerRadius, isYAxis, chart.settings.cicleSmoothness, invert);
}
}
else
{
if (serie.clip)
{
2021-11-23 13:20:07 +08:00
prb = chart.ClampInGrid(grid, prb);
plb = chart.ClampInGrid(grid, plb);
plt = chart.ClampInGrid(grid, plt);
prt = chart.ClampInGrid(grid, prt);
}
var itemWidth = Mathf.Abs(prt.x - plb.x);
var itemHeight = Mathf.Abs(plt.y - prb.y);
var center = new Vector3((plb.x + prt.x) / 2, (plt.y + prb.y) / 2);
if (itemWidth > 0 && itemHeight > 0)
{
var invert = center.y < plb.y;
if (ItemStyleHelper.IsNeedCorner(itemStyle))
{
2021-01-11 08:54:28 +08:00
UGL.DrawRoundRectangle(vh, center, itemWidth, itemHeight, areaColor, areaToColor, 0,
2021-11-23 13:20:07 +08:00
itemStyle.cornerRadius, isYAxis, chart.settings.cicleSmoothness, invert);
}
else
{
2021-11-23 13:20:07 +08:00
chart.DrawClipPolygon(vh, ref prb, ref plb, ref plt, ref prt, areaColor, areaToColor,
2021-01-11 08:54:28 +08:00
serie.clip, grid);
}
UGL.DrawBorder(vh, center, itemWidth, itemHeight, borderWidth, itemStyle.borderColor,
2021-11-23 13:20:07 +08:00
itemStyle.borderToColor, 0, itemStyle.cornerRadius, isYAxis, chart.settings.cicleSmoothness, invert);
}
}
}
private void DrawZebraBar(VertexHelper vh, Serie serie, SerieData serieData, ItemStyle itemStyle, int colorIndex,
2022-03-04 22:17:32 +08:00
bool highlight, float gap, float barWidth, float pX, float pY, Vector3 plb, Vector3 plt, Vector3 prt,
2022-01-13 21:45:59 +08:00
Vector3 prb, bool isYAxis, GridCoord grid, Axis axis, Color32 barColor, Color32 barToColor)
{
2022-03-04 22:17:32 +08:00
DrawBarBackground(vh, serie, serieData, itemStyle, colorIndex, highlight, pX, pY, gap, barWidth, isYAxis, grid, axis);
if (isYAxis)
{
plt = (plb + plt) / 2;
prt = (prt + prb) / 2;
2021-11-23 13:20:07 +08:00
chart.DrawClipZebraLine(vh, plt, prt, barWidth / 2, serie.barZebraWidth, serie.barZebraGap,
2022-01-07 09:48:59 +08:00
barColor, barToColor, serie.clip, grid, grid.context.width);
}
else
{
plb = (prb + plb) / 2;
plt = (plt + prt) / 2;
2021-11-23 13:20:07 +08:00
chart.DrawClipZebraLine(vh, plb, plt, barWidth / 2, serie.barZebraWidth, serie.barZebraGap,
2022-01-07 09:48:59 +08:00
barColor, barToColor, serie.clip, grid, grid.context.height);
}
}
private void DrawCapsuleBar(VertexHelper vh, Serie serie, SerieData serieData, ItemStyle itemStyle, int colorIndex,
2022-03-04 22:17:32 +08:00
bool highlight, float gap, float barWidth, float pX, float pY, Vector3 plb, Vector3 plt, Vector3 prt,
2022-01-13 21:45:59 +08:00
Vector3 prb, bool isYAxis, GridCoord grid, Axis axis, Color32 areaColor, Color32 areaToColor)
{
2022-03-04 22:17:32 +08:00
DrawBarBackground(vh, serie, serieData, itemStyle, colorIndex, highlight, pX, pY, gap, barWidth, isYAxis, grid, axis);
var borderWidth = itemStyle.runtimeBorderWidth;
var radius = barWidth / 2 - borderWidth;
2020-05-27 09:09:45 +08:00
var isGradient = !ChartHelper.IsValueEqualsColor(areaColor, areaToColor);
if (isYAxis)
{
var diff = Vector3.right * radius;
if (plt.x < prt.x)
{
var pcl = (plt + plb) / 2 + diff;
var pcr = (prt + prb) / 2 - diff;
if (pcr.x > pcl.x)
{
2020-05-27 09:09:45 +08:00
if (isGradient)
{
var barLen = prt.x - plt.x;
var rectStartColor = Color32.Lerp(areaColor, areaToColor, radius / barLen);
var rectEndColor = Color32.Lerp(areaColor, areaToColor, (barLen - radius) / barLen);
2021-11-23 13:20:07 +08:00
chart.DrawClipPolygon(vh, plb + diff, plt + diff, prt - diff, prb - diff, rectStartColor,
2021-01-11 08:54:28 +08:00
rectEndColor, serie.clip, grid);
UGL.DrawSector(vh, pcl, radius, areaColor, rectStartColor, 180, 360, 1, isYAxis);
UGL.DrawSector(vh, pcr, radius, rectEndColor, areaToColor, 0, 180, 1, isYAxis);
2020-05-27 09:09:45 +08:00
}
else
{
2021-11-23 13:20:07 +08:00
chart.DrawClipPolygon(vh, plb + diff, plt + diff, prt - diff, prb - diff, areaColor,
2021-01-11 08:54:28 +08:00
areaToColor, serie.clip, grid);
UGL.DrawSector(vh, pcl, radius, areaColor, 180, 360);
UGL.DrawSector(vh, pcr, radius, areaToColor, 0, 180);
2020-05-27 09:09:45 +08:00
}
}
}
else if (plt.x > prt.x)
{
var pcl = (plt + plb) / 2 - diff;
var pcr = (prt + prb) / 2 + diff;
if (pcr.x < pcl.x)
{
2020-05-27 09:09:45 +08:00
if (isGradient)
{
var barLen = plt.x - prt.x;
var rectStartColor = Color32.Lerp(areaColor, areaToColor, radius / barLen);
var rectEndColor = Color32.Lerp(areaColor, areaToColor, (barLen - radius) / barLen);
2021-11-23 13:20:07 +08:00
chart.DrawClipPolygon(vh, plb - diff, plt - diff, prt + diff, prb + diff, rectStartColor,
2021-01-11 08:54:28 +08:00
rectEndColor, serie.clip, grid);
UGL.DrawSector(vh, pcl, radius, rectStartColor, areaColor, 0, 180, 1, isYAxis);
UGL.DrawSector(vh, pcr, radius, areaToColor, rectEndColor, 180, 360, 1, isYAxis);
2020-05-27 09:09:45 +08:00
}
else
{
2021-11-23 13:20:07 +08:00
chart.DrawClipPolygon(vh, plb - diff, plt - diff, prt + diff, prb + diff, areaColor,
2021-01-11 08:54:28 +08:00
areaToColor, serie.clip, grid);
UGL.DrawSector(vh, pcl, radius, areaColor, 0, 180);
UGL.DrawSector(vh, pcr, radius, areaToColor, 180, 360);
2020-05-27 09:09:45 +08:00
}
}
}
}
else
{
var diff = Vector3.up * radius;
if (plt.y > plb.y)
{
var pct = (plt + prt) / 2 - diff;
var pcb = (plb + prb) / 2 + diff;
if (pct.y > pcb.y)
{
2020-05-27 09:09:45 +08:00
if (isGradient)
{
var barLen = plt.y - plb.y;
var rectStartColor = Color32.Lerp(areaColor, areaToColor, radius / barLen);
var rectEndColor = Color32.Lerp(areaColor, areaToColor, (barLen - radius) / barLen);
2021-11-23 13:20:07 +08:00
chart.DrawClipPolygon(vh, prb + diff, plb + diff, plt - diff, prt - diff, rectStartColor,
2021-01-11 08:54:28 +08:00
rectEndColor, serie.clip, grid);
UGL.DrawSector(vh, pct, radius, rectEndColor, areaToColor, 270, 450, 1, isYAxis);
UGL.DrawSector(vh, pcb, radius, rectStartColor, areaColor, 90, 270, 1, isYAxis);
2020-05-27 09:09:45 +08:00
}
else
{
2021-11-23 13:20:07 +08:00
chart.DrawClipPolygon(vh, prb + diff, plb + diff, plt - diff, prt - diff, areaColor,
2021-01-11 08:54:28 +08:00
areaToColor, serie.clip, grid);
UGL.DrawSector(vh, pct, radius, areaToColor, 270, 450);
UGL.DrawSector(vh, pcb, radius, areaColor, 90, 270);
2020-05-27 09:09:45 +08:00
}
}
}
else if (plt.y < plb.y)
{
var pct = (plt + prt) / 2 + diff;
var pcb = (plb + prb) / 2 - diff;
if (pct.y < pcb.y)
{
2020-05-27 09:09:45 +08:00
if (isGradient)
{
var barLen = plb.y - plt.y;
var rectStartColor = Color32.Lerp(areaColor, areaToColor, radius / barLen);
var rectEndColor = Color32.Lerp(areaColor, areaToColor, (barLen - radius) / barLen);
2021-11-23 13:20:07 +08:00
chart.DrawClipPolygon(vh, prb - diff, plb - diff, plt + diff, prt + diff, rectStartColor,
2021-01-11 08:54:28 +08:00
rectEndColor, serie.clip, grid);
UGL.DrawSector(vh, pct, radius, rectEndColor, areaToColor, 90, 270, 1, isYAxis);
UGL.DrawSector(vh, pcb, radius, rectStartColor, areaColor, 270, 450, 1, isYAxis);
2020-05-27 09:09:45 +08:00
}
else
{
2021-11-23 13:20:07 +08:00
chart.DrawClipPolygon(vh, prb - diff, plb - diff, plt + diff, prt + diff, areaColor,
2021-01-11 08:54:28 +08:00
areaToColor, serie.clip, grid);
UGL.DrawSector(vh, pct, radius, areaToColor, 90, 270);
UGL.DrawSector(vh, pcb, radius, areaColor, 270, 450);
2020-05-27 09:09:45 +08:00
}
}
}
}
}
2021-01-11 08:54:28 +08:00
private void DrawBarBackground(VertexHelper vh, Serie serie, SerieData serieData, ItemStyle itemStyle,
2022-03-04 22:17:32 +08:00
int colorIndex, bool highlight, float pX, float pY, float gap, float barWidth, bool isYAxis,
2022-01-13 21:45:59 +08:00
GridCoord grid, Axis axis)
{
2021-11-23 13:20:07 +08:00
var color = SerieHelper.GetItemBackgroundColor(serie, serieData, chart.theme, colorIndex, highlight, false);
if (ChartHelper.IsClearColor(color)) return;
if (isYAxis)
{
2021-11-23 13:20:07 +08:00
var axisWidth = axis.axisLine.GetWidth(chart.theme.axis.lineWidth);
2022-03-04 22:17:32 +08:00
Vector3 plt = new Vector3(grid.context.x + axisWidth, pY + gap + barWidth);
Vector3 prt = new Vector3(grid.context.x + axisWidth + grid.context.width, pY + gap + barWidth);
Vector3 prb = new Vector3(grid.context.x + axisWidth + grid.context.width, pY + gap);
Vector3 plb = new Vector3(grid.context.x + axisWidth, pY + gap);
if (serie.barType == BarType.Capsule)
{
var radius = barWidth / 2;
var diff = Vector3.right * radius;
var pcl = (plt + plb) / 2 + diff;
var pcr = (prt + prb) / 2 - diff;
2021-11-23 13:20:07 +08:00
chart.DrawClipPolygon(vh, plb + diff, plt + diff, prt - diff, prb - diff, color, color, serie.clip, grid);
2021-01-11 08:54:28 +08:00
UGL.DrawSector(vh, pcl, radius, color, 180, 360);
UGL.DrawSector(vh, pcr, radius, color, 0, 180);
if (itemStyle.NeedShowBorder())
{
var borderWidth = itemStyle.borderWidth;
var borderColor = itemStyle.borderColor;
2021-11-23 13:20:07 +08:00
var smoothness = chart.settings.cicleSmoothness;
var inRadius = radius - borderWidth;
var outRadius = radius;
var p1 = plb + diff + Vector3.up * borderWidth / 2;
var p2 = prb - diff + Vector3.up * borderWidth / 2;
var p3 = plt + diff - Vector3.up * borderWidth / 2;
var p4 = prt - diff - Vector3.up * borderWidth / 2;
2021-01-11 08:54:28 +08:00
UGL.DrawLine(vh, p1, p2, borderWidth / 2, borderColor);
UGL.DrawLine(vh, p3, p4, borderWidth / 2, borderColor);
UGL.DrawDoughnut(vh, pcl, inRadius, outRadius, borderColor, ChartConst.clearColor32,
180, 360, smoothness);
UGL.DrawDoughnut(vh, pcr, inRadius, outRadius, borderColor, ChartConst.clearColor32,
0, 180, smoothness);
}
}
else
{
2021-11-23 13:20:07 +08:00
chart.DrawClipPolygon(vh, ref plb, ref plt, ref prt, ref prb, color, color, serie.clip, grid);
}
}
else
{
2021-11-23 13:20:07 +08:00
var axisWidth = axis.axisLine.GetWidth(chart.theme.axis.lineWidth);
2022-03-04 22:17:32 +08:00
Vector3 plb = new Vector3(pX + gap, grid.context.y + axisWidth);
Vector3 plt = new Vector3(pX + gap, grid.context.y + grid.context.height + axisWidth);
Vector3 prt = new Vector3(pX + gap + barWidth, grid.context.y + grid.context.height + axisWidth);
Vector3 prb = new Vector3(pX + gap + barWidth, grid.context.y + axisWidth);
if (serie.barType == BarType.Capsule)
{
var radius = barWidth / 2;
var diff = Vector3.up * radius;
var pct = (plt + prt) / 2 - diff;
var pcb = (plb + prb) / 2 + diff;
2021-11-23 13:20:07 +08:00
chart.DrawClipPolygon(vh, prb + diff, plb + diff, plt - diff, prt - diff, color, color,
2021-01-11 08:54:28 +08:00
serie.clip, grid);
UGL.DrawSector(vh, pct, radius, color, 270, 450);
UGL.DrawSector(vh, pcb, radius, color, 90, 270);
if (itemStyle.NeedShowBorder())
{
var borderWidth = itemStyle.borderWidth;
var borderColor = itemStyle.borderColor;
2021-11-23 13:20:07 +08:00
var smoothness = chart.settings.cicleSmoothness;
var inRadius = radius - borderWidth;
var outRadius = radius;
var p1 = plb + diff + Vector3.right * borderWidth / 2;
var p2 = plt - diff + Vector3.right * borderWidth / 2;
var p3 = prb + diff - Vector3.right * borderWidth / 2;
var p4 = prt - diff - Vector3.right * borderWidth / 2;
2021-01-11 08:54:28 +08:00
UGL.DrawLine(vh, p1, p2, borderWidth / 2, borderColor);
UGL.DrawLine(vh, p3, p4, borderWidth / 2, borderColor);
UGL.DrawDoughnut(vh, pct, inRadius, outRadius, borderColor, ChartConst.clearColor32,
270, 450, smoothness);
UGL.DrawDoughnut(vh, pcb, inRadius, outRadius, borderColor, ChartConst.clearColor32,
90, 270, smoothness);
}
}
else
{
2021-11-23 13:20:07 +08:00
chart.DrawClipPolygon(vh, ref prb, ref plb, ref plt, ref prt, color, color, serie.clip, grid);
2021-01-11 08:54:28 +08:00
}
}
}
}
}