图表事件
此主题相关图片如下:微信图片_20241003214152.png
Dim cht As WinForm.Chart = e.Sender
If e.SeriesIndex >= 0 AndAlso e.PointIndex >= 0 Then
Dim info As String
If ModifierKey = Keys.Control Then '如果按下了Ctrl键
Dim r As Row = Tables("图表测试数据").rows(e.PointIndex)
info = r("月份")
For Each cl As Col In Tables("图表测试数据").Cols
If cl.Name <> "年份" And cl.Name <> "月份" And cl.Name <> "品牌" And cl.Name <> "销售增长" Then
info = info & vbCrLf & cl.Name & ":" & r(cl.Name)
End If
Next
Else
Dim nm As String = cht.SeriesList(e.SeriesIndex).Y.DataField
info = "期间:" & nm
For Each r As Row In Tables("图表测试数据").Rows
info = info & vbCrLf & r("月份") & ":" & r(nm)
Next
End If
If info > "" Then
e.ToolTipText = info
End If
End If
生成图表后,鼠标悬浮图上时会跳出如下错误,不知问题在哪里
此主题相关图片如下:微信图片_20241003214916.png
Dim Chart As WinForm.Chart '定义一个图表变量
Dim Series As WinForm.ChartSeries '定义一个图系变量
Dim t As Table = Tables("图表测试数据") '定义一个变量t引用数据表
Chart = e.Form.Controls("Chart2") ' 引用窗口中的图表
Chart.ChartType = ChartTypeEnum.Bar '图表类型改为Bar(条形)
Chart.SeriesList.Clear() '清除图表原来的图系
Chart.AxisY.Min = 0 '指定Y轴的最小值
Chart.AxisY.Max = 150 '指定Y轴的最大值
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
For r As Integer = 0 To t.Rows.Count - 1
Chart.AxisX.SetValueLabel(r, t.Rows(r)("月份") & "月份") '指定字符表示,设置X轴数值的字符表示
Next
Chart.AxisX.AnnoWithLabels = True '启用字符标示
Chart.VisualEffect = True '加上这一行,让你的图表更漂亮
Chart.LegendVisible = True '显示图列
Chart.LegendCompass = CompassEnum.South '图列显示在南方(底端)
'设置数据标示
For cc As Integer = 0 To Chart.SeriesList.count - 1
Chart.SeriesList(cc).DataLabelText = "{#YVAL}" '显示数据标点的值
Chart.SeriesList(cc).DataLabelCompass = LabelCompassEnum.North '设置数据标点值的位置
Next
'清除网格线
Chart.AxisX.GridMajorVisible = False
Chart.AxisY.GridMajorVisible = False
'设置图表区域样式2(去掉全部边框,并将全部背景设置为透明)
Chart.Style.Border.BorderStyle = C1Chart.BorderStyleEnum.None
Chart.ChartAreaStyle.Border.BorderStyle = C1Chart.BorderStyleEnum.None
Chart.Style.BackColor = Color.Transparent
Chart.ChartAreaStyle.BackColor = Color.Transparent
Chart.PlotAreaStyle.BackColor = Color.Transparent
'生成图表标题
Dim fnt As New Font("黑体", 18, FontStyle.Bold) '设置图表标题字体
Chart.HeaderText = "品牌各品类销售占比" '设置图表上标题
Chart.HeaderFont = fnt
和帮助的用法不一样。上面的代码没有使用绑定的方式使用,【
cht.SeriesList(e.SeriesIndex).Y.DataField】是不会有内容的,无法使用这种方式获取对应的列