当按下“折线图”按钮时,调用三个函数,分别是绘制坐标轴、绘制折线、显示到窗口:
Private Sub btnLineChart_Click(sender As Object,e As EventArgs) Handles btnLineChart.Click Call DrawAxis() Call DrawLine() Call DrawToForm() End Sub
DrawAxis()和DrawToForm()在上一期中已经介绍,以下是DrawLine的代码:
'绘制折线 Private Sub DrawLine() '标注每个季度 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 = {"一季度","二季度","三季度","四季度"} '先要获得每个值所在的坐标点 '为了显眼,绘制点的显示为一个直径为8的蓝色圆形 Dim signX,signY As Integer '将每个坐标点存入数组,画折线时候需要 Dim pointSign() As Point ReDim pointSign(3) For i As Integer = 0 To 3 signX = (i + 1) * 80 '不要忘了比例1:3--http://blog.csdn.net/uruseibest signY = 320 - seasonValue(i) * 3 pointSign(i) = New Point(signX,signY) '请注意画园时候的Rectangle位置 g.FillEllipse(New SolidBrush(Color.Blue),New Rectangle(signX - 4,signY - 4,8,8)) Next '使用红色画折线 Dim penSign As New Pen(Color.Red,2) '将四个坐标点连接起来,注意画的是三条线 For i As Integer = 0 To 2 g.DrawLine(penSign,pointSign(i),pointSign(i + 1)) Next '标出每个季度 Dim strX,strY As Integer For i As Integer = 0 To 3 strX = (i + 1) * 80 - 15 strY = 325 g.DrawString(seasonName(i),New Font("黑体",10),New SolidBrush(Color.Blue),New Point(strX,strY)) Next End Sub
按下“折线图”按钮时,如下图所示:
最后强调一下关于绘制矩形、圆形等时候需要注意的起点位置:
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看 vb.net 教程 目录
原文链接:https://www.f2er.com/vb/256729.html