Winform练习---三角函数的绘制

仅作自己使用


效果

在这里插入图片描述

代码

using OxyPlot.Axes;
using OxyPlot.Series;
using OxyPlot.WindowsForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OxyPlot;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace Winform_0920
{
    public partial class Form1 : Form
    {
        double[,] data;
        LineSeries Ls;
        public Form1()
        {
            InitializeComponent();
            InitPlot();
        }

        /// <summary>
        /// 浏览按钮
        /// </summary>
        private void button_browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Excel(*.csv)|*.csv|Excel(*.xls)|*.xls|Excel(*.xlsx)|*.xlsx|Text(*.txt)|*.txt"; // 文件过滤:参考自博客https://blog.csdn.net/qq_40467670/article/details/118229405
            if (ofd.ShowDialog() == DialogResult.OK)// 判断文件是否打开成功
            {
                textBox_fileName.Text = ofd.FileName;
                data = LoadData(ofd.FileName);
                Plot();
            }
        }

        void InitPlot()
        {
            plotView.Model = new PlotModel();
            plotView.Model.Background = OxyColors.White;
            plotView.Model.PlotAreaBorderThickness = new OxyThickness(0); // 去掉边框(这会把x和y轴的线也去掉了)
            Axis x = new LinearAxis();
            x.AxislineStyle = LineStyle.Solid; // 设置x轴的横线
            x.Minimum = 0;
            x.Maximum = 2 * Math.PI;
            x.Title = "x";
            x.Position = AxisPosition.Bottom;
            
            
            plotView.Model.Axes.Add(x);
            Axis y = new LinearAxis();
            y.AxislineStyle = LineStyle.Solid; // 设置y轴的横线
            y.Minimum = -1.2;
            y.Maximum = 1.2;
            y.Position = AxisPosition.Left;
            y.Title = "y";
            plotView.Model.Axes.Add(y);
        }

        /// <summary>
        /// 绘制图像
        /// </summary>
        void Plot()
        {
            Ls = new LineSeries() { Title = "xy" };
            for (int i = 0; i < data.GetLength(1); i++)
            {
                Ls.Points.Add(new DataPoint(data[0, i], data[1, i]));
            }
            Ls.Color = OxyColors.Black; // 设置曲线颜色
            plotView.Model.Series.Add(Ls);
            plotView.InvalidatePlot(true);
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private double[,] LoadData(string filePath)
        {
            string[] strArray = System.IO.File.ReadAllLines(filePath);
            string[] xStr = strArray[0].Split(',');
            string[] yStr = strArray[1].Split(',');
            double[] x = new double[xStr.Length];
            double[] y = new double[yStr.Length];
            double[,] data = new double[x.Length, y.Length];
            for (int i = 0; i < x.Length; i++)
            {
                x[i] = double.Parse(xStr[i]);
                y[i] = double.Parse(yStr[i]);
            }
            for (int i = 0; i < x.Length; i++)
            {
                data[0, i] = x[i];
                data[1, i] = y[i];
            }
            return data;
        }

        /// <summary>
        /// 清空按钮
        /// </summary>
        private void button_clear_Click(object sender, EventArgs e)
        {
            plotView.Model.Series.Clear();
            plotView.InvalidatePlot(true);

        }

        /// <summary>
        /// 保存图片
        /// </summary>
        private void button_save_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog();
            sf.Filter = "PNG|*.png|PDF|*.pdf";
            if (sf.ShowDialog() == DialogResult.OK)
            {
                switch (sf.FilterIndex)
                {
                    case 0:
                        {
                            PngExporter pe = new PngExporter();
                            pe.Width = plotView.Width;
                            pe.Height = plotView.Height;
                            pe.Resolution = 1920 * 1680;
                            pe.ExportToFile(plotView.Model, sf.FileName);
                            break;
                        }
                    case 1:
                        {
                            PdfExporter pe = new PdfExporter();
                            pe.Width = plotView.Width;
                            pe.Height = plotView.Height;
                            pe.ExportToFile(plotView.Model, sf.FileName);
                            break;
                        }
                }
                MessageBox.Show("保存完成~");
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亲爱的老吉先森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值