继续上一期《绘图实例之统计图2》,继续讲解如何绘制饼图:
当按下“饼图”按钮时,调用三个函数,分别是绘制饼图、显示到窗口,由于不需要坐标轴,所以这里不再绘制坐标轴:
Private Sub btnPie_Click(sender As Object,e As EventArgs) Handles btnPie.Click Call DrawPie() Call DrawToForm() End Sub
DrawToForm()在之前已经介绍,以下是DrawPie的代码:
'绘制饼图 Private Sub DrawPie() '标注每个季度 Dim seasonValue(3) As Integer seasonValue(0) = nudSeason1.Value seasonValue(1) = nudSeason2.Value seasonValue(2) = nudSeason3.Value seasonValue(3) = nudSeason4.Value Dim seasonName() As String = {"一季度","二季度","三季度","四季度"} '我们要获得4个季度总的盈利 Dim seasonSum As Integer = 0 For i As Integer = 0 To 3 seasonSum += seasonValue(i) Next '根据总赢利情况,来获得每个季度在饼图中所占的份额(角度) '为了简化起见,这里直接取整数 Dim seasonAngle(4) As Integer seasonAngle(0) = 0 seasonAngle(1) = seasonValue(0) * 360 \ seasonSum seasonAngle(2) = seasonValue(1) * 360 \ seasonSum + seasonAngle(1) seasonAngle(3) = seasonValue(2) * 360 \ seasonSum + seasonAngle(2) seasonAngle(4) = 360 '分别用4种颜色表示不同季度的盈利--http://blog.csdn.net/uruseibest Dim seasonColor() As Color = {Color.Red,Color.Blue,Color.Green,Color.GreenYellow} For i As Integer = 0 To 3 g.FillPie(New SolidBrush(seasonColor(i)),New Rectangle(100,100,200,200),seasonAngle(i),seasonAngle(i + 1) - seasonAngle(i)) '饼图中特别需要说明每个季度对应的颜色 g.FillRectangle(New SolidBrush(seasonColor(i)),New Rectangle(360,i * 50 + 80,60,40)) '标出每个季度 g.DrawString(seasonName(i),New Font("宋体",12),New SolidBrush(Color.Black),New Point(450,i * 50 + 90)) Next End Sub
按下“饼图”按钮时,如下图所示:
需要强调的是FillPie()最后两个关于角度的参数:
第1个角度参数(startAngle):扇形起始角度,是构成扇形的第一条边从x轴方向按照顺时针旋转的角度。
第2个角度参数(sweepAngle):扇形第二条边从第一条边按照顺时针方向旋转的角度
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看 vb.net 教程 目录