mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-17 14:00:12 +00:00
增加Serie的barPercentStack参数,可配置百分比堆叠柱状图
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System.Net.Mime;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
@@ -435,12 +434,14 @@ namespace XCharts
|
||||
/// <param name="maxValue"></param>
|
||||
/// <param name="dataZoom"></param>
|
||||
/// <returns></returns>
|
||||
public string GetLabelName(float coordinateWidth, int index, float minValue, float maxValue, DataZoom dataZoom)
|
||||
public string GetLabelName(float coordinateWidth, int index, float minValue, float maxValue,
|
||||
DataZoom dataZoom, bool forcePercent)
|
||||
{
|
||||
int split = GetSplitNumber(coordinateWidth, dataZoom);
|
||||
if (m_Type == AxisType.Value)
|
||||
{
|
||||
float value = 0;
|
||||
if (forcePercent) maxValue = 100;
|
||||
if (m_Interval > 0)
|
||||
{
|
||||
if (index == split - 1) value = maxValue;
|
||||
@@ -450,7 +451,8 @@ namespace XCharts
|
||||
{
|
||||
value = (minValue + (maxValue - minValue) * index / (split - 1));
|
||||
}
|
||||
return m_AxisLabel.GetFormatterContent(value);
|
||||
if (forcePercent) return string.Format("{0}%", (int)value);
|
||||
else return m_AxisLabel.GetFormatterContent(value);
|
||||
}
|
||||
var showData = GetDataList(dataZoom);
|
||||
int dataCount = showData.Count;
|
||||
@@ -521,13 +523,13 @@ namespace XCharts
|
||||
/// 更新刻度标签文字
|
||||
/// </summary>
|
||||
/// <param name="dataZoom"></param>
|
||||
public void UpdateLabelText(float coordinateWidth, DataZoom dataZoom)
|
||||
public void UpdateLabelText(float coordinateWidth, DataZoom dataZoom, bool forcePercent)
|
||||
{
|
||||
for (int i = 0; i < axisLabelTextList.Count; i++)
|
||||
{
|
||||
if (axisLabelTextList[i] != null)
|
||||
{
|
||||
axisLabelTextList[i].text = GetLabelName(coordinateWidth, i, minValue, maxValue, dataZoom);
|
||||
axisLabelTextList[i].text = GetLabelName(coordinateWidth, i, minValue, maxValue, dataZoom, forcePercent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using UnityEngine;
|
||||
@@ -119,6 +120,22 @@ namespace XCharts
|
||||
DashDotDot
|
||||
}
|
||||
|
||||
public enum BarType
|
||||
{
|
||||
/// <summary>
|
||||
/// 普通柱形图
|
||||
/// </summary>
|
||||
Normal,
|
||||
/// <summary>
|
||||
/// 斑马柱形图
|
||||
/// </summary>
|
||||
ZebraLine,
|
||||
/// <summary>
|
||||
/// 胶囊柱形图
|
||||
/// </summary>
|
||||
Capsule
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 采样类型
|
||||
/// </summary>
|
||||
@@ -163,23 +180,25 @@ namespace XCharts
|
||||
[SerializeField] protected int m_MaxCache;
|
||||
[SerializeField] private AreaStyle m_AreaStyle = AreaStyle.defaultAreaStyle;
|
||||
[SerializeField] private SerieSymbol m_Symbol = new SerieSymbol();
|
||||
[SerializeField] private LineType m_LineType = LineType.Normal;
|
||||
|
||||
[SerializeField] private float m_SampleDist = 0;
|
||||
[SerializeField] private SampleType m_SampleType = SampleType.Average;
|
||||
[SerializeField] private float m_SampleAverage = 0;
|
||||
|
||||
[SerializeField] private LineType m_LineType = LineType.Normal;
|
||||
[SerializeField] private LineStyle m_LineStyle = new LineStyle();
|
||||
|
||||
[SerializeField] private BarType m_BarType = BarType.Normal;
|
||||
[SerializeField] private bool m_BarPercentStack = false;
|
||||
[SerializeField] private float m_BarWidth = 0.6f;
|
||||
[SerializeField] private float m_BarGap = 0.3f; // 30%
|
||||
[SerializeField] private float m_BarCategoryGap = 0.2f; // 20%
|
||||
|
||||
#region PieChart field
|
||||
[SerializeField] private bool m_ClickOffset = true;
|
||||
[SerializeField] private RoseType m_RoseType = RoseType.None;
|
||||
[SerializeField] private float m_Space;
|
||||
[SerializeField] private float[] m_Center = new float[2] { 0.5f, 0.5f };
|
||||
[SerializeField] private float[] m_Radius = new float[2] { 0, 80 };
|
||||
#endregion
|
||||
[SerializeField] private SerieLabel m_Label = new SerieLabel();
|
||||
[SerializeField] private Animation m_Animation = new Animation();
|
||||
[SerializeField] private LineArrow m_LineArrow = new LineArrow();
|
||||
@@ -266,7 +285,6 @@ namespace XCharts
|
||||
/// The style of area.
|
||||
/// 区域填充样式。
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public AreaStyle areaStyle { get { return m_AreaStyle; } set { m_AreaStyle = value; } }
|
||||
/// <summary>
|
||||
/// the symbol of serie data item.
|
||||
@@ -277,13 +295,11 @@ namespace XCharts
|
||||
/// The type of line chart.
|
||||
/// 折线图样式类型。
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public LineType lineType { get { return m_LineType; } set { m_LineType = value; } }
|
||||
/// <summary>
|
||||
/// the min pixel dist of sample.
|
||||
/// 采样的最小像素距离,默认为0时不采样。当两个数据点间的水平距离小于改值时,开启采样,保证两点间的水平距离不小于改值。
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public float sampleDist { get { return m_SampleDist; } set { m_SampleDist = value < 0 ? 0 : value; } }
|
||||
/// <summary>
|
||||
/// the type of sample.
|
||||
@@ -293,19 +309,24 @@ namespace XCharts
|
||||
/// <summary>
|
||||
/// 设定的采样平均值。当sampleType 为 Peak 时,用于和过滤数据的平均值做对比是取最大值还是最小值。默认为0时会实时计算所有数据的平均值。
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public float sampleAverage { get { return m_SampleAverage; } set { m_SampleAverage = value; } }
|
||||
/// <summary>
|
||||
/// The style of line.
|
||||
/// 线条样式。
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public LineStyle lineStyle { get { return m_LineStyle; } set { m_LineStyle = value; } }
|
||||
/// <summary>
|
||||
/// 柱形图类型。
|
||||
/// </summary>
|
||||
public BarType barType { get { return m_BarType; } set { m_BarType = value; } }
|
||||
/// <summary>
|
||||
/// 柱形图是否为百分比堆积。
|
||||
/// </summary>
|
||||
public bool barPercentStack { get { return m_BarPercentStack; } set { m_BarPercentStack = value; } }
|
||||
/// <summary>
|
||||
/// The width of the bar. Adaptive when default 0.
|
||||
/// 柱条的宽度,不设时自适应。支持设置成相对于类目宽度的百分比。
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public float barWidth { get { return m_BarWidth; } set { m_BarWidth = value; } }
|
||||
/// <summary>
|
||||
/// The gap between bars between different series, is a percent value like '0.3f' , which means 30% of the bar width, can be set as a fixed value.
|
||||
@@ -327,7 +348,6 @@ namespace XCharts
|
||||
/// 同一系列的柱间距离,默认为类目间距的20%,可设固定值。
|
||||
/// 在同一坐标系上,此属性会被多个 'bar' 系列共享。此属性应设置于此坐标系中最后一个 'bar' 系列上才会生效,并且是对此坐标系中所有 'bar' 系列生效。
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public float barCategoryGap { get { return m_BarCategoryGap; } set { m_BarCategoryGap = value; } }
|
||||
/// <summary>
|
||||
/// Whether offset when mouse click pie chart item.
|
||||
@@ -363,7 +383,6 @@ namespace XCharts
|
||||
/// The start animation.
|
||||
/// 起始动画。
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public Animation animation { get { return m_Animation; } set { m_Animation = value; } }
|
||||
/// <summary>
|
||||
/// The arrow of line.
|
||||
|
||||
@@ -574,27 +574,38 @@ namespace XCharts
|
||||
{
|
||||
float min = int.MaxValue;
|
||||
float max = int.MinValue;
|
||||
var isPercentStack = IsPercentStack(SerieType.Bar);
|
||||
if (!IsStack() || (isValueAxis && !yValue))
|
||||
{
|
||||
for (int i = 0; i < m_Series.Count; i++)
|
||||
{
|
||||
if (m_Series[i].axisIndex != axisIndex) continue;
|
||||
var serie = m_Series[i];
|
||||
if (serie.axisIndex != axisIndex) continue;
|
||||
|
||||
if (IsActive(i))
|
||||
{
|
||||
var showData = m_Series[i].GetDataList(dataZoom);
|
||||
foreach (var data in showData)
|
||||
if (isPercentStack && IsPercentStack(serie.name, SerieType.Bar))
|
||||
{
|
||||
if (yValue)
|
||||
Debug.LogError("minmax:" + serie.name);
|
||||
if (100 > max) max = 100;
|
||||
if (0 < min) min = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
var showData = m_Series[i].GetDataList(dataZoom);
|
||||
foreach (var data in showData)
|
||||
{
|
||||
if (data.data[1] > max) max = data.data[1];
|
||||
if (data.data[1] < min) min = data.data[1];
|
||||
if (yValue)
|
||||
{
|
||||
if (data.data[1] > max) max = data.data[1];
|
||||
if (data.data[1] < min) min = data.data[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (data.data[0] > max) max = data.data[0];
|
||||
if (data.data[0] < min) min = data.data[0];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (data.data[0] > max) max = data.data[0];
|
||||
if (data.data[0] < min) min = data.data[0];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -610,13 +621,24 @@ namespace XCharts
|
||||
var serie = ss.Value[i];
|
||||
if (serie.axisIndex != axisIndex || !IsActive(i)) continue;
|
||||
var showData = serie.GetDataList(dataZoom);
|
||||
for (int j = 0; j < showData.Count; j++)
|
||||
if (IsPercentStack(serie.stack, SerieType.Bar))
|
||||
{
|
||||
if (!_serieTotalValueForMinMax.ContainsKey(j))
|
||||
_serieTotalValueForMinMax[j] = 0;
|
||||
_serieTotalValueForMinMax[j] = _serieTotalValueForMinMax[j] +
|
||||
(yValue ? showData[j].data[1] : showData[i].data[0]);
|
||||
for (int j = 0; j < showData.Count; j++)
|
||||
{
|
||||
_serieTotalValueForMinMax[j] = 100;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int j = 0; j < showData.Count; j++)
|
||||
{
|
||||
if (!_serieTotalValueForMinMax.ContainsKey(j))
|
||||
_serieTotalValueForMinMax[j] = 0;
|
||||
_serieTotalValueForMinMax[j] = _serieTotalValueForMinMax[j] +
|
||||
(yValue ? showData[j].data[1] : showData[i].data[0]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
float tmax = int.MinValue;
|
||||
float tmin = int.MaxValue;
|
||||
@@ -661,6 +683,12 @@ namespace XCharts
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否堆叠
|
||||
/// </summary>
|
||||
/// <param name="stackName"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public bool IsStack(string stackName, SerieType type)
|
||||
{
|
||||
if (string.IsNullOrEmpty(stackName)) return false;
|
||||
@@ -676,6 +704,56 @@ namespace XCharts
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否时百分比堆叠
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public bool IsPercentStack(SerieType type)
|
||||
{
|
||||
int count = 0;
|
||||
bool isPercentStack = false;
|
||||
foreach (var serie in m_Series)
|
||||
{
|
||||
if (serie.show && serie.type == type)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(serie.stack))
|
||||
{
|
||||
count++;
|
||||
if (serie.barPercentStack) isPercentStack = true;
|
||||
}
|
||||
if (count >= 2 && isPercentStack) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否时百分比堆叠
|
||||
/// </summary>
|
||||
/// <param name="stackName"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public bool IsPercentStack(string stackName, SerieType type)
|
||||
{
|
||||
if (string.IsNullOrEmpty(stackName)) return false;
|
||||
int count = 0;
|
||||
bool isPercentStack = false;
|
||||
foreach (var serie in m_Series)
|
||||
{
|
||||
if (serie.show && serie.type == type)
|
||||
{
|
||||
if (stackName.Equals(serie.stack))
|
||||
{
|
||||
count++;
|
||||
if (serie.barPercentStack) isPercentStack = true;
|
||||
}
|
||||
if (count >= 2 && isPercentStack) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获得堆叠系列列表
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user