版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
继续上一期《绘图实例之统计图1》,继续讲解如何绘制折线图:
当按下“折线图”按钮时,调用三个函数,分别是绘制坐标轴、绘制折线、显示到窗口:
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教程 目录
————————————————
版权声明:本文为CSDN博主「VB.Net」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。