c#根据excel数据绘制坐标图

效果如下图

在这里插入图片描述

界面

在这里插入图片描述

代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        //x和y轴数据
        double[] x = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        double[] y = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        List<Double> xList = new List<Double>();
        List<Double> yList = new List<Double>();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string fname = "";
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Excel File Dialog";
            fdlg.InitialDirectory = @"c:\";
            fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                fname = fdlg.FileName;
            }


            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fname);
            Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            for (int i = 1; i <= rowCount; i++)
            {
                double px = System.Convert.ToDouble(xlRange.Cells[i, 1].Value2.ToString());
                double py = System.Convert.ToDouble(xlRange.Cells[i, 2].Value2.ToString());
                Console.Out.WriteLine("第" + i + "行 :" + px + "," + py);
                xList.Add(px);
                yList.Add(py);
                //for (int j = 1; j <= colCount; j++)
                //{
                //write the value to the Grid  
                //if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
                //{
                //xList.Add(xlRange.Cells[i, j]);

                // Console.WriteLine(xlRange.Cells[i, j].Value2.ToString());
                //add useful things here! 
                // }
                //}
            }
            chart1.Series[0].Points.DataBindXY(xList, yList);


            //cleanup  
            GC.Collect();
            GC.WaitForPendingFinalizers();

            //rule of thumb for releasing com objects:  
            //  never use two dots, all COM objects must be referenced and released individually  
            //  ex: [somthing].[something].[something] is bad  

            //release com objects to fully kill excel process from running in the background  
            Marshal.ReleaseComObject(xlRange);
            Marshal.ReleaseComObject(xlWorksheet);

            //close and release  
            xlWorkbook.Close();
            Marshal.ReleaseComObject(xlWorkbook);

            //quit and release  
            xlApp.Quit();
            Marshal.ReleaseComObject(xlApp);


        }
        //Graphics g = this.CreateGraphics();
        //Pen pen = new Pen(Brushes.Red, 1);
        //g.DrawLine(pen, new Point(30, 50), new Point(250, 250));

        private void Form1_Load(object sender, EventArgs e)
        {
            //控件chart背景色
            //chart1.BackColor = Color.Transparent;//Color.Transparent系统定义的颜色
            //chart1.BackColor = Color.White;
            //图表标题,
            chart1.Titles.Add("测试数据"); //添加title到titleCollection集合的末尾
            chart1.Titles[0].ForeColor = Color.DarkBlue;//设置title的文本颜色
            chart1.Titles[0].Font = new Font("微软雅黑", 15f, FontStyle.Regular);//设置title的字体
            chart1.Titles[0].Alignment = ContentAlignment.TopCenter;//设置title的对齐方式

            //图表区chartAreas
            chart1.ChartAreas[0].BackColor = Color.White;//chartAreas背景颜色
            chart1.ChartAreas[0].BorderColor = Color.Red;//chartAreas边框颜色
            chart1.ChartAreas[0].BackGradientStyle = GradientStyle.None;//chartAreas背景渐变,不使用


            //AxisX表示图表的主x轴; 
            chart1.ChartAreas[0].AxisX.LineColor = Color.Red; //线条颜色
            chart1.ChartAreas[0].AxisX.Interval = 0.5;//设置x轴的间隔
            chart1.ChartAreas[0].AxisX.Minimum = 0;
            chart1.ChartAreas[0].AxisX.Maximum = 25;//Y轴坐标固定,不会随绑定的数据而变

            chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1;//设置X轴标签间距,如果不设置默认为x轴的间隔
            chart1.ChartAreas[0].AxisX.IsLabelAutoFit = false;
            chart1.ChartAreas[0].AxisX.LabelStyle.Font = new Font("微软雅黑", 13f, FontStyle.Regular); //标签字体

            //设置x轴标题的字体样式和颜色
            chart1.ChartAreas[0].AxisX.Title = "圆周位置,mm";
            chart1.ChartAreas[0].AxisX.TitleFont = new Font("微软雅黑", 15f, FontStyle.Regular);// 标题字体
            chart1.ChartAreas[0].AxisX.TitleForeColor = Color.Blue; //轴标题颜色
            chart1.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Horizontal;//轴标题文本方向
            chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Far;//轴标题对齐方式
            //X轴网格线
            chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false; //启用网格刻度线,一排竖线
            //chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d"); //线条颜色
            //chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Yellow;

            //y轴
            chart1.ChartAreas[0].AxisY.LineColor = Color.Red; //线条颜色
            chart1.ChartAreas[0].AxisY.Interval = 0.05;//设置Y轴的间隔
            chart1.ChartAreas[0].AxisY.Minimum = 5;//Y轴坐标固定,不会随绑定的数据而变
            chart1.ChartAreas[0].AxisY.Maximum = 6.35;//Y轴坐标固定,不会随绑定的数据而变
            chart1.ChartAreas[0].AxisY.LabelStyle.Interval = 0.05;//设置X轴标签间距,如果不设置默认为x轴的间隔
            //Y坐标轴标题
            chart1.ChartAreas[0].AxisY.Title = "圆周半径,mm"; //轴标题
            chart1.ChartAreas[0].AxisY.TitleFont = new Font("微软雅黑", 15f, FontStyle.Regular); //标题字体
            chart1.ChartAreas[0].AxisY.TitleForeColor = Color.Blue; //轴标题颜色
            chart1.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated270; //标题文本方向
            chart1.ChartAreas[0].AxisY.TitleAlignment = StringAlignment.Far;
            //y轴标签样式
            chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.Black; //标签颜色
            chart1.ChartAreas[0].AxisY.LabelStyle.Font = new Font("微软雅黑", 13f, FontStyle.Regular); //标签字体
            //Y轴网格线条
            chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;//一排横线
            //chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Yellow;

            //#VAL为y轴的值,#VALX为x轴数据
            //chart1.Series[0].Label = "hello";//数据点标签文本  
            //chart1.Series[0].Label = "#VAL";//数据点标签为其对于的y值
            //chart1.Series[0].LabelBackColor = Color.Blue; //数据点标签背景色
            //chart1.Series[0].LabelForeColor = Color.White; //数据点标签颜色
            //chart1.Series[0].Color = Color.Red; //数据点颜色,数据点之间曲线的颜色
            //chart1.Series[0].BorderWidth = 3;//数据点边框宽度,曲线的宽度
            //chart1.Series[0].ToolTip = "#VALX:#VAL";//鼠标移动到对应点显示数值 元素的工具提示
            chart1.Series[0].ChartType = SeriesChartType.Spline; //图表类型(折线) 绘制该序列的图表类型

            Legend legend = new Legend("波形显示");//初始化具有指定的图例名称
            legend.Title = "legendTitle"; //图例标题文本
            chart1.Series[0].LegendText = legend.Name; //图例中项的文本
            chart1.Legends.Add(legend);
            chart1.Legends[0].Position.Auto = false; //图例矩形位置 - 元素自动定位标志

            //绑定数据
            //数据绑定到指定数据源的第一列的x值和y值的集合的数据点
            chart1.Series[0].Color = Color.Black;

            chart1.Series[0].Points.DataBindXY(x, y);
        }
    }
}

下载

已上传

  • 3
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
第2章 形基础 34 2.1 笔和画刷 34 2.1.1 pen 类 34 2.1.2 brush 类 35 2.2 基本形形状 37 2.2.1 点 37 2.2.2 直线和曲线 37 2.2.3 矩形、椭圆形和圆弧形 40 2.2.4 多边形 42 2.3 颜色 44 2.4 双倍缓存 66 第3章 坐标系统和颜色变换 69 3.1 坐标系统 69 3.2 颜色变换 77 第二部分 二维形的基本算法 第4章 二维矩阵和变换 82 4.1 矩阵基础和变换 82 4.2 齐次坐标 82 4.2.1 齐次坐标中的缩放 83 4.2.2 齐次坐标中的平移 83 4.2.3 齐次坐标中的旋转 84 4.2.4 变换组合 85 4.2.5 c#中矩阵的定义 86 4 .2.6 c#中的矩阵操作 87 4.2.7 c#中基本的矩阵变换 89 4.3 c#形对象的变换 93 基本变换 93 4.4 c#中的多对象变换 101 4.5 文字变换 105 第5章 二维线形形 109 5.1 序列化和反序列化及二维形的基本框架 109 5.1.1 c#序列化和反序列化 110 5.1.2 二维形的基本框架 113 5.2 二维形 248 5.2.1 简单实例 248 5.2.2 例 278 5.2.3 符号 289 5.2.4 对数比例 302 5.2.5 形的修饰 308 5.3 阶梯状 316 5.4 多y轴 318 第6章 特殊二维形 327 6.1 创建柱状 327 6.1.1 水平柱状 327 6.1.2 垂直柱状 343 6.1.3 形充填柱状 344 6.1.4 重叠柱状 346 6.2 饼状 348 6.3 误差 361 6.4 股票 367 6.4.1 最高最低收盘价股票 368 6.4.2 最高最低开盘收盘价股票 369 6.4.3 最高最低价股票 377 6.4.4 k 线(阴阳烛) 380 6.5 面积 389 6.6 综合 390 第三部分 三维形的相关知识及三维形的实现 第7章 三维矩阵和变换 396 7.1 三维数学概念 396 7.1.1 操作三维对象 396 7.1.2 数学结构 397 7.2 三维中的基本矩阵和变换 402 7.2.1 c#中三维点和矩阵的操作 403 7.2.2 三维的基本变换 405 7.3 方位角和仰角 434 7.4 三维形中的特殊坐标系统 439 7.4.1 球坐标系统 440 7.4.2 圆柱坐标系统 443 7.5 特殊坐标中的实际应用 447 7.5.1 球坐标示例 447 7.5.2 双缓存 463 第8章 三维形 473 8.1 三维形基础 473 8.1.1 point3和matrix3类 473 8.1.2 chartstyle类 476 8.1.3 坐标轴 496 8.1.4 网格线 496 8.1.5 标签 497 8.2 三维折线 503 8.3 三维形函数包 508 8.3.1 chartstyle2d类 509 8.3.2 point4类 515 8.3.3 dataseries类 516 8.3.4 chartfunctions类 521 8.3.5 drawchart类 526 8.4 曲面的实现 541 8.4.1 网格 541 8.4.2 幕布网格 548 8.4.3 瀑布网格 551 8.4.4 曲面 553 8.5 x-y平面色彩 559 8.6 轮廓 564 8.6.1 轮廓的算法 564 8.6.2 轮廓的实现 564 8.7 组合 569 8.7.1 三维体系中的x-y色彩 570 8.7.2 三维体系中的轮廓 571 8.7.3 网格-轮廓组合 575 8.7.4 曲面-轮廓组合 576 8.7.5 填充曲面-轮廓组合 576 8.8 三维柱状 577 实现柱状 577 8.9 切片 591 切片的实现 591 第四部分 c#中应用微软office的excel实现各种二维及三维形 第9章 应用程序中的excel表 600 9.1 excelc#间的互操作 600 9.2 c#应用程序中的excel表示例 602 9.2.1 excel表对象模型 602 9.2.2 创建独立的excel表 604 9.2.3 创建嵌入式excel表 607 9.3 更多的excel表 608 9.3.1 柱状 608 9.3.2 饼状 611 9.3.3 面积 613 9.3.4 圆环 615 9.3.5 雷达 615 9.3.6 股价 617 9.3.7 曲面 619 9.3.8 颜色映射 622 9.4 整合excel表到windows forms应用程序中 627 9.4.1 windows窗体上的独立excel表 627 9.4.2 windows窗体上的嵌入式excel表 631 第五部分 实现文件的相关知识 第10章 文件的读/写 634 10.1 c#文件读/写常用类 634 10.1.1 system.io.file类和system.io.fileinfo类 634 10.1.2 system.io.directory类和system.directoryinfo类 637 10.2 c#基于流的输入/输出 639 流的继承结构 640 10.3 文件读/写操作涉及的类 643 10.4 一些常见的问题及其解决 方案 643

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值