Telerik®RadChartView控件WinForm中的简单使用(柱状图)
前言
刚入手Telerik,对其使用方法还有待学习,某度后发现其对winform的使用介绍非常少,即使有都是古董级别稀稀碎碎的内容,特只有根据官方例子自己琢磨。Telerik软件安装及添加控件在这儿就不介绍了,直接进入正题。
样式
步骤及代码
1.拖一个RadChartView到Form里面,命名为occupiedPerDayChartView(便于与下面代码对应,可根据情况自行命名)。
2.在界面初始化时也初始化该控件,注释详细介绍了各功能。为方便演示,初始化时我也从Sql Server数据库中加载了数据到DataTable中,Sql Server数据数据库结构下面再贴出来。
public Form1()
{
InitializeComponent();
string selectSql = "select UName,UValue,UTime from UTable order by UName,UTime";
DataTable dataTable = SqlHelper.Querytable(selectSql);
this.occupiedPerDayChartView.ShowTitle = true;//显示标题
this.occupiedPerDayChartView.Title = "柱状图展示测试";//标题名称
this.occupiedPerDayChartView.ChartElement.TitleElement.TextAlignment =
ContentAlignment.MiddleCenter;//标题显示位置
this.occupiedPerDayChartView.ChartElement.TitleElement.CustomFont = "宋体";//标题字体
this.occupiedPerDayChartView.ChartElement.TitleElement.CustomFontSize = 20f;//标题字体大小
CartesianArea area = occupiedPerDayChartView.GetArea<CartesianArea>();//实例化网格
area.ShowGrid = true;//显示网格
CartesianGrid grid = area.GetGrid<CartesianGrid>();//创建网格
grid.DrawHorizontalFills = false;//水平画满
grid.BorderDashStyle = System.Drawing.Drawing2D.DashStyle.Dot;//边框样式
this.occupiedPerDayChartView.Area.View.Palette = KnownPalette.Rainbow;//调色板
LoadChart(dataTable);
}
3.显示加载数据及初始化Label
private void LoadChart(DataTable bookings)
{
//初始化Label背景
this.occupiedPerDayChartView.LabelFormatting -= occupiedPerDayChartView_LabelFormatting;
this.occupiedPerDayChartView.LabelFormatting += occupiedPerDayChartView_LabelFormatting;
int occupiedPerDay = bookings.Rows.Count;//Datatable数据数量
int TotalCount = 0;//值总数量(用于下面取平均值)
/*添加第一部分数据*/
BarSeries barSeries = new BarSeries();//定义一个柱状图集合
barSeries.ShowLabels = true;//显示值数据
foreach (var booking in bookings.Select("UName='UA'"))
{
//添加值集合
barSeries.DataPoints.Add(new CategoricalDataPoint(Convert.ToInt32( booking["UValue"].ToString()), booking["UTime"].ToString()));
TotalCount += Convert.ToInt32(booking["UValue"].ToString());
}
this.occupiedPerDayChartView.Series.Add(barSeries);//添加柱状图集合
/*添加第二部分数据*/
barSeries = new BarSeries();
barSeries.ShowLabels = true;//显示值数据
foreach (var booking in bookings.Select("UName='UB'"))
{
barSeries.DataPoints.Add(new CategoricalDataPoint(Convert.ToInt32(booking["UValue"].ToString()), booking["UTime"].ToString()));
TotalCount += Convert.ToInt32(booking["UValue"].ToString());
}
this.occupiedPerDayChartView.Series.Add(barSeries);
/*添加垂直Y轴信息*/
LinearAxis verticalAxis = barSeries.VerticalAxis as LinearAxis;
//verticalAxis.BorderColor = Color.FromArgb(209, 209, 209);//刻度值/边框/刻度颜色
verticalAxis.CustomFont = "微软雅黑";
verticalAxis.CustomFontSize = 10f;
verticalAxis.Minimum = 0;//设置Y轴起值
//verticalAxis.Maximum = Math.Max(verticalMaxValue, 5);//设置Y轴最大刻度值
verticalAxis.MajorStep = 10;//设置Y轴刻度间隔值
/*添加水平X轴信息*/
CategoricalAxis horizontalAxis = barSeries.HorizontalAxis as CategoricalAxis;
horizontalAxis.LabelFitMode = AxisLabelFitMode.Rotate;// MultiLine;//设置X轴刻度值字体类型(倾斜/水平)
horizontalAxis.GapLength = 0.3;//柱体之间间距,间接设置柱体宽度
horizontalAxis.CustomFont = "微软雅黑";
horizontalAxis.CustomFontSize = 10f;
//horizontalAxis.LabelFormat = "{0:dd/MM/yy}";
horizontalAxis.BorderColor = Color.FromArgb(209, 209, 209);//刻度值/边框/刻度颜色
this.occupiedPerDayChartView.ChartElement.View.Margin = new Padding(60, 20, 40, 20);
if (occupiedPerDay > 0)//设置准线(即指示注释线,这里是设置的是平均值)
{
CartesianGridLineAnnotation annotation1 = new CartesianGridLineAnnotation();
annotation1.Axis = verticalAxis;//设置水平线
annotation1.Value = TotalCount/ occupiedPerDay;//平均值
annotation1.BorderColor = Color.FromArgb(255, 145, 0);//线条颜色
annotation1.BorderDashStyle = DashStyle.Dot;//线条类型
annotation1.BorderWidth = 1;//线条宽度
annotation1.Label = (TotalCount / occupiedPerDay).ToString();//显示平均值
annotation1.ForeColor = Color.Red;//字体颜色
this.occupiedPerDayChartView.Annotations.Add(annotation1);//添加注释
}
this.occupiedPerDayChartView.ChartElement.InvalidateMeasure(true);
this.occupiedPerDayChartView.ChartElement.UpdateLayout();
}
/// <summary>
/// 初始化值数据显示背景颜色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void occupiedPerDayChartView_LabelFormatting(object sender, ChartViewLabelFormattingEventArgs e)
{
e.LabelElement.BackColor = Color.Transparent;//设置值数据背景色
e.LabelElement.BorderColor = Color.Transparent;//设置值数据边框色
}
Sql Server数据
UID | UName | UValue | UTime | UData |
---|---|---|---|---|
1 | UA | 5 | 201026 | |
2 | UA | 23 | 201027 | |
3 | UA | 44 | 201028 | |
4 | UA | 29 | 201029 | |
5 | UA | 11 | 201030 | |
6 | UA | 13 | 201031 | |
7 | UA | 3 | 201032 | |
8 | UA | 33 | 201033 | |
9 | UA | 79 | 201034 | |
10 | UA | 88 | 201035 | |
11 | UB | 78 | 201026 | |
12 | UB | 15 | 201027 | |
13 | UB | 79 | 201028 | |
14 | UB | 16 | 201029 | |
15 | UB | 49 | 201030 | |
16 | UB | 85 | 201031 | |
17 | UB | 36 | 201032 | |
18 | UB | 4 | 201033 | |
19 | UB | 27 | 201034 | |
20 | UB | 84 | 201035 |
样式说明
RadChartView自带样式(图表颜色)可以鼠标右键该控件上选择其样式主题
本章完