AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
Example20_BarChart.cs
1 /************************************************/
2 /* */
3 /* Copyright (c) 2018 - 2021 monitor1394 */
4 /* https://github.com/monitor1394 */
5 /* */
6 /************************************************/
7 
8 using System.Collections;
9 using UnityEngine;
10 
11 namespace XCharts.Examples
12 {
13  [DisallowMultipleComponent]
14  public class Example20_BarChart : MonoBehaviour
15  {
16  private BarChart chart;
17  private Serie serie, serie2;
18  private int m_DataNum = 5;
19 
20  void Awake()
21  {
22  LoopDemo();
23  }
24 
25  private void OnEnable()
26  {
27  LoopDemo();
28  }
29 
30  void LoopDemo()
31  {
32  StopAllCoroutines();
33  StartCoroutine(PieDemo());
34  }
35 
36  IEnumerator PieDemo()
37  {
38  StartCoroutine(AddSimpleBar());
39  yield return new WaitForSeconds(2);
40  StartCoroutine(BarMutilSerie());
41  yield return new WaitForSeconds(3);
42  StartCoroutine(ZebraBar());
43  yield return new WaitForSeconds(3);
44  StartCoroutine(SameBarAndNotStack());
45  yield return new WaitForSeconds(3);
46  StartCoroutine(SameBarAndStack());
47  yield return new WaitForSeconds(3);
48  StartCoroutine(SameBarAndPercentStack());
49  yield return new WaitForSeconds(10);
50 
51  LoopDemo();
52  }
53 
54  IEnumerator AddSimpleBar()
55  {
56  chart = gameObject.GetComponent<BarChart>();
57  if (chart == null) chart = gameObject.AddComponent<BarChart>();
58  chart.title.text = "BarChart - 柱状图";
59  chart.title.subText = "普通柱状图";
60 
61  chart.yAxis0.minMaxType = Axis.AxisMinMaxType.Default;
62 
63  chart.RemoveData();
64  serie = chart.AddSerie(SerieType.Bar, "Bar1");
65 
66  for (int i = 0; i < m_DataNum; i++)
67  {
68  chart.AddXAxisData("x" + (i + 1));
69  chart.AddData(0, UnityEngine.Random.Range(30, 90));
70  }
71  yield return new WaitForSeconds(1);
72  }
73 
74 
75  IEnumerator BarMutilSerie()
76  {
77  chart.title.subText = "多条柱状图";
78 
79  float now = serie.barWidth - 0.35f;
80  while (serie.barWidth > 0.35f)
81  {
82  serie.barWidth -= now * Time.deltaTime;
83  chart.RefreshChart();
84  yield return null;
85  }
86 
87  serie2 = chart.AddSerie(SerieType.Bar, "Bar2");
88  serie2.lineType = LineType.Normal;
89  serie2.barWidth = 0.35f;
90  for (int i = 0; i < m_DataNum; i++)
91  {
92  chart.AddData(1, UnityEngine.Random.Range(20, 90));
93  }
94  yield return new WaitForSeconds(1);
95  }
96 
97  IEnumerator ZebraBar()
98  {
99  chart.title.subText = "斑马柱状图";
100  serie.barType = BarType.Zebra;
101  serie2.barType = BarType.Zebra;
102  serie.barZebraWidth = serie.barZebraGap = 4;
103  serie2.barZebraWidth = serie2.barZebraGap = 4;
104  chart.RefreshChart();
105  yield return new WaitForSeconds(1);
106  }
107 
108  IEnumerator SameBarAndNotStack()
109  {
110  chart.title.subText = "非堆叠同柱";
111  serie.barType = serie2.barType = BarType.Normal;
112  serie.stack = "";
113  serie2.stack = "";
114  serie.barGap = -1;
115  serie2.barGap = -1;
116  chart.RefreshAxisMinMaxValue();
117  yield return new WaitForSeconds(1);
118  }
119 
120  IEnumerator SameBarAndStack()
121  {
122  chart.title.subText = "堆叠同柱";
123  serie.barType = serie2.barType = BarType.Normal;
124  serie.stack = "samename";
125  serie2.stack = "samename";
126  chart.RefreshAxisMinMaxValue();
127  yield return new WaitForSeconds(1);
128  float now = 0.6f - serie.barWidth;
129  while (serie.barWidth < 0.6f)
130  {
131  serie.barWidth += now * Time.deltaTime;
132  serie2.barWidth += now * Time.deltaTime;
133  chart.RefreshChart();
134  yield return null;
135  }
136  serie.barWidth = serie2.barWidth;
137  chart.RefreshChart();
138  yield return new WaitForSeconds(1);
139  }
140 
141  IEnumerator SameBarAndPercentStack()
142  {
143  chart.title.subText = "百分比堆叠同柱";
144  serie.barType = serie2.barType = BarType.Normal;
145  serie.stack = "samename";
146  serie2.stack = "samename";
147 
148  serie.barPercentStack = true;
149 
150  serie.label.show = true;
151  serie.label.position = SerieLabel.Position.Center;
152  serie.label.border = false;
153  serie.label.textStyle.color = Color.white;
154  serie.label.formatter = "{d:f0}%";
155 
156  serie2.label.show = true;
157  serie2.label.position = SerieLabel.Position.Center;
158  serie2.label.border = false;
159  serie2.label.textStyle.color = Color.white;
160  serie2.label.formatter = "{d:f0}%";
161 
162  chart.RefreshLabel();
163  chart.RefreshChart();
164  yield return new WaitForSeconds(1);
165  }
166  }
167 }
XCharts.SerieLabel.position
Position position
The position of label. 标签的位置。
Definition: SerieLabel.cs:158
XCharts.Serie.barPercentStack
bool barPercentStack
柱形图是否为百分比堆积。相同stack的serie只要有一个barPercentStack为true,则就显示成百分比堆叠柱状图。
Definition: Serie.cs:561
XCharts.Serie.barGap
float barGap
The gap between bars between different series, is a percent value like '0.3f' , which means 30% of th...
Definition: Serie.cs:585
XCharts.SerieLabel.textStyle
TextStyle textStyle
the sytle of text. 文本样式。
Definition: SerieLabel.cs:351
XCharts.SerieType
SerieType
the type of serie. 系列类型。
Definition: Serie.cs:19
XCharts.SerieLabel.formatter
string formatter
标签内容字符串模版格式器。支持用 换行。 模板变量有: {a}:系列名。 {b}:数据名。 {c}:数据值。 {d}:百分比。
Definition: SerieLabel.cs:176
XCharts.LineType
LineType
the type of line chart. 折线图样式类型
Definition: Serie.cs:99
XCharts.BarType
BarType
Definition: Serie.cs:149
XCharts.Serie.barZebraGap
float? barZebraGap
斑马线的间距。
Definition: Serie.cs:614
XCharts.Serie.label
SerieLabel label
Text label of graphic element,to explain some data information about graphic item like value,...
Definition: Serie.cs:821
XCharts.SerieLabel.border
bool border
Whether to show border. 是否显示边框。
Definition: SerieLabel.cs:303
XCharts.Axis
The axis in rectangular coordinate. 直角坐标系的坐标轴组件。
Definition: Axis.cs:20
XCharts.Serie.lineType
LineType lineType
The type of line chart. 折线图样式类型。
Definition: Serie.cs:510
XCharts.SerieLabel.show
bool show
Whether the label is showed. 是否显示文本标签。
Definition: SerieLabel.cs:149
XCharts.BarChart
Definition: BarChart_API.cs:14
XCharts.Serie
系列。每个系列通过 type 决定自己的图表类型。
Definition: Serie.cs:261
XCharts.Serie.barWidth
float barWidth
The width of the bar. Adaptive when default 0. 柱条的宽度,不设时自适应。支持设置成相对于类目宽度的百分比。
Definition: Serie.cs:570
XCharts.SerieLabel
Text label of chart, to explain some data information about graphic item like value,...
Definition: SerieLabel.cs:18
XCharts.Examples
Definition: RewardChart.cs:14
XCharts.TextStyle.color
Color color
the color of text. 文本的颜色。 [default: Color.clear]
Definition: TextStyle.cs:68
XCharts.Serie.barType
BarType barType
柱形图类型。
Definition: Serie.cs:553
XCharts.Examples.Example20_BarChart
Definition: Example20_BarChart.cs:14
XCharts.SerieLabel.Position
Position
The position of label. 标签的位置。
Definition: SerieLabel.cs:24
XCharts.Serie.stack
string stack
If stack the value. On the same category axis, the series with the same stack name would be put on to...
Definition: Serie.cs:409
XCharts.Axis.AxisMinMaxType
AxisMinMaxType
the type of axis min and max value. 坐标轴最大最小刻度显示类型。
Definition: Axis.cs:54
XCharts.Serie.barZebraWidth
float? barZebraWidth
斑马线的粗细。
Definition: Serie.cs:606