Dim Chart As WinForm.Chart '定义一个图表变量
Dim Series As WinForm.ChartSeries '定义一个图系变量
Dim Series2 As WinForm.ChartSeries '定义一个图系变量
Dim t As Table = Tables("图表测试数据") '定义一个变量t引用数据表
Chart = e.Form.Controls("Chart1") ' 引用窗口中的图表
Chart.VisualEffect = True '加上这一行,让你的图表更漂亮
Chart.ChartType = ChartTypeEnum.Bar '图表类型改为Bar(条形)
Chart.ChartType2 = ChartTypeEnum.XYPlot '图表2类型该为线形
Chart.SeriesList.Clear() '清除图表原来的图系
Chart.AxisY.Min = 0 '指定Y轴的最小值
Chart.AxisY.Max = 150 '指定Y轴的最大值
'Chart.BackColor = Color.Blue '设置图表背景颜色,因后面设置了图表区域样式,所以这里会不起作用
For Each c As Col In t.Cols
If c.Name <> "年份" And c.Name <> "月份" And c.Name <> "品牌" And c.Name <> "销售增长" Then
Series = Chart.SeriesList.Add() '增加一个图系
Series.Text = c.Name '设置图系的标题
Series.Length = t.Rows.Count '设置图系的长度
For r As Integer = 0 To t.Rows.Count - 1
Series.X(r) = r
Series.Y(r) = t.Rows(r)(c.Name)
Next
End If
Next
Chart.ChartType2 = ChartTypeEnum.XYPlot '图表2类型该为线形
Chart.SeriesList2.Clear() '清除图表原来的图系
Series2 = Chart.SeriesList2.Add() '给图表2增加一个图系
Series2.Text = "销售增长" '设置图系的标题
Series2.Length = t.Rows.Count '图系的数据点数等于表的行数
For i As Integer = 0 To t.Rows.Count - 1 '指定每个数据点的位置
Series2.X(i) = t.Rows(i)("月份") '指定水平坐标
Series2.Y(i) = t.Rows(i)("销售增长") '指定垂直坐标
Next
Series2.LineColor = Color.Red '设置连线的颜色
Series2.LineThickNess = 0.5 '设置连线的大小
Series2.MarkColor = Color.Red '设置数据点的颜色
Series2.MarkShape = MarkShapeEnum.Dot '设置数据点的形状
Series2.MarkSize = 2 '设置数据点标记大小
For r As Integer = 0 To t.Rows.Count - 1
Chart.AxisX.SetValueLabel(r, t.Rows(r)("月份") & "月份") '指定字符表示,设置X轴数值的字符表示
Next
Chart.AxisX.AnnoWithLabels = True
结果如下图,如何将折线图前移一个数据点呢
此主题相关图片如下:微信图片_20241004114418.png