mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-18 14:30:10 +00:00
增加Axis的Interval强制设置坐标轴分割间隔
This commit is contained in:
@@ -27,6 +27,7 @@ namespace XCharts
|
||||
SerializedProperty m_Show = prop.FindPropertyRelative("m_Show");
|
||||
SerializedProperty m_Type = prop.FindPropertyRelative("m_Type");
|
||||
SerializedProperty m_SplitNumber = prop.FindPropertyRelative("m_SplitNumber");
|
||||
SerializedProperty m_Interval = prop.FindPropertyRelative("m_Interval");
|
||||
SerializedProperty m_AxisLabel = prop.FindPropertyRelative("m_AxisLabel");
|
||||
SerializedProperty m_ShowSplitLine = prop.FindPropertyRelative("m_ShowSplitLine");
|
||||
SerializedProperty m_SplitLineType = prop.FindPropertyRelative("m_SplitLineType");
|
||||
@@ -74,6 +75,8 @@ namespace XCharts
|
||||
}
|
||||
EditorGUI.PropertyField(drawRect, m_SplitNumber);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
EditorGUI.PropertyField(drawRect, m_Interval);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
if (m_ShowSplitLine.boolValue)
|
||||
{
|
||||
drawRect.width = EditorGUIUtility.labelWidth + 10;
|
||||
@@ -140,7 +143,7 @@ namespace XCharts
|
||||
SerializedProperty m_AxisLabel = prop.FindPropertyRelative("m_AxisLabel");
|
||||
SerializedProperty m_SplitArea = prop.FindPropertyRelative("m_SplitArea");
|
||||
float height = 0;
|
||||
height += 10 * EditorGUIUtility.singleLineHeight + 9 * EditorGUIUtility.standardVerticalSpacing;
|
||||
height += 11 * EditorGUIUtility.singleLineHeight + 10 * EditorGUIUtility.standardVerticalSpacing;
|
||||
Axis.AxisType type = (Axis.AxisType)m_Type.enumValueIndex;
|
||||
if (type == Axis.AxisType.Category)
|
||||
{
|
||||
|
||||
@@ -84,6 +84,7 @@ namespace XCharts
|
||||
[SerializeField] protected int m_Min;
|
||||
[SerializeField] protected int m_Max;
|
||||
[SerializeField] protected int m_SplitNumber = 5;
|
||||
[SerializeField] protected float m_Interval = 0;
|
||||
[SerializeField] protected bool m_ShowSplitLine = false;
|
||||
[SerializeField] protected SplitLineType m_SplitLineType = SplitLineType.Dashed;
|
||||
[SerializeField] protected bool m_BoundaryGap = true;
|
||||
@@ -94,6 +95,8 @@ namespace XCharts
|
||||
[SerializeField] protected AxisLabel m_AxisLabel = AxisLabel.defaultAxisLabel;
|
||||
[SerializeField] protected AxisSplitArea m_SplitArea = AxisSplitArea.defaultSplitArea;
|
||||
|
||||
[NonSerialized] private float m_ValueRange;
|
||||
|
||||
/// <summary>
|
||||
/// Set this to false to prevent the axis from showing.
|
||||
/// 是否显示坐标轴。
|
||||
@@ -125,6 +128,11 @@ namespace XCharts
|
||||
/// </summary>
|
||||
public int splitNumber { get { return m_SplitNumber; } set { m_SplitNumber = value; } }
|
||||
/// <summary>
|
||||
/// 强制设置坐标轴分割间隔。无法在类目轴中使用。
|
||||
/// Compulsively set segmentation interval for axis.This is unavailable for category axis.
|
||||
/// </summary>
|
||||
public float interval { get { return m_Interval; } set { m_Interval = value; } }
|
||||
/// <summary>
|
||||
/// showSplitLineSet this to false to prevent the splitLine from showing. value type axes are shown by default, while category type axes are hidden.
|
||||
/// 是否显示分隔线。默认数值轴显示,类目轴不显示。
|
||||
/// </summary>
|
||||
@@ -210,6 +218,7 @@ namespace XCharts
|
||||
m_Min = other.min;
|
||||
m_Max = other.max;
|
||||
m_SplitNumber = other.splitNumber;
|
||||
m_Interval = other.interval;
|
||||
|
||||
m_ShowSplitLine = other.showSplitLine;
|
||||
m_SplitLineType = other.splitLineType;
|
||||
@@ -336,9 +345,24 @@ namespace XCharts
|
||||
/// </summary>
|
||||
/// <param name="dataZoom"></param>
|
||||
/// <returns></returns>
|
||||
public int GetSplitNumber(DataZoom dataZoom)
|
||||
public int GetSplitNumber(float coordinateWid, DataZoom dataZoom)
|
||||
{
|
||||
if (type == AxisType.Value) return m_SplitNumber;
|
||||
if (type == AxisType.Value)
|
||||
{
|
||||
if (m_Interval > 0)
|
||||
{
|
||||
if (coordinateWid <= 0) return 0;
|
||||
int num = Mathf.CeilToInt(m_ValueRange / m_Interval) + 1;
|
||||
int maxNum = Mathf.CeilToInt(coordinateWid / 15);
|
||||
if (num > maxNum)
|
||||
{
|
||||
m_Interval = m_ValueRange / (maxNum - 1);
|
||||
num = Mathf.CeilToInt(m_ValueRange / m_Interval) + 1;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
else return m_SplitNumber;
|
||||
}
|
||||
int dataCount = GetDataList(dataZoom).Count;
|
||||
if (dataCount > 2 * m_SplitNumber || dataCount <= 0)
|
||||
return m_SplitNumber;
|
||||
@@ -354,7 +378,8 @@ namespace XCharts
|
||||
/// <returns></returns>
|
||||
public float GetSplitWidth(float coordinateWidth, DataZoom dataZoom)
|
||||
{
|
||||
return coordinateWidth / (m_BoundaryGap ? GetSplitNumber(dataZoom) : GetSplitNumber(dataZoom) - 1);
|
||||
int split = GetSplitNumber(coordinateWidth, dataZoom);
|
||||
return coordinateWidth / (m_BoundaryGap ? split : split - 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -388,11 +413,21 @@ namespace XCharts
|
||||
/// <param name="maxValue"></param>
|
||||
/// <param name="dataZoom"></param>
|
||||
/// <returns></returns>
|
||||
public string GetLabelName(int index, float minValue, float maxValue, DataZoom dataZoom)
|
||||
public string GetLabelName(float coordinateWidth, int index, float minValue, float maxValue, DataZoom dataZoom)
|
||||
{
|
||||
int split = GetSplitNumber(coordinateWidth, dataZoom);
|
||||
if (m_Type == AxisType.Value)
|
||||
{
|
||||
float value = (minValue + (maxValue - minValue) * index / (GetSplitNumber(dataZoom) - 1));
|
||||
float value = 0;
|
||||
if (m_Interval > 0)
|
||||
{
|
||||
if (index == split - 1) value = maxValue;
|
||||
else value = minValue + index * m_Interval;
|
||||
}
|
||||
else
|
||||
{
|
||||
value = (minValue + (maxValue - minValue) * index / (split - 1));
|
||||
}
|
||||
if (_cacheValue2str.ContainsKey(value)) return _cacheValue2str[value];
|
||||
else
|
||||
{
|
||||
@@ -407,13 +442,13 @@ namespace XCharts
|
||||
int dataCount = showData.Count;
|
||||
if (dataCount <= 0) return "";
|
||||
|
||||
if (index == GetSplitNumber(dataZoom) - 1 && !m_BoundaryGap)
|
||||
if (index == split - 1 && !m_BoundaryGap)
|
||||
{
|
||||
return showData[dataCount - 1];
|
||||
}
|
||||
else
|
||||
{
|
||||
float rate = dataCount / GetSplitNumber(dataZoom);
|
||||
float rate = dataCount / split;
|
||||
if (rate < 1) rate = 1;
|
||||
int offset = m_BoundaryGap ? (int)(rate / 2) : 0;
|
||||
int newIndex = (int)(index * rate >= dataCount - 1 ?
|
||||
@@ -427,11 +462,12 @@ namespace XCharts
|
||||
/// </summary>
|
||||
/// <param name="dataZoom"></param>
|
||||
/// <returns></returns>
|
||||
public int GetScaleNumber(DataZoom dataZoom)
|
||||
public int GetScaleNumber(float coordinateWidth, DataZoom dataZoom)
|
||||
{
|
||||
if (type == AxisType.Value)
|
||||
{
|
||||
return m_BoundaryGap ? m_SplitNumber + 1 : m_SplitNumber;
|
||||
int splitNum = GetSplitNumber(coordinateWidth, dataZoom);
|
||||
return m_BoundaryGap ? splitNum + 1 : splitNum;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -450,24 +486,33 @@ namespace XCharts
|
||||
/// <param name="coordinateWidth"></param>
|
||||
/// <param name="dataZoom"></param>
|
||||
/// <returns></returns>
|
||||
public float GetScaleWidth(float coordinateWidth, DataZoom dataZoom)
|
||||
public float GetScaleWidth(float coordinateWidth, int index, DataZoom dataZoom)
|
||||
{
|
||||
int num = GetScaleNumber(dataZoom) - 1;
|
||||
int num = GetScaleNumber(coordinateWidth, dataZoom) - 1;
|
||||
if (num <= 0) num = 1;
|
||||
return coordinateWidth / num;
|
||||
if (type == AxisType.Value && m_Interval > 0)
|
||||
{
|
||||
if (index == num - 1) return coordinateWidth - (num - 1) * m_Interval * coordinateWidth / m_ValueRange;
|
||||
else return m_Interval * coordinateWidth / m_ValueRange;
|
||||
}
|
||||
else
|
||||
{
|
||||
return coordinateWidth / num;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新刻度标签文字
|
||||
/// </summary>
|
||||
/// <param name="dataZoom"></param>
|
||||
public void UpdateLabelText(DataZoom dataZoom)
|
||||
public void UpdateLabelText(float coordinateWidth, DataZoom dataZoom)
|
||||
{
|
||||
for (int i = 0; i < axisLabelTextList.Count; i++)
|
||||
{
|
||||
if (axisLabelTextList[i] != null)
|
||||
{
|
||||
axisLabelTextList[i].text = GetLabelName(i, minValue, maxValue, dataZoom);
|
||||
axisLabelTextList[i].text = GetLabelName(coordinateWidth, i, minValue, maxValue, dataZoom);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -554,6 +599,7 @@ namespace XCharts
|
||||
break;
|
||||
}
|
||||
}
|
||||
m_ValueRange = maxValue - minValue;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
@@ -583,6 +629,7 @@ namespace XCharts
|
||||
min == other.min &&
|
||||
max == other.max &&
|
||||
splitNumber == other.splitNumber &&
|
||||
interval == other.interval &&
|
||||
showSplitLine == other.showSplitLine &&
|
||||
m_AxisLabel.Equals(other.axisLabel) &&
|
||||
splitLineType == other.splitLineType &&
|
||||
@@ -637,6 +684,7 @@ namespace XCharts
|
||||
axis.min = min;
|
||||
axis.max = max;
|
||||
axis.splitNumber = splitNumber;
|
||||
axis.interval = interval;
|
||||
|
||||
axis.showSplitLine = showSplitLine;
|
||||
axis.splitLineType = splitLineType;
|
||||
@@ -689,6 +737,7 @@ namespace XCharts
|
||||
axis.min = min;
|
||||
axis.max = max;
|
||||
axis.splitNumber = splitNumber;
|
||||
axis.interval = interval;
|
||||
|
||||
axis.showSplitLine = showSplitLine;
|
||||
axis.splitLineType = splitLineType;
|
||||
|
||||
@@ -145,11 +145,6 @@ namespace XCharts
|
||||
/// <returns>the added serie</returns>
|
||||
public virtual Serie AddSerie(string serieName, SerieType type, bool show = true)
|
||||
{
|
||||
if(type != SerieType.Pie)
|
||||
{
|
||||
m_Legend.AddData(serieName);
|
||||
}
|
||||
|
||||
return m_Series.AddSerie(serieName, type);
|
||||
}
|
||||
|
||||
|
||||
@@ -345,7 +345,7 @@ namespace XCharts
|
||||
yAxis.minValue = 0;
|
||||
yAxis.maxValue = 100;
|
||||
yAxis.axisLabelTextList.Clear();
|
||||
float labelWidth = yAxis.GetScaleWidth(coordinateHig, m_DataZoom);
|
||||
|
||||
string objName = yAxisIndex > 0 ? s_DefaultAxisY + "2" : s_DefaultAxisY;
|
||||
|
||||
var axisObj = ChartHelper.AddObject(objName, transform, chartAnchorMin,
|
||||
@@ -357,7 +357,9 @@ namespace XCharts
|
||||
var labelColor = yAxis.axisLabel.color == Color.clear ?
|
||||
(Color)m_ThemeInfo.axisTextColor :
|
||||
yAxis.axisLabel.color;
|
||||
for (int i = 0; i < yAxis.GetSplitNumber(m_DataZoom); i++)
|
||||
int splitNumber = yAxis.GetSplitNumber(coordinateHig, m_DataZoom);
|
||||
float totalWidth = 0;
|
||||
for (int i = 0; i < splitNumber; i++)
|
||||
{
|
||||
Text txt;
|
||||
bool inside = yAxis.axisLabel.inside;
|
||||
@@ -375,12 +377,15 @@ namespace XCharts
|
||||
Vector2.zero, new Vector2(1, 0.5f), new Vector2(m_Grid.left, 20),
|
||||
yAxis.axisLabel.fontSize, yAxis.axisLabel.rotate, yAxis.axisLabel.fontStyle);
|
||||
}
|
||||
float labelWidth = yAxis.GetScaleWidth(coordinateHig, i, m_DataZoom);
|
||||
|
||||
txt.transform.localPosition = GetLabelYPosition(labelWidth, i, yAxisIndex, yAxis);
|
||||
txt.text = yAxis.GetLabelName(i, yAxis.minValue, yAxis.maxValue, m_DataZoom);
|
||||
txt.transform.localPosition = GetLabelYPosition(totalWidth + (yAxis.boundaryGap ? labelWidth / 2 : 0), i, yAxisIndex, yAxis);
|
||||
|
||||
txt.text = yAxis.GetLabelName(coordinateHig, i, yAxis.minValue, yAxis.maxValue, m_DataZoom);
|
||||
txt.gameObject.SetActive(yAxis.show &&
|
||||
(yAxis.axisLabel.interval == 0 || i % (yAxis.axisLabel.interval + 1) == 0));
|
||||
yAxis.axisLabelTextList.Add(txt);
|
||||
totalWidth += labelWidth;
|
||||
}
|
||||
if (yAxis.axisName.show)
|
||||
{
|
||||
@@ -447,7 +452,7 @@ namespace XCharts
|
||||
xAxis.minValue = 0;
|
||||
xAxis.maxValue = 100;
|
||||
xAxis.axisLabelTextList.Clear();
|
||||
float labelWidth = xAxis.GetScaleWidth(coordinateWid, m_DataZoom);
|
||||
|
||||
string objName = xAxisIndex > 0 ? ChartHelper.Cancat(s_DefaultAxisX, 2) : s_DefaultAxisX;
|
||||
var axisObj = ChartHelper.AddObject(objName, transform, chartAnchorMin,
|
||||
chartAnchorMax, chartPivot, new Vector2(chartWidth, chartHeight));
|
||||
@@ -457,16 +462,21 @@ namespace XCharts
|
||||
var labelColor = xAxis.axisLabel.color == Color.clear ?
|
||||
(Color)m_ThemeInfo.axisTextColor :
|
||||
xAxis.axisLabel.color;
|
||||
for (int i = 0; i < xAxis.GetSplitNumber(m_DataZoom); i++)
|
||||
int splitNumber = xAxis.GetSplitNumber(coordinateWid, m_DataZoom);
|
||||
float totalWidth = 0;
|
||||
for (int i = 0; i < splitNumber; i++)
|
||||
{
|
||||
float labelWidth = xAxis.GetScaleWidth(coordinateWid, i, m_DataZoom);
|
||||
bool inside = xAxis.axisLabel.inside;
|
||||
Text txt = ChartHelper.AddTextObject(ChartHelper.Cancat(objName, i), axisObj.transform,
|
||||
m_ThemeInfo.font, labelColor, TextAnchor.MiddleCenter, new Vector2(0, 1),
|
||||
new Vector2(0, 1), new Vector2(1, 0.5f), new Vector2(labelWidth, 20),
|
||||
xAxis.axisLabel.fontSize, xAxis.axisLabel.rotate, xAxis.axisLabel.fontStyle);
|
||||
|
||||
txt.transform.localPosition = GetLabelXPosition(labelWidth, i, xAxisIndex, xAxis);
|
||||
txt.text = xAxis.GetLabelName(i, xAxis.minValue, xAxis.maxValue, m_DataZoom);
|
||||
txt.transform.localPosition = GetLabelXPosition(totalWidth + (xAxis.boundaryGap ? labelWidth : labelWidth / 2),
|
||||
i, xAxisIndex, xAxis);
|
||||
totalWidth += labelWidth;
|
||||
txt.text = xAxis.GetLabelName(coordinateWid, i, xAxis.minValue, xAxis.maxValue, m_DataZoom);
|
||||
txt.gameObject.SetActive(xAxis.show &&
|
||||
(xAxis.axisLabel.interval == 0 || i % (xAxis.axisLabel.interval + 1) == 0));
|
||||
xAxis.axisLabelTextList.Add(txt);
|
||||
@@ -560,14 +570,7 @@ namespace XCharts
|
||||
{
|
||||
posX = startX - yAxis.axisLabel.margin;
|
||||
}
|
||||
if (yAxis.boundaryGap)
|
||||
{
|
||||
return new Vector3(posX, coordinateY + (i + 0.5f) * scaleWid, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Vector3(posX, coordinateY + i * scaleWid, 0);
|
||||
}
|
||||
return new Vector3(posX, coordinateY + scaleWid, 0);
|
||||
}
|
||||
|
||||
private Vector3 GetLabelXPosition(float scaleWid, int i, int xAxisIndex, XAxis xAxis)
|
||||
@@ -583,14 +586,15 @@ namespace XCharts
|
||||
{
|
||||
posY = startY - xAxis.axisLabel.margin - xAxis.axisLabel.fontSize / 2;
|
||||
}
|
||||
if (xAxis.boundaryGap)
|
||||
{
|
||||
return new Vector3(coordinateX + (i + 1) * scaleWid, posY);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Vector3(coordinateX + (i + 1 - 0.5f) * scaleWid, posY);
|
||||
}
|
||||
// if (xAxis.boundaryGap)
|
||||
// {
|
||||
// return new Vector3(coordinateX + (i + 1) * scaleWid, posY);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return new Vector3(coordinateX + (i + 1 - 0.5f) * scaleWid, posY);
|
||||
// }
|
||||
return new Vector3(coordinateX + scaleWid, posY);
|
||||
}
|
||||
|
||||
private void CheckCoordinate()
|
||||
@@ -683,7 +687,8 @@ namespace XCharts
|
||||
axis.maxValue < 0 ? coordinateHig :
|
||||
Mathf.Abs(axis.minValue) * (coordinateHig / (Mathf.Abs(axis.minValue) + Mathf.Abs(axis.maxValue)));
|
||||
}
|
||||
axis.UpdateLabelText(m_DataZoom);
|
||||
float coordinateWidth = axis is XAxis ? coordinateWid : coordinateHig;
|
||||
axis.UpdateLabelText(coordinateWidth, m_DataZoom);
|
||||
RefreshChart();
|
||||
}
|
||||
}
|
||||
@@ -748,12 +753,13 @@ namespace XCharts
|
||||
{
|
||||
if (yAxis.show)
|
||||
{
|
||||
var scaleWidth = yAxis.GetScaleWidth(coordinateHig, m_DataZoom);
|
||||
var size = yAxis.GetScaleNumber(m_DataZoom);
|
||||
var size = yAxis.GetScaleNumber(coordinateWid, m_DataZoom);
|
||||
var totalWidth = coordinateY;
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
var scaleWidth = yAxis.GetScaleWidth(coordinateHig, i, m_DataZoom);
|
||||
float pX = 0;
|
||||
float pY = coordinateY + i * scaleWidth;
|
||||
float pY = totalWidth;
|
||||
if (yAxis.boundaryGap && yAxis.axisTick.alignWithLabel)
|
||||
{
|
||||
pY -= scaleWidth / 2;
|
||||
@@ -787,6 +793,7 @@ namespace XCharts
|
||||
DrawSplitLine(vh, yAxis, yAxis.splitLineType, new Vector3(coordinateX, pY),
|
||||
new Vector3(coordinateX + coordinateWid, pY), m_ThemeInfo.axisSplitLineColor);
|
||||
}
|
||||
totalWidth += scaleWidth;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -795,11 +802,12 @@ namespace XCharts
|
||||
{
|
||||
if (xAxis.show)
|
||||
{
|
||||
var scaleWidth = xAxis.GetScaleWidth(coordinateWid, m_DataZoom);
|
||||
var size = xAxis.GetScaleNumber(m_DataZoom);
|
||||
var size = xAxis.GetScaleNumber(coordinateWid, m_DataZoom);
|
||||
var totalWidth = coordinateX;
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
float pX = coordinateX + i * scaleWidth;
|
||||
var scaleWidth = xAxis.GetScaleWidth(coordinateWid, i, m_DataZoom);
|
||||
float pX = totalWidth;
|
||||
float pY = 0;
|
||||
if (xAxis.boundaryGap && xAxis.axisTick.alignWithLabel)
|
||||
{
|
||||
@@ -834,6 +842,7 @@ namespace XCharts
|
||||
DrawSplitLine(vh, xAxis, xAxis.splitLineType, new Vector3(pX, coordinateY),
|
||||
new Vector3(pX, coordinateY + coordinateHig), m_ThemeInfo.axisSplitLineColor);
|
||||
}
|
||||
totalWidth += scaleWidth;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1124,7 +1133,7 @@ namespace XCharts
|
||||
for (int j = 0; j < serie.data.Count; j++)
|
||||
{
|
||||
var serieData = serie.data[j];
|
||||
if (serie.label.show )
|
||||
if (serie.label.show)
|
||||
{
|
||||
var pos = serie.dataPoints[j];
|
||||
var value = serieData.data[1];
|
||||
|
||||
Reference in New Issue
Block a user