您的位置:首页 > 其它

WinForm"ZedGraph柱状图"控件使用经验分享 之配餐系统的开发

2011-04-07 16:55 876 查看
此文主要是和大家分享下在做配餐系统的新模块开发中,对"ZedGraph柱状图"控件的使用,先看看我在项目所应用的效果:



如上图所示,此柱状图只有Y轴,且每个柱的颜色都不同 ——没有X轴,却需要显示每种体质的名称和分数,这就需要在常规的(网上的示例)基础上做修改。

关键代码如下:

void ShowBar(double[] tzZhfArr)
{
string[] tzTypeArr = ComHelper.GetTzTypeArray();
Dictionary<int, double[]> dict = new Dictionary<int, double[]>();
Color[] colorArr ={ Color.Orange,Color.PaleGreen,Color.SlateBlue,
Color.Pink,Color.Green,Color.LightSkyBlue,
Color.Gray,Color.GreenYellow,Color.RosyBrown};

this.zedGraphControl_res.SuspendLayout();

this.zedGraphControl_res.Controls.Clear();
ZedGraph.GraphPane myPane = this.zedGraphControl_res.GraphPane;

myPane.Title.Text = "体质测试结果柱状图";
myPane.XAxis.Title.Text = "";
myPane.YAxis.Title.Text = "";

int start_x = 5;
double tzZhz = 0;
for (int i = 0; i < tzTypeArr.Length; i++)
{
tzZhz = tzZhfArr[i] < 0 ? 0 : tzZhfArr[i];
ZedGraph.BarItem bar = myPane.AddBar("", new double[] { start_x }, new double[] { tzZhz }, colorArr[i]);
bar.Bar.Fill = new ZedGraph.Fill(colorArr[i], Color.White, colorArr[i]);

ZedGraph.TextObj myText = new ZedGraph.TextObj(string.Format("{0},{1}", tzTypeArr[i], tzZhz), start_x, tzZhz + 3);
myText.Location.CoordinateFrame = ZedGraph.CoordType.AxisXYScale;
myText.Location.AlignH = ZedGraph.AlignH.Center;
myText.Location.AlignV = ZedGraph.AlignV.Center;
myText.FontSpec.Family = "宋体";
myText.FontSpec.Size = 16f;
myText.FontSpec.Fill.IsVisible = false;
myText.FontSpec.Border.IsVisible = false;
//myText.FontSpec.Angle = 35;//控制 文字 倾斜度
myPane.GraphObjList.Add(myText);
start_x += 15;
}

myPane.Fill = new ZedGraph.Fill(Color.WhiteSmoke, Color.Lavender, 0F);
myPane.Chart.Fill = new ZedGraph.Fill(Color.FromArgb(255, 255, 245),
Color.FromArgb(255, 255, 190), 90F);
//设置 柱体的宽度
myPane.BarSettings.ClusterScaleWidth = 30;
// Bars are stacked
myPane.BarSettings.Type = ZedGraph.BarType.Cluster;

myPane.XAxis.IsVisible = false;

// Enable the X and Y axis grids
myPane.XAxis.MajorGrid.IsVisible = false;
myPane.YAxis.MajorGrid.IsVisible = true;

myPane.XAxis.Scale.Min = -10;
this.zedGraphControl_res.ResumeLayout();
this.zedGraphControl_res.AxisChange();
}


其中,有几个重要的属性,需要特殊说明下:

1. //设置 柱体的宽度
myPane.BarSettings.ClusterScaleWidth = 30;
// Bars are stacked
myPane.BarSettings.Type = ZedGraph.BarType.Cluster;

设置 柱体的宽度 只在 myPane.BarSettings.Type(柱体布局设置或类型)中的某些值时有效。

2. myText.FontSpec.Angle = 35;//控制 文字 倾斜度

3. 当myPane.XAxis.IsVisible = false时,X轴下方(一般为-y的值)的文本等将无法显示。

——ZedGraph是国外的控件,功能貌似很强大,我实现效果可能还有更方便或合适的方法,对其也算是有个大概的了解,希望路过的朋友能分享下你的使用经验!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐