写码背景:
试验台要做多个实验,每个实验都会收集数据做成不同的报告,最后保存到一个文档里
第一步:写一个试验类(为了简便,就用中文命名参数了)
第二步:在类中判别是哪种试验的图片
第三步:写把数据生成图片的类
第四步:在实验方法的最后收集数据
第五步:加到报告文档中
//yH类
public class yH
{
// 通用试验文档参数
public string 路径;
public string savePath;
public string pathModel;
// 构造函数,每次初始化文档的路径
public yH()
{
path = 路径.GetFullPath("..");
savePath = 路径 + "\\report\\动态油缸试验报告.docx";
pathModel = 路径 + "\\template\\动态油缸试验报告模板.docx";
}
//接下来写具体实现方法
}
接下来在这个类中写方法,判别是哪种试验的图片(这里只列了两个,实现都是一样的):
public void GraphChoose()
{
//频率响应试验图片生成路径
if (MainForm.test ==10)
{
图片路径3 = 路径 + "\\report\\pict\\频率响应试验曲线" + DateTime.Now.ToString("yy-MM-dd hhmmss") + ".bmp";
testGraphInfo = new GraphInfo("频率响应试验报告图", "频率HZ","振幅比dB" ,图片路径3);
}
//阶跃响应试验图片生成路径
if (MainForm.test == 11)
{
图片路径4 = 路径 + "\\report\\pict\\阶跃响应试验曲线" + DateTime.Now.ToString("yy-MM-dd hhmmss") + ".bmp";
testGraphInfo = new GraphInfo("阶跃响应试验报告图", "响应时间*10ms", "响应位移mm", 图片路径4);
}
}
接下来就是把数据生成图片的类了:
public class GraphInfo
{
private string xtitle, ytitle,y2title, title;
private int xMax, xMin, yMax, yMin;
private PointPairList list, list2;
public string imageSavePath;
#region properties
//传递参数都是属性,这里就不展开声明了
#endregion
public GraphInfo(string title, string xtitle, string ytitle, string path)
{
this.title = title;
this.xtitle = xtitle;
this.ytitle = ytitle;
this.list = new PointPairList();
this.imageSavePath = path;
this.hasY2 = false;
}
第三步,是写一个图中成像的方法
public FormZedGraphWithSingle(GraphInfo info)
{
InitializeComponent();
//如果是一次性采集数据生成点
//GraphPane是系统方法ZedGraphControl的对象
GraphPane.Title.Text = info.Title;//设置标题内容
GraphPane.XAxis.Title.Text = info.XTitle;//X轴标题
GraphPane.YAxis.Title.Text = info.YTitle;
PointPairList list1 = info.List;//数据
GraphPane.Title.FontSpec.Size = 18;//设置标题大小
GraphPane.XAxis.Title.FontSpec.Size = 14;//设置x轴标题大小
GraphPane.YAxis.Title.FontSpec.Size = 14;//设置y轴标题大小
GraphPane.CurveList.Clear();
LineItem mycurve = GraphPane.AddCurve(info.YTitle + "-" + info.XTitle + "图线", list1, Color.Red, SymbolType.None);//绘制图表
接下来,只要在具体的试验方法中写以下采集数据的语句即可:
testGraphInfo.List.Add(yH.Array3[i], yH.Array4[i]);
最后,我们还是把添加图片到文档中的方法写到试验类中。其中WordHelper类是实现文档生成的类,这里不是我只是调用,所以就不马上来了,见谅
/// <summary>
/// 生成报告的方法
/// </summary>
public void GenerateReport()
{
LOG.Info("正在写入基础信息...");
Dictionary<string, string> Pict = new Dictionary<string, string>();
WordHelper helper = SetReportBasicInfo(true);
//选择试验生成图片
if (File.Exists(图片路径1))
{
Pict.Add("$动态油缸位移时间曲线$", 图片路径1);
}
if (File.Exists(图片路径2))
{
Pict.Add("$动态油缸负载效率试验曲线$", 图片路径2);
}
helper.Insertpicture(Pict);
helper.SaveDocument(保存路径);
MessageBox.Show("本次试验报告文档保存在项目的。。。中,请查收");
}
奉上一张结果,没错,是有可行性的