C#使用Chart繪制曲線
本文實(shí)例為大家分享了C#使用Chart繪制曲線的具體代碼,供大家參考,具體內(nèi)容如下
新建一個(gè)控制臺(tái)應(yīng)用程序,程序名:WindowsFormsApp2,將下面的代碼拷貝進(jìn)去即可
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 System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace WindowsFormsApp2
{
? ? public partial class Form1 : Form
? ? {
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? ??
? ? ? ? ? ??
? ? ? ? ? ? timer1.Interval = 5000;
? ? ? ? ? ? textBox1.Text = "0";
? ? ??
? ? ? ? }
? ? ? ? private void Form1_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ??
? ? ? ? }
? ? ? ? private void chart1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? }
? ? ? ? private void button1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? timer1.Start();
? ? ? ? }
? ? ? ? private void timer1_Tick(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? // 設(shè)置曲線的樣式
? ? ? ? ? ? Series series = chart1.Series[0];
? ? ? ? ? ? // 畫樣條曲線(Spline)
? ? ? ? ? ? series.ChartType = SeriesChartType.Spline;
? ? ? ? ? ? // 線寬2個(gè)像素
? ? ? ? ? ? series.BorderWidth = 2;
? ? ? ? ? ? // 線的顏色:紅色
? ? ? ? ? ? series.Color = System.Drawing.Color.Red;
? ? ? ? ? ? // 圖示上的文字
? ? ? ? ? ? series.LegendText = "動(dòng)態(tài)曲線";
? ? ? ? ? ? // 測(cè)試代碼 數(shù)據(jù)轉(zhuǎn)換
? ? ? ? ? ? int values = Convert.ToInt16(textBox1.Text);
? ? ? ? ? ? // 在chart中顯示數(shù)據(jù)
? ? ? ? ? ? int x = 0;
? ? ? ? ? ? series.Points.AddXY(x, values);
? ? ? ? ? ? x++;
? ? ? ? ? ? if (x == 100) x = 0;
? ? ? ? ? ? // 設(shè)置顯示范圍
? ? ? ? ? ? ChartArea chartArea = chart1.ChartAreas[0];
? ? ? ? ? ? chartArea.AxisX.Minimum = 0;
? ? ? ? ? ? chartArea.AxisX.Maximum = System.Double.NaN;
? ? ? ? ? ? chartArea.AxisY.Minimum = 0d;
? ? ? ? ? ? chartArea.AxisY.Maximum = System.Double.NaN; ?//自動(dòng)去判斷
? ? ? ? }
? ? ? ? private void textBox1_TextChanged(object sender, EventArgs e)
? ? ? ? {
? ? ? ? }
? ? ? ? private void textBox2_TextChanged(object sender, EventArgs e)
? ? ? ? {
? ? ? ? }
? ? }
}小編分享一段收藏的代碼:C#更新數(shù)組實(shí)現(xiàn)動(dòng)態(tài)曲線:
private void timer定時(shí)_Tick(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (Flash == 0) ? ? ? ? ?//當(dāng)刷新時(shí)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Pen middleLine = new Pen(Color.White);
? ? ? ? ? ? ? ? SolidBrush blackBrush = new SolidBrush(Color.Black); ? //定義了一個(gè)單色的黑色畫筆
? ? ? ? ? ? ? ? graphic = Graphics.FromImage(pictureBox圖像.Image);
? ? ? ? ? ? ? ? graphic.FillRectangle(blackBrush, 0, 0, 500, 251); ? ? //將畫布填充為黑色矩形 ? ? ? ? ? ?
? ? ? ? ? ? ? ? Point pt1 = new Point(0, 125); ? ? ? ? ?//中間線的兩點(diǎn)
? ? ? ? ? ? ? ? Point pt2 = new Point(500, 125);
? ? ? ? ? ? ? ? graphic.DrawLine(middleLine, pt1, pt2); ? ? ? ?//畫中間線
? ? ? ? ? ? }
?
?
? ? ? ? ? ? Random random = new Random();
? ? ? ? ? ? pointList = new Point[50]; ? ?//實(shí)例化坐標(biāo)數(shù)組*
? ? ? ? ? ? int data = random.Next(-800, 800); ? //生成一個(gè)隨機(jī)數(shù)
? ? ? ? ? ?// Point p; ? ? ? //申明一個(gè)坐標(biāo)變量
? ? ? ? ? ? for (int i = 1; i <50 ; i++) ? ? ? ? //更新數(shù)組
? ? ? ? ? ? {
? ? ? ? ? ? ? ? N[i - 1] = N[i];
? ? ? ? ? ? ? ? pointList[i - 1] = pointList[i]; ? ? ?//更新點(diǎn)的坐標(biāo)
? ? ? ? ? ? }
? ? ? ? ? ? N[49] = data;
? ? ? ? ? ? pointList[49].X = 49 * time;
? ? ? ? ? ? pointList[49].Y = data;
? ? ? ? ? ? ? ? ?
? ? ? ? ? ? if (Flash == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (Flash == 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? int k1 = 0, k2 = 0;
? ? ? ? ? ? ? ? ? ? for (int i = 0; i < 49; i++)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? k1 = (int)(lineD - N[i] / times);
? ? ? ? ? ? ? ? ? ? ? ? k2 = (int)(lineD - N[i + 1] / times);
? ? ? ? ? ? ? ? ? ? ? ? graphic.FillEllipse(new SolidBrush(Color.Red), (i * time), k1, 4, 4); //填充邊框所定義的橢圓內(nèi)部,由橢圓邊框左上角坐標(biāo)和橢圓寬度和高度組成
? ? ? ? ? ? ? ? ? ? ? ? //graphic.DrawLine(new Pen(Color.FromArgb(255, 255, 0), 1), new Point(i * time, k1), new Point((i + 1) * time, k2)); ? //兩點(diǎn)之間連線
? ? ? ? ? ? ? ? ? ? ? ? pointList[i].X = i * time; ?pointList[i].Y = k1;?
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? graphic.FillEllipse(new SolidBrush(Color.Red), (49 * time), k2, 4, 4);
? ? ? ? ? ? ? ? ? ? graphic.DrawCurve(new Pen(Color.FromArgb(255, 255, 0), 1), pointList, 0.5f);
? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? pictureBox圖像.Refresh(); //刷新picturebox
? ? ? ? ? ? }
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#、ASP.NET通用擴(kuò)展工具類之TypeParse
這篇文章主要介紹了C#、ASP.NET通用擴(kuò)展工具類之TypeParse,使用了此類,類型轉(zhuǎn)換方便多了,本文直接給出實(shí)現(xiàn)代碼和使用方法,需要的朋友可以參考下2015-06-06
詳解C#如何使用重載方法實(shí)現(xiàn)不同類型數(shù)據(jù)的計(jì)算
這篇文章主要為大家詳細(xì)介紹了C#如何使用重載方法實(shí)現(xiàn)不同類型數(shù)據(jù)的計(jì)算,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02
C#調(diào)用EXE文件實(shí)現(xiàn)傳參和獲取返回結(jié)果
本文主要介紹了C#調(diào)用EXE文件實(shí)現(xiàn)傳參和獲取返回結(jié)果,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
C#中用foreach語(yǔ)句遍歷數(shù)組及將數(shù)組作為參數(shù)的用法
這篇文章主要介紹了C#中用foreach語(yǔ)句遍歷數(shù)組及將數(shù)組作為參數(shù)的用法,C#的數(shù)組可作為實(shí)參傳遞給方法形參,需要的朋友可以參考下2016-01-01
C#幾種獲取網(wǎng)頁(yè)源文件代碼的實(shí)例
C#幾種獲取網(wǎng)頁(yè)源文件代碼的實(shí)例,需要的朋友可以參考一下2013-04-04

