mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-29 12:48:47 +00:00
优化坐标轴刻度和数据对齐
This commit is contained in:
@@ -96,10 +96,16 @@ namespace XCharts
|
|||||||
}
|
}
|
||||||
EditorGUI.PropertyField(drawRect, m_SplitNumber);
|
EditorGUI.PropertyField(drawRect, m_SplitNumber);
|
||||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||||
EditorGUI.PropertyField(drawRect, m_Interval);
|
if (type == Axis.AxisType.Category)
|
||||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
{
|
||||||
EditorGUI.PropertyField(drawRect, m_BoundaryGap);
|
EditorGUI.PropertyField(drawRect, m_BoundaryGap);
|
||||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
EditorGUI.PropertyField(drawRect, m_Interval);
|
||||||
|
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||||
|
}
|
||||||
DrawExtended(ref drawRect, prop);
|
DrawExtended(ref drawRect, prop);
|
||||||
EditorGUI.PropertyField(drawRect, m_AxisLine);
|
EditorGUI.PropertyField(drawRect, m_AxisLine);
|
||||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||||
@@ -170,7 +176,7 @@ namespace XCharts
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
height += 1 * EditorGUIUtility.singleLineHeight + 1 * EditorGUIUtility.standardVerticalSpacing;
|
height += 0 * EditorGUIUtility.singleLineHeight + 0 * EditorGUIUtility.standardVerticalSpacing;
|
||||||
}
|
}
|
||||||
if (m_ShowJsonDataArea)
|
if (m_ShowJsonDataArea)
|
||||||
{
|
{
|
||||||
@@ -179,7 +185,7 @@ namespace XCharts
|
|||||||
}
|
}
|
||||||
else if (type == Axis.AxisType.Value)
|
else if (type == Axis.AxisType.Value)
|
||||||
{
|
{
|
||||||
height += 3 * EditorGUIUtility.singleLineHeight + 2 * EditorGUIUtility.standardVerticalSpacing;
|
height += 2 * EditorGUIUtility.singleLineHeight + 1 * EditorGUIUtility.standardVerticalSpacing;
|
||||||
SerializedProperty m_MinMaxType = prop.FindPropertyRelative("m_MinMaxType");
|
SerializedProperty m_MinMaxType = prop.FindPropertyRelative("m_MinMaxType");
|
||||||
if (m_MinMaxType.enumValueIndex == (int)Axis.AxisMinMaxType.Custom)
|
if (m_MinMaxType.enumValueIndex == (int)Axis.AxisMinMaxType.Custom)
|
||||||
{
|
{
|
||||||
@@ -188,7 +194,7 @@ namespace XCharts
|
|||||||
}
|
}
|
||||||
else if (type == Axis.AxisType.Log)
|
else if (type == Axis.AxisType.Log)
|
||||||
{
|
{
|
||||||
height += 2 * EditorGUIUtility.singleLineHeight + 1 * EditorGUIUtility.standardVerticalSpacing;
|
height += 1 * EditorGUIUtility.singleLineHeight + 1 * EditorGUIUtility.standardVerticalSpacing;
|
||||||
SerializedProperty m_MinMaxType = prop.FindPropertyRelative("m_MinMaxType");
|
SerializedProperty m_MinMaxType = prop.FindPropertyRelative("m_MinMaxType");
|
||||||
if (m_MinMaxType.enumValueIndex == (int)Axis.AxisMinMaxType.Custom)
|
if (m_MinMaxType.enumValueIndex == (int)Axis.AxisMinMaxType.Custom)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -154,12 +154,12 @@ namespace XCharts
|
|||||||
set { if (PropertyUtility.SetStruct(ref m_Interval, value)) SetAllDirty(); }
|
set { if (PropertyUtility.SetStruct(ref m_Interval, value)) SetAllDirty(); }
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The boundary gap on both sides of a coordinate axis.
|
/// The boundary gap on both sides of a coordinate axis, which is valid only for category axis with type: 'Category'.
|
||||||
/// 坐标轴两边是否留白。
|
/// 坐标轴两边是否留白。只对类目轴有效。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool boundaryGap
|
public bool boundaryGap
|
||||||
{
|
{
|
||||||
get { return m_BoundaryGap; }
|
get { return IsCategory() ? m_BoundaryGap : false; }
|
||||||
set { if (PropertyUtility.SetStruct(ref m_BoundaryGap, value)) SetAllDirty(); }
|
set { if (PropertyUtility.SetStruct(ref m_BoundaryGap, value)) SetAllDirty(); }
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -527,8 +527,10 @@ namespace XCharts
|
|||||||
var labelColor = ChartHelper.IsClearColor(yAxis.axisLabel.color) ?
|
var labelColor = ChartHelper.IsClearColor(yAxis.axisLabel.color) ?
|
||||||
(Color)m_ThemeInfo.axisTextColor :
|
(Color)m_ThemeInfo.axisTextColor :
|
||||||
yAxis.axisLabel.color;
|
yAxis.axisLabel.color;
|
||||||
int splitNumber = AxisHelper.GetSplitNumber(yAxis, m_CoordinateHeight, m_DataZoom);
|
int splitNumber = AxisHelper.GetScaleNumber(yAxis, m_CoordinateHeight, m_DataZoom);
|
||||||
float totalWidth = 0;
|
float totalWidth = 0;
|
||||||
|
float eachWidth = AxisHelper.GetEachWidth(yAxis, m_CoordinateHeight, m_DataZoom);
|
||||||
|
float gapWidth = yAxis.boundaryGap ? eachWidth / 2 : 0;
|
||||||
for (int i = 0; i < splitNumber; i++)
|
for (int i = 0; i < splitNumber; i++)
|
||||||
{
|
{
|
||||||
Text txt;
|
Text txt;
|
||||||
@@ -550,7 +552,7 @@ namespace XCharts
|
|||||||
|
|
||||||
float labelWidth = AxisHelper.GetScaleWidth(yAxis, m_CoordinateHeight, i, m_DataZoom);
|
float labelWidth = AxisHelper.GetScaleWidth(yAxis, m_CoordinateHeight, i, m_DataZoom);
|
||||||
if (i == 0) yAxis.axisLabel.SetRelatedText(txt, labelWidth);
|
if (i == 0) yAxis.axisLabel.SetRelatedText(txt, labelWidth);
|
||||||
txt.transform.localPosition = GetLabelYPosition(totalWidth + (yAxis.boundaryGap ? labelWidth / 2 : 0), i, yAxisIndex, yAxis);
|
txt.transform.localPosition = GetLabelYPosition(totalWidth + gapWidth, i, yAxisIndex, yAxis);
|
||||||
|
|
||||||
var isPercentStack = SeriesHelper.IsPercentStack(m_Series, SerieType.Bar);
|
var isPercentStack = SeriesHelper.IsPercentStack(m_Series, SerieType.Bar);
|
||||||
txt.text = AxisHelper.GetLabelName(yAxis, m_CoordinateHeight, i, yAxis.runtimeMinValue, yAxis.runtimeMaxValue, m_DataZoom, isPercentStack);
|
txt.text = AxisHelper.GetLabelName(yAxis, m_CoordinateHeight, i, yAxis.runtimeMinValue, yAxis.runtimeMaxValue, m_DataZoom, isPercentStack);
|
||||||
@@ -633,8 +635,10 @@ namespace XCharts
|
|||||||
var labelColor = ChartHelper.IsClearColor(xAxis.axisLabel.color) ?
|
var labelColor = ChartHelper.IsClearColor(xAxis.axisLabel.color) ?
|
||||||
(Color)m_ThemeInfo.axisTextColor :
|
(Color)m_ThemeInfo.axisTextColor :
|
||||||
xAxis.axisLabel.color;
|
xAxis.axisLabel.color;
|
||||||
int splitNumber = AxisHelper.GetSplitNumber(xAxis, m_CoordinateWidth, m_DataZoom);
|
int splitNumber = AxisHelper.GetScaleNumber(xAxis, m_CoordinateWidth, m_DataZoom);
|
||||||
float totalWidth = 0;
|
float totalWidth = 0;
|
||||||
|
float eachWidth = AxisHelper.GetEachWidth(xAxis, m_CoordinateWidth, m_DataZoom);
|
||||||
|
float gapWidth = xAxis.boundaryGap ? eachWidth / 2 : 0;
|
||||||
for (int i = 0; i < splitNumber; i++)
|
for (int i = 0; i < splitNumber; i++)
|
||||||
{
|
{
|
||||||
float labelWidth = AxisHelper.GetScaleWidth(xAxis, m_CoordinateWidth, i, m_DataZoom);
|
float labelWidth = AxisHelper.GetScaleWidth(xAxis, m_CoordinateWidth, i, m_DataZoom);
|
||||||
@@ -644,7 +648,7 @@ namespace XCharts
|
|||||||
new Vector2(0, 1), new Vector2(1, 0.5f), new Vector2(labelWidth, 20),
|
new Vector2(0, 1), new Vector2(1, 0.5f), new Vector2(labelWidth, 20),
|
||||||
xAxis.axisLabel.fontSize, xAxis.axisLabel.rotate, xAxis.axisLabel.fontStyle);
|
xAxis.axisLabel.fontSize, xAxis.axisLabel.rotate, xAxis.axisLabel.fontStyle);
|
||||||
if (i == 0) xAxis.axisLabel.SetRelatedText(txt, labelWidth);
|
if (i == 0) xAxis.axisLabel.SetRelatedText(txt, labelWidth);
|
||||||
txt.transform.localPosition = GetLabelXPosition(totalWidth + (xAxis.boundaryGap ? labelWidth : labelWidth / 2),
|
txt.transform.localPosition = GetLabelXPosition(totalWidth + labelWidth / 2 + gapWidth,
|
||||||
i, xAxisIndex, xAxis);
|
i, xAxisIndex, xAxis);
|
||||||
totalWidth += labelWidth;
|
totalWidth += labelWidth;
|
||||||
var isPercentStack = SeriesHelper.IsPercentStack(m_Series, SerieType.Bar);
|
var isPercentStack = SeriesHelper.IsPercentStack(m_Series, SerieType.Bar);
|
||||||
@@ -1083,9 +1087,12 @@ namespace XCharts
|
|||||||
{
|
{
|
||||||
pY += startY - xAxis.axisTick.length;
|
pY += startY - xAxis.axisTick.length;
|
||||||
}
|
}
|
||||||
|
if (pX <= m_CoordinateX + m_CoordinateWidth + yAxis.axisLine.width)
|
||||||
|
{
|
||||||
ChartDrawer.DrawLine(vh, new Vector3(pX, startY), new Vector3(pX, pY),
|
ChartDrawer.DrawLine(vh, new Vector3(pX, startY), new Vector3(pX, pY),
|
||||||
AxisHelper.GetTickWidth(xAxis), m_ThemeInfo.axisLineColor);
|
AxisHelper.GetTickWidth(xAxis), m_ThemeInfo.axisLineColor);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
totalWidth += scaleWidth;
|
totalWidth += scaleWidth;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,13 +58,23 @@ namespace XCharts
|
|||||||
{
|
{
|
||||||
return axis.splitNumber;
|
return axis.splitNumber;
|
||||||
}
|
}
|
||||||
|
else if (axis.type == Axis.AxisType.Category)
|
||||||
|
{
|
||||||
int dataCount = axis.GetDataList(dataZoom).Count;
|
int dataCount = axis.GetDataList(dataZoom).Count;
|
||||||
|
if (dataCount <= 0) return 0;
|
||||||
if (axis.splitNumber <= 0) return dataCount;
|
if (axis.splitNumber <= 0) return dataCount;
|
||||||
if (dataCount > 2 * axis.splitNumber || dataCount <= 0)
|
if (dataCount >= 2 * axis.splitNumber)
|
||||||
return axis.splitNumber;
|
{
|
||||||
|
int tick = dataCount / axis.splitNumber;
|
||||||
|
return (int)dataCount / tick;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
return dataCount;
|
return dataCount;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获得分割段的宽度
|
/// 获得分割段的宽度
|
||||||
@@ -146,20 +156,12 @@ namespace XCharts
|
|||||||
var showData = axis.GetDataList(dataZoom);
|
var showData = axis.GetDataList(dataZoom);
|
||||||
int dataCount = showData.Count;
|
int dataCount = showData.Count;
|
||||||
if (dataCount <= 0) return "";
|
if (dataCount <= 0) return "";
|
||||||
|
if (split == 1) return axis.axisLabel.GetFormatterContent(showData[0]);
|
||||||
if (index == split - 1 && !axis.boundaryGap)
|
int rate = dataCount / (split - (axis.boundaryGap ? 0 : 1));
|
||||||
{
|
|
||||||
return axis.axisLabel.GetFormatterContent(showData[dataCount - 1]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
float rate = dataCount / split;
|
|
||||||
if (rate < 1) rate = 1;
|
if (rate < 1) rate = 1;
|
||||||
int offset = axis.boundaryGap ? (int)(rate / 2) : 0;
|
int newIndex = index * rate;
|
||||||
int newIndex = (int)(index * rate >= dataCount - 1 ?
|
if (newIndex > dataCount - 1) return string.Empty;
|
||||||
dataCount - 1 : offset + index * rate);
|
else return axis.axisLabel.GetFormatterContent(showData[newIndex]);
|
||||||
return axis.axisLabel.GetFormatterContent(showData[newIndex]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -168,23 +170,10 @@ namespace XCharts
|
|||||||
/// <param name="dataZoom"></param>
|
/// <param name="dataZoom"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
internal static int GetScaleNumber(Axis axis, float coordinateWidth, DataZoom dataZoom = null)
|
internal static int GetScaleNumber(Axis axis, float coordinateWidth, DataZoom dataZoom = null)
|
||||||
{
|
|
||||||
if (axis.type == Axis.AxisType.Value || axis.type == Axis.AxisType.Log)
|
|
||||||
{
|
{
|
||||||
int splitNum = GetSplitNumber(axis, coordinateWidth, dataZoom);
|
int splitNum = GetSplitNumber(axis, coordinateWidth, dataZoom);
|
||||||
return axis.boundaryGap ? splitNum + 1 : splitNum;
|
return axis.boundaryGap ? splitNum + 1 : splitNum;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
var showData = axis.GetDataList(dataZoom);
|
|
||||||
int dataCount = showData.Count;
|
|
||||||
if (axis.splitNumber <= 0) return axis.boundaryGap ? dataCount + 1 : dataCount;
|
|
||||||
if (dataCount > 2 * axis.splitNumber || dataCount <= 0)
|
|
||||||
return axis.boundaryGap ? axis.splitNumber + 1 : axis.splitNumber;
|
|
||||||
else
|
|
||||||
return axis.boundaryGap ? dataCount + 1 : dataCount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获得分割段宽度
|
/// 获得分割段宽度
|
||||||
@@ -198,12 +187,32 @@ namespace XCharts
|
|||||||
if (num <= 0) num = 1;
|
if (num <= 0) num = 1;
|
||||||
if (axis.type == Axis.AxisType.Value && axis.interval > 0)
|
if (axis.type == Axis.AxisType.Value && axis.interval > 0)
|
||||||
{
|
{
|
||||||
if (index == num - 1) return coordinateWidth - (num - 1) * axis.interval * coordinateWidth / axis.runtimeMinMaxRange;
|
return axis.interval * coordinateWidth / axis.runtimeMinMaxRange;
|
||||||
else return axis.interval * coordinateWidth / axis.runtimeMinMaxRange;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return coordinateWidth / num;
|
if (axis.data.Count > 0)
|
||||||
|
{
|
||||||
|
int tick = axis.data.Count / num;
|
||||||
|
var count = axis.boundaryGap ? axis.data.Count : axis.data.Count - 1;
|
||||||
|
var each = coordinateWidth / count;
|
||||||
|
return each * tick;
|
||||||
|
}
|
||||||
|
else return coordinateWidth / num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static float GetEachWidth(Axis axis, float coordinateWidth, DataZoom dataZoom = null)
|
||||||
|
{
|
||||||
|
if (axis.data.Count > 0)
|
||||||
|
{
|
||||||
|
var count = axis.boundaryGap ? axis.data.Count : axis.data.Count - 1;
|
||||||
|
return count > 0 ? coordinateWidth / count : coordinateWidth;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int num = GetScaleNumber(axis, coordinateWidth, dataZoom) - 1;
|
||||||
|
return num > 0 ? coordinateWidth / num : coordinateWidth;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user