Skip to content

Commit

Permalink
增加Line平滑曲线对Dash虚线的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
monitor1394 committed Sep 22, 2023
1 parent 51697a5 commit c065245
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Documentation~/en/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,9 @@ The style of line.
|width|0||the width of line.
|length|0||the length of line.
|opacity|1||Opacity of the line. Supports value from 0 to 1, and the line will not be drawn when set to 0.
|dashLength|4|v3.8.1|the length of dash line. default value is 0, which means the length of dash line is 12 times of line width. Represents a multiple of the number of segments in a line chart.
|dotLength|2|v3.8.1|the length of dot line. default value is 0, which means the length of dot line is 2 times of line width. Represents a multiple of the number of segments in a line chart.
|gapLength|2|v3.8.1|the length of gap line. default value is 0, which means the length of gap line is 3 times of line width. Represents a multiple of the number of segments in a line chart.

```mdx-code-block
</APITable>
Expand Down
1 change: 1 addition & 0 deletions Documentation~/zh/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ slug: /changelog

## master

* (2023.09.22) 增加`Line`的平滑曲线对`Dash`虚线的支持
* (2023.09.16) 修复`Tooltip`在类目轴无数据时异常报错的问题 (#279)
* (2023.09.16) 修复`Pie`无数据时绘制异常的问题 (#278)
* (2023.09.12) 增加`Pie``radiusGradient`可设置半径方向的渐变效果
Expand Down
3 changes: 3 additions & 0 deletions Documentation~/zh/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,9 @@ Drawing grid in rectangular coordinate. Line chart, bar chart, and scatter chart
|width|0||线宽。
|length|0||线长。
|opacity|1||线的透明度。支持从 0 到 1 的数字,为 0 时不绘制该图形。
|dashLength|4|v3.8.1|虚线的长度。默认0时为线条宽度的12倍。在折线图中代表分割段数的倍数。
|dotLength|2|v3.8.1|点线的长度。默认0时为线条宽度的3倍。在折线图中代表分割段数的倍数。
|gapLength|2|v3.8.1|点线的长度。默认0时为线条宽度的3倍。在折线图中代表分割段数的倍数。

```mdx-code-block
</APITable>
Expand Down
3 changes: 3 additions & 0 deletions Editor/ChildComponents/LineStyleDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
PropertyField(prop, "m_Width");
PropertyField(prop, "m_Length");
PropertyField(prop, "m_Opacity");
PropertyField(prop, "m_DashLength");
PropertyField(prop, "m_DotLength");
PropertyField(prop, "m_GapLength");
--EditorGUI.indentLevel;
}
}
Expand Down
53 changes: 50 additions & 3 deletions Runtime/Component/Child/LineStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public enum Type
[SerializeField] private float m_Width = 0;
[SerializeField] private float m_Length = 0;
[SerializeField][Range(0, 1)] private float m_Opacity = 1;
[SerializeField][Since("v3.8.1")] private float m_DashLength = 4;
[SerializeField][Since("v3.8.1")] private float m_DotLength = 2;
[SerializeField][Since("v3.8.1")] private float m_GapLength = 2;

/// <summary>
/// Whether show line.
Expand Down Expand Up @@ -121,6 +124,39 @@ public float opacity
set { if (PropertyUtil.SetStruct(ref m_Opacity, value)) SetVerticesDirty(); }
}

/// <summary>
/// the length of dash line. default value is 0, which means the length of dash line is 12 times of line width.
/// Represents a multiple of the number of segments in a line chart.
/// |虚线的长度。默认0时为线条宽度的12倍。在折线图中代表分割段数的倍数。
/// </summary>
public float dashLength
{
get { return m_DashLength; }
set { if (PropertyUtil.SetStruct(ref m_DashLength, value)) SetVerticesDirty(); }
}

/// <summary>
/// the length of dot line. default value is 0, which means the length of dot line is 2 times of line width.
/// Represents a multiple of the number of segments in a line chart.
/// |点线的长度。默认0时为线条宽度的3倍。在折线图中代表分割段数的倍数。
/// </summary>
public float dotLength
{
get { return m_DotLength; }
set { if (PropertyUtil.SetStruct(ref m_DotLength, value)) SetVerticesDirty(); }
}

/// <summary>
/// the length of gap line. default value is 0, which means the length of gap line is 3 times of line width.
/// Represents a multiple of the number of segments in a line chart.
/// |点线的长度。默认0时为线条宽度的3倍。在折线图中代表分割段数的倍数。
/// </summary>
public float gapLength
{
get { return m_GapLength; }
set { if (PropertyUtil.SetStruct(ref m_GapLength, value)) SetVerticesDirty(); }
}

public LineStyle()
{ }

Expand Down Expand Up @@ -150,6 +186,9 @@ public LineStyle Clone()
lineStyle.toColor2 = toColor2;
lineStyle.width = width;
lineStyle.opacity = opacity;
lineStyle.dashLength = dashLength;
lineStyle.dotLength = dotLength;
lineStyle.gapLength = gapLength;
return lineStyle;
}

Expand All @@ -162,6 +201,14 @@ public void Copy(LineStyle lineStyle)
toColor2 = lineStyle.toColor2;
width = lineStyle.width;
opacity = lineStyle.opacity;
dashLength = lineStyle.dashLength;
dotLength = lineStyle.dotLength;
gapLength = lineStyle.gapLength;
}

public bool IsNotSolidLine()
{
return type != Type.Solid && type != Type.None;
}

public Color32 GetColor()
Expand All @@ -170,7 +217,7 @@ public Color32 GetColor()
return m_Color;

var color = m_Color;
color.a = (byte) (color.a * m_Opacity);
color.a = (byte)(color.a * m_Opacity);
return color;
}

Expand Down Expand Up @@ -201,7 +248,7 @@ public Color32 GetGradientColor(float value, Color32 defaultColor)
}
if (m_Opacity != 1)
{
color.a = (byte) (color.a * m_Opacity);
color.a = (byte)(color.a * m_Opacity);
}
return color;
}
Expand Down Expand Up @@ -230,7 +277,7 @@ public Color32 GetColor(Color32 themeColor)
else
{
var color = themeColor;
color.a = (byte) (color.a * opacity);
color.a = (byte)(color.a * opacity);
return color;
}
}
Expand Down
45 changes: 31 additions & 14 deletions Runtime/Serie/Line/LineHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,11 @@ internal static void DrawSerieLine(VertexHelper vh, ThemeStyle theme, Serie seri
var lineColor = SerieHelper.GetLineColor(serie, null, theme, serie.context.colorIndex);

var lastDataIsIgnore = datas[0].isIgnoreBreak;
var smooth = serie.lineType == LineType.Smooth;
var firstInGridPointIndex = serie.clip ? -1 : 1;
var segmentCount = 0;
var dashLength = serie.lineStyle.dashLength;
var gapLength = serie.lineStyle.gapLength;
var dotLength = serie.lineStyle.dotLength;
for (int i = 1; i < dataCount; i++)
{
var cdata = datas[i];
Expand Down Expand Up @@ -315,31 +318,45 @@ internal static void DrawSerieLine(VertexHelper vh, ThemeStyle theme, Serie seri
firstInGridPointIndex = i;
if (isClip) isIgnore = true;
}
if (!smooth)
if (serie.lineStyle.type == LineStyle.Type.None)
{
handled = true;
break;
}
{
segmentCount++;
var index = 0f;
switch (serie.lineStyle.type)
{
case LineStyle.Type.Dashed:
UGL.DrawDashLine(vh, lp, cp, lineWidth, lineColor, lineColor, 0, 0);
handled = true;
index = segmentCount % (dashLength + gapLength);
if (index >= dashLength)
isIgnore = true;
break;
case LineStyle.Type.Dotted:
UGL.DrawDotLine(vh, lp, cp, lineWidth, lineColor, lineColor, 0, 0);
handled = true;
index = segmentCount % (dotLength + gapLength);
if (index >= dotLength)
isIgnore = true;
break;
case LineStyle.Type.DashDot:
UGL.DrawDashDotLine(vh, lp, cp, lineWidth, lineColor, 0, 0, 0);
handled = true;
index = segmentCount % (dashLength + dotLength + 2 * gapLength);
if (index >= dashLength && index < dashLength + gapLength)
isIgnore = true;
else if (index >= dashLength + gapLength + dotLength)
isIgnore = true;
break;
case LineStyle.Type.DashDotDot:
UGL.DrawDashDotDotLine(vh, lp, cp, lineWidth, lineColor, 0, 0, 0);
handled = true;
break;
case LineStyle.Type.None:
handled = true;
index = segmentCount % (dashLength + 2 * dotLength + 3 * gapLength);
if (index >= dashLength && index < dashLength + gapLength)
isIgnore = true;
else if (index >= dashLength + gapLength + dotLength && index < dashLength + dotLength + 2 * gapLength)
isIgnore = true;
else if (index >= dashLength + 2 * gapLength + 2 * dotLength)
isIgnore = true;
break;
}
}

if (handled)
{
lastDataIsIgnore = isIgnore;
Expand Down Expand Up @@ -476,7 +493,7 @@ internal static void UpdateSerieDrawPoints(Serie serie, Settings setting, ThemeS
private static void UpdateNormalLineDrawPoints(Serie serie, Settings setting, VisualMap visualMap)
{
var isVisualMapGradient = VisualMapHelper.IsNeedGradient(visualMap);
if (isVisualMapGradient || serie.clip)
if (isVisualMapGradient || serie.clip || (serie.lineStyle.IsNotSolidLine()))
{
var dataPoints = serie.context.dataPoints;
if (dataPoints.Count > 1)
Expand Down

0 comments on commit c065245

Please sign in to comment.