C#根据excel数据绘制坐标图的方法

本文实例为大家分享了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);
        }
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • C#使用Gembox.SpreadSheet向Excel写入数据及图表的实例

    开发工具:VS2017 语言:C# DotNet版本:.Net FrameWork 4.0及以上 使用的DLL工具名称:GemBox.Spreadsheet.dll (版本:37.3.30.1185) 一.GemBox.Spreadsheet工具: 该DLL是由GemBox公司开发的基于Excel功能的开发工具,该DLL很轻量,且使用起来很方便,在这里推荐下来来使用. 下载地址: http://xiazai.jb51.net/201712/yuanma/GemBox_Spreadsheet.zi

  • C#中如何在Excel工作表创建混合型图表实例

    在进行图表分析的时候,我们可能需要在一张图表呈现两个或多个样式的图表,以便更加清晰.直观地查看不同的数据大小和变化趋势.在这篇文章中,我将分享C#中如何在一张图表中创建不同的图表类型,其中包括如何在同一个图表添加第二个轴. 下面是一个简单的excel工作表,可以看到系列3数据不同于系列1和2,这样我们就可以绘制不同的图表类型和不同的坐标轴来表示变化的数据: 代码片段: 步骤1:新建一个Workbook类的对象并加载要创建图表的excel文件. Workbook workbook = new Wo

  • C# 添加、修改以及删除Excel迷你图表的实现方法

    Excel表格中的迷你图表能够直观地向我们展示出数据的变化趋势.本文将介绍C#如何实现为表格数据生成迷你图表,以及修改和删除迷你图表的方法.下面将详细讲述. 所用组件工具:Spire.XLS for .NET 原Excel图表: 一.添加迷你图表(折线图.柱形图.盈亏图) 1.添加命名空间 using System; using Spire.Xls; using System.Drawing; 2.主要代码 //创建一个Workbook类对象并加载Excel文档 Workbook workboo

  • c# 应用NPOI获取Excel中的图片,保存至本地的算法

    要求:读取excel中的图片,保存到指定路径 思路:  利用NPOI中 GetAllPictures()方法获取图片信息 步骤: 1.新建一个Windows窗体应用程序 2.桌面新建一个excel,贴入两张图片 如下图: 3.在Form中拖入一个button 4.点击button,在点击事件方法中写入,要读取图片的方法:ExcelToImage 点击事件方法如下: private string exclePath = @"C:\users\lenovo\Desktop\testPic.xls&q

  • C#插入图片到Excel表格单元格代码详解

    dll文件获取及引用: 方法1:通过官网下载dll文件包,并解压.解压文件后,将bin文件夹下的Spire.Xls.dll文件引用到C#程序. 方法2:通过Nuget网站获取dll. C#代码示例 using Spire.Xls; using System.Drawing; namespace InsertImage_XLS { class Program { static void Main(string[] args) { //创建Workbook对象 Workbook workbook =

  • C#删除Excel中的图片实例代码

    dll文件获取及引用: 方法1:通过官网下载dll文件包,并解压.解压文件后,将bin文件夹下的Spire.Xls.dll文件引用到C#程序. 方法2:通过Nuget网站获取dll. using Spire.Xls; namespace RemoveImg { class Program { static void Main(string[] args) { //加载Excel文档 Workbook workbook = new Workbook(); workbook.LoadFromFile

  • C# 填充Excel图表、图例背景色的实例代码

    填充背景色,一般可以选择多种不同样式来填充背景,包括填充为纯色背景.渐变背景.图片背景或者纹理背景等.下面的内容将分别介绍通过C#来设置Excel中图表背景色.以及图表中的图例背景色的方法. 使用工具:Spire.XLS for .NET dll引用:下载安装后,注意在程序中添加引用Spire.Xls.dll(dll文件在安装路径下的bin文件夹中获取) [示例1]填充图表背景色 测试文档如下: Step1:加载文档 //实例化Workbook类的对象 Workbook workbook = n

  • C# 创建Excel气泡图的实例代码

    气泡图(Bubble Chart)是可用于展示三个变量之间的关系.通过绘制x 值, y 值和大小值即可确定图表中气泡的坐标及大小.下面通过后端C#代码及VB.NET代码展示如何来实现在Excel中创建气泡图的方法. 程序环境: Visual Studio .Net FrameWork 4.5.1 Spire.XLS for .NET Version 10.12.0 注:编辑代码前先将Spire.Xls.dll(dll文件可在解压包Bin文件夹下获取)添加引用至VS程序,可通过官网下载或者Nuge

  • C# 创建EXCEL图表并保存为图片的实例

    数据表格能够清晰的呈现数据信息,但是我们对于一些繁杂多变的数据想要很直观的看到数据变化走势或者数据的占比时,数据图表会更具代表性,并且在呈现数据信息上也更形象,也能获取更多纯数字信息所不能直接展现的信息.在下面的代码中,将向您展示如何通过使用免费的Free Spire XLS for .NET组件来实现. 原数据表格: C# using Spire.Xls; using System.Drawing; using System.Drawing.Imaging; namespace CreateC

  • C#根据excel数据绘制坐标图的方法

    本文实例为大家分享了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.Data

  • php使用Jpgraph绘制简单X-Y坐标图的方法

    本文实例讲述了php使用Jpgraph绘制简单X-Y坐标图的方法.分享给大家供大家参考.具体实现方法如下: <?php include ("src/jpgraph.php"); include ("src/jpgraph_line.php"); //将要用于图表创建的数据存放在数组中 $data = array(19,23,34,38,45,67,71,78,85,87,90,96); $graph = new Graph(400,300); //创建新的Gr

  • php使用Jpgraph绘制复杂X-Y坐标图的方法

    本文实例讲述了php使用Jpgraph绘制复杂X-Y坐标图的方法.分享给大家供大家参考.具体实现方法如下: <?php include ("src/jpgraph.php"); include ("src/jpgraph_line.php"); $data1 = array(19,23,34,38,45,67,71,78,85,87,90,96); //第一条曲线的数组 $data2 = array(523,634,371,278,685,587,490,25

  • python读取excel数据绘制简单曲线图的完整步骤记录

    python读写excel文件有很多种方法: 用xlrd和xlwt进行excel读写 用openpyxl进行excel读写 用pandas进行excel读写 本文使用xlrd读取excel文件(xls,sxls格式),使用xlwt向excel写入数据 一.xlrd和xlwt的安装 安装很简单,windos+r调出运行窗口,输入cmd,进入命令行窗口,输入以下命令. 安装xlrd: pip install xlrd 安装xlwt: pip install xlwt xlrd的API(applica

  • asp.net中EXCEL数据导入到数据库的方法

    本文实例讲述了asp.net中EXCEL数据导入到数据库的方法.分享给大家供大家参考.具体分析如下: excel是办公中非常常用的一个办公表格了,但我们在开发中通常会需要直接把excel数据快速导入到数据库中了,这里整理了一个asp.net中EXCEL数据导入到数据库的例子供各位参考学习. 注意:EXCEL中的第一行不能导入. 下面是源码:IntoExcel.aspx: 复制代码 代码如下: <%@ Page  AutoEventWireup="true" CodeFile=&q

  • python库matplotlib绘制坐标图

    很多时候我们数据处理的时候要画坐标图,下面我用第三方库matplotlib以及scipy绘制光滑的曲线图 需要安装的库有 matplotlib,scipy, numpy import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.axisartist.axislines import Subplot from scipy import interpolate def sommth_plot(x_arr, y_arr):

  • vue 封装导出Excel数据的公共函数的方法

    vue+element UI 封装一个导出Excel数据的公共函数 将公共方法封装在store的modules的common.js中,如下图: 代码如下: const download = { actions: { downloadData({ commit, state }, data) { return new Promise((resolve, reject) => { data.event(data.formData).then(res => { const blob = new Bl

  • R语言-绘制双坐标图直方图与折线的结合方式

    看代码吧~ par(mar = c(5, 5, 3, 4)+0.1) #似乎是设置图片位置 bar<-barplot(gu[1:22,6],xlim=c(0.5,26),ylim=c(0,200000),ylab="交易量", col="blue",col.axis="blue",col.lab="blue") mtext(c(1:22),side=1,line=1,at=bar,cex=0.8,col="bl

  • Python读取excel文件中的数据,绘制折线图及散点图

    目录 一.导包 二.绘制简单折线 三.pandas操作Excel的行列 四.pandas处理Excel数据成为字典 五.绘制简单折线图 六.绘制简单散点图 一.导包 import pandas as pd import matplotlib.pyplot as plt 二.绘制简单折线 数据:有一个Excel文件lemon.xlsx,有两个表单,表单名分别为:Python 以及student. Python的表单数据如下所示: student的表单数据如下所示:  1.在利用pandas模块进行

  • asp.net导出excel数据的常见方法汇总

    本文实例讲述了asp.net中一些常用的excel数据导出方法,同时也介绍了在数据导入或导出时可能碰到的一些问题总结,分享给大家供大家参考.希望文章对你会有所帮助.具体实现方法如下: 1.由dataset生成 复制代码 代码如下: public void CreateExcel(DataSet ds,string typeid,string FileName)    {    HttpResponse resp;    resp = Page.Response;    resp.ContentE

随机推荐