C# WPF 通过 ZedGraph 绘制柱状图

本文详细介绍如何使用ZedGraph在WPF应用中创建柱状图,并为每个柱子添加文本标签,包括设置图表标题、轴标题、添加数据点、设置颜色等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先参考我以前的博客介绍如何使用ZedGraph点击,查看
参考:https://blog.youkuaiyun.com/lan_liang/article/details/7528837

 private void Window_Loaded(object sender, RoutedEventArgs e)
{
            zedGraphControl1.GraphPane.CurveList.Clear();
            zedGraphControl1.GraphPane.GraphObjList.Clear();        
            GraphPane myPane = zedGraphControl1.GraphPane;
            myPane.Title.Text = "统计消费者";  //设计图表的标题
            myPane.XAxis.Title.Text = "男/女"; //X轴标题
            myPane.YAxis.Title.Text = "人数"; //Y轴标题          
            PointPairList PPLa = new PointPairList();
            PointPairList PPLb = new PointPairList();   
            for (int i = 1; i < 2; i++)
            {
                PPLa.Add(i, i + 3);
                PPLb.Add(i + 1, i + 4);       
            }         
            BarItem myBara = myPane.AddBar("A", PPLa, System.Drawing.Color.Red);
            BarItem myBarb = myPane.AddBar("B", PPLb, System.Drawing.Color.Blue);        
            zedGraphControl1.AxisChange();
            zedGraphControl1.Refresh();//这句话非常重要,否则不会立即显示
}
          

下面这部分是在柱状图上添加标签。假如我有3个柱子,参考https://www.cnblogs.com/know/archive/2011/04/07/2008096.html

for (int i=0;i<3;i++)
            {
                BarItem myBara = myPane.AddBar(tongdaoshengsu[i], new double[] { start_x }, new double[] { shengdaoydata[i] }, colorarr[i]);//加柱子
                myPane.BarSettings.ClusterScaleWidth = 4;//设置柱体的宽度
                myPane.BarSettings.Type = ZedGraph.BarType.Cluster;
                TextObj textobj1 = new TextObj(strshengdao[i], start_x + start_x2, (double)shengdaoydata[i]+0.1 , CoordType.AxisXY2Scale, AlignH.Left, AlignV.Bottom);这里是设置标签
                textobj1.FontSpec.Border.IsVisible = false;
                textobj1.FontSpec.Fill.IsVisible = false;
                textobj1.FontSpec.Fill.Color = System.Drawing.Color.White;
                textobj1.FontSpec.Size = 16;//控制标签显示大小
                                           // 选择标注字体90°
                textobj1.FontSpec.Angle = 0;//横向显示
                myPane.GraphObjList.Add(textobj1);//加上这就会显示标签
                start_x++;
                start_x2++;//不写这句标签会和柱子对不齐
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值