forked from radzenhq/radzen-blazor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadzenBarSeries.razor.cs
256 lines (217 loc) · 7.94 KB
/
RadzenBarSeries.razor.cs
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using Microsoft.AspNetCore.Components;
using Radzen.Blazor.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Radzen.Blazor
{
/// <summary>
/// Renders bar series in <see cref="RadzenChart" />.
/// </summary>
/// <typeparam name="TItem">The type of the series data item.</typeparam>
public partial class RadzenBarSeries<TItem> : CartesianSeries<TItem>, IChartBarSeries
{
/// <summary>
/// Specifies the fill (background color) of the bar series.
/// </summary>
/// <value>The fill.</value>
[Parameter]
public string Fill { get; set; }
/// <summary>
/// Specifies a list of colors that will be used to set the individual bar backgrounds.
/// </summary>
/// <value>The fills.</value>
[Parameter]
public IEnumerable<string> Fills { get; set; }
/// <summary>
/// Specifies the stroke (border color) of the bar series.
/// </summary>
/// <value>The stroke.</value>
[Parameter]
public string Stroke { get; set; }
/// <summary>
/// Specifies a list of colors that will be used to set the individual bar borders.
/// </summary>
/// <value>The strokes.</value>
[Parameter]
public IEnumerable<string> Strokes { get; set; }
/// <summary>
/// Gets or sets the width of the stroke (border).
/// </summary>
/// <value>The width of the stroke.</value>
[Parameter]
public double StrokeWidth { get; set; }
/// <summary>
/// Gets or sets the type of the line used to render the bar border.
/// </summary>
/// <value>The type of the line.</value>
[Parameter]
public LineType LineType { get; set; }
/// <summary>
/// Gets or sets the color range of the fill.
/// </summary>
/// <value>The color range of the fill.</value>
[Parameter]
public IList<SeriesColorRange> FillRange { get; set; }
/// <summary>
/// Gets or sets the color range of the stroke.
/// </summary>
/// <value>The color range of the stroke.</value>
[Parameter]
public IList<SeriesColorRange> StrokeRange { get; set; }
/// <inheritdoc />
public override string Color
{
get
{
return Fill;
}
}
/// <inheritdoc />
public override ScaleBase TransformCategoryScale(ScaleBase scale)
{
return base.TransformValueScale(scale);
}
/// <inheritdoc />
public override ScaleBase TransformValueScale(ScaleBase scale)
{
return base.TransformCategoryScale(scale);
}
/// <inheritdoc />
protected override IList<object> GetCategories()
{
return base.GetCategories().Reverse().ToList();
}
private IList<IChartSeries> BarSeries
{
get
{
return Chart.Series.Where(series => series is IChartBarSeries).Cast<IChartSeries>().ToList();
}
}
private IList<IChartSeries> VisibleBarSeries
{
get
{
return BarSeries.Where(series => series.Visible).ToList();
}
}
/// <inheritdoc />
protected override string TooltipStyle(TItem item)
{
var style = base.TooltipStyle(item);
var index = Items.IndexOf(item);
if (index >= 0)
{
var color = PickColor(index, Fills, Fill, FillRange, Value(item));
if (color != null)
{
style = $"{style}; border-color: {color};";
}
}
return style;
}
private double BandHeight
{
get
{
var barSeries = VisibleBarSeries;
if (Chart.BarOptions.Height.HasValue)
{
return Chart.BarOptions.Height.Value * barSeries.Count;
}
else
{
var availableHeight = Chart.ValueScale.OutputSize; // - (Chart.ValueAxis.Padding * 2);
var bands = barSeries.Cast<IChartBarSeries>().Max(series => series.Count) + 2;
return availableHeight / bands;
}
}
}
int IChartBarSeries.Count
{
get
{
return Items.Count;
}
}
/// <inheritdoc />
public override bool Contains(double x, double y, double tolerance)
{
return DataAt(x, y).Item1 != null;
}
/// <inheritdoc />
internal override double TooltipX(TItem item)
{
var value = Chart.CategoryScale.Compose(Value);
var x = value(item);
return x;
}
/// <inheritdoc />
protected override string TooltipValue(TItem item)
{
return Chart.ValueAxis.Format(Chart.CategoryScale, Chart.CategoryScale.Value(Value(item)));
}
/// <inheritdoc />
protected override string TooltipTitle(TItem item)
{
var category = Category(Chart.ValueScale);
return Chart.CategoryAxis.Format(Chart.ValueScale, Chart.ValueScale.Value(category(item)));
}
/// <inheritdoc />
public override (object, Point) DataAt(double x, double y)
{
var value = ComposeValue(Chart.CategoryScale);
var category = ComposeCategory(Chart.ValueScale);
var ticks = Chart.CategoryScale.Ticks(Chart.ValueAxis.TickDistance);
var x0 = Chart.CategoryScale.Scale(Math.Max(0, ticks.Start));
var barSeries = VisibleBarSeries;
var index = barSeries.IndexOf(this);
var padding = Chart.BarOptions.Margin;
var bandHeight = BandHeight;
var height = bandHeight / barSeries.Count() - padding + padding / barSeries.Count();
foreach (var data in Items)
{
var startY = category(data) - bandHeight / 2 + index * height + index * padding;
var endY = startY + height;
var dataX = value(data);
var startX = Math.Min(dataX, x0);
var endX = Math.Max(dataX, x0);
if (startX <= x && x <= endX && startY <= y && y <= endY)
{
return (data, new Point() { X = x, Y = y });
}
}
return (null, null);
}
/// <inheritdoc />
internal override double TooltipY(TItem item)
{
var category = ComposeCategory(Chart.ValueScale);
var barSeries = VisibleBarSeries;
var index = barSeries.IndexOf(this);
var padding = Chart.BarOptions.Margin;
var bandHeight = BandHeight;
var height = bandHeight / barSeries.Count() - padding + padding / barSeries.Count();
var y = category(item) - bandHeight / 2 + index * height + index * padding;
return y + height / 2;
}
/// <inheritdoc />
public override IEnumerable<ChartDataLabel> GetDataLabels(double offsetX, double offsetY)
{
var list = new List<ChartDataLabel>();
(string Anchor, int Sign) position;
foreach (var d in Data)
{
position = Value(d) < 0 ? ("end", -1) : Value(d) == 0 ? ("middle", 0) : ("start", 1);
list.Add(new ChartDataLabel
{
Position = new Point() { X = TooltipX(d) + offsetX + (8 * position.Sign), Y = TooltipY(d) + offsetY },
TextAnchor = position.Anchor,
Text = Chart.ValueAxis.Format(Chart.CategoryScale, Value(d))
});
}
return list;
}
}
}