赞
踩
Chart控件集成了颇多的统计图模型,拿来即用的理念大大节省了开发的时间。下面演示最常见的三种统计图模型的使用。
效果展示:
C#代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Windows.Forms.DataVisualization.Charting;
-
- namespace Chart控件的使用
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- ColumnAndLine();
- Pie();
- }
- private void ColumnAndLine() {
- //设置统计图标题
- chart1.Titles.Add("条形统计图和折现统计的使用");
- //设置XY轴上面的标签
- chart1.ChartAreas[0].AxisX.Title = "月份(月)";
- chart1.ChartAreas[0].AxisY.Title = "数量(个)";
- //添加统计对象
- chart1.Series.Add("Column1");
- chart1.Series.Add("Line1");
- //设置统计对象的统计图类型
- chart1.Series["Column1"].ChartType = SeriesChartType.Column;
- chart1.Series["Line1"].ChartType = SeriesChartType.Line;
- //设置统计对象颜色
- chart1.Series["Column1"].Color = Color.Blue;
- chart1.Series["Line1"].Color = Color.Red;
- //设置统计对象粗细,单位为pixel
- chart1.Series["Column1"].BorderWidth = 5;
- chart1.Series["Line1"].BorderWidth = 3;
- //设置XY轴上的值类型
- chart1.Series["Column1"].XValueType = ChartValueType.String;
- chart1.Series["Column1"].YValueType = ChartValueType.Int64;
- chart1.Series["Line1"].XValueType = ChartValueType.String;
- chart1.Series["Line1"].YValueType = ChartValueType.Int64;
-
- //准备数据
- String[] AllX = { "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月" };
- int[] ColumnY = { 20, 40, 10, 50, 60, 80, 70, 90 };
- int[] LineY = { 90, 70, 80, 60, 50, 10, 40, 20 };
- //绑定数据
- chart1.Series["Column1"].Points.DataBindXY(AllX, ColumnY);
- chart1.Series["Line1"].Points.DataBindXY(AllX, LineY);
- //鼠标移动到对应点显示数值
- chart1.Series["Column1"].ToolTip = "#VALX:#VAL(个)";
- chart1.Series["Line1"].ToolTip = "#VALX:#VAL(个)";
- }
-
- private void Pie() {
- //标题
- chart2.Titles.Add("扇形统计图的使用");
- chart2.Titles[0].ForeColor = Color.Black;
- chart2.Titles[0].Font = new Font("微软雅黑", 16f, FontStyle.Regular);
- //设置统计对象的统计图类型
- chart2.Series[0].ChartType = SeriesChartType.Pie;
- //准备数据
- String[] PieX = { "A", "B", "C", "D"};
- int[] PieY = { 20, 40, 30, 50 };
- //设置XY轴上的值类型
- chart2.Series[0].XValueType = ChartValueType.String;
- chart2.Series[0].YValueType = ChartValueType.Int64;
- //绑定数据
- chart2.Series[0].Points.DataBindXY(PieX, PieY);
- //鼠标移动到对应点显示数值
- chart2.Series[0].ToolTip = "#VAL";
- }
-
- }
- }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。