mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-19 15:00:08 +00:00
增加Demo首页BarChart的代码动态控制效果
This commit is contained in:
163
Assets/XCharts/Demo/Scripts/Demo20_BarChart.cs
Normal file
163
Assets/XCharts/Demo/Scripts/Demo20_BarChart.cs
Normal file
@@ -0,0 +1,163 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using XCharts;
|
||||
|
||||
[DisallowMultipleComponent]
|
||||
public class Demo20_BarChart : MonoBehaviour
|
||||
{
|
||||
private BarChart chart;
|
||||
private Serie serie, serie2;
|
||||
private float m_RadiusSpeed = 100f;
|
||||
private float m_CenterSpeed = 1f;
|
||||
private int m_DataNum = 5;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
LoopDemo();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
LoopDemo();
|
||||
}
|
||||
|
||||
void LoopDemo()
|
||||
{
|
||||
StopAllCoroutines();
|
||||
StartCoroutine(PieDemo());
|
||||
}
|
||||
|
||||
IEnumerator PieDemo()
|
||||
{
|
||||
StartCoroutine(AddSimpleBar());
|
||||
yield return new WaitForSeconds(2);
|
||||
StartCoroutine(BarMutilSerie());
|
||||
yield return new WaitForSeconds(3);
|
||||
StartCoroutine(ZebraBar());
|
||||
yield return new WaitForSeconds(3);
|
||||
StartCoroutine(SameBarAndNotStack());
|
||||
yield return new WaitForSeconds(3);
|
||||
StartCoroutine(SameBarAndStack());
|
||||
yield return new WaitForSeconds(3);
|
||||
StartCoroutine(SameBarAndPercentStack());
|
||||
yield return new WaitForSeconds(10);
|
||||
|
||||
LoopDemo();
|
||||
}
|
||||
|
||||
IEnumerator AddSimpleBar()
|
||||
{
|
||||
chart = gameObject.GetComponent<BarChart>();
|
||||
if (chart == null) chart = gameObject.AddComponent<BarChart>();
|
||||
chart.title.text = "BarChart - 柱状图";
|
||||
chart.title.subText = "普通柱状图";
|
||||
|
||||
chart.yAxis0.minMaxType = Axis.AxisMinMaxType.Default;
|
||||
|
||||
chart.RemoveData();
|
||||
serie = chart.AddSerie(SerieType.Bar, "Bar1");
|
||||
|
||||
for (int i = 0; i < m_DataNum; i++)
|
||||
{
|
||||
chart.AddXAxisData("x" + (i + 1));
|
||||
chart.AddData(0, UnityEngine.Random.Range(30, 90));
|
||||
}
|
||||
yield return new WaitForSeconds(1);
|
||||
}
|
||||
|
||||
|
||||
IEnumerator BarMutilSerie()
|
||||
{
|
||||
chart.title.subText = "多条柱状图";
|
||||
|
||||
float now = serie.barWidth - 0.35f;
|
||||
while (serie.barWidth > 0.35f)
|
||||
{
|
||||
serie.barWidth -= now * Time.deltaTime;
|
||||
chart.RefreshChart();
|
||||
yield return null;
|
||||
}
|
||||
|
||||
serie2 = chart.AddSerie(SerieType.Bar, "Bar2");
|
||||
serie2.lineType = LineType.Normal;
|
||||
serie2.barWidth = 0.35f;
|
||||
for (int i = 0; i < m_DataNum; i++)
|
||||
{
|
||||
chart.AddData(1, UnityEngine.Random.Range(20, 90));
|
||||
}
|
||||
yield return new WaitForSeconds(1);
|
||||
}
|
||||
|
||||
IEnumerator ZebraBar()
|
||||
{
|
||||
chart.title.subText = "斑马柱状图";
|
||||
serie.barType = BarType.Zebra;
|
||||
serie2.barType = BarType.Zebra;
|
||||
serie.barZebraWidth = serie.barZebraGap = 4;
|
||||
serie2.barZebraWidth = serie2.barZebraGap = 4;
|
||||
chart.RefreshChart();
|
||||
yield return new WaitForSeconds(1);
|
||||
}
|
||||
|
||||
IEnumerator SameBarAndNotStack()
|
||||
{
|
||||
chart.title.subText = "非堆叠同柱";
|
||||
serie.barType = serie2.barType = BarType.Normal;
|
||||
serie.stack = "";
|
||||
serie2.stack = "";
|
||||
serie.barGap = -1;
|
||||
serie2.barGap = -1;
|
||||
chart.RefreshAxisMinMaxValue();
|
||||
yield return new WaitForSeconds(1);
|
||||
}
|
||||
|
||||
IEnumerator SameBarAndStack()
|
||||
{
|
||||
chart.title.subText = "堆叠同柱";
|
||||
serie.barType = serie2.barType = BarType.Normal;
|
||||
serie.stack = "samename";
|
||||
serie2.stack = "samename";
|
||||
chart.RefreshAxisMinMaxValue();
|
||||
yield return new WaitForSeconds(1);
|
||||
float now = 0.6f - serie.barWidth;
|
||||
while (serie.barWidth < 0.6f)
|
||||
{
|
||||
serie.barWidth += now * Time.deltaTime;
|
||||
serie2.barWidth += now * Time.deltaTime;
|
||||
chart.RefreshChart();
|
||||
yield return null;
|
||||
}
|
||||
serie.barWidth = serie2.barWidth;
|
||||
chart.RefreshChart();
|
||||
yield return new WaitForSeconds(1);
|
||||
}
|
||||
|
||||
IEnumerator SameBarAndPercentStack()
|
||||
{
|
||||
chart.title.subText = "百分比堆叠同柱";
|
||||
serie.barType = serie2.barType = BarType.Normal;
|
||||
serie.stack = "samename";
|
||||
serie2.stack = "samename";
|
||||
|
||||
serie.barPercentStack = true;
|
||||
|
||||
serie.label.show = true;
|
||||
serie.label.position = SerieLabel.Position.Center;
|
||||
serie.label.border = false;
|
||||
serie.label.color = Color.white;
|
||||
serie.label.formatter = "{d:f0}%";
|
||||
|
||||
serie2.label.show = true;
|
||||
serie2.label.position = SerieLabel.Position.Center;
|
||||
serie2.label.border = false;
|
||||
serie2.label.color = Color.white;
|
||||
serie2.label.formatter = "{d:f0}%";
|
||||
|
||||
chart.RefreshLabel();
|
||||
chart.RefreshChart();
|
||||
yield return new WaitForSeconds(1);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
11
Assets/XCharts/Demo/Scripts/Demo20_BarChart.cs.meta
Normal file
11
Assets/XCharts/Demo/Scripts/Demo20_BarChart.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8831c4ea94fff40da9a552399d9aab0c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
File diff suppressed because it is too large
Load Diff
@@ -185,6 +185,14 @@ namespace XCharts
|
||||
{
|
||||
RefreshDataZoomLabel();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 立即刷新数值坐标轴的最大最小值
|
||||
/// </summary>
|
||||
public void RefreshAxisMinMaxValue()
|
||||
{
|
||||
CheckMinMaxValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
|
||||
@@ -355,11 +355,11 @@ namespace XCharts
|
||||
/// <summary>
|
||||
/// 斑马线的粗细。
|
||||
/// </summary>
|
||||
public float barZebraWidth { get { return m_BarZebraWidth; } set { m_BarZebraWidth = value; } }
|
||||
public float barZebraWidth { get { return m_BarZebraWidth; } set { m_BarZebraWidth = value > 0 ? value : 0; } }
|
||||
/// <summary>
|
||||
/// 斑马线的间距。
|
||||
/// </summary>
|
||||
public float barZebraGap { get { return m_BarZebraGap; } set { m_BarZebraGap = value; } }
|
||||
public float barZebraGap { get { return m_BarZebraGap; } set { m_BarZebraGap = value > 0 ? value : 0; } }
|
||||
|
||||
/// <summary>
|
||||
/// Whether offset when mouse click pie chart item.
|
||||
|
||||
@@ -308,13 +308,15 @@ namespace XCharts
|
||||
var labelObj = ChartHelper.AddSerieLabel(textName, labelObject.transform, m_ThemeInfo.font,
|
||||
color, backgroundColor, serie.label.fontSize, serie.label.fontStyle, serie.label.rotate,
|
||||
serie.label.backgroundWidth, serie.label.backgroundHeight);
|
||||
|
||||
var iconObj = ChartHelper.AddIcon("Icon", labelObj.transform, serieData.iconWidth, serieData.iconHeight);
|
||||
serieData.SetIconObj(iconObj);
|
||||
|
||||
var isAutoSize = serie.label.backgroundWidth == 0 || serie.label.backgroundHeight == 0;
|
||||
serieData.InitLabel(labelObj, isAutoSize, serie.label.paddingLeftRight, serie.label.paddingTopBottom);
|
||||
serieData.SetLabelActive(false);
|
||||
serieData.SetLabelText(serieData.name);
|
||||
|
||||
var iconObj = ChartHelper.AddIcon("Icon", labelObj.transform, serieData.iconWidth, serieData.iconHeight);
|
||||
serieData.SetIconObj(iconObj);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -756,7 +756,7 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateAxisMinMaxValue(int axisIndex, Axis axis)
|
||||
private void UpdateAxisMinMaxValue(int axisIndex, Axis axis, bool updateChart = true)
|
||||
{
|
||||
if (axis.IsCategory() || !axis.show) return;
|
||||
int tempMinValue = 0;
|
||||
@@ -801,10 +801,13 @@ namespace XCharts
|
||||
Mathf.Abs(axis.minValue) * (coordinateHeight / (Mathf.Abs(axis.minValue) + Mathf.Abs(axis.maxValue)));
|
||||
}
|
||||
}
|
||||
float coordinateWidth = axis is XAxis ? this.coordinateWidth : coordinateHeight;
|
||||
var isPercentStack = m_Series.IsPercentStack(SerieType.Bar);
|
||||
axis.UpdateLabelText(coordinateWidth, m_DataZoom, isPercentStack);
|
||||
RefreshChart();
|
||||
if (updateChart)
|
||||
{
|
||||
float coordinateWidth = axis is XAxis ? this.coordinateWidth : coordinateHeight;
|
||||
var isPercentStack = m_Series.IsPercentStack(SerieType.Bar);
|
||||
axis.UpdateLabelText(coordinateWidth, m_DataZoom, isPercentStack);
|
||||
RefreshChart();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1377,7 +1380,7 @@ namespace XCharts
|
||||
var content = "";
|
||||
if (anyPercentStack && isPercentStack)
|
||||
{
|
||||
var tempTotal = GetSameStackTotalValue(serie.stack,j);
|
||||
var tempTotal = GetSameStackTotalValue(serie.stack, j);
|
||||
content = serie.label.GetFormatterContent(serie.name, serieData.name, value, tempTotal);
|
||||
}
|
||||
else
|
||||
|
||||
@@ -124,6 +124,7 @@ namespace XCharts
|
||||
var yAxis = m_YAxises[serie.axisIndex];
|
||||
var xAxis = m_XAxises[serie.axisIndex];
|
||||
if (!xAxis.show) xAxis = m_XAxises[(serie.axisIndex + 1) % m_XAxises.Count];
|
||||
|
||||
float categoryWidth = xAxis.GetDataWidth(coordinateWidth, m_DataZoom);
|
||||
float barGap = GetBarGap();
|
||||
float totalBarWidth = GetBarTotalWidth(categoryWidth, barGap);
|
||||
@@ -142,6 +143,7 @@ namespace XCharts
|
||||
seriesHig.Add(0);
|
||||
}
|
||||
}
|
||||
|
||||
var isPercentStack = m_Series.IsPercentStack(serie.stack, SerieType.Bar);
|
||||
for (int i = serie.minShow; i < maxCount; i++)
|
||||
{
|
||||
|
||||
@@ -65,6 +65,7 @@
|
||||
* `CoordinateChart.AddYAxisData(string category, int yAxisIndex = 0)`:添加一个类目数据到指定的 `Y` 轴。
|
||||
* `CoordinateChart.IsValue()`:是否是纯数值坐标。
|
||||
* `CoordinateChart.RefreshDataZoom()`:在下一帧刷新DataZoom组件。
|
||||
* `CoordinateChart.RefreshAxisMinMaxValue()`:立即刷新数值坐标轴的最大最小值(更新坐标轴标签并触发重绘)。
|
||||
|
||||
## `LineChart`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user