场景
ZedGraph
ZedGraph 是一个开源的.NET图表类库, 全部代码都是用C#开发的。它可以利用任意的数据集合创建2D的线性和柱形图表。
控件下载
https://download.csdn.net/download/badao_liumang_qizhi/11578445
英文手册下载
https://download.csdn.net/download/badao_liumang_qizhi/11578491
示例源码下载
https://download.csdn.net/download/badao_liumang_qizhi/11578558
效果
实现
VS2013新建项目-Windows窗体程序。
然后在窗体设计页面,打开工具箱(ctrl+alt+x)。
将上面下载的控件(以.dll结尾的)拖拽到工具箱中。
然后调整其大小使其充满整个form。
打开Form1.cs进入代码的编写。
在Form初始化后编写createPane,传递的参数是ZedGraph控件对象。
public Form1()
{
InitializeComponent();
//Form1初始化后创建设置控件的方法并将当前ZedGraph控件传递
createPane(zedGraphControl1);
}
然后在方法createPane中。
//需要引入命名空间--using ZedGraph;
public void createPane(ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane;
//设置图表标题 和 x y 轴标题
myPane.Title.Text = "霸道测试标题";
myPane.XAxis.Title.Text = "X轴标题";
myPane.YAxis.Title.Text = "Y轴标题";
//更改标题的字体
FontSpec myFont = new FontSpec("Arial",20,Color.Red,false,false,false);
myPane.Title.FontSpec = myFont;
myPane.XAxis.Title.FontSpec = myFont;
myPane.YAxis.Title.FontSpec = myFont;
// 造一些数据,PointPairList里有数据对x,y的数组
Random y = new Random();
PointPairList list1 = new PointPairList();
for (int i = 0; i < 36; i++)
{
double x = i;
//double y1 = 1.5 + Math.Sin((double)i * 0.2);
double y1 = y.NextDouble() * 1000;
list1.Add(x, y1); //添加一组数据
}
// 用list1生产一条曲线,标注是“曲线1”
LineItem myCurve = myPane.AddCurve("曲线1", list1, Color.Red, SymbolType.Star);
//填充图表颜色
myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);
//以上生成的图标X轴为数字,下面将转换为日期的文本
string[] labels = new string[36];
for (int i = 0; i < 36; i++)
{
labels[i] = System.DateTime.Now.AddDays(i).ToShortDateString();
}
myPane.XAxis.Scale.TextLabels = labels; //X轴文本取值
myPane.XAxis.Type = AxisType.Text; //X轴类型
//画到zedGraphControl1控件中,此句必加
zgc.AxisChange();//在数据变化时绘图
//更新图表
zedGraphControl1.Invalidate();
//重绘控件
Refresh();
}
详细见注释,运行项目。