建一个winform项目,将ZedGraph.dll添加到bin里,这样就可以调用它的方法了.打开winform设计界面,从工具箱上拖一个zedGraphControl控件进来(注:这里须要你先将zedgraph的控件都添加进来的哦).我们先声时一个静态的全局变量和一个全局方法:
private static int num = 1;
public double[] getArrvalues()
{
double[] arr ={ 100, 135, 115, 125, 75, 120 };
{
double[] arr ={ 100, 135, 115, 125, 75, 120 };
arr[0] += num;
arr[1] += num * 2;
arr[2] += num * 3;
arr[3] += num * 4;
arr[4] += num * 5;
arr[5] += num * 6;
num++;
arr[1] += num * 2;
arr[2] += num * 3;
arr[3] += num * 4;
arr[4] += num * 5;
arr[5] += num * 6;
num++;
return arr;
}
}
然后把下面这段代码加到Form_Load里面:
dGraphControl1.GraphPane.Title.Text = "快乐男声实时投票显示";
dGraphControl1.GraphPane.XAxis.Title.Text = "选手";
dGraphControl1.GraphPane.YAxis.Title.Text = "票数";
dGraphControl1.GraphPane.XAxis.Type = ZedGraph.AxisType.DateAsOrdinal;
dGraphControl1.GraphPane.XAxis.Title.Text = "选手";
dGraphControl1.GraphPane.YAxis.Title.Text = "票数";
dGraphControl1.GraphPane.XAxis.Type = ZedGraph.AxisType.DateAsOrdinal;
double[] arrValues = getArrvalues();
string[] xtitles = { "王栎鑫", "吉杰", "魏晨", "苏醒", "张杰", "陈楚生" };
吉杰string[] xtitles = { "王栎鑫", "吉杰", "魏晨", "苏醒", "张杰", "陈楚生" };
BarItem myBar = dGraphControl1.GraphPane.AddBar("票数", null, arrValues, Color.Red);
myBar.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red);
myBar.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red);
myBar.Bar.Border.Color = Color.Transparent;
dGraphControl1.GraphPane.XAxis.Scale.TextLabels = xtitles;
dGraphControl1.GraphPane.XAxis.Scale.FontSpec.Size = 10f;
dGraphControl1.GraphPane.XAxis.Scale.FontSpec.FontColor = Color.Blue;
dGraphControl1.AxisChange();
dGraphControl1.GraphPane.XAxis.Type = AxisType.Text;
dGraphControl1.GraphPane.XAxis.Scale.TextLabels = xtitles;
dGraphControl1.GraphPane.XAxis.Scale.FontSpec.Size = 10f;
dGraphControl1.GraphPane.XAxis.Scale.FontSpec.FontColor = Color.Blue;
dGraphControl1.AxisChange();
dGraphControl1.GraphPane.XAxis.Type = AxisType.Text;
那如何动态显示数据呢?在Winform里面,我们可用Timer控件,也可以用多线程,这里我用的是Timer控件.
从工具箱里面拖一个Tmier控件到Form窗体,然后将其Enable置为true,在其timer1_Tick事件里面加入下面代码(注:这里大家一般是实时读取数据库作为柱图的数据源):
double[] arrValues = getArrvalues();
// Shift the text items up by 5 user scale units above the bars
// Shift the text items up by 5 user scale units above the bars
string[] xtitles = { "王栎鑫", "吉杰", "魏晨", "苏醒", "张杰", "陈楚生" };
const float shift = 5;
const float shift = 5;
dGraphControl1.GraphPane.YAxis.Scale.MaxAuto = false;
dGraphControl1.GraphPane.YAxis.Scale.Max = 10000;
dGraphControl1.GraphPane.YAxis.Scale.Min = 0;
dGraphControl1.GraphPane.YAxis.Scale.MinorStep = 1;
dGraphControl1.GraphPane.YAxis.Color = Color.Transparent;
dGraphControl1.GraphPane.Chart.Fill = new Fill(Color.White, Color.Green, 45.0F);
dGraphControl1.GraphPane.YAxis.IsVisible = false;
for (int i = 0; i < arrValues.Length; i++)
{
dGraphControl1.GraphPane.YAxis.Scale.Max = 10000;
dGraphControl1.GraphPane.YAxis.Scale.Min = 0;
dGraphControl1.GraphPane.YAxis.Scale.MinorStep = 1;
dGraphControl1.GraphPane.YAxis.Color = Color.Transparent;
dGraphControl1.GraphPane.Chart.Fill = new Fill(Color.White, Color.Green, 45.0F);
dGraphControl1.GraphPane.YAxis.IsVisible = false;
for (int i = 0; i < arrValues.Length; i++)
{
// format the label string to have 1 decimal place
string lab = arrValues[i].ToString();
string lab = arrValues[i].ToString();
// create the text item (assumes the x axis is ordinal or text)
// for negative bars, the label appears just above the zero value
TextObj text = new TextObj(lab, (float)(i + 1), (float)(arrValues[i] < 0 ? 0.0 : arrValues[i]) + shift);
// tell Zedgraph to use user scale units for locating the TextObj
text.Location.CoordinateFrame = CoordType.AxisXYScale;
// AlignH the left-center of the text to the specified point
text.Location.AlignH = AlignH.Center;
text.Location.AlignV = AlignV.Center;
// for negative bars, the label appears just above the zero value
TextObj text = new TextObj(lab, (float)(i + 1), (float)(arrValues[i] < 0 ? 0.0 : arrValues[i]) + shift);
// tell Zedgraph to use user scale units for locating the TextObj
text.Location.CoordinateFrame = CoordType.AxisXYScale;
// AlignH the left-center of the text to the specified point
text.Location.AlignH = AlignH.Center;
text.Location.AlignV = AlignV.Center;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill.IsVisible = false;
// rotate the text 90 degrees
text.FontSpec.Angle = 0;
text.FontSpec.Fill.IsVisible = false;
// rotate the text 90 degrees
text.FontSpec.Angle = 0;
// add the TextObj to the list
dGraphControl1.GraphPane.GraphObjList.Add(text);
}
BarItem myBar = dGraphControl1.GraphPane.AddBar(null, null, arrValues, Color.Red);
}
BarItem myBar = dGraphControl1.GraphPane.AddBar(null, null, arrValues, Color.Red);
myBar.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red);
myBar.Bar.Border.Color = Color.Transparent;
myBar.Bar.Border.Color = Color.Transparent;
dGraphControl1.GraphPane.XAxis.Scale.TextLabels = xtitles;
dGraphControl1.GraphPane.XAxis.Scale.FontSpec.Size = 4.5f;
dGraphControl1.GraphPane.BarSettings.Type = BarType.Overlay;
dGraphControl1.GraphPane.XAxis.Type = AxisType.Text;
int num = 0;
num = dGraphControl1.GraphPane.GraphObjList.Count;
dGraphControl1.GraphPane.GraphObjList.RemoveRange(0, num - 6);
dGraphControl1.GraphPane.XAxis.Scale.FontSpec.Size = 4.5f;
dGraphControl1.GraphPane.BarSettings.Type = BarType.Overlay;
dGraphControl1.GraphPane.XAxis.Type = AxisType.Text;
int num = 0;
num = dGraphControl1.GraphPane.GraphObjList.Count;
dGraphControl1.GraphPane.GraphObjList.RemoveRange(0, num - 6);
dGraphControl1.AxisChange();
dGraphControl1.Refresh();
运行后,你就可以看到动态显示的柱图了,是不是很简单?
发布评论