- 下载ZedGraph
- 添加 ZedGraph.dll 和ZedGraph.Web.dll的引用
在控件库中添加ZedGraph控件
右键点击工具箱 - 选择项 - .Net Framework 组件 - 浏览 - 找到ZedGraph.dll 和ZedGraph.Web.dll添加
zedGraphControl 控件就出现在工具箱中
- 线图示例程序
- 从工具箱中拖出一个 edGraphControl 控件 (edGraphControl 1 )
- 在Form1初始化之后调用函数createPane,并将zedGraphControl1作为参数
public Form1()
{
InitializeComponent();
createPane(zedGraphControl1);
}
- zedGraphControl1设置函数
public void createPane(ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane;
//设置图标标题和x、y轴标题
myPane.Title.Text = "机票波动情况";
myPane.XAxis.Title.Text = "波动日期";
myPane.YAxis.Title.Text = "机票价格";
//更改标题的字体
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生产一条曲线,标注是“东航”
LineItem myCurve = myPane.AddCurve("东航",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();
}
注意:GraphPane.CurveList.Clear(); 方法清除图表中所有图画(如:曲线)
c# zedgraph控件的简单例子
最新推荐文章于 2024-05-29 19:54:41 发布