最近在WPF使用ZedGraph控件,发现这个控件的功能很强大,据说采用了双缓冲机制来绘制图,只要控制好显示的帧速,能够显示速度较快的动态图。
参考:https://blog.csdn.net/qq_26093511/article/details/51329059
1、常用属性设置
zedGraphControl2.IsShowPointValues = true; //鼠标经过图表上的点时是否气泡显示该点所对应的值
zedGraphControl2.IsZoomOnMouseCenter = true;//使用滚轮时以鼠标所在点进行缩放还是以图形中心进行缩放 true为以鼠标所在点进行缩放
zedGraphControl2.GraphPane.Title.Text = "Spectrogram";
zedGraphControl2.GraphPane.XAxis.Title.Text = "WaveLength/nm";
zedGraphControl2.GraphPane.YAxis.Title.Text = "Intensity";
//设置X、Y轴范围
zedGraphControl2.GraphPane.XAxis.Scale.Min = 0;//MainWindow.waveData[1] - 3; //X轴最小值0 x轴的两边留出一定余量
zedGraphControl2.GraphPane.XAxis.Scale.Max = 512;// MainWindow.waveData[512] + 3;
zedGraphControl2.GraphPane.YAxis.Scale.Min = 0;
zedGraphControl2.GraphPane.YAxis.Scale.Max = 4000; //这里要改成动态的,大小随波形改变
// zedGraphControl2.GraphPane.YAxis.Scale.MaxAuto = true;//根据数值的大小自动的设置Y轴的大小
//设置X、Y轴的刻度大小
zedGraphControl2.GraphPane.XAxis.Scale.MinorStep = 5; //X轴小刻度 小跨度
zedGraphControl2.GraphPane.XAxis.Scale.MajorStep = 50; //X轴显示的最大大刻度
// zedGraphControl2.GraphPane.YAxis.Scale.MinorStep = 100;
//zedGraphControl2.GraphPane.YAxis.Scale.MajorStep = 1000;
zedGraphControl2.GraphPane.YAxis.Scale.MajorStepAuto = true;//自动设置Y轴的大刻度大小
zedGraphControl2.GraphPane.YAxis.Scale.MinorStepAuto = true;//自动设置Y轴的小刻度大小
zedGraphControl2.GraphPane.YAxis.Scale.IsUseTenPower = false;//Y轴不以10的幂显示
zedGraphControl2.AxisChange();
//将X、Y轴的对面坐标轴隐藏
zedGraphControl2.GraphPane.XAxis.MajorTic.IsOpposite = false;
zedGraphControl2.GraphPane.YAxis.MajorTic.IsOpposite = false;
zedGraphControl2.GraphPane.YAxis.MinorTic.IsOpposite = false;
zedGraphControl2.GraphPane.XAxis.MinorTic.IsOpposite = false;
//zedgraph显示参考线/辅助线/网格线
zedGraphControl2.GraphPane.XAxis.MajorGrid.IsVisible = true;
zedGraphControl2.GraphPane.YAxis.MajorGrid.IsVisible = true;
//控制x,y轴
zedGraphControl2.IsEnableHZoom = false;//不允许横向缩放
2、常用方法设置
这个控件的常用方法有保存图片、打印预览、打印、复制图表到剪贴板、图表自适应等。
//保存图片
private void Save_image_Click(object sender, RoutedEventArgs e)
{
zedGraphControl2.SaveAs();//打开图像另存为对话框 此处调用zedgraph控件的内部功能
}
//打印预览
private void PrintPreview_Click(object sender, RoutedEventArgs e)
{
zedGraphControl2.DoPrintPreview();
}
//打印当前图片
private void Print_Click(object sender, RoutedEventArgs e)
{
zedGraphControl2.DoPrint();
}
//打印设置对话框
private void PrintPageSetup_Click(object sender, RoutedEventArgs e)
{
zedGraphControl2.DoPageSetup();
}
//复制图片到剪贴板
private void CopyImage_Click(object sender, RoutedEventArgs e)
{
zedGraphControl2.Copy(true);
}
3、添加标签功能设置
给要绘制的曲线的某个点上添加一个标签。
LineItem ADCurve= zedGraphControl2.GraphPane.AddCurve("AD", frameList, Data .adLineColor , SymbolType.None);//生成曲线和图例 ,标签加到这条线上的某个点
PointPair pt = ADCurve.Points[maxXValue];//获取线条上最大值点坐标 maxXValue代表第几个元素
double showX = 0;
string label = showX.ToString("f2") + "," + maxYValue.ToString("f2");
TextObj text = new TextObj(label, pt.X, pt.Y - 10, CoordType.AxisXYScale, AlignH.Left, AlignV.Center);//设置标签要显示的内容和位置
text.FontSpec.Border.IsVisible = true;//添加标签的边框
text.FontSpec.Fill.IsVisible = true;//对标签填充颜色
text.FontSpec.Fill.Color = System.Drawing.Color.WhiteSmoke;
text.FontSpec.Angle = 0;//横向显示
zedGraphControl2.GraphPane.GraphObjList.Add(text);//标签添加到图上