强大的Winform Chart图表控件使用说明

  • 引言
    • 使用说明
    • 使用示例
    • 资料

引言

以前经常开发网页端的软件,图表组件一般用echart,功能和样式都非常齐全。但是当用winform开发时,类似的组件就很少了,而且稍微好点的都还收费。
后来探索了下winform自带的 chart控件,发现其功能非常强大,只要你有好看的设计图,基本上都可以按设计图调整出来,下面展示下自己做技术测试的图片:

使用说明

工具箱-数据分组中我们就可以看到chart控件,使用时拖拽到界面即可。可以通过属性面板设置其样式,也可以通过代码来调整样式。设置的属性可参照:
https://www.cnblogs.com/arxive/p/5861960.html

总结了部分设置chart样式功能的通用类:

 public class ChartHelper{/// <summary>/// Name:添加序列/// Author:by boxuming 2019-04-28 13:59/// </summary>/// <param name="chart">图表对象</param>/// <param name="seriesName">序列名称</param>/// <param name="chartType">图表类型</param>/// <param name="color">颜色</param>/// <param name="markColor">标记点颜色</param>/// <param name="showValue">是否显示数值</param>public static void AddSeries(Chart chart, string seriesName, SeriesChartType chartType, Color color, Color markColor, bool showValue = false){chart.Series.Add(seriesName);chart.Series[seriesName].ChartType = chartType;chart.Series[seriesName].Color = color;if (showValue){chart.Series[seriesName].IsValueShownAsLabel = true;chart.Series[seriesName].MarkerStyle = MarkerStyle.Circle;chart.Series[seriesName].MarkerColor = markColor;chart.Series[seriesName].LabelForeColor = color;chart.Series[seriesName].LabelAngle = -90;}}/// <summary>/// Name:设置标题/// Author:by boxuming 2019-04-28 14:25/// </summary>/// <param name="chart">图表对象</param>/// <param name="chartName">图表名称</param>public static void SetTitle(Chart chart, string chartName, Font font, Docking docking, Color foreColor){chart.Titles.Add(chartName);chart.Titles[0].Font = font;chart.Titles[0].Docking = docking;chart.Titles[0].ForeColor = foreColor;}/// <summary>/// Name:设置样式/// Author:by boxuming 2019-04-23 14:04/// </summary>/// <param name="chart">图表对象</param>/// <param name="backColor">背景颜色</param>/// <param name="foreColor">字体颜色</param>public static void SetStyle(Chart chart, Color backColor, Color foreColor){chart.BackColor = backColor;chart.ChartAreas[0].BackColor = backColor;chart.ForeColor = Color.Red;}/// <summary>/// Name:设置图例/// Author:by boxuming 2019-04-23 14:30/// </summary>/// <param name="chart">图表对象</param>/// <param name="docking">停靠位置</param>/// <param name="align">对齐方式</param>/// <param name="backColor">背景颜色</param>/// <param name="foreColor">字体颜色</param>public static void SetLegend(Chart chart, Docking docking, StringAlignment align, Color backColor, Color foreColor){chart.Legends[0].Docking = docking;chart.Legends[0].Alignment = align;chart.Legends[0].BackColor = backColor;chart.Legends[0].ForeColor = foreColor;}/// <summary>/// Name:设置XY轴/// Author:by boxuming 2019-04-23 14:35/// </summary>/// <param name="chart">图表对象</param>/// <param name="xTitle">X轴标题</param>/// <param name="yTitle">Y轴标题</param>/// <param name="align">坐标轴标题对齐方式</param>/// <param name="foreColor">坐标轴字体颜色</param>/// <param name="lineColor">坐标轴颜色</param>/// <param name="arrowStyle">坐标轴箭头样式</param>/// <param name="xInterval">X轴的间距</param>/// <param name="yInterval">Y轴的间距</param>public static void SetXY(Chart chart, string xTitle, string yTitle, StringAlignment align, Color foreColor, Color lineColor, AxisArrowStyle arrowStyle, double xInterval, double yInterval){chart.ChartAreas[0].AxisX.Title = xTitle;chart.ChartAreas[0].AxisY.Title = yTitle;chart.ChartAreas[0].AxisX.TitleAlignment = align;chart.ChartAreas[0].AxisY.TitleAlignment = align;chart.ChartAreas[0].AxisX.TitleForeColor = foreColor;chart.ChartAreas[0].AxisY.TitleForeColor = foreColor;chart.ChartAreas[0].AxisX.LabelStyle = new LabelStyle() { ForeColor = foreColor };chart.ChartAreas[0].AxisY.LabelStyle = new LabelStyle() { ForeColor = foreColor };chart.ChartAreas[0].AxisX.LineColor = lineColor;chart.ChartAreas[0].AxisY.LineColor = lineColor;chart.ChartAreas[0].AxisX.ArrowStyle = arrowStyle;chart.ChartAreas[0].AxisY.ArrowStyle = arrowStyle;chart.ChartAreas[0].AxisX.Interval = xInterval;chart.ChartAreas[0].AxisY.Interval = yInterval;}/// <summary>/// Name:设置网格/// Author:by boxuming 2019-04-23 14:55/// </summary>/// <param name="chart">图表对象</param>/// <param name="lineColor">网格线颜色</param>/// <param name="xInterval">X轴网格的间距</param>/// <param name="yInterval">Y轴网格的间距</param>public static void SetMajorGrid(Chart chart, Color lineColor, double xInterval, double yInterval){chart.ChartAreas[0].AxisX.MajorGrid.LineColor = lineColor;chart.ChartAreas[0].AxisY.MajorGrid.LineColor = lineColor;chart.ChartAreas[0].AxisX.MajorGrid.Interval = xInterval;chart.ChartAreas[0].AxisY.MajorGrid.Interval = yInterval;}}

使用示例

文章开头展示的图片中的四个统计图表可通过以下代码实现:

         chart1.Series.Clear();ChartHelper.AddSeries(chart1, "柱状图", SeriesChartType.Column, Color.Lime, Color.Red, true);ChartHelper.AddSeries(chart1, "曲线图", SeriesChartType.Spline, Color.Red, Color.Red);ChartHelper.SetTitle(chart1, "柱状图与曲线图", new Font("微软雅黑", 12), Docking.Bottom, Color.White);ChartHelper.SetStyle(chart1, Color.Transparent, Color.White);ChartHelper.SetLegend(chart1, Docking.Top, StringAlignment.Center, Color.Transparent, Color.White);ChartHelper.SetXY(chart1, "序号", "数值", StringAlignment.Far, Color.White, Color.White, AxisArrowStyle.SharpTriangle, 1, 2);ChartHelper.SetMajorGrid(chart1, Color.Gray, 20, 2);chart2.Series.Clear();ChartHelper.AddSeries(chart2, "饼状图", SeriesChartType.Pie, Color.Lime, Color.Red, true);ChartHelper.SetStyle(chart2, Color.Transparent, Color.White);ChartHelper.SetLegend(chart2, Docking.Top, StringAlignment.Center, Color.Transparent, Color.White);chart3.Series.Clear();ChartHelper.AddSeries(chart3, "曲线图", SeriesChartType.SplineRange, Color.FromArgb(100,46, 199, 201), Color.Red, true);ChartHelper.SetTitle(chart3, "曲线图", new Font("微软雅黑", 12), Docking.Bottom, Color.FromArgb(46, 199, 201));ChartHelper.SetStyle(chart3, Color.Transparent, Color.White);ChartHelper.SetLegend(chart3, Docking.Top, StringAlignment.Center, Color.Transparent, Color.White);ChartHelper.SetXY(chart3, "序号", "数值", StringAlignment.Far, Color.White, Color.White, AxisArrowStyle.SharpTriangle, 1, 2);ChartHelper.SetMajorGrid(chart3, Color.Gray, 20, 2);chart4.Series.Clear();ChartHelper.AddSeries(chart4, "饼状图", SeriesChartType.Funnel, Color.Lime, Color.Red, true);ChartHelper.SetStyle(chart4, Color.Transparent, Color.White);ChartHelper.SetLegend(chart4, Docking.Top, StringAlignment.Center, Color.Transparent, Color.White);RefreshData();

其中,均是通过调用通用类来进行设置,这样更方便一些。最后的RefreshData()是绑定图表数据的部分,否则没有数据的话图表是无法显示的。绑定数据部分代码如下:

     public void RefreshData(){List<int> x1 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };List<int> y1 = new List<int>();Random ra = new Random();y1 = new List<int>() {ra.Next(1, 10),ra.Next(1, 10),ra.Next(1, 10),ra.Next(1, 10),ra.Next(1, 10),ra.Next(1, 10),ra.Next(1, 10),ra.Next(1, 10),ra.Next(1, 10),ra.Next(1, 10),ra.Next(1, 10),ra.Next(1, 10)};RefreshChart(x1, y1, "chart1");RefreshChart(x1, y1, "chart2");RefreshChart(x1, y1, "chart3");RefreshChart(x1, y1, "chart4");}public delegate void RefreshChartDelegate(List<int> x, List<int> y, string type);public void RefreshChart(List<int> x, List<int> y, string type){if (type == "chart1"){if (this.chart1.InvokeRequired){RefreshChartDelegate stcb = new RefreshChartDelegate(RefreshChart);this.Invoke(stcb, new object[] { x, y, type });}else{chart1.Series[0].Points.DataBindXY(x, y);chart1.Series[1].Points.DataBindXY(x, y);}}else if (type == "chart2"){if (this.chart2.InvokeRequired){RefreshChartDelegate stcb = new RefreshChartDelegate(RefreshChart);this.Invoke(stcb, new object[] { x, y, type });}else{chart2.Series[0].Points.DataBindXY(x, y);List<Color> colors = new List<Color>() {Color.Red,Color.DarkRed,Color.IndianRed,Color.MediumVioletRed,Color.OrangeRed,Color.PaleVioletRed,Color.Purple,Color.DarkOrange,Color.Maroon,Color.LightCoral,Color.LightPink,Color.Magenta};DataPointCollection points = chart2.Series[0].Points;for (int i = 0; i < points.Count; i++){points[i].Color = colors[i];}}}else if (type == "chart3"){if (this.chart3.InvokeRequired){RefreshChartDelegate stcb = new RefreshChartDelegate(RefreshChart);this.Invoke(stcb, new object[] { x, y, type });}else{chart3.Series[0].Points.DataBindXY(x, y);}}else if (type == "chart4"){if (this.chart4.InvokeRequired){RefreshChartDelegate stcb = new RefreshChartDelegate(RefreshChart);this.Invoke(stcb, new object[] { x, y, type });}else{chart4.Series[0].Points.DataBindXY(x, y);List<Color> colors = new List<Color>() {Color.Red,Color.DarkRed,Color.IndianRed,Color.MediumVioletRed,Color.OrangeRed,Color.PaleVioletRed,Color.Purple,Color.DarkOrange,Color.Maroon,Color.LightCoral,Color.LightPink,Color.Magenta};DataPointCollection points = chart4.Series[0].Points;for (int i = 0; i < points.Count; i++){points[i].Color = colors[i];}}}}

为了让图表展现动画效果,添加了一个定时器,用来实时改变数据,这样就可以看到动起来的图表了。定时器代码如下:

     private void Timer1_Tick(object sender, EventArgs e){new Thread(new ThreadStart(RefreshData)).Start();}

资料

以上只是使用chart控件的几个简单示例,而chart控件的功能远远不止这些。为了更方便地了解和使用其他功能,特提供了一些资料,其中包括图表的属性说明文档、各类图表的样式和对应的代码、以及程序示例等,下载地址如下:
链接:https://pan.baidu.com/s/1mCxiwrEBxeCKkSltXMJPpA
提取码:9pgr

强大的Winform Chart图表控件使用说明相关推荐

  1. JQuery Highcharts图表控件使用说明

    JQuery Highcharts图表控件使用说明 Highcharts 官网:http://www.highcharts.com Highcharts 官网示例:http://www.highcha ...

  2. 微软C#中的CHART图表控件

    http://www.cnblogs.com/winshe/articles/6604406.html https://blog.csdn.net/akof1314/article/details/5 ...

  3. .net chart(图表)控件的使用

    .Net chart control for .net framework 3.5 文章类型:原创文章 摘要: 这个案例指在介绍微软这套免费又功能强大的图表控件Microsoft Chart Cont ...

  4. c# chart图表控件总结

    一.什么是Micosoft.Chart.Controls  Micosoft.Chart.Controls是微软自带的一个图形可视化的组件,可以在Web程序和窗体程序中(Windowsform)中使用 ...

  5. 微软图表控件MsChart使用说明[转]

    微软图表控件MsChart使用说明 建立一个.NET3.5的Web项目,像使用普通控件一样拖放到要使用的Web界面即可.初步研究了一下,整个图形控件主要由以下几个部份组成: 1.Annotations ...

  6. Essential Chart for ASP.NET MVC商业图表控件相关介绍及下载

    Essential Chart for ASP.NET MVC是一款功能强大的商业图表控件,提供了创新的数据对象模型可以很容易地与多种数据源进行绑定,提供了35种图表类型,支持2D和3D显示,多轴显示 ...

  7. .NET Framework 3.5 SP1的图表控件——Chart (转)

    基于.NET Framework 3.5 SP1的图表控件--Chart,可 在WinForm和WebForm下使用!并同时提供了大量的示例 官方主页:http://code.msdn.microso ...

  8. java绘制图表控件_画图控件 Chart Control -Java架构师必看

    .NET3.5中中推出了图表控件,可以同时支持Web和WinForm两种方式,由于平时很少使用,一直网络 .NET3.5中中推出了图表控件,可以同时支持Web和WinForm两种方式,由于平时很少使用 ...

  9. 可用于wpf的图表控件:WPFTookit Chart

    当前项目需要一个图表控件,尝试用过mschart和livechart,都不是很满意.这里试一下WPFToolkit.DataVisualization. 引用dll,通过nuget包管理器下载WPFT ...

最新文章

  1. linux 重新分区挂载,Linux:挂载磁盘分区,linux已挂载磁盘重新分区
  2. Windows Azure Cloud Service (23) 使用Full IIS模式部署多站点和虚拟目录
  3. 【java】深入了解JAVA可变长度的参数
  4. 计算机学术论文shortessay,期末论文essay
  5. 使用nmake编译Gdal源代码(Win10, VS2022)
  6. 已将该虚拟机配置为使用 64 位客户机操作系统。但是,无法执行 64 位操作。.....LeoMoon CPU-V.exe虚拟机检测工具
  7. 西门子触摸屏函数翻译_触摸屏的中英文切换怎么做?
  8. 开源控件My97DatePicker的基本用法
  9. yuv图片旋转180度,镜像水平翻转
  10. 远程调试监视器 已在计算机上关闭,错误:“Microsoft Visual Studio 远程调试监视器”(MSVSMON.EXE) 似乎没有在远程计算机上运行。...
  11. 人民日报喊你学数学!实力不允许?8本书带你入门
  12. 无线网DNS服务器有错误,关于dns错误的原因和解决办法
  13. Python切图九宫格
  14. 【多模态】多模态特征融合策略——门控多模态融合方法
  15. 新海诚画集[秒速5センチメートル:樱花抄·學舍]
  16. (听说标题越短事越大)
  17. Simpson积分方法计算NURBS曲线弧长,详细原理+代码实现
  18. unity游戏开发之令人上瘾的6大手游设计’潜规则’
  19. 高手最爱的5大沟通技巧,管下属、谈客户都能用得上
  20. 灰度共生矩阵(GLCM)的概念及其相关特征

热门文章

  1. 辽宁教师计算机能力提升,辽宁省中学教师信息化教学能力的现状分析与提升策略研究...
  2. Python while语句2021-08-27
  3. 不花钱,模拟登录古诗中文网
  4. Android Studio 默认keystore 以及自定义keystore
  5. 大数据十年回顾(2):当代理论与Google云
  6. 90个常用词根,30个前缀30个后缀
  7. 数字化转型中基于交付价值数据治理的6大原则
  8. win7 计算机不显示收藏夹,在WIN7中隐藏“库”和“收藏夹”
  9. 25款经典老芯片,认识5款以上的工程师证明你老了!
  10. 如何快速搭建一个 “简单模式” 的微服务架构