ZedGraph使用也挺简单的,就是属性多,想要实现漂亮一点的效果,就多研究研究一下它的属性。ZedGraph实质上就相当于一个用户自定义控件,核心类ZedGraphControl就是一个控件,new一个ZedGraphControl对象,再初始化这个对象,再添加到某一容器中就OK了。
做了个小Test,先看下效果:
怎么样,效果很漂亮吧,其实代码精简起来很简单,其他图形效果大家就自己研究吧!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void DrawCurve()
{
ZedGraphControl zgc = new ZedGraphControl();
GraphPane myPane = zgc.GraphPane;
myPane.Title.Text = "Y=X*X函数曲线图";
myPane.XAxis.Title.Text = "X";
myPane.YAxis.Title.Text = "Y";
myPane.Border = new Border(Color.Gray, 2.0F);
PointPairList list = new PointPairList();
for (int x = 0; x < 100; x++)
{
list.Add((double)x, (double)(x * x));
}
LineItem myCurve = myPane.AddCurve("", list, Color.Red, SymbolType.Default);
zgc.AxisChange();
splitContainer1.Panel2.Controls.Clear();
splitContainer1.Panel2.Controls.Add(zgc);
zgc.Width = this.Width;
zgc.Height = this.Height;
zgc.Dock = DockStyle.Fill;
}
private void DrawPie()
{
ZedGraphControl zgc = new ZedGraphControl();
GraphPane myPane = zgc.GraphPane;
myPane.Title.Text = "人员缺勤比例饼形图";
myPane.Border = new Border(Color.Gray, 2.0F);
PieItem segment1 = myPane.AddPieSlice(20, Color.Navy, Color.White, 45f, 0, "小华");
PieItem segment2 = myPane.AddPieSlice(40, Color.SandyBrown, Color.White, 45f, 0.0, "张三");
PieItem segment3 = myPane.AddPieSlice(30, Color.Purple, Color.White, 45f, .0, "王五");
PieItem segment4 = myPane.AddPieSlice(10.21, Color.LimeGreen, Color.White, 45f, 0, "李四");
PieItem segment6 = myPane.AddPieSlice(250, Color.Red, Color.White, 45f, 0, "小明");
PieItem segment7 = myPane.AddPieSlice(50, Color.Blue, Color.White, 45f, 0.0, "流倏");
PieItem segment8 = myPane.AddPieSlice(400, Color.Green, Color.White, 45f, 0, "花花");
PieItem segment9 = myPane.AddPieSlice(50, Color.Yellow, Color.White, 45f, 0.0, "如意");
zgc.AxisChange();
splitContainer1.Panel2.Controls.Clear();
splitContainer1.Panel2.Controls.Add(zgc);
zgc.Width = this.Width;
zgc.Height = this.Height;
zgc.Dock = DockStyle.Fill;
}
private void DrawBar()
{
ZedGraphControl zgc = new ZedGraphControl();
GraphPane myPane = zgc.GraphPane;
myPane.Title.Text = "体重柱状图";
myPane.XAxis.Title.Text = "姓名";
myPane.YAxis.Title.Text = "体重(kg)";
double[] y = { 48, 56, 65, 32, 40 };
string[] str = { "王五", "流倏", "张三", "花花", "如意" };
myPane.XAxis.Type = AxisType.Text;
myPane.XAxis.Scale.TextLabels = str;
myCurve.Bar.Border.IsVisible = false;
myPane.Chart.Fill = new Fill(Color.White, Color.SteelBlue, 45.0f);
myPane.Chart.Border.Color = Color.Gray;
myPane.Chart.Border.Width = 2.0F;
myPane.XAxis.Scale.FontSpec.FontColor = Color.Red;
BarItem myCurve = myPane.AddBar("", null, y, Color.Green);
zgc.AxisChange();
splitContainer1.Panel2.Controls.Clear();
splitContainer1.Panel2.Controls.Add(zgc);
zgc.Width = this.Width;
zgc.Height = this.Height;
zgc.Dock = DockStyle.Fill;
}
private void rdo_CheckedChanged(object sender, EventArgs e)
{
if (rdoCurve.Checked == true)
DrawCurve();
if (rdoPie.Checked == true)
DrawPie();
if (rdoBar.Checked == true)
DrawBar();
}
private void Form1_Load(object sender, EventArgs e)
{
DrawPie();
}
}
}