Skip to content

Commit

Permalink
修复Tooltip在对数轴时指示不准确的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
monitor1394 committed Nov 28, 2023
1 parent 7ea260b commit 0a19b2a
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 22 deletions.
3 changes: 2 additions & 1 deletion Documentation~/zh/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ slug: /changelog
* 增加`Axis`的对数轴子刻度的支持
* 增加`MarkLine``onTop`设置是否显示在最上层
* 增加`UITable`表格组件的轮播功能,重构`UITable`
* 完善注释和文档
* 完善代码注释和手册文档
* 修复若干问题

日志详情:

* (2023.11.28) 修复`Tooltip`在对数轴时指示不准确的问题
* (2023.11.24) 修复`Chart``UpdateData()`接口返回值不准确的问题
* (2023.11.24) 修复`Axis`的更新数据时效果不顺畅的问题
* (2023.11.23) 增加`Axis``Animation`支持动画效果
Expand Down
7 changes: 7 additions & 0 deletions Runtime/Component/Axis/Axis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,13 @@ public float GetDistance(double value, float axisLength)
var each = axisLength / data.Count;
return (float)(each * (value + 0.5f));
}
else if (IsLog())
{
var logValue = GetLogValue(value);
var logMin = GetLogValue(context.minValue);
var logMax = GetLogValue(context.maxValue);
return axisLength * (float)((logValue - logMin) / (logMax - logMin));
}
else
{
return axisLength * (float)((value - context.minValue) / context.minMaxRange);
Expand Down
26 changes: 19 additions & 7 deletions Runtime/Component/Axis/AxisHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,23 @@ protected virtual void UpdatePointerValue(Axis axis)
}
else
{
var xRate = axis.context.minMaxRange / grid.context.width;
var xValue = xRate * (chart.pointerPos.x - grid.context.x - axis.context.offset);
if (axis.context.minValue > 0)
xValue += axis.context.minValue;

double xValue;
if (axis.IsLog())
{
var logBase = axis.logBase;
var minLog = Math.Log(axis.context.minValue, logBase);
var maxLog = Math.Log(axis.context.maxValue, logBase);
var logRange = maxLog - minLog;
var pointerLog = minLog + logRange * (chart.pointerPos.x - grid.context.x - axis.context.offset) / grid.context.width;
xValue = Math.Pow(logBase, pointerLog);
}
else
{
var xRate = axis.context.minMaxRange / grid.context.width;
xValue = xRate * (chart.pointerPos.x - grid.context.x - axis.context.offset);
if (axis.context.minValue > 0)
xValue += axis.context.minValue;
}
var labelY = axis.GetLabelObjectPosition(0).y;
axis.context.pointerValue = xValue;
axis.context.pointerLabelPosition = new Vector3(chart.pointerPos.x, labelY);
Expand Down Expand Up @@ -153,7 +165,7 @@ internal void UpdateAxisMinMaxValue(int axisIndex, Axis axis, bool updateChart =
else
dataZoom.SetYAxisIndexValueInfo(axisIndex, ref tempMinValue, ref tempMaxValue);
}

if (tempMinValue != axis.context.destMinValue ||
tempMaxValue != axis.context.destMaxValue ||
m_LastInterval != axis.interval ||
Expand Down Expand Up @@ -898,7 +910,7 @@ protected void DrawAxisSplit(VertexHelper vh, AxisTheme theme, DataZoom dataZoom
if (isLogAxis)
{
var count = 0;
var logRange = (axis.logBase - 1f);
var logRange = axis.logBase - 1f;
minorTickDistance = scaleWidth * axis.GetLogValue(1 + (count + 1) * logRange / minorTickSplitNumber);
var tickTotal = lastSplitX + minorTickDistance;
while (tickTotal < current && count < minorTickSplitNumber - 1)
Expand Down
48 changes: 35 additions & 13 deletions Runtime/Component/Tooltip/TooltipHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public override void InitComponent()
InitTooltip(component);
}

public override void BeforceSerieUpdate()
{
UpdateTooltipData(component);
}

public override void Update()
{
UpdateTooltip(component);
Expand Down Expand Up @@ -86,8 +91,9 @@ private ChartLabel GetIndicatorLabel(Axis axis)
}
}

private void UpdateTooltip(Tooltip tooltip)
private void UpdateTooltipData(Tooltip tooltip)
{
showTooltip = false;
if (tooltip.trigger == Tooltip.Trigger.None) return;
if (!chart.isPointerInChart || !tooltip.show)
{
Expand All @@ -98,28 +104,48 @@ private void UpdateTooltip(Tooltip tooltip)
}
return;
}
var showTooltip = false;
for (int i = chart.series.Count - 1; i >= 0; i--)
{
var serie = chart.series[i];
if (!(serie is INeedSerieContainer))
{
showTooltip = true;
return;
}
}
containerSeries = ListPool<Serie>.Get();
UpdatePointerContainerAndSeriesAndTooltip(tooltip, ref containerSeries);
if (containerSeries.Count > 0)
{
showTooltip = true;
}
}

private bool showTooltip;
private List<Serie> containerSeries;
private void UpdateTooltip(Tooltip tooltip)
{
if (!showTooltip) return;
for (int i = chart.series.Count - 1; i >= 0; i--)
{
var serie = chart.series[i];
if (!(serie is INeedSerieContainer))
{
if (SetSerieTooltip(tooltip, serie))
{
showTooltip = true;
chart.RefreshTopPainter();
return;
}
}
}
var containerSeries = ListPool<Serie>.Get();
UpdatePointerContainerAndSeriesAndTooltip(tooltip, ref containerSeries);
if (containerSeries.Count > 0)
if (containerSeries != null)
{
if (SetSerieTooltip(tooltip, containerSeries))
showTooltip = true;
if (!SetSerieTooltip(tooltip, containerSeries))
{
showTooltip = false;
}
ListPool<Serie>.Release(containerSeries);
}
ListPool<Serie>.Release(containerSeries);
if (!showTooltip)
{
if (tooltip.context.type == Tooltip.Type.Corss && m_PointerContainer != null && m_PointerContainer.IsPointerEnter())
Expand All @@ -138,10 +164,6 @@ private void UpdateTooltip(Tooltip tooltip)
}
}

private void UpdateTooltipTypeAndTrigger(Tooltip tootip)
{
}

private void UpdateTooltipIndicatorLabelText(Tooltip tooltip)
{
if (!tooltip.show) return;
Expand Down
1 change: 1 addition & 0 deletions Runtime/Internal/BaseChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ protected override void Update()
CheckRefreshChart();
Internal_CheckAnimation();
foreach (var handler in m_SerieHandlers) handler.BeforeUpdate();
foreach (var handler in m_ComponentHandlers) handler.BeforceSerieUpdate();
foreach (var handler in m_SerieHandlers) handler.Update();
foreach (var handler in m_ComponentHandlers) handler.Update();
foreach (var handler in m_SerieHandlers) handler.AfterUpdate();
Expand Down
1 change: 1 addition & 0 deletions Runtime/Internal/Basic/MainComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public abstract class MainComponentHandler
public virtual void InitComponent() { }
public virtual void RemoveComponent() { }
public virtual void CheckComponent(StringBuilder sb) { }
public virtual void BeforceSerieUpdate() { }
public virtual void Update() { }
public virtual void DrawBase(VertexHelper vh) { }
public virtual void DrawUpper(VertexHelper vh) { }
Expand Down
14 changes: 14 additions & 0 deletions Runtime/Internal/Utilities/ChartCached.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static class ChartCached

private static Dictionary<double, Dictionary<string, string>> s_NumberToStr = new Dictionary<double, Dictionary<string, string>>();
private static Dictionary<int, Dictionary<string, string>> s_PrecisionToStr = new Dictionary<int, Dictionary<string, string>>();
private static Dictionary<string, Dictionary<int, string>> s_StringIntDict = new Dictionary<string, Dictionary<int, string>>();

public static string FloatToStr(double value, string numericFormatter = "F", int precision = 0)
{
Expand Down Expand Up @@ -113,6 +114,19 @@ public static string GetSerieLabelName(string prefix, int i, int j)
}
}

public static string GetString(string prefix, int suffix)
{
if (!s_StringIntDict.ContainsKey(prefix))
{
s_StringIntDict[prefix] = new Dictionary<int, string>();
}
if (!s_StringIntDict[prefix].ContainsKey(suffix))
{
s_StringIntDict[prefix][suffix] = prefix + suffix;
}
return s_StringIntDict[prefix][suffix];
}

internal static string GetComponentObjectName(MainComponent component)
{
Dictionary<int, string> dict;
Expand Down
3 changes: 2 additions & 1 deletion Runtime/Internal/Utilities/ChartHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,8 @@ public static ChartLabel AddChartLabel2(string name, Transform parent, LabelStyl
label.color = (!labelStyle.background.autoColor || autoColor == Color.clear) ?
labelStyle.background.color : autoColor;
label.sprite = labelStyle.background.sprite;
label.type = labelStyle.background.type;
if(label.type != labelStyle.background.type)
label.type = labelStyle.background.type;
}
else
{
Expand Down

0 comments on commit 0a19b2a

Please sign in to comment.