当前位置:网站首页>C# Winfrom Chart图表控件 柱状图、折线图
C# Winfrom Chart图表控件 柱状图、折线图
2022-06-29 16:08:00 【熊思宇】
目录
效果
一、参考
强大的Winform Chart图表控件使用说明 - 走看看
二、柱状图
1.新建项目
新建一个.NET Framework 类型的 Winfrom 项目,在数据里面找到 Chart 控件,拖入到界面中
如下:
拖入控件时,默认的有一个图表的样式,实际运行其实是一片空白
2.修改图表样式
在Series这里,点击集合后面到三个点
这里可以修改对应的名字,在左边添加按钮这里,可以添加一组柱状图,如果是折线图,则是两根线。
这里可以修改图表的样式,比如柱状图,折线图
3.绑定数据
此时图表还没有任何数据,可以用绑定方式添加数据,如下
private List<int> XList = new List<int>();
private List<int> YList = new List<int>();
private Random randoms = new Random();
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 6; i++)
{
XList.Add(i);
YList.Add(randoms.Next(10, 100));
}
chart1.Series["Series1"].Points.DataBindXY(XList, YList);
}
运行后如下
上图有两个问题,
1.柱状图没有显示具体数据,根本看不出来实际数据是多少,
2.背景的网格没有去掉,样式比较难看,
用代码可以解决这两个问题
private List<int> XList = new List<int>();
private List<int> YList = new List<int>();
private Random randoms = new Random();
//private int index = -1;
private void Form1_Load(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.NotSet; //设置网格类型为虚线
chart1.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash; //设置网格类型为虚线
chart1.Series[0].IsValueShownAsLabel = true;//设置显示示数
for (int i = 0; i < 6; i++)
{
XList.Add(i);
YList.Add(randoms.Next(10, 100));
}
//写法同上面到 chart1.ChartAreas[0] 类似
chart1.Series["Series1"].Points.DataBindXY(XList, YList);
}
效果
此时和我们想要的效果就差不多了
三、折线图
1.图表设置
新建一个.NET Framework 类型的 Winfrom 项目,操作同上面一致,将ChartType 这里设置成 Line,并且在Form1界面中添加一个按钮
运行以后,图表依然是一片空白
2.绑定数据
添加代码
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace 折线图
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private List<int> XList = new List<int>();
private List<int> YList = new List<int>();
private Random randoms = new Random();
private int index = -1;
private void Form1_Load(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.NotSet; //设置网格类型为虚线
chart1.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash; //设置网格类型为虚线
chart1.Series[0].IsValueShownAsLabel = true;//设置显示示数
}
private void button1_Click(object sender, EventArgs e)
{
index++;
XList.Add(index);
YList.Add(randoms.Next(10, 100));
//chart1.Series[0].Points.AddXY(index, randoms.Next(10, 100));
chart1.Series[0].Points.DataBindXY(XList, YList);
}
}
}
运行后,点击添加按钮,效果如下
这里你会发现一个问题,图中到93这个点,已经跑到顶部了,字都被挡了一半 ,这里可以用代码设置,比如,遍历整个Y轴的值,然后计算出最大值和最小值,并给chart1的Maximum,Minimum赋值,就可以让折线图居中了
chart1.ChartAreas[0].AxisY.Maximum = 200;
如果你想要下面的效果,也可以参考我的帖子,虽然DLL不一样,写法基本一致,在下面的源码中也有这个案例到效果,有需要的可以支持一下我了,谢谢。
C# System.Windows.Forms.DataVisualization Demo案例_熊思宇的博客-CSDN博客_c# system.windows.forms
源码下载:点击下载
结束
如果这个帖子对你有用,欢迎 关注 + 点赞 + 留言,谢谢
end
边栏推荐
- 按键精灵打怪学习-多窗口多线程后台技能
- Metadata management Apache Atlas Compilation integration deployment and testing
- 迪赛智慧数——其他图表(基本旭日图):毕业演讲高频词
- Profil de risque de monnaie stable: l'usdt et l'USDC sont - ils sûrs?
- MATLAB输出格式控制 %d,%f,%c,%s的用法
- Magento 和 WordPress 的区别
- Cortical traceability analysis of ERP
- SSL free certificate application, how about the actual effect of free SSL certificate?
- Blue bridge cup several full array questions
- Science: the interrelated causes and consequences of sleep in the brain
猜你喜欢
随机推荐
UWB精准定位方案,厘米级高精度技术应用,智能配对感应技术
telnet+ftp 对设备进行 操控 和 升级
隐私计算助力数据的安全流通与共享
Key sprite fighting monsters - window binding skill
天龙八部TLBB系列 - 网单用数据库修改为其他门派
MATLAB给数据加噪声/扰动
C语言——printf打印进制前缀
Small programs have a "big" role in the industrial Internet
Sophon autocv: help AI industrial production and realize visual intelligent perception
What is the strength of a software testing engineer who can get a salary increase twice a year?
Cv5200 ad hoc network remote WiFi module, UAV wireless image transmission application, HD low delay scheme
Flutter技术与实战(1)
对于产业互联网的认识,直接关系着我们究竟会以怎样的心态来看待它
What are the top level Chinese programmers?
同样是做测试,为什么别人年薪30W+?
DAP large screen theme development description
C11新特性——auto、decltype类型指示符
MySQL进阶——存储引擎
卫龙辣条第三次冲刺上市:业绩增速下滑,刘卫平、刘福平提前套现
The understanding of industrial Internet is directly related to how we view it