扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
在窗体上添加一个Chart1控件和一个Button1控件:
我们提供的服务有:成都做网站、成都网站建设、微信公众号开发、网站优化、网站认证、定海ssl等。为上千多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的定海网站制作公司
完整代码如下:(复制就可以用)
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Chart1.Series.RemoveAt(0) '清除原来的系列
Dim i As Integer
Dim j As Integer
For i = 0 To 11 '设置有12个系列
Chart1.Series.Add("系列" i + 1)
''添加数据点的个数
For j = 0 To 3 '设置有4个数据点
Chart1.Series(i).Points.Add()
Next
Next
'用生成的随机数(范围[2.0,9.9]),作为数据
Randomize()
For i = 0 To 11
For j = 0 To 3
Chart1.Series(i).Points(j).YValues = {Int((99 - 20 + 1) * Rnd() + 20) / 10} '将随机数据赋值给图表点的Y值
Next
Chart1.Series(i).ChartType = DataVisualization.Charting.SeriesChartType.Column '设置图表类型
Next
End Sub
End Class
这个说起来有点小复杂,建议你找专门的文章学习。
比如:
1、检查一下是不是序列的名称不对应,进行了修改,不是默认的"Series1";
3、修改正确或是没有错误时可全部重新生成一次看看。
希望对你有帮助,还有疑问请追问或是Hi
你可以通过用VB.net控制excel,让excel生成曲线图,然后利用excelVBA将图输出,最后导入到VB.net就可以了。
首先你要在工程里面添加至少一个imagelist1控件,把里面放上合适的图标。然后在listview的largeimagelist(对应大图表显示模式)或者smalllimagelist(对应其他显示模式)属性里面指定imagelist1控件。
最后在你的代码添加上文件类型判断代码,根据不同的文件选择不同的图标,最后添加到集合当中去。
Public Class Form3
Protected Sub iniChart()
Dim dt As New DataTable
'表增加月份、收入、支出三列
dt.Columns.Add("月份")
dt.Columns.Add("收入")
dt.Columns.Add("支出")
Dim dr As DataRow
For i As Integer = 1 To 12
'新增行
dr = dt.NewRow()
'月份 1-12月
dr.Item(0) = i "月"
Randomize()
'收入
dr.Item(1) = 3000 + Int(5000 * Rnd(8))
Randomize()
'支出
dr.Item(2) = 600 + Int(2000 * Rnd(7))
dt.Rows.Add(dr)
Next
dr = Nothing
With Me.Chart1
.DataSource = dt 'dt作为chart1的数据源
.Series.Clear()
.Legends.Clear()
.ChartAreas.Clear()
.ChartAreas.Add("收入")
.ChartAreas.Add("支出")
.Legends.Add("收入")
.Legends.Add("支出")
.Series.Add("收入")
.Series.Add("支出")
.Series("支出").ChartArea = "支出" '指定Series所属ChartArea
.Series("支出").Legend = "支出" '指定Legend所属Series
.Series("收入").LegendToolTip = "收入图例"
.Series("收入").IsValueShownAsLabel = True '标签显示数据值
.Legends("收入").DockedToChartArea = "收入" '指定Legend所属ChartArea
.Legends("支出").DockedToChartArea = "支出"
.ChartAreas("支出").Area3DStyle.Enable3D = True '启用3D样式
End With
With (Chart1.Series(0))
'指定x、y轴数据列
.YValueMembers = "收入"
.XValueMember = "月份"
'图表类型
.ChartType = DataVisualization.Charting.SeriesChartType.Column
End With
With Chart1.Series(1)
.YValueMembers = "支出"
.XValueMember = "月份"
.ChartType = DataVisualization.Charting.SeriesChartType.Pie
End With
Me.Chart1.DataBind() '绑定数据源
With Me.Chart1.Series("收入")
Dim s1 As Integer
For i As Integer = 0 To .Points.Count - 1
s1 = s1 + Val(.Points(i).GetValueByName("y"))
.Points(i).ToolTip = .Points(i).AxisLabel .Points(i).GetValueByName("y")
Next
'图例显示总收入
Me.Chart1.Legends("收入").Title = "总收入"
.LegendText = s1.ToString
End With
With Me.Chart1.Series("支出")
.IsValueShownAsLabel = True
For i As Integer = 0 To .Points.Count - 1
.Points(i).ToolTip = .Points(i).AxisLabel .Points(i).GetValueByName("y")
.Points(i).LegendText = .Points(i).AxisLabel
.Points(i).Label = "#PERCENT" '饼状图显示百分比
.SmartLabelStyle.AllowOutsidePlotArea = True
Next
End With
Me.Chart1.AlignDataPointsByAxisLabel("支出")
With Me.Chart1.Legends("支出")
.LegendStyle = DataVisualization.Charting.LegendStyle.Column
.Title = "支出"
End With
End Sub
Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Call iniChart()
Call iniCombChart()
End Sub
Private Sub iniCombChart()
Dim tps As Array
'枚举所有SeriesChartType类型
tps = System.Enum.GetValues(GetType(Windows.Forms.DataVisualization.Charting.SeriesChartType))
For Each i As Windows.Forms.DataVisualization.Charting.SeriesChartType In tps
Me.ComboBox1.Items.Add(Val(i))
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call iniChart()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
With Chart1.Series(0)
'改变图表样式
Dim tps As Array
tps = System.Enum.GetValues(GetType(System.Windows.Forms.DataVisualization.Charting.SeriesChartType))
For Each i As Windows.Forms.DataVisualization.Charting.SeriesChartType In tps
If Val(i) = Me.ComboBox1.Text Then
.ChartType = i
Exit For
End If
Next
End With
End Sub
End Class
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流