实战 Chart 图表使用(1) #294
-
BootstrapBlazor组件 Chart 图表介绍通过给定数据,绘画各种图表的组件 本文主要介绍三种图表使用:折线图,柱状图,饼图 1.新建工程新建工程b06chart,使用 nuget.org 进行 BootstrapBlazor 组件安装, Chart 库,字体. 将项目添加到解决方案中
2.样式表和Javascript 引用增加主题样式表到 删除 并在下面添加三行
添加 Javascript 引用到 在
3.添加增加命名空间引用到
|
参数 | 说明 | 可选值 | 默认值 |
---|---|---|---|
Width | 组件宽度支持单位 如: 100px 75% | ||
ChartType | 图表类型 | Line/Bar/Pie/Doughnut/Bubble | Line |
7.事件
参数 | 说明 | 类型 |
---|---|---|
OnInitAsync | 组件数据初始化委托方法 | Func<Task> |
OnAfterInitAsync | 客户端绘制图表完毕后回调此委托方法 | Func |
OnAfterUpdateAsync | 客户端更新图表完毕后回调此委托方法 | Func<ChartAction, Task> |
8.为了快速建立随机数据,我们建立一个工具类 UtilityChart.cs
代码来源于bb演示工程
using BootstrapBlazor.Components;
namespace b06chart;
/// <summary>
/// Chart 工具类
/// </summary>
internal static class UtilityChart
{
public static IEnumerable<string> Colors { get; } = new List<string>() { "Red", "Blue", "Green", "Orange", "Yellow", "Tomato", "Pink", "Violet" };
/// <summary>
///
/// </summary>
/// <param name="chart"></param>
public static Task RandomData(Chart chart) => chart.Update(ChartAction.Update);
/// <summary>
///
/// </summary>
/// <param name="chart"></param>
/// <param name="dsCount"></param>
public static void AddDataSet(Chart chart, ref int dsCount)
{
if (dsCount < Colors.Count())
{
dsCount++;
_ = chart.Update(ChartAction.AddDataset);
}
}
/// <summary>
///
/// </summary>
/// <param name="chart"></param>
/// <param name="dsCount"></param>
public static void RemoveDataSet(Chart chart, ref int dsCount)
{
if (dsCount > 1)
{
dsCount--;
_ = chart.Update(ChartAction.RemoveDataset);
}
}
/// <summary>
///
/// </summary>
/// <param name="chart"></param>
/// <param name="daCount"></param>
public static void AddData(Chart chart, ref int daCount)
{
var limit = chart.ChartType switch
{
ChartType.Line => 14,
ChartType.Bar => 14,
ChartType.Bubble => 14,
_ => Colors.Count()
};
if (daCount < limit)
{
daCount++;
_ = chart.Update(ChartAction.AddData);
}
}
/// <summary>
///
/// </summary>
/// <param name="chart"></param>
/// <param name="daCount"></param>
public static void RemoveData(Chart chart, ref int daCount)
{
var limit = chart.ChartType switch
{
ChartType.Line => 7,
ChartType.Bar => 7,
ChartType.Bubble => 4,
_ => 2
};
if (daCount > limit)
{
daCount--;
_ = chart.Update(ChartAction.RemoveData);
}
}
}
9.折线图
<p>折线图</p>
<Chart @ref="LineChart" OnInitAsync="() => OnInit(0.4f, false)" Width="50%" />
<button class="btn btn-primary" @onclick="e => UtilityChart.RandomData(LineChart)"><i class="fa fa-line-chart"></i><span>随机数据</span></button>
@code{
private Random Randomer { get; } = new Random();
private int LineDatasetCount = 2;
private int LineDataCount = 7;
private Chart? LineChart { get; set; }
private Task<ChartDataSource> OnInit(float tension, bool hasNull)
{
var ds = new ChartDataSource();
ds.Options.Title = "Line 折线图";
ds.Options.X.Title = "天数";
ds.Options.Y.Title = "数值";
ds.Labels = Enumerable.Range(1, LineDataCount).Select(i => i.ToString());
for (var index = 0; index < LineDatasetCount; index++)
{
ds.Data.Add(new ChartDataset()
{
Tension = tension,
Label = $"数据集 {index}",
Data = Enumerable.Range(1, LineDataCount).Select((i, index) => (index == 2 && hasNull) ? null! : (object)Randomer.Next(20, 37))
});
}
return Task.FromResult(ds);
}
}
10.柱状图
<p>柱状图</p>
<Chart ChartType="ChartType.Bar" OnInitAsync="() => OnInit(false)" @ref="BarChart" Width="50%" />
<button class="btn btn-primary" @onclick="e => UtilityChart.RandomData(BarChart)"><i class="fa fa-bar-chart"></i><span>随机数据</span></button>
@code{
private int BarDatasetCount = 2;
private int BarDataCount = 7;
private Chart? BarChart { get; set; }
private Task<ChartDataSource> OnInit(bool stacked)
{
var ds = new ChartDataSource();
ds.Options.Title = "Bar 柱状图";
ds.Options.X.Title = "天数";
ds.Options.Y.Title = "数值";
ds.Options.X.Stacked = stacked;
ds.Options.Y.Stacked = stacked;
ds.Labels = Enumerable.Range(1, BarDataCount).Select(i => i.ToString());
for (var index = 0; index < BarDatasetCount; index++)
{
ds.Data.Add(new ChartDataset()
{
Label = $"数据集 {index}",
Data = Enumerable.Range(1, BarDataCount).Select(i => Randomer.Next(20, 37)).Cast<object>()
});
}
return Task.FromResult(ds);
}
}
11.饼图
<p>饼图</p>
<Chart ChartType="ChartType.Pie" OnInitAsync="@OnInit" @ref="PieChart" Width="50%" />
<button class="btn btn-primary" @onclick="e => UtilityChart.RandomData(PieChart)"><i class="fa fa-pie-chart"></i><span>随机数据</span></button>
@code{
private int PieDatasetCount = 1;
private int PieDataCount = 5;
private Chart? PieChart { get; set; }
private Task<ChartDataSource> OnInit()
{
var ds = new ChartDataSource();
ds.Options.Title = "Pie 饼图";
ds.Labels = UtilityChart.Colors.Take(PieDataCount);
for (var index = 0; index < PieDatasetCount; index++)
{
ds.Data.Add(new ChartDataset()
{
Label = $"数据集 {index}",
Data = Enumerable.Range(1, PieDataCount).Select(i => Randomer.Next(20, 37)).Cast<object>()
});
}
return Task.FromResult(ds);
}
}
12.未完待续 :)
通过简单的步骤,已经可以初步体验图表组件,由于近日事务较多,这篇只是初略的带大家快速入门,体验一下BootstrapBlazor的Chart 图表使用. 下一篇章我们将会对组件进行一些调整,例如添加移除数据集,添加移除数据,X轴Y轴数据改变后如何刷新组件,以及跟数据库结合查询生成图表.
项目源码
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
CV一条龙系列教程 |
Beta Was this translation helpful? Give feedback.
CV一条龙系列教程