首先参考我以前的博客介绍如何使用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++;//不写这句标签会和柱子对不齐
}