Skip to content

Commit

Permalink
修复PieChart的Label无法自动更新的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
monitor1394 committed Aug 14, 2019
1 parent 8ea961d commit 614d340
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 10 deletions.
13 changes: 13 additions & 0 deletions Assets/XCharts/Scripts/UI/Component/Serie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,19 @@ public void UpdateData(int index, int dimension, float value)
}
}

public void UpdateDataName(int index, string name)
{
if (index >= 0 && index < m_Data.Count)
{
var serieData = m_Data[index];
serieData.name = name;
if (serieData.label != null)
{
serieData.label.text = name == null ? "" : name;
}
}
}

/// <summary>
/// 清除所有数据的高亮标志
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions Assets/XCharts/Scripts/UI/Component/Series.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,24 @@ public void UpdateData(string name, float value, int dataIndex = 0)
}
}

public void UpdateDataName(string serieName, string dataName, int dataIndex = 0)
{
var serie = GetSerie(serieName);
if (serie != null)
{
serie.UpdateDataName(dataIndex, dataName);
}
}

public void UpdateDataName(int serieIndex, string dataName, int dataIndex = 0)
{
var serie = GetSerie(serieIndex);
if (serie != null)
{
serie.UpdateDataName(dataIndex, dataName);
}
}

/// <summary>
/// 更新指定系列的维度Y数据
/// </summary>
Expand Down
15 changes: 10 additions & 5 deletions Assets/XCharts/Scripts/UI/Internal/BaseChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public partial class BaseChart : Graphic, IPointerDownHandler, IPointerUpHandler
[NonSerialized] private List<string> m_CheckSerieName = new List<string>();
[NonSerialized] private bool m_RefreshChart = false;
[NonSerialized] private bool m_RefreshLabel = false;
[NonSerialized] private bool m_ReinitLabel = false;

protected Vector2 chartAnchorMax { get { return rectTransform.anchorMax; } }
protected Vector2 chartAnchorMin { get { return rectTransform.anchorMin; } }
Expand Down Expand Up @@ -199,7 +200,6 @@ private void InitTitle()
private void InitLegend()
{
m_Legend.OnChanged();
ChartHelper.HideAllObject(transform, s_LegendObjectName);
TextAnchor anchor = m_Legend.location.textAnchor;
Vector2 anchorMin = m_Legend.location.anchorMin;
Vector2 anchorMax = m_Legend.location.anchorMax;
Expand All @@ -208,8 +208,8 @@ private void InitLegend()
var legendObject = ChartHelper.AddObject(s_LegendObjectName, transform, anchorMin, anchorMax,
pivot, new Vector2(chartWidth, chartHeight));
legendObject.transform.localPosition = m_Legend.location.GetPosition(chartWidth, chartHeight);
ChartHelper.HideAllObject(legendObject, s_LegendObjectName);

ChartHelper.DestoryAllChilds(legendObject.transform);
if (!m_Legend.show) return;
var serieNameList = m_Series.GetSerieNameList();
List<string> datas;
if (m_Legend.data.Count > 0)
Expand Down Expand Up @@ -287,9 +287,9 @@ private void InitLegend()

private void InitSerieLabel()
{
ChartHelper.HideAllObject(transform, s_SerieLabelObjectName);
var labelObject = ChartHelper.AddObject(s_SerieLabelObjectName, transform, chartAnchorMin,
chartAnchorMax, chartPivot, new Vector2(chartWidth, chartHeight));
ChartHelper.DestoryAllChilds(labelObject.transform);
int count = 0;
for (int i = 0; i < m_Series.Count; i++)
{
Expand All @@ -308,7 +308,7 @@ private void InitSerieLabel()
m_ThemeInfo.font, color, TextAnchor.MiddleCenter, anchorMin, anchorMax, pivot,
new Vector2(50, serie.label.fontSize), serie.label.fontSize);
serieData.label.text = serieData.name;
serieData.label.gameObject.SetActive(true);
ChartHelper.SetActive(serieData.label.gameObject, false);
}
}
}
Expand Down Expand Up @@ -445,6 +445,11 @@ protected void CheckRefreshChart()

protected void CheckRefreshLabel()
{
if (m_ReinitLabel)
{
m_ReinitLabel = false;
InitSerieLabel();
}
if (m_RefreshLabel)
{
m_RefreshLabel = false;
Expand Down
62 changes: 57 additions & 5 deletions Assets/XCharts/Scripts/UI/Internal/BaseChart_API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ public virtual bool AddData(string serieName, float data, string dataName = null
public virtual bool AddData(int serieIndex, float data, string dataName = null)
{
var success = m_Series.AddData(serieIndex, data, dataName, m_MaxCacheDataNumber);
if (success) RefreshChart();
if (success)
{
RefreshChart();
ReinitChartLabel();
}
return success;
}

Expand All @@ -183,7 +187,11 @@ public virtual bool AddData(int serieIndex, float data, string dataName = null)
public virtual bool AddData(string serieName, List<float> multidimensionalData, string dataName = null)
{
var success = m_Series.AddData(serieName, multidimensionalData, dataName, m_MaxCacheDataNumber);
if (success) RefreshChart();
if (success)
{
RefreshChart();
ReinitChartLabel();
}
return success;
}

Expand All @@ -198,7 +206,11 @@ public virtual bool AddData(string serieName, List<float> multidimensionalData,
public virtual bool AddData(int serieIndex, List<float> multidimensionalData, string dataName = null)
{
var success = m_Series.AddData(serieIndex, multidimensionalData, dataName, m_MaxCacheDataNumber);
if (success) RefreshChart();
if (success)
{
RefreshChart();
ReinitChartLabel();
}
return success;
}

Expand All @@ -214,7 +226,11 @@ public virtual bool AddData(int serieIndex, List<float> multidimensionalData, st
public virtual bool AddData(string serieName, float xValue, float yValue, string dataName)
{
var success = m_Series.AddXYData(serieName, xValue, yValue, dataName, m_MaxCacheDataNumber);
if (success) RefreshChart();
if (success)
{
RefreshChart();
ReinitChartLabel();
}
return true;
}

Expand All @@ -230,7 +246,11 @@ public virtual bool AddData(string serieName, float xValue, float yValue, string
public virtual bool AddData(int serieIndex, float xValue, float yValue, string dataName = null)
{
var success = m_Series.AddXYData(serieIndex, xValue, yValue, dataName, m_MaxCacheDataNumber);
if (success) RefreshChart();
if (success)
{
RefreshChart();
ReinitChartLabel();
}
return success;
}

Expand Down Expand Up @@ -260,6 +280,30 @@ public virtual void UpdateData(int serieIndex, float value, int dataIndex = 0)
RefreshChart();
}

/// <summary>
/// Update serie data name.
/// 更新指定系列中的指定索引数据名称。
/// </summary>
/// <param name="serieName"></param>
/// <param name="dataName"></param>
/// <param name="dataIndex"></param>
public virtual void UpdateDataName(string serieName, string dataName, int dataIndex = 0)
{
m_Series.UpdateDataName(serieName, dataName, dataIndex);
}

/// <summary>
/// Update serie data name.
/// 更新指定系列中的指定索引数据名称。
/// </summary>
/// <param name="serieIndex"></param>
/// <param name="dataName"></param>
/// <param name="dataIndex"></param>
public virtual void UpdateDataName(int serieIndex, string dataName, int dataIndex)
{
m_Series.UpdateDataName(serieIndex, dataName, dataIndex);
}

/// <summary>
/// Whether to show serie.
/// 设置指定系列是否显示。
Expand Down Expand Up @@ -352,6 +396,14 @@ public void RefreshChart()
m_RefreshChart = true;
}

/// <summary>
/// 重新初始化Label。
/// </summary>
public void ReinitChartLabel()
{
m_ReinitLabel = true;
}

/// <summary>
/// Update chart theme.
/// 切换图表主题。
Expand Down
1 change: 1 addition & 0 deletions Assets/XCharts/Scripts/UI/PieChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ private void DrawLabel(Serie serie, SerieData serieData, PieTempData tempData, C
var isHighlight = (serieData.highlighted && serie.highlightLabel.show);
if (serie.label.show || isHighlight)
{
ChartHelper.SetActive(serieData.label.gameObject,true);
float rotate = 0;
bool isInsidePosition = serie.label.position == SerieLabel.Position.Inside;
if (serie.label.rotate > 0 && isInsidePosition)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ QQ交流群:XCharts交流群(202030963)

## 更新日志

* (2019.08.14)修复`PieChart``Label`无法自动更新的问题
* (2019.08.13)修复`UpdateData`接口无法更新数据的问题
* (2019.08.07)增加`SerieSymbol``Color``Opacity`配置
* (2019.08.07)发布`v0.8.2`版本
* (2019.08.07)修复区域平滑折线图显示异常的问题
Expand Down

0 comments on commit 614d340

Please sign in to comment.