我的DOTNET

我就是咖啡豆

用ZedGraph作图表(一)

需要作一个统计的功能,当然图表是最直观的选择,至于控件么,当然要使用开源的,首先从网上查了查资料,作图表的东东还真不少,有ZedGraph、.Net Charts、NPlot、XSCharting、DaveChart、NChart,每个都有自己的特点,我的这个统计功能比较简单,也就没有细细的选择使用那个了,我直接选了ZedGraph。
首先做个饼图,然后在分析一下代码

创建一个Window应用程序,引入ZedGraph的dll,在form上放置一个ZedGraphControl控件,并设置好他的Height和Width, 添加如下的代码,并在Form_Load中调用该方法。

public void CreateChart( ZedGraphControl zgc )
{
GraphPane myPane = zgc.GraphPane;

// 设置图表的标题和标题的样式
myPane.Title.Text = "2004 ZedGraph Sales by Region\n($M)";
myPane.Title.FontSpec.IsItalic = true;
myPane.Title.FontSpec.Size = 24f;
myPane.Title.FontSpec.Family = "Times New Roman";

// 设置背景色
myPane.Fill = new Fill( Color.White, Color.Goldenrod, 45.0f );
// 设置图表的颜色填充,如果设置为FillType.None,则填充色和背景色相同
myPane.Chart.Fill.Type = FillType.None;

// 设置图例的大小和位置
myPane.Legend.Position = LegendPos.Float;
myPane.Legend.Location = new Location( 0.95f, 0.15f, CoordType.PaneFraction,
AlignH.Right, AlignV.Top );
myPane.Legend.FontSpec.Size = 10f;
myPane.Legend.IsHStack = false;

/*
* 设置饼图的各个部分
* AddPieSlice方法的参数是 value值, 颜色,渐变色,渐变大小,离开中心点的距离,名称
*/
PieItem segment1 = myPane.AddPieSlice( 20, Color.Navy, Color.White, 45f, 0, "North" );
PieItem segment3 = myPane.AddPieSlice( 30, Color.Purple, Color.White, 45f, 0, "East" );
PieItem segment4 = myPane.AddPieSlice( 10.21, Color.LimeGreen, Color.White, 45f, 0, "West" );
PieItem segment2 = myPane.AddPieSlice( 40, Color.SandyBrown, Color.White, 45f, 0.2, "South" );
PieItem segment6 = myPane.AddPieSlice( 250, Color.Red, Color.White, 45f, 0, "Europe" );
PieItem segment7 = myPane.AddPieSlice( 50, Color.Blue, Color.White, 45f, 0.2, "Pac Rim" );
PieItem segment8 = myPane.AddPieSlice( 400, Color.Green, Color.White, 45f, 0, "South America" );
PieItem segment9 = myPane.AddPieSlice( 50, Color.Yellow, Color.White, 45f, 0.2, "Africa" );

zgc.AxisChange();
}


好了,直接运行,就有了一个简单的饼图出现了。

Technorati : 用ZedGraph作图表

posted on 2006-08-09 08:47  小小~咖啡豆  阅读(9130)  评论(0编辑  收藏  举报

导航