C#下用zedGraph生成大量数据统计图表的方法(通过修改一些源码)

 zedGraph是C#下非常优秀的开源的生成统计图表的库,最近需要用zedGraph生成大量数据的图表(数据非常多,图表非常大),遇到了一些问题,通过修改了一些源码实现


zedGraph的源码可以在这里下载,http://dxdown1.onlinedown.net/down/Zegraph.rar,使用时引用zedGraph.dll和zedGraph.Web.dll即可。


zedGraph初始化:

           List<string> xtitles = new List<string>();   //xtitles是x轴的数据 
           this.zedGraphControl1.Size = new Size(xtitles.Count * 20, 700);    //根据x轴数据的个数设置宽度
            this.zedGraphControl1.GraphPane.Title.FontSpec.Size = 20f;    //设置标题字号
            this.zedGraphControl1.GraphPane.IsFontsScaled = false;     //设置字体不随控件大小而自动缩放
            this.zedGraphControl1.GraphPane.XAxis.Type = ZedGraph.AxisType.Text;   //设置x轴是字符串
            this.zedGraphControl1.GraphPane.XAxis.Scale.TextLabels = xtitles.ToArray();    //设置x轴数据

需要注意的时,如果宽度很大要显示出来,需要设置zedGraph显示x轴的滚动轴,AutoSize设为true,同时在外面放一个panel,设置AutoScroll为true,这样可以滚动显示图表。


画柱状体

List<double> yValues  = new List<double();   //y轴的数据
string yTitle = "数据“;   //x轴的标题
GraphPane graphPane = this.zedGraphControl1.GraphPane;
BarItem myBar = graphPane.AddBar(yTitle, null, yValues.ToArray(), Color.Blue);   //画柱状图
myBar.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue);   //给柱状图设置颜色
myBar.Bar.Border.Color = Color.Transparent;   


刷新显示图表

this.zedGraphControl1.AxisChange();

保存为png图片

string imageName = "D:\\统计图表.png";
RectangleF rect = this.zedGraphControl1.GraphPane.Rect;
Bitmap bitmap = new Bitmap((int)rect.Width, (int)rect.Height);
            using (Graphics bitmapGraphics = Graphics.FromImage(bitmap))
            {
                bitmapGraphics.TranslateTransform(-rect.Left, -rect.Top);
                this.zedGraphControl1.MasterPane.Draw(bitmapGraphics);
            }
bitmap.Save(imageName, ImageFormat.Png);




遇到的一些问题

1. 设置柱状图的宽度无效

设置this.zedGraphControl1.GraphPane.BarSettings.ClusterScaleWidth = 0.5 无效,可以通过修改源码

在scale.cs中

将return Math.Abs( Transform( basisVal + ( IsAnyOrdinal ? 1.0 : pane._barSettings._clusterScaleWidth ) ) - Transform( basisVal ) );

改为return Math.Abs(Transform(basisVal + (pane._barSettings._clusterScaleWidth)) - Transform(basisVal));

当XAxis.Type为AxisType.Text时,IsAnyOrdinal始终为true, 使得clusterScaleWidth不起作用。


2.大数据 当宽度超过10000时,后面的不会画出来,同时宽度很大时,x轴后面的数据不显示

在Bar.cs中,将

if ( top < -10000 )
				top = -10000;
			else if ( top > 10000 )
				top = 10000;
			if ( left < -10000 )
				left = -10000;
			else if ( left > 10000 )
				left = 10000;
			if ( right < -10000 )
				right = -10000;
			else if ( right > 10000 )
				right = 10000;
			if ( bottom < -10000 )
				bottom = -10000;
			else if ( bottom > 10000 )
				bottom = 10000;
中的10000都改为100000

 在Scale.cs,CalcNumTics()中将

if ( nTics < 1 )
nTics = 1;

else if ( nTics > 1000 )

nTics = 1000;

中的1000改为10000


在Scale.cs,DrawLabels函数中,

float lastPixVal = -10000;
// loop for each major tic
for ( int i = firstTic; i < nTics + firstTic; i++ )

在这两行中间加上

               nTics = 50000;

变为

float lastPixVal = -10000;
// loop for each major tic
nTics = 50000;
for ( int i = firstTic; i < nTics + firstTic; i++ )

PS:这个是胡乱加上的,发现有效,也没出现什么问题


修改源码后,重新编译,将新的zedGraph.dll重新导入即可


3. 尺寸过大,保存为图片时出现error

发现当宽度*高度 大约大于 70000*2500时出现问题,这个是bitmap生成时内存不够,只能控制宽度和高度不要过大


我最后生成的图片最大的是50000*3000,8MB,效果还挺不错的。











  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值