-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathVertical.razor
More file actions
144 lines (121 loc) · 4.04 KB
/
Vertical.razor
File metadata and controls
144 lines (121 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
@page "/charts/bar/vertical"
@using ChartJs.Blazor.BarChart
@using System.Drawing
@layout SampleLayout
<Chart Config="_config" @ref="_chart"></Chart>
<button @onclick="RandomizeData">Randomize Data</button>
<button @onclick="() => AddDataset()">Add Dataset</button>
<button @onclick="() => AddDataset(true)">Add Hidden Dataset</button>
<button @onclick="RemoveDataset">Remove Dataset</button>
<button @onclick="AddData">Add Data</button>
<button @onclick="RemoveData">Remove Data</button>
@code {
private const int InitalCount = 7;
private BarConfig _config;
private Random _rng = new Random();
private Chart _chart;
protected override void OnInitialized()
{
_config = new BarConfig
{
Options = new BarOptions
{
Responsive = true,
Legend = new Legend
{
Position = Position.Top
},
Title = new OptionsTitle
{
Display = true,
Text = "ChartJs.Blazor Bar Chart"
}
}
};
IDataset<int> dataset1 = new BarDataset<int>(RandomScalingFactor(InitalCount))
{
Label = "My first dataset",
BackgroundColor = ColorUtil.FromDrawingColor(Color.FromArgb(128, ChartColors.Red)),
BorderColor = ColorUtil.FromDrawingColor(ChartColors.Red),
BorderWidth = 1
};
IDataset<int> dataset2 = new BarDataset<int>(RandomScalingFactor(InitalCount))
{
Label = "My second dataset",
BackgroundColor = ColorUtil.FromDrawingColor(Color.FromArgb(128, ChartColors.Blue)),
BorderColor = ColorUtil.FromDrawingColor(ChartColors.Blue),
BorderWidth = 1
};
_config.Data.Labels.AddRange(Months.Take(InitalCount));
_config.Data.Datasets.Add(dataset1);
_config.Data.Datasets.Add(dataset2);
}
private void RandomizeData()
{
foreach (IDataset<int> dataset in _config.Data.Datasets)
{
int count = dataset.Count;
dataset.Clear();
for (int i = 0; i < count; i++)
{
if (_rng.NextDouble() < 0.2)
{
dataset.Add(0);
}
else
{
dataset.Add(RandomScalingFactor());
}
}
}
_chart.Update();
}
private void AddDataset(bool? hidden = null)
{
Color color = ChartColors.All[_config.Data.Datasets.Count % ChartColors.All.Count];
IDataset<int> dataset = new BarDataset<int>(RandomScalingFactor(_config.Data.Labels.Count))
{
Label = $"Dataset {_config.Data.Datasets.Count}",
BackgroundColor = ColorUtil.FromDrawingColor(Color.FromArgb(128, color)),
BorderColor = ColorUtil.FromDrawingColor(color),
BorderWidth = 1,
Hidden = hidden
};
_config.Data.Datasets.Add(dataset);
_chart.Update();
}
private void RemoveDataset()
{
IList<IDataset> datasets = _config.Data.Datasets;
if (datasets.Count == 0)
return;
datasets.RemoveAt(datasets.Count - 1);
_chart.Update();
}
private void AddData()
{
if (_config.Data.Datasets.Count == 0)
return;
string month = Months[_config.Data.Labels.Count % Months.Count];
_config.Data.Labels.Add(month);
foreach (IDataset<int> dataset in _config.Data.Datasets)
{
dataset.Add(RandomScalingFactor());
}
_chart.Update();
}
private void RemoveData()
{
if (_config.Data.Datasets.Count == 0 ||
_config.Data.Labels.Count == 0)
{
return;
}
_config.Data.Labels.RemoveAt(_config.Data.Labels.Count - 1);
foreach (IDataset<int> dataset in _config.Data.Datasets)
{
dataset.RemoveAt(dataset.Count - 1);
}
_chart.Update();
}
}