c#使用System.Windows.Forms.DataVisualization.Charting.dll绘制图表实例

2023-05-16

首先下载System.Windows.Forms.DataVisualization.Charting.dll,然后引用到项目中

手动在代码中创建chart类型并将其添加到某个控件中(control.controls.add(chart)),然后参数初始化添加样式和数据就可以了。

以下是效果图和代码

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace test_chart
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitChart();

            for (int i = 0; i < 3; i++)
            {
                AddSeries(r.Next(1,100).ToString(), Color.Red);
            }
        }
        static int range = 0;
        Random r = new Random(6);

        private void AddSeries(string seriersName, Color serierscolor)
        {
            Series series = new Series(seriersName);
            //图表类型  设置为样条图曲线
            series.ChartType = SeriesChartType.Line;
            series.IsXValueIndexed = true;
            series.XValueType = ChartValueType.Time;
            series.MarkerStyle = MarkerStyle.Circle;
            series.MarkerColor = Color.Black;
            //设置点的大小
            series.MarkerSize = 5;
            //设置曲线的颜色
            series.Color = serierscolor;
            //设置曲线宽度
            series.BorderWidth = 2;
            series.CustomProperties = "PointWidth=2";
            series.IsValueShownAsLabel = true;
            chart.Series.Add(series);
        }


        private void CreateChart()
        {
            chart = new Chart();
            this.panel1.Controls.Add(chart);
            chart.Dock = DockStyle.Fill;
            chart.Visible = true;

            ChartArea chartArea = new ChartArea();
            //chartArea.Name = "FirstArea";

            chartArea.CursorX.IsUserEnabled = true;
            chartArea.CursorX.IsUserSelectionEnabled = true;
            chartArea.CursorX.SelectionColor = Color.SkyBlue;
            chartArea.CursorY.IsUserEnabled = true;
            chartArea.CursorY.AutoScroll = true;
            chartArea.CursorY.IsUserSelectionEnabled = true;
            chartArea.CursorY.SelectionColor = Color.SkyBlue;

            chartArea.CursorX.IntervalType = DateTimeIntervalType.Auto;
            chartArea.AxisX.ScaleView.Zoomable = false;
            chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;//启用X轴滚动条按钮

            chartArea.BackColor = Color.BlueViolet;                      //背景色
            chartArea.BackSecondaryColor = Color.White;                 //渐变背景色
            chartArea.BackGradientStyle = GradientStyle.TopBottom;      //渐变方式
            chartArea.BackHatchStyle = ChartHatchStyle.None;            //背景阴影
            chartArea.BorderDashStyle = ChartDashStyle.NotSet;          //边框线样式
            chartArea.BorderWidth = 1;                                  //边框宽度
            chartArea.BorderColor = Color.Black;

            //chartArea.AxisX.
            chartArea.AxisX.MajorGrid.Enabled = true;
            chartArea.AxisY.MajorGrid.Enabled = true;

            // Axis
            chartArea.AxisY.Title = @"Value";
            chartArea.AxisY.LineWidth = 2;
            chartArea.AxisY.LineColor = Color.Black;
            chartArea.AxisY.Enabled = AxisEnabled.True;

            chartArea.AxisX.Title = @"Time";
            chartArea.AxisX.IsLabelAutoFit = true;
            chartArea.AxisX.LabelAutoFitMinFontSize = 5;
            chartArea.AxisX.LabelStyle.Angle = -15;

            chartArea.AxisX.LabelStyle.IsEndLabelVisible = true;        //show the last label
            chartArea.AxisX.Interval = 10;
            chartArea.AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount;
            chartArea.AxisX.IntervalType = DateTimeIntervalType.NotSet;
            chartArea.AxisX.TextOrientation = TextOrientation.Auto;
            chartArea.AxisX.LineWidth = 2;
            chartArea.AxisX.LineColor = Color.Black;
            chartArea.AxisX.Enabled = AxisEnabled.True;
            chartArea.AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Months;
            chartArea.AxisX.Crossing = 0;

            chartArea.Position.Height = 85;
            chartArea.Position.Width = 85;
            chartArea.Position.X = 0;
            chartArea.Position.Y = 13;

            chart.ChartAreas.Add(chartArea);
            chart.BackGradientStyle = GradientStyle.TopBottom;
            //图表的边框颜色、
            chart.BorderlineColor = Color.FromArgb(26, 59, 105);
            //图表的边框线条样式
            chart.BorderlineDashStyle = ChartDashStyle.Solid;
            //图表边框线条的宽度
            chart.BorderlineWidth = 2;
            //图表边框的皮肤
            chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
        }
        bool flag = false;
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem.ToString() == "Zoom")
            {
                flag = true;
            }
            else if (comboBox1.SelectedItem.ToString() == "OverView" || comboBox1.SelectedItem.ToString() == "Follow")
            {
                comboBox1.Items.Remove("Zoom");
                flag = false;
            }
        }

        private void button_Stop_Click(object sender, EventArgs e)
        {
            switch (button_Stop.Text)
            {
                case "Stop":
                    {
                        button_Stop.Text = "Start";
                        t.Stop();
                        break;
                    }
                case "Start":
                    {
                        button_Stop.Text = "Stop";
                        t.Start();
                        break;
                    }
            }
        }

        private void InitChart()
        {
            CreateChart();
            chart.ChartAreas[0].AxisX.ScaleView.Scroll(ScrollType.Last);
            t.Interval = 100;
            t.Start();
        }

        int sum = 0;
        private void t_Tick(object sender, EventArgs e)
        {
            sum++;
            Random ra = new Random();
            DateTime nowTime = DateTime.Now;
            if (chart.Series.Count <= 0)
            {
                return;
            }
            for (int i = 0; i < chart.Series.Count; i++)
            {
                
                Series series = chart.Series[i];
                string value = ra.Next(1, 10).ToString();
                series.Points.AddXY(nowTime, value);
                if (comboBox1.SelectedItem.ToString() == "OverView")
                {
                    chart.ChartAreas[0].AxisX.ScaleView.Position = 1;
                    if (sum > 10)
                    {
                        double max = chart.ChartAreas[0].AxisX.Maximum;
                        max = (sum / 10 + 1) * 10;
                        chart.ChartAreas[0].AxisX.Interval = max / 10;
                    }
                    chart.ChartAreas[0].AxisX.ScaleView.Size = sum * 1.1;
                }
                if (comboBox1.SelectedItem.ToString() == "Follow")
                {
                    chart.ChartAreas[0].AxisX.Interval = 1D;
                    chart.ChartAreas[0].AxisX.ScaleView.Size = 10D;
                    if (sum <= chart.ChartAreas[0].AxisX.ScaleView.Size)
                        chart.ChartAreas[0].AxisX.ScaleView.Position = 1;
                    else
                        chart.ChartAreas[0].AxisX.ScaleView.Position = sum - chart.ChartAreas[0].AxisX.ScaleView.Size;
                }
                
            }
        }
    }
}
 

//设计器

namespace test_chart
{
    partial class Form1
    {
        /// <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()
        {
            this.components = new System.ComponentModel.Container();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.button_Stop = new System.Windows.Forms.Button();
            this.t = new System.Windows.Forms.Timer(this.components);
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // comboBox1
            // 
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Items.AddRange(new object[] {
            "Follow",
            "OverView"});
            this.comboBox1.Location = new System.Drawing.Point(840, 429);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(121, 20);
            this.comboBox1.TabIndex = 1;
            this.comboBox1.Text = "Follow";
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
            // 
            // button_Stop
            // 
            this.button_Stop.Location = new System.Drawing.Point(12, 429);
            this.button_Stop.Name = "button_Stop";
            this.button_Stop.Size = new System.Drawing.Size(75, 23);
            this.button_Stop.TabIndex = 2;
            this.button_Stop.Text = "Stop";
            this.button_Stop.UseVisualStyleBackColor = true;
            this.button_Stop.Click += new System.EventHandler(this.button_Stop_Click);
            // 
            // t
            // 
            this.t.Tick += new System.EventHandler(this.t_Tick);
            // 
            // panel1
            // 
            this.panel1.Location = new System.Drawing.Point(12, 13);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(906, 410);
            this.panel1.TabIndex = 3;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(973, 461);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.button_Stop);
            this.Controls.Add(this.comboBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataVisualization.Charting.Chart chart;
        private System.Windows.Forms.ComboBox comboBox1;
        private System.Windows.Forms.Button button_Stop;
        private System.Windows.Forms.Timer t;
        private System.Windows.Forms.Panel panel1;
    }
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

c#使用System.Windows.Forms.DataVisualization.Charting.dll绘制图表实例 的相关文章

  • HTTP 协议报文解析

    本篇主要是为了记录HTTP中报文的格式 xff0c 以便针对报文进行解析 首先会介绍基础的HTTP报文 xff0c 之后会介绍 文件上传时的数据报文格式 HTTP基础报文格式 按照HTTP报文类型进行介绍 xff0c HTTP报文类型分为请
  • C++实现通过UDP传输文件

    一 程序说明 1 本程序通过 UDP 来传输文件及其管理元数据 xff08 文件名 大小和日期等 xff09 xff0c 包括client cpp和server cpp xff0c 分别是客户端程序和服务端程序 2 文件以二进制形式传输 3
  • C/C++ ini配置文件的格式及如何读写ini配置文件

    一 ini配置文件的格式 为什么要用INI文件 xff1f 如果我们程序没有任何配置文件时 xff0c 这样的程序对外是全封闭的 xff0c 一旦程序需要修改一些参数必须要修改程序代码本身并重新编译 xff0c 这样很不好 xff0c 所以
  • C# Aspose.Words 插入纵页、横页、图片

    引用Aspose Words dll 对word文档进行操作 1 插入纵页或者横页 Document doc 61 new Document tempFile DocumentBuilder builder 61 new DocumentB
  • C#操作Word Aspose.Words组件介绍及使用 基本介绍与DOM概述

    1 基本介绍 Aspose Words是一个商业 NET类库 xff0c 可以使得应用程序处理大量的文件任务 Aspose Words支持Doc xff0c Docx xff0c RTF xff0c HTML xff0c OpenDocum
  • C# Aspose.Word 操作word文档(利用模板)

    上一篇我们介绍了用书签的方式来填充word中数据 xff0c 今天介绍第二种方法操作word xff01 依旧是先自己建好word模板 xff0c 然后这里就不需要插入书签了 1 建立模板 能看到红色标示的区域依旧用到了书签 xff0c 没
  • C#使用Aspose.Words操作word文档(利用模板2)

    最近接到个需求 xff0c 由于客服这边要导出大量有一定规则的word文件 xff0c 里面的内容希望系统自动填充 xff0c 例如 这里我使用Aspose Words dll这个类库 xff0c 1 首先 xff0c 我们需要创建模板文件
  • aspose 生成word 简单的文档操作

    1 使用Aspose Words 插件 这个插件的好处是 xff0c 发布网站的服务不需要安装office xff0c 也可以进行数据生成word文档 在生成word问当前需要我们先做好一个word模板 xff0c 需要在 xff1a 后边
  • Aspose.Word 的常见使用(不用模板创建)

    起因 因项目需要 xff0c 而且使用html转Word的时候 xff0c 样式不兼容问题 xff0c 于是只能使用Aspose Word通过代码生成 下面是通过DocumentBuilder来设计Word的 xff0c 但是和使用模型拼接
  • FileAlterationListenerAdaptor监听文件和文件夹

    背景 项目中有需要监听文件 文件夹的需求 xff0c 以便在文件 文件夹发生变化时出发相应的业务流程 这里使用Spring Boot 43 Apache Commons IO方案 另外 xff0c Apache Commons IO涉及到多
  • C# 设置word文档页面大小

    我们知道 xff0c 在MS word中 xff0c 默认的页面大小是letter 8 5 x11 xff0c 除此之外 xff0c word还提供了其他一些预定义的页面大小 xff0c 如Legal 5 4 x14 xff0c A3 11
  • C# 设置Word文档中图片的大小

    在创建Word文档时 xff0c 我们经常需要向文档中插入图片 xff0c 但插入图片的大小有时候可能会太大或太小 xff0c 这时候我们就需要对图片的大小进行调整 xff0c 使得图片与文章更加协调 美观 这篇文章将介绍如何使用Free
  • C#无损高质量压缩图片实现代码

    最近 xff0c 项目上涉及到了图像压缩 xff0c 发现原有的图像压缩功能 xff0c 虽然保证了图像的大小300K以内 xff0c 但是压缩后的图像看的不在清晰 xff0c 并且 xff0c 限定了图片的Height或者是Width 在
  • c# Bitmap byte[] Stream 文件相互转换

    byte 转图片 public static Bitmap BytesToBitmap byte Bytes MemoryStream stream 61 null try stream 61 new MemoryStream Bytes
  • bitmap与memoryStream转换bug

    image Save PicMainMs System Drawing Imaging ImageFormat Png Bitmap RawFormat获取格式会有异常情况encode为空的bug xff0c 所以设为System Draw
  • C#类的属性遍历及属性值获取

    1 定义一个类 public class Person public string Name get set public int ID get set 2 获取属性 方法一 定义一个类的对象获取 Person p 61 new Perso
  • c# 遍历对象属性给对象赋值

    using System using System Collections Generic using System Linq using System Web using System Web UI using System Web UI
  • C#中5步完成word文档打印的方法

    在日常工作中 xff0c 我们可能常常需要打印各种文件资料 xff0c 比如word文档 对于编程员 xff0c 应用程序中文档的打印是一项非常重要的功能 xff0c 也一直是一个非常复杂的工作 特别是提到Web打印 xff0c 这的确会很
  • 保存文件对话框实例

    string savePath 61 34 34 SaveFileDialog sfd 61 new SaveFileDialog sfd Filter 61 34 txt files docx docx All files 34 sfd
  • 项目移植,项目环境问题

    1 调用windows组件时遇到的问题 提示Interop Microsoft Office Core等找不到接口 xff0c 可在引用处将其dll的嵌入式互操作属性改为false 2 配置框架问题 确认 netframework版本是否合

随机推荐

  • javabean拷贝,list拷贝,工具类

    可以实现单个对象拷贝 xff0c List拷贝 xff0c 源和目标类属性字段不一致时也可以拷贝 回调类 xff1a 用于处理在拷贝过程中源和目标类字段不一致的情况 lt p gt Title BeanCopierUtilCallBack
  • c++:json字符串拼接,json对象组装

    c 43 43 算法使用json输出最终结果给java使用 xff0c 于是 xff0c 开始了json对象的组装之旅 首先 xff0c 对不同数据类型 xff0c 封装不同的数据组装函数 拼接int std string getKeyVa
  • C/C++由字符串转JSON/JSON转字符串/数组解析/数组添加

    字符串转成JSON xff08 其中str为字符串 xff09 1 2 3 4 5 Json Reader Reader Json Value DevJson Reader parse str DevJson int dev id 61 D
  • c#解析json字符串处理清晰易懂的方法

    JSON文件读取到内存中就是字符串 xff0c NET操作JSON就是生成与解析JSON字符串 操作JSON通常有以下几种方式 xff1a 1 原始方式 xff1a 按照JSON字符串自己来解析 2 通用方式 xff1a 这种方式是使用开源
  • c# 拼接Json串的几种方法

    C 定义多行字符串的方式 在定义的前面加上 64 符号 xff1a 1 string aa 61 64 34 asdfsdfsd 2 fsdsfsdfsdfsdfsdfsdfs 3 safasfsadfsdfasfsfsdfsd 34 在C
  • std::string::append函数使用方法

    string amp append const string amp str string amp append const string amp str size t subpos size t sublen string amp app
  • 2020.3

    1 项目中引用的dll如果为感叹号 xff0c 编译不能正常进行 xff0c 则将其dll下载放到exe运行文件夹下 xff0c 如还有其他错误提示 xff0c 试试将该dll的嵌入式互操作改为false
  • c#图像灰度化、灰度反转、二值化

    图像灰度化 xff1a 将彩色图像转化成为灰度图像的过程成为图像的灰度化处理 彩色图像中的每个像素的颜色有R G B三个分量决定 xff0c 而每个分量有255中值可取 xff0c 这样一个像素点可以有1600多万 xff08 255 25
  • opencv中查看mat位图的像素幅度(Cv::matStep)

    实例 其中step里的 xff0c 其中数据指针首地址是p 61 0x000000000028d7b0 xff0c 1280是每行数据所占的字节数 xff0c 1是每个元素的字节数 Mat的作用 The class Mat represen
  • vs中c#的项目配置,平台配置

    1 右键项目属性 gt 配置属性 gt 配置 2 一般在此界面的右上角里的配置管理器中配置才有效 3 活动解决方案配置是项目生成的exe路径 xff0c 活动解决方案平台是每个项目对应的cpu架构 4 在主项目中把cpu平台改成啥 xff0
  • vs中c++项目的配置

    首先 xff0c 我们一般不会修改解决方案的属性 xff0c 而是设置每个项目各自的属性 接着上一篇文章 xff0c 我们来看看我们应该怎样来设置各项目的项目属性更好 xff1a 我们以NYOJ 001项目的Debug版的设置为例 xff1
  • 正则表达式校验日期时间格式

    目录 日期部分校验 概念 校验yyyyMMdd 校验yyyy xff0d MM xff0d dd 时间部分校验 校验HHmmss 校验HH mm ss 校验日期 43 时间部分 校验yyyyMMddHHmmss 校验 yyyy MM dd
  • c#利用宏定义调试代码

    define 使用 define 来定义符号 将符号用作传递给 if 指令的表达式时 xff0c 该表达式的计算结果为 true xff0c 如以下示例所示 xff1a define DEBUG 说明 define 指令不能用于声明常量值
  • C++项目库包含,dll引用问题,直接把缺失的dll或库放置可执行文件里

    在c 43 43 的项目中如果设置了库包含于库引用 xff0c 设置了引用或包含的路径 xff0c 如果单拿出debug文件运行里面的exe xff0c 提示报错缺少引用的文件 xff0c 可直接将缺少的文件等放置debug目录下即可运行
  • c#中invoke和beginvoke的区别

    thread th 61 new thread 61 gt control invoke new Action 61 gt thread sleep 5000 dosometings th start 这个线程里的dosometings将在
  • vs项目中在生成中设置exe的输出路径,可定为不同版本

    vs项目中在生成中设置exe的输出路径 xff0c 可定为不同版本 xff0c 便于调试发布
  • c# 深拷贝各种实现方式

    1 xff1a 利用反射实现 public T DeepCopy lt T gt T obj object retval 61 Activator CreateInstance typeof T PropertyInfo pis 61 ty
  • C#序列化与反序列化以及深拷贝浅拷贝方法

    基于二进制数据流的序列化和反序列化 lt summary gt 序列化 lt summary gt lt typeparam name 61 34 T 34 gt lt typeparam gt lt param name 61 34 ob
  • c#中chart绘制曲线,柱状图等

    通常我们需要在界面中绘制波形图 xff0c 柱状图 xff0c 折线图等等 此时用到Chart控件是非常方便的 先以图一条样条曲线 xff08 Spline xff09 为例 xff08 1 xff09 在Visual Studio中新建一
  • c#使用System.Windows.Forms.DataVisualization.Charting.dll绘制图表实例

    首先下载System Windows Forms DataVisualization Charting dll xff0c 然后引用到项目中 手动在代码中创建chart类型并将其添加到某个控件中 xff08 control controls