C#使用Chart进行统计,切换不同的图表类型

这篇具有很好参考价值的文章主要介绍了C#使用Chart进行统计,切换不同的图表类型。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

WindowsForm应用程序中Chart图表控件所属的命名空间:

Chart

命名空间:

System.Windows.Forms.DataVisualization.Charting

对应的dll路径:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Windows.Forms.DataVisualization.dll

示例:我们使用chart来统计某种产品的OK和NG个数。

每隔5秒钟自动刷新OK和NG数量,可以切换使用柱状图、环形、线性图、饼图等显示

新建windows窗体应用程序:

ChartDemo,将默认的Form1,重命名为FormChart,

窗体设计器源程序如下:

文件:FormChart.Designer.cs


namespace ChartDemo
{
    partial class FormChart
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
            System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
            this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
            this.label1 = new System.Windows.Forms.Label();
            this.cboChartType = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.lblOKCount = new System.Windows.Forms.Label();
            this.lblNGCount = new System.Windows.Forms.Label();
            this.lblTotalCount = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
            this.SuspendLayout();
            // 
            // chart1
            // 
            chartArea1.Name = "ChartArea1";
            this.chart1.ChartAreas.Add(chartArea1);
            legend1.Name = "Legend1";
            this.chart1.Legends.Add(legend1);
            this.chart1.Location = new System.Drawing.Point(22, 68);
            this.chart1.Name = "chart1";
            series1.ChartArea = "ChartArea1";
            series1.Legend = "Legend1";
            series1.Name = "产量";
            this.chart1.Series.Add(series1);
            this.chart1.Size = new System.Drawing.Size(652, 559);
            this.chart1.TabIndex = 0;
            this.chart1.Text = "chart1";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("宋体", 16F);
            this.label1.Location = new System.Drawing.Point(19, 21);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(98, 22);
            this.label1.TabIndex = 1;
            this.label1.Text = "图表类型";
            // 
            // cboChartType
            // 
            this.cboChartType.Font = new System.Drawing.Font("宋体", 16F);
            this.cboChartType.FormattingEnabled = true;
            this.cboChartType.Location = new System.Drawing.Point(123, 18);
            this.cboChartType.Name = "cboChartType";
            this.cboChartType.Size = new System.Drawing.Size(214, 29);
            this.cboChartType.TabIndex = 2;
            this.cboChartType.SelectedIndexChanged += new System.EventHandler(this.cboChartType_SelectedIndexChanged);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("宋体", 16F);
            this.label2.Location = new System.Drawing.Point(93, 640);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(43, 22);
            this.label2.TabIndex = 3;
            this.label2.Text = "OK:";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Font = new System.Drawing.Font("宋体", 16F);
            this.label3.Location = new System.Drawing.Point(93, 670);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(43, 22);
            this.label3.TabIndex = 4;
            this.label3.Text = "NG:";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Font = new System.Drawing.Font("宋体", 16F);
            this.label4.Location = new System.Drawing.Point(60, 700);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(76, 22);
            this.label4.TabIndex = 5;
            this.label4.Text = "Total:";
            // 
            // lblOKCount
            // 
            this.lblOKCount.AutoSize = true;
            this.lblOKCount.Font = new System.Drawing.Font("宋体", 16F);
            this.lblOKCount.Location = new System.Drawing.Point(144, 640);
            this.lblOKCount.Name = "lblOKCount";
            this.lblOKCount.Size = new System.Drawing.Size(21, 22);
            this.lblOKCount.TabIndex = 6;
            this.lblOKCount.Text = "0";
            // 
            // lblNGCount
            // 
            this.lblNGCount.AutoSize = true;
            this.lblNGCount.Font = new System.Drawing.Font("宋体", 16F);
            this.lblNGCount.Location = new System.Drawing.Point(144, 670);
            this.lblNGCount.Name = "lblNGCount";
            this.lblNGCount.Size = new System.Drawing.Size(21, 22);
            this.lblNGCount.TabIndex = 7;
            this.lblNGCount.Text = "0";
            // 
            // lblTotalCount
            // 
            this.lblTotalCount.AutoSize = true;
            this.lblTotalCount.Font = new System.Drawing.Font("宋体", 16F);
            this.lblTotalCount.Location = new System.Drawing.Point(144, 700);
            this.lblTotalCount.Name = "lblTotalCount";
            this.lblTotalCount.Size = new System.Drawing.Size(21, 22);
            this.lblTotalCount.TabIndex = 8;
            this.lblTotalCount.Text = "0";
            // 
            // FormChart
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(862, 747);
            this.Controls.Add(this.lblTotalCount);
            this.Controls.Add(this.lblNGCount);
            this.Controls.Add(this.lblOKCount);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.cboChartType);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.chart1);
            this.Name = "FormChart";
            this.Text = "图表示例:切换图表类型";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FormChart_FormClosing);
            this.Load += new System.EventHandler(this.FormChart_Load);
            ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.DataVisualization.Charting.Chart chart1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ComboBox cboChartType;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label lblOKCount;
        private System.Windows.Forms.Label lblNGCount;
        private System.Windows.Forms.Label lblTotalCount;
    }
}

FormChart.代码如下:

文件:FormChart.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ChartDemo
{
    public partial class FormChart : Form
    {
        /// <summary>
        /// 是否运行线程
        /// </summary>
        public static bool isRun = false;
        public FormChart()
        {
            InitializeComponent();
            //添加不同的图表枚举
            cboChartType.Items.AddRange(new object[] {
            "Spline",
            "Pie",
            "Column",
            "Doughnut"});
            cboChartType.SelectedIndex = 3;
        }

        /// <summary>
        /// 切换图表类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cboChartType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboChartType.SelectedIndex == -1)
            {
                return;
            }
            System.Windows.Forms.DataVisualization.Charting.SeriesChartType chartType;
            Enum.TryParse(cboChartType.Text, true, out chartType);
            chart1.Series[0].ChartType = chartType;
        }

        private void FormChart_Load(object sender, EventArgs e)
        {
            isRun = true;
            Task.Factory.StartNew(() =>
            {
                while (isRun)
                {
                    Tuple<int, int, int> tuple = GetProductCount();
                    RefreshChart(tuple);
                    Thread.Sleep(5000);
                }
            });
        }

        /// <summary>
        /// 返回指定时间区间的OK数量、NG数量、总数量
        /// </summary>
        /// <returns></returns>
        private Tuple<int, int, int> GetProductCount()
        {
            int okCount = 0;
            int ngCount = 0;
            okCount = new Random().Next(1000, 10000);
            ngCount = new Random(Guid.NewGuid().GetHashCode()).Next(1000, 10000);
            return Tuple.Create(okCount, ngCount, okCount + ngCount);
        }

        /// <summary>
        /// 刷新图表
        /// </summary>
        /// <param name="tuple"></param>
        void RefreshChart(Tuple<int, int, int> tuple)
        {
            List<Tuple<string, int>> dataList = new List<Tuple<string, int>>
            {
                Tuple.Create("OK", tuple.Item1),
                Tuple.Create("NG", tuple.Item2),
            };
            this.BeginInvoke(new Action(() =>
            {
                try
                {
                    if (!this.IsHandleCreated)
                    {
                        return;
                    }
                    lblOKCount.Text = tuple.Item1.ToString();
                    lblNGCount.Text = tuple.Item2.ToString();
                    lblTotalCount.Text = tuple.Item3.ToString();
                    chart1.Series[0].Points.Clear();
                    for (int i = 0; i < dataList.Count; i++)
                    {
                        //chart的X轴为索引,Y轴为实际值
                        chart1.Series[0].Points.AddXY(dataList[i].Item1, dataList[i].Item2);
                    }
                    chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;//隐藏X网格线
                    chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;//隐藏Y网格线
                    chart1.Series[0].IsValueShownAsLabel = true; //显示Y轴的具体值
                    //传入相等数量的集合:甜甜圈
                    System.Windows.Forms.DataVisualization.Charting.SeriesChartType chartType;
                    Enum.TryParse(cboChartType.Text, true, out chartType);
                    chart1.Series[0].ChartType = chartType;
                    //chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Doughnut;
                    chart1.Series[0].Points[0].Color = Color.ForestGreen;//第一个柱状图OK显示绿色
                    chart1.Series[0].Points[1].Color = Color.Red;//第二个柱状图NG显示红色
                    chart1.Series[0].MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"RefreshChart Occur Exception:{ex.Message}");
                }
            }));
        }

        /// <summary>
        /// 窗体正在关闭事件,关闭线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormChart_FormClosing(object sender, FormClosingEventArgs e)
        {
            isRun = false;
        }
    }
}

运行如图:

①柱状图:

C#使用Chart进行统计,切换不同的图表类型,GDI,C#,.Net Core,c#,Chart,图表

②线性图:

C#使用Chart进行统计,切换不同的图表类型,GDI,C#,.Net Core,c#,Chart,图表

 ③环形图【甜甜圈】

C#使用Chart进行统计,切换不同的图表类型,GDI,C#,.Net Core,c#,Chart,图表

 文章来源地址https://www.toymoban.com/news/detail-580344.html

到了这里,关于C#使用Chart进行统计,切换不同的图表类型的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • C# | 使用Chart动态展示实时折线图数据

    实时折线图是展示数据变化趋势的有效方式,可以用于监控系统性能、物理实验、股票走势等多个领域。 在C#中,我们可以使用Chart控件来实现实时折线图的展示,其动态性和可交互性可以帮助用户更好地理解数据。 本文将介绍如何使用Chart控件展示实时折线图数据,希望能

    2023年04月08日
    浏览(29)
  • Baumer工业相机堡盟工业相如何使用BGAPISDK通过两种不同的方法进行图像回调函数的使用(C#)

    ​ Baumer工业相机堡盟相机是一种高性能、高质量的工业相机,可用于各种应用场景,如物体检测、计数和识别、运动分析和图像处理。 Baumer的万兆网相机拥有出色的图像处理性能,可以实时传输高分辨率图像。此外,该相机还具有快速数据传输、低功耗、易于集成以及高度

    2024年02月08日
    浏览(30)
  • 使用matplotlib绘制3D图表和统计地图

    目录 使用mplot3D绘制3D图表 mplot3d概述 绘制3D线框图(plot_wireframe()) 绘制3D曲面图(plot_surface()) 绘制3D条形图  绘制3D柱形图 np.meshgrid()函数的补充 使用animation制作动图 例子1  例子2 使用basemap绘制统计地图 实例:美国部分城镇人口分布(basemap) 实例1:三维空间的星星(3

    2024年02月04日
    浏览(34)
  • 【数字孪生百科】可视化图表知识科普——Pareto图(Pareto Chart)

    Pareto图 (Pareto Chart)又称 帕累托图 、 排列图 ,是一种特殊类型的 条形图 。图中标绘的值是按照事件发生的频率排序而成,显示由于各种原因引起的缺陷数量或不一致的排列顺序。Pareto图是根据 Vilfredo Pareto 命名的,他的原理是“二八原则”,即20%的原因造成80%的问题,如

    2024年02月12日
    浏览(36)
  • 使用Apache ECharts同时绘制多个统计图表

    目录 1、介绍 2、相关知识 3、代码 4、效果 🍃作者介绍:双非本科大三网络工程专业在读,阿里云专家博主,专注于Java领域学习,擅长web应用开发、数据结构和算法,初步涉猎Python人工智能开发和前端开发。 🦅主页:@逐梦苍穹 🍔所属专栏:前端 📕您的一键三连,是我创

    2024年02月21日
    浏览(29)
  • 浅谈C#中await运算符在不同类型应用中的不同行为

    C#中的await运算符,因其不会阻塞主线程,可以给用户更好的操作体验,所以广泛的应用于各种C#的应用中。 本文主要分析其在Console程序,Winform程序和WebApi程序中,其工作行为的异同点,来更好的使用该运算符。 await运算符的基本功能是挂起当前的async方法,并直到异步方法

    2024年02月16日
    浏览(27)
  • 合宙Air724UG LuatOS-Air LVGL API控件--图表 (Chart)

    图表 (Chart) 一幅图胜过一千个字,通过图表展示出的数据内容能让用户更快速有效的了解数据特征。 代码示例 – 创建图表 chart = lvgl.chart_create(lvgl.scr_act(), nil) lvgl.obj_set_size(chart, 200, 150) lvgl.obj_align(chart, nil, lvgl.ALIGN_CENTER, 0, 0) – 设置 Chart 的显示模式 (折线图) lvgl.chart_set_typ

    2024年02月10日
    浏览(33)
  • mysql查询一周内的数据(起始日期为星期一)、根据一周不同星期进行分组统计

    其中YEARWEEK ( time, 1) 获取的是时间的年以及是该年内的第几周,而其中的1则代表以星期一为一周的第一天。 不加1的情况就是,获取的数据是以星期日为一周的第一天的,与我们的需求不符。 直接在YEARWEEK ( now(), 1) 加上-1即是减一周,也就是上周。 数字1则代表星期一,2代表

    2024年02月13日
    浏览(38)
  • 使用 C# 在Word中插入图表

    Word中的图表功能将数据可视化地呈现在文档中。这为展示数据和进行数据分析提供了一种方便且易于使用的工具,使作者能够以直观的方式传达信息。要 通过C#代码来实现在Word中绘制图表 ,可以借助  Spire.Doc for .NET  控件,具体操作参考下文。 C# 在Word中插入柱状图 C# 在

    2024年02月08日
    浏览(27)
  • C#中chart控件

    图表的5大集合 例子 第一步:创建工程 放入chart控件 选择图标类型 选择绘制曲线的宽度和颜色。 显示数据标签 添加标题 调整标题字体:大小和颜色 对坐标轴进行说明 设置间隔 设置刻度 设置游标:具有自动拖拽和放大功能 设置网格线的样式 调整表格的位置 编写代码 结果

    2024年01月23日
    浏览(23)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包