1、题目
从文本文件中读取数据,数据格式为:空格分隔;每一行为一组数据,依次读取出来,之后将数据存于数组中;继而将其显示至图形中
2、完整代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FORM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i = 1;//此全局变量为确定画均值图中点数
int m = 0;//此全局变量为确定图中为第几组数据
int TIME_FLag = 1;//此变量为了启动时显示数轴标志位
public float[,] DA = new float[10, 12];//数据存储数组
/// <summary>
/// 按钮控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if (i == 1 && TIME_FLag == 1)
{
TIME_FLag = 0;
this.Gphics();
}
timer1.Start();//开启定时器
}
//定义数组,其中[1,]表示最大值[2,]表示最小值[3,]表示平均值[4,]表示标准差
public float[,] statistics = new float[4, 10];
float Max = 0; //最大值变量
float Min = 0; //最小值变量
float Total = 0; //求和变量
float Average = 0; //计算平均值变量
float Div_Sum = 0; //计算方差求和
float stand_div = 0; //计算标准差
private void pictureBox2_Click(object sender, EventArgs e)
{
}
/// <summary>
/// 定时器控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
m++;
i++;
if (m >= 9)
{
m = 9;
}
if (i >= 10)
{
i = 10;
}
this.Gphics();
}
/// <summary>
/// 一个画图的方法
/// </summary>
private void Gphics()
{
//创建 FileStream 的对象,说白了告诉程序,文件在那里,对文件如何处理,对文件内容采取的处理方式
FileStream fs = new FileStream("Niit.txt", FileMode.Open, FileAccess.Read);
//仅对文本进行读写操作
StreamReader sr = new StreamReader(fs, Encoding.Default);
string str = sr.ReadToEnd();//这里已经把txt中的全部数据都读出来了
sr.Close();
//如果需要将数据转换为数组,只需要对text字符串进行处理即可
string[] H_data = str.Split(new char[] { '\n', '\r' });//这样就可以把每行放到数组中的一项里
string[] data = new string[str.Length];//此变量为每一行的数据量
int number = 0; //此变量为最终数据的统计
int array_b = 0;
for (int i = 0; i < H_data.Length; i += 2)//将每行的每一个数据量进行分离
{
data = H_data[i].Split(new char[] { ' ' });//分离一行变量
for (int j = 0; j < data.Length; j++) //将分离的变量存储置数组中
{
if (number == 12)
{
number = 0;
}
DA[array_b, number] = float.Parse(data[j]);//将字符串数据转换成整型数
number++;
}
array_b++;
}
string[] Time = new string[12] { " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10", "11", "12" };
//画图初始化
Bitmap bmap = new Bitmap(500, 250);
Graphics gph = Graphics.FromImage(bmap);
gph.Clear(Color.White);
PointF cpt = new PointF(40, 200);//中心点
PointF[] xpt = new PointF[3] { new PointF(cpt.Y+220 + 15, cpt.Y),
new PointF(cpt.Y+220, cpt.Y - 8),
new PointF(cpt.Y+220, cpt.Y + 8) };//x轴三角形
PointF[] ypt = new PointF[3] { new PointF(cpt.X, cpt.X - 15),
new PointF(cpt.X - 8, cpt.X),
new PointF(cpt.X + 8, cpt.X) };//y轴三角形
gph.DrawString("数据折线图", new Font("宋体", 14), Brushes.Black, new PointF(cpt.X + 140, cpt.X - 40));//图表标题
//画x轴
gph.DrawLine(Pens.Black, cpt.X, cpt.Y, cpt.Y + 220, cpt.Y);
gph.DrawPolygon(Pens.Black, xpt);
gph.FillPolygon(new SolidBrush(Color.Black), xpt);
gph.DrawString("数据/点", new Font("宋体", 12), Brushes.Black, new PointF(cpt.Y + 220, cpt.Y + 20));
//画y轴
gph.DrawLine(Pens.Black, cpt.X, cpt.Y, cpt.X, cpt.X);
gph.DrawPolygon(Pens.Black, ypt);
gph.FillPolygon(new SolidBrush(Color.Black), ypt);
gph.DrawString("数值", new Font("宋体", 12), Brushes.Black, new PointF(0, 7));
for (int j = 1; j <= DA.GetLength(1); j++)
{
//画y轴刻度
if (j < 6)
{
gph.DrawString((j * 20).ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X - 30, cpt.Y - j * 30 - 6));
gph.DrawLine(Pens.Black, cpt.X - 3, cpt.Y - j * 30, cpt.X, cpt.Y - j * 30);
}
//画x轴项目
gph.DrawString(Time[j - 1].Substring(0, 2), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + j * 30 - 5, cpt.Y + 10));
gph.DrawEllipse(Pens.Black, cpt.X + j * 30 - 1.5f, cpt.Y - DA[m, j - 1] / 2 * 3 - 1.5f, 3, 3);//画点,绘制一个由边框定义的椭圆,该边框由矩形的左上角坐标、高度和宽度指定。
gph.FillEllipse(new SolidBrush(Color.Black), cpt.X + j * 30 - 1.5f, cpt.Y - DA[m, j - 1] / 2 * 3 - 1.5f, 3, 3);//填充边框所定义的椭圆的内部,该边框由一对坐标、一个宽度和一个高度指定。
gph.DrawString(DA[m, j - 1].ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + j * 30, cpt.Y - DA[m, j - 1] / 2 * 3)); //画数值
if (j > 1)
{
gph.DrawLine(Pens.Red, cpt.X + (j - 1) * 30, cpt.Y - DA[m, j - 2] / 2 * 3, cpt.X + j * 30, cpt.Y - DA[m, j - 1] / 2 * 3);//画折线
}
}
PictureBox1.Image = bmap;
for (int arr_before = 0; arr_before < DA.GetLength(0); arr_before++)//DA.GetLength(1)计算总共有多少行,即有多少组数据
{
Max = DA[arr_before, 0];
Min = DA[arr_before, 0];
for (int arr_after = 0; arr_after < DA.GetLength(1); arr_after++)// DA.GetLength(0)计算总共有多少列,即每一组有多少个数据
{
if (DA[arr_before, arr_after] > Max)
{
Max = DA[arr_before, arr_after];
}
if (DA[arr_before, arr_after] < Min)
{
Min = DA[arr_before, arr_after];
}
Total = Total + DA[arr_before, arr_after];//计算一行相加总值,
Average = Total / DA.GetLength(1); //计算平均值;
for (int div = 0; div < DA.GetLength(1); div++)
{
Div_Sum += (DA[arr_before, div] - Average) * (DA[arr_before, div] - Average);//计算方差
}
Div_Sum = Div_Sum / DA.GetLength(1);
stand_div = (float)Math.Sqrt(Div_Sum);//计算标准差
}
Total = 0;
Div_Sum = 0;
statistics[0, arr_before] = Max;//将所得数据存于数组
statistics[1, arr_before] = Min;
statistics[2, arr_before] = (float)Math.Round(Average, 2);
statistics[3, arr_before] = (float)Math.Round(stand_div, 2);
Max_value.Text = statistics[0, i - 1].ToString();//将相应数据显示至文本框
Min_value.Text = statistics[1, i - 1].ToString();
Sad_div.Text = statistics[3, i - 1].ToString();
}
string[] Array = new string[10] { " 4", " 8", "12", "16", "20", "24", "28", "32", "36", "40" };
//统计图初始化
Bitmap satistic_Map = new Bitmap(500, 250);
Graphics Sat_Map = Graphics.FromImage(satistic_Map);
Sat_Map.Clear(Color.White);
PointF Sat_Centre = new PointF(40, 210);//第二张图的中心点
PointF[] X_Centre = new PointF[3] { new PointF(Sat_Centre.Y+220 + 15, Sat_Centre.Y),
new PointF(Sat_Centre.Y+220, Sat_Centre.Y - 8),
new PointF(Sat_Centre.Y+220, Sat_Centre.Y + 8) };//x轴三角形
PointF[] Y_Centre = new PointF[3] { new PointF(Sat_Centre.X, Sat_Centre.X - 15),
new PointF(Sat_Centre.X - 8, Sat_Centre.X),
new PointF(Sat_Centre.X + 8, Sat_Centre.X) };//y轴三角形
Sat_Map.DrawString("数据统计图", new Font("宋体", 14), Brushes.Black, new PointF(Sat_Centre.X + 140, Sat_Centre.X - 40));//图表标题
Sat_Map.DrawLine(Pens.Black, Sat_Centre.X, Sat_Centre.Y, Sat_Centre.Y + 220, Sat_Centre.Y);
Sat_Map.DrawPolygon(Pens.Black, X_Centre);
Sat_Map.FillPolygon(new SolidBrush(Color.Black), X_Centre);
Sat_Map.DrawString("时间/S", new Font("宋体", 12), Brushes.Black, new PointF(Sat_Centre.Y + 220, Sat_Centre.Y + 20));
//画y轴
Sat_Map.DrawLine(Pens.Black, Sat_Centre.X, Sat_Centre.Y, Sat_Centre.X, Sat_Centre.X);
Sat_Map.DrawPolygon(Pens.Black, Y_Centre);
Sat_Map.FillPolygon(new SolidBrush(Color.Black), Y_Centre);
Sat_Map.DrawString("均值", new Font("宋体", 12), Brushes.Black, new PointF(0, 7));
for (int j = 1; j <= i; j++)
{
//画y轴刻度
if (j < 6)
{
Sat_Map.DrawString((j * 20).ToString(), new Font("宋体", 11), Brushes.Black, new PointF(Sat_Centre.X - 30, Sat_Centre.Y - j * 30 - 6));
Sat_Map.DrawLine(Pens.Black, Sat_Centre.X - 3, Sat_Centre.Y - j * 30, Sat_Centre.X, Sat_Centre.Y - j * 30);
}
//画x轴项目
Sat_Map.DrawString(Array[j - 1].Substring(0, 2), new Font("宋体", 11), Brushes.Black, new PointF(Sat_Centre.X + j * 30 - 5, Sat_Centre.Y + 10));
Sat_Map.DrawEllipse(Pens.Black, Sat_Centre.X + j * 30 - 1.5f, Sat_Centre.Y - statistics[2, j - 1] / 2 * 3 - 1.5f, 3, 3);//画点,绘制一个由边框定义的椭圆,该边框由矩形的左上角坐标、高度和宽度指定。
Sat_Map.FillEllipse(new SolidBrush(Color.Black), Sat_Centre.X + j * 30 - 1.5f, Sat_Centre.Y - statistics[2, j - 1] / 2 * 3 - 1.5f, 3, 3);//填充边框所定义的椭圆的内部,该边框由一对坐标、一个宽度和一个高度指定。
Sat_Map.DrawString(statistics[2, j - 1].ToString(), new Font("宋体", 11), Brushes.Black, new PointF(Sat_Centre.X + j * 30, Sat_Centre.Y - statistics[2, j - 1] / 2 * 3)); //画数值
if (j > 1)
{
Sat_Map.DrawLine(Pens.Red, Sat_Centre.X + (j - 1) * 30, Sat_Centre.Y - statistics[2, j - 2] / 2 * 3, Sat_Centre.X + j * 30, Sat_Centre.Y - statistics[2, j - 1] / 2 * 3);//画折线
}
}
PictureBox2.Image = satistic_Map;
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
3、运行结果