|
| 1 | +@page "/scales/time/line" |
| 2 | +@using ChartJs.Blazor.LineChart |
| 3 | +@layout SampleLayout |
| 4 | + |
| 5 | +<Chart Config="_config" @ref="_chart"></Chart> |
| 6 | + |
| 7 | +<button @onclick="RandomizeData">Randomize Data</button> |
| 8 | +<button @onclick="AddDataset">Add Dataset</button> |
| 9 | +<button @onclick="RemoveDataset">Remove Dataset</button> |
| 10 | +<button @onclick="AddData">Add Data</button> |
| 11 | +<button @onclick="RemoveData">Remove Data</button> |
| 12 | + |
| 13 | +@code { |
| 14 | + private const int InitalCount = 7; |
| 15 | + private LineConfig _config; |
| 16 | + private Chart _chart; |
| 17 | + |
| 18 | + protected override void OnInitialized() |
| 19 | + { |
| 20 | + _config = new LineConfig |
| 21 | + { |
| 22 | + Options = new LineOptions |
| 23 | + { |
| 24 | + Responsive = true, |
| 25 | + Title = new OptionsTitle |
| 26 | + { |
| 27 | + Display = true, |
| 28 | + Text = "ChartJs.Blazor Time Scale Chart" |
| 29 | + }, |
| 30 | + Tooltips = new Tooltips |
| 31 | + { |
| 32 | + Mode = InteractionMode.Nearest, |
| 33 | + Intersect = true |
| 34 | + }, |
| 35 | + Hover = new Hover |
| 36 | + { |
| 37 | + Mode = InteractionMode.Nearest, |
| 38 | + Intersect = true |
| 39 | + }, |
| 40 | + Scales = new Scales |
| 41 | + { |
| 42 | + XAxes = new List<CartesianAxis> |
| 43 | + { |
| 44 | + new TimeAxis |
| 45 | + { |
| 46 | + ScaleLabel = new ScaleLabel |
| 47 | + { |
| 48 | + LabelString = "Date" |
| 49 | + }, |
| 50 | + Time = new TimeOptions |
| 51 | + { |
| 52 | + TooltipFormat = "ll HH:mm" |
| 53 | + }, |
| 54 | + } |
| 55 | + }, |
| 56 | + YAxes = new List<CartesianAxis> |
| 57 | + { |
| 58 | + new LinearCartesianAxis |
| 59 | + { |
| 60 | + ScaleLabel = new ScaleLabel |
| 61 | + { |
| 62 | + LabelString = "Value" |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + _config.Data.Labels.AddRange(GetNextDays(InitalCount).Select(d => d.ToString("o"))); |
| 71 | + |
| 72 | + IDataset<int> dataset1 = new LineDataset<int>(RandomScalingFactor(InitalCount)) |
| 73 | + { |
| 74 | + Label = "My first dataset", |
| 75 | + BackgroundColor = ColorUtil.FromDrawingColor(ChartColors.Red), |
| 76 | + BorderColor = ColorUtil.FromDrawingColor(ChartColors.Red), |
| 77 | + Fill = FillingMode.Disabled |
| 78 | + }; |
| 79 | + |
| 80 | + IDataset<int> dataset2 = new LineDataset<int>(RandomScalingFactor(InitalCount)) |
| 81 | + { |
| 82 | + Label = "My second dataset", |
| 83 | + BackgroundColor = ColorUtil.FromDrawingColor(ChartColors.Blue), |
| 84 | + BorderColor = ColorUtil.FromDrawingColor(ChartColors.Blue), |
| 85 | + Fill = FillingMode.Disabled |
| 86 | + }; |
| 87 | + |
| 88 | + IDataset<TimePoint> dataset3 = new LineDataset<TimePoint>() |
| 89 | + { |
| 90 | + Label = "Dataset with point data", |
| 91 | + BackgroundColor = ColorUtil.FromDrawingColor(ChartColors.Green), |
| 92 | + BorderColor = ColorUtil.FromDrawingColor(ChartColors.Green), |
| 93 | + Fill = FillingMode.Disabled |
| 94 | + }; |
| 95 | + |
| 96 | + DateTime now = DateTime.Now; |
| 97 | + dataset3.Add(new TimePoint(now, RandomScalingFactor())); |
| 98 | + dataset3.Add(new TimePoint(now.AddDays(5), RandomScalingFactor())); |
| 99 | + dataset3.Add(new TimePoint(now.AddDays(7), RandomScalingFactor())); |
| 100 | + dataset3.Add(new TimePoint(now.AddDays(15), RandomScalingFactor())); |
| 101 | + |
| 102 | + _config.Data.Datasets.Add(dataset1); |
| 103 | + _config.Data.Datasets.Add(dataset2); |
| 104 | + _config.Data.Datasets.Add(dataset3); |
| 105 | + } |
| 106 | + |
| 107 | + private void RandomizeData() |
| 108 | + { |
| 109 | + foreach (IDataset dataset in _config.Data.Datasets) |
| 110 | + { |
| 111 | + if (dataset is IDataset<TimePoint> pointDataset) |
| 112 | + { |
| 113 | + for (int i = 0; i < pointDataset.Count; i++) |
| 114 | + { |
| 115 | + pointDataset[i] = new TimePoint(pointDataset[i].Time, RandomScalingFactor()); |
| 116 | + } |
| 117 | + } |
| 118 | + else if (dataset is IDataset<int> intDataset) |
| 119 | + { |
| 120 | + int count = intDataset.Count; |
| 121 | + intDataset.Clear(); |
| 122 | + intDataset.AddRange(RandomScalingFactor(count)); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + _chart.Update(); |
| 127 | + } |
| 128 | + |
| 129 | + private void AddDataset() |
| 130 | + { |
| 131 | + string color = ColorUtil.FromDrawingColor(ChartColors.All[_config.Data.Datasets.Count % ChartColors.All.Count]); |
| 132 | + IDataset<int> dataset = new LineDataset<int>(RandomScalingFactor(_config.Data.Labels.Count)) |
| 133 | + { |
| 134 | + Label = $"Dataset {_config.Data.Datasets.Count}", |
| 135 | + BackgroundColor = color, |
| 136 | + BorderColor = color, |
| 137 | + Fill = FillingMode.Disabled |
| 138 | + }; |
| 139 | + |
| 140 | + _config.Data.Datasets.Add(dataset); |
| 141 | + _chart.Update(); |
| 142 | + } |
| 143 | + |
| 144 | + private void RemoveDataset() |
| 145 | + { |
| 146 | + IList<IDataset> datasets = _config.Data.Datasets; |
| 147 | + if (datasets.Count == 0) |
| 148 | + return; |
| 149 | + |
| 150 | + datasets.RemoveAt(datasets.Count - 1); |
| 151 | + _chart.Update(); |
| 152 | + } |
| 153 | + |
| 154 | + private void AddData() |
| 155 | + { |
| 156 | + if (_config.Data.Datasets.Count == 0) |
| 157 | + return; |
| 158 | + |
| 159 | + DateTime now = DateTime.Now; |
| 160 | + _config.Data.Labels.Add(now.AddDays(_config.Data.Labels.Count).ToString("o")); |
| 161 | + |
| 162 | + foreach (IDataset dataset in _config.Data.Datasets) |
| 163 | + { |
| 164 | + if (dataset is IDataset<TimePoint> pointDataset) |
| 165 | + { |
| 166 | + pointDataset.Add(new TimePoint(now.AddDays(pointDataset.Count), RandomScalingFactor())); |
| 167 | + } |
| 168 | + else if (dataset is IDataset<int> intDataset) |
| 169 | + { |
| 170 | + intDataset.Add(RandomScalingFactor()); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + _chart.Update(); |
| 175 | + } |
| 176 | + |
| 177 | + private void RemoveData() |
| 178 | + { |
| 179 | + if (_config.Data.Datasets.Count == 0) |
| 180 | + return; |
| 181 | + |
| 182 | + IList<string> labels = _config.Data.Labels; |
| 183 | + if (labels.Count > 0) |
| 184 | + labels.RemoveAt(labels.Count - 1); |
| 185 | + |
| 186 | + foreach (IDataset dataset in _config.Data.Datasets) |
| 187 | + { |
| 188 | + if (dataset is IDataset<TimePoint> pointDataset && |
| 189 | + pointDataset.Count > 0) |
| 190 | + { |
| 191 | + pointDataset.RemoveAt(pointDataset.Count - 1); |
| 192 | + } |
| 193 | + else if (dataset is IDataset<int> intDataset && |
| 194 | + intDataset.Count > 0) |
| 195 | + { |
| 196 | + intDataset.RemoveAt(intDataset.Count - 1); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + _chart.Update(); |
| 201 | + } |
| 202 | +} |
0 commit comments