@page "/test"
Test
@code {
PieChart pieChart;
LineChart lineChart;
HorizontalBarChart barChartHoriz;
BarChart barChart;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await HandleRedraw();
}
}
object horizontalLineChartOptions = new
{
//Title = new
//{
// Display = true,
// Text = "Line chart sample"
//},
//Scales = new
//{
// XAxes = new object[]
// {
// new {
// //ScaleLabel = new {
// //Display = true, LabelString = "value" },
// Stacked = true
// }
// }
//},
Tooltips = new
{
Mode = "nearest",
Intersect = false
},
Hover = new
{
Mode = "nearest",
Intersect = false
},
Legend = new
{
Display = true,
FullWidth = true
},
AspectRatio = 2.5
};
async Task HandleRedraw()
{
await pieChart.Clear();
await pieChart.AddLabelsDatasetsAndUpdate(Labels, GetPieChartDataset());
await lineChart.Clear();
await lineChart.AddLabelsDatasetsAndUpdate(Labels, GetLineChartDataset());
await barChart.Clear();
await barChart.AddLabelsDatasetsAndUpdate(Labels, GetBarChartDataset());
await barChartHoriz.Clear();
await barChartHoriz.AddLabelsDatasetsAndUpdate(Labels, GetHorizBarChartDataset());
}
PieChartDataset GetPieChartDataset()
{
return new PieChartDataset
{
Label = "# of randoms",
Data = RandomizeData(),
BackgroundColor = backgroundColors,
BorderColor = borderColors
};
}
LineChartDataset GetLineChartDataset()
{
return new LineChartDataset
{
Label = "# of randoms",
Data = RandomizeData(),
BackgroundColor = backgroundColors,
BorderColor = borderColors,
Fill = true,
PointRadius = 2,
BorderDash = new List { }
};
}
BarChartDataset GetBarChartDataset()
{
return new BarChartDataset
{
Label = "# of randoms",
Data = RandomizeData(),
BackgroundColor = backgroundColors,
BorderColor = borderColors,
HoverBorderWidth = 5
};
}
BarChartDataset GetHorizBarChartDataset()
{
return new BarChartDataset
{
Label = "# of randoms",
Data = RandomizeData(),
BackgroundColor = backgroundColors,
BorderColor = borderColors,
HoverBorderWidth = 5
};
}
string[] Labels = { "Red", "Blue", "Yellow", "Green", "Purple", "Orange" };
List backgroundColors = new List { ChartColor.FromRgba(255, 99, 132, 0.2f), ChartColor.FromRgba(54, 162, 235, 0.2f), ChartColor.FromRgba(255, 206, 86, 0.2f), ChartColor.FromRgba(75, 192, 192, 0.2f), ChartColor.FromRgba(153, 102, 255, 0.2f), ChartColor.FromRgba(255, 159, 64, 0.2f) };
List borderColors = new List { ChartColor.FromRgba(255, 99, 132, 1f), ChartColor.FromRgba(54, 162, 235, 1f), ChartColor.FromRgba(255, 206, 86, 1f), ChartColor.FromRgba(75, 192, 192, 1f), ChartColor.FromRgba(153, 102, 255, 1f), ChartColor.FromRgba(255, 159, 64, 1f) };
List RandomizeData()
{
var r = new Random(DateTime.Now.Millisecond);
return new List { r.Next(3, 30) * r.NextDouble(), r.Next(3, 30) * r.NextDouble(), r.Next(3, 30) * r.NextDouble(), r.Next(3, 30) * r.NextDouble(), r.Next(3, 30) * r.NextDouble(), r.Next(3, 30) * r.NextDouble() };
}
}