mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-20 15:30:09 +00:00
优化坐标轴刻度和数据对齐
This commit is contained in:
@@ -96,10 +96,16 @@ namespace XCharts
|
||||
}
|
||||
EditorGUI.PropertyField(drawRect, m_SplitNumber);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
EditorGUI.PropertyField(drawRect, m_Interval);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
EditorGUI.PropertyField(drawRect, m_BoundaryGap);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
if (type == Axis.AxisType.Category)
|
||||
{
|
||||
EditorGUI.PropertyField(drawRect, m_BoundaryGap);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
}
|
||||
else
|
||||
{
|
||||
EditorGUI.PropertyField(drawRect, m_Interval);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
}
|
||||
DrawExtended(ref drawRect, prop);
|
||||
EditorGUI.PropertyField(drawRect, m_AxisLine);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
@@ -170,7 +176,7 @@ namespace XCharts
|
||||
}
|
||||
else
|
||||
{
|
||||
height += 1 * EditorGUIUtility.singleLineHeight + 1 * EditorGUIUtility.standardVerticalSpacing;
|
||||
height += 0 * EditorGUIUtility.singleLineHeight + 0 * EditorGUIUtility.standardVerticalSpacing;
|
||||
}
|
||||
if (m_ShowJsonDataArea)
|
||||
{
|
||||
@@ -179,7 +185,7 @@ namespace XCharts
|
||||
}
|
||||
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");
|
||||
if (m_MinMaxType.enumValueIndex == (int)Axis.AxisMinMaxType.Custom)
|
||||
{
|
||||
@@ -188,7 +194,7 @@ namespace XCharts
|
||||
}
|
||||
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");
|
||||
if (m_MinMaxType.enumValueIndex == (int)Axis.AxisMinMaxType.Custom)
|
||||
{
|
||||
|
||||
@@ -154,12 +154,12 @@ namespace XCharts
|
||||
set { if (PropertyUtility.SetStruct(ref m_Interval, value)) SetAllDirty(); }
|
||||
}
|
||||
/// <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>
|
||||
public bool boundaryGap
|
||||
{
|
||||
get { return m_BoundaryGap; }
|
||||
get { return IsCategory() ? m_BoundaryGap : false; }
|
||||
set { if (PropertyUtility.SetStruct(ref m_BoundaryGap, value)) SetAllDirty(); }
|
||||
}
|
||||
/// <summary>
|
||||
|
||||
@@ -527,8 +527,10 @@ namespace XCharts
|
||||
var labelColor = ChartHelper.IsClearColor(yAxis.axisLabel.color) ?
|
||||
(Color)m_ThemeInfo.axisTextColor :
|
||||
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 eachWidth = AxisHelper.GetEachWidth(yAxis, m_CoordinateHeight, m_DataZoom);
|
||||
float gapWidth = yAxis.boundaryGap ? eachWidth / 2 : 0;
|
||||
for (int i = 0; i < splitNumber; i++)
|
||||
{
|
||||
Text txt;
|
||||
@@ -550,7 +552,7 @@ namespace XCharts
|
||||
|
||||
float labelWidth = AxisHelper.GetScaleWidth(yAxis, m_CoordinateHeight, i, m_DataZoom);
|
||||
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);
|
||||
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) ?
|
||||
(Color)m_ThemeInfo.axisTextColor :
|
||||
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 eachWidth = AxisHelper.GetEachWidth(xAxis, m_CoordinateWidth, m_DataZoom);
|
||||
float gapWidth = xAxis.boundaryGap ? eachWidth / 2 : 0;
|
||||
for (int i = 0; i < splitNumber; i++)
|
||||
{
|
||||
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),
|
||||
xAxis.axisLabel.fontSize, xAxis.axisLabel.rotate, xAxis.axisLabel.fontStyle);
|
||||
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);
|
||||
totalWidth += labelWidth;
|
||||
var isPercentStack = SeriesHelper.IsPercentStack(m_Series, SerieType.Bar);
|
||||
@@ -1083,8 +1087,11 @@ namespace XCharts
|
||||
{
|
||||
pY += startY - xAxis.axisTick.length;
|
||||
}
|
||||
ChartDrawer.DrawLine(vh, new Vector3(pX, startY), new Vector3(pX, pY),
|
||||
AxisHelper.GetTickWidth(xAxis), m_ThemeInfo.axisLineColor);
|
||||
if (pX <= m_CoordinateX + m_CoordinateWidth + yAxis.axisLine.width)
|
||||
{
|
||||
ChartDrawer.DrawLine(vh, new Vector3(pX, startY), new Vector3(pX, pY),
|
||||
AxisHelper.GetTickWidth(xAxis), m_ThemeInfo.axisLineColor);
|
||||
}
|
||||
}
|
||||
totalWidth += scaleWidth;
|
||||
}
|
||||
@@ -1229,7 +1236,7 @@ namespace XCharts
|
||||
var xAxis = m_XAxises[i];
|
||||
var yAxis = m_YAxises[i];
|
||||
if (!xAxis.show) continue;
|
||||
if(m_Tooltip.runtimeXValues[i] < 0) continue;
|
||||
if (m_Tooltip.runtimeXValues[i] < 0) continue;
|
||||
float splitWidth = AxisHelper.GetDataWidth(xAxis, m_CoordinateWidth, dataCount, m_DataZoom);
|
||||
switch (m_Tooltip.type)
|
||||
{
|
||||
@@ -1275,7 +1282,7 @@ namespace XCharts
|
||||
var yAxis = m_YAxises[i];
|
||||
var xAxis = m_XAxises[i];
|
||||
if (!yAxis.show) continue;
|
||||
if(m_Tooltip.runtimeYValues[i] < 0) continue;
|
||||
if (m_Tooltip.runtimeYValues[i] < 0) continue;
|
||||
float splitWidth = AxisHelper.GetDataWidth(yAxis, m_CoordinateHeight, dataCount, m_DataZoom);
|
||||
switch (m_Tooltip.type)
|
||||
{
|
||||
|
||||
@@ -58,12 +58,22 @@ namespace XCharts
|
||||
{
|
||||
return axis.splitNumber;
|
||||
}
|
||||
int dataCount = axis.GetDataList(dataZoom).Count;
|
||||
if (axis.splitNumber <= 0) return dataCount;
|
||||
if (dataCount > 2 * axis.splitNumber || dataCount <= 0)
|
||||
return axis.splitNumber;
|
||||
else
|
||||
return dataCount;
|
||||
else if (axis.type == Axis.AxisType.Category)
|
||||
{
|
||||
int dataCount = axis.GetDataList(dataZoom).Count;
|
||||
if (dataCount <= 0) return 0;
|
||||
if (axis.splitNumber <= 0) return dataCount;
|
||||
if (dataCount >= 2 * axis.splitNumber)
|
||||
{
|
||||
int tick = dataCount / axis.splitNumber;
|
||||
return (int)dataCount / tick;
|
||||
}
|
||||
else
|
||||
{
|
||||
return dataCount;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -146,20 +156,12 @@ namespace XCharts
|
||||
var showData = axis.GetDataList(dataZoom);
|
||||
int dataCount = showData.Count;
|
||||
if (dataCount <= 0) return "";
|
||||
|
||||
if (index == split - 1 && !axis.boundaryGap)
|
||||
{
|
||||
return axis.axisLabel.GetFormatterContent(showData[dataCount - 1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
float rate = dataCount / split;
|
||||
if (rate < 1) rate = 1;
|
||||
int offset = axis.boundaryGap ? (int)(rate / 2) : 0;
|
||||
int newIndex = (int)(index * rate >= dataCount - 1 ?
|
||||
dataCount - 1 : offset + index * rate);
|
||||
return axis.axisLabel.GetFormatterContent(showData[newIndex]);
|
||||
}
|
||||
if (split == 1) return axis.axisLabel.GetFormatterContent(showData[0]);
|
||||
int rate = dataCount / (split - (axis.boundaryGap ? 0 : 1));
|
||||
if (rate < 1) rate = 1;
|
||||
int newIndex = index * rate;
|
||||
if (newIndex > dataCount - 1) return string.Empty;
|
||||
else return axis.axisLabel.GetFormatterContent(showData[newIndex]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -169,21 +171,8 @@ namespace XCharts
|
||||
/// <returns></returns>
|
||||
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);
|
||||
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;
|
||||
}
|
||||
int splitNum = GetSplitNumber(axis, coordinateWidth, dataZoom);
|
||||
return axis.boundaryGap ? splitNum + 1 : splitNum;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -198,12 +187,32 @@ namespace XCharts
|
||||
if (num <= 0) num = 1;
|
||||
if (axis.type == Axis.AxisType.Value && axis.interval > 0)
|
||||
{
|
||||
if (index == num - 1) return coordinateWidth - (num - 1) * axis.interval * coordinateWidth / axis.runtimeMinMaxRange;
|
||||
else return axis.interval * coordinateWidth / axis.runtimeMinMaxRange;
|
||||
return axis.interval * coordinateWidth / axis.runtimeMinMaxRange;
|
||||
}
|
||||
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