我动态数据绑定多个系列的图表。
<asp:Chart ID="Chart1" runat="server" EnableViewState="true">
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
这是页面代码,请注意启用视图状态设置为true(我也试过这个没有启用视图状态)
Public Sub PlotChart(Chart As Chart, Datasource As Object, ChartTitle As String, X_Axis As String, Y_Axis As String, Chart_Width As Integer, Chart_Height As Integer)
If Not Page.IsPostBack Then
Chart.Titles.Add(ChartTitle)
Chart.ChartAreas(0).AxisX.Interval = 1
Chart.ChartAreas(0).AxisX.TextOrientation = DataVisualization.Charting.TextOrientation.Rotated90
Chart.Legends.Add("Legend")
Chart.ChartAreas(0).AxisX.Title = X_Axis
Chart.ChartAreas(0).AxisX.TextOrientation = DataVisualization.Charting.TextOrientation.Horizontal
Chart.ChartAreas(0).AxisY.Title = Y_Axis
Chart.Width = Chart_Width
Chart.Height = Chart_Height
End If
Chart.DataSource = Datasource
Dim NewCheckBoxList As New CheckBoxList
Dim NewControl As New HtmlGenericControl("div")
Form.Controls.Add(NewControl)
Chart.Series.Clear()
For x = 0 To Datasource.Columns.Count - 2
Dim ChildControl As New HtmlGenericControl("div")
Dim NewCheckBox As New CheckBox
NewCheckBox.Text = Datasource.Columns(x + 1).Caption
NewCheckBox.ID = "chk" & Chart.ID & Datasource.Columns(x + 1).Caption
If Not Page.IsPostBack Then
NewCheckBox.Checked = True
End If
If x = 0 Or x = 2 Then
NewCheckBox.Checked = False
End If
ChildControl.Controls.Add(NewCheckBox)
NewControl.Controls.Add(ChildControl)
Chart.Series.Add(Datasource.Columns(x + 1).Caption)
Chart.Series(x).XValueMember = Datasource.Columns(0).Caption
Chart.Series(x).YValueMembers = Datasource.Columns(x + 1).Caption
Chart.Series(x).ChartType = DataVisualization.Charting.SeriesChartType.Line
Dim findCheckbox As CheckBox = FindControl("ctl00$chk" & Chart.ID & Datasource.Columns(x + 1).Caption)
If Not findCheckbox.Checked Then
Chart.Series(x).Enabled = False
End If
Next x
Chart.DataBind()
End Sub
这是我用来绑定图表的过程,这是在每次回发时运行的。
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load ' runs when the page is loaded
Dim NSPAMonthlyFiguresTable_Adapter As New NSPATableAdapters.blaSp_GetNSPAMonthlyFiguresTableAdapter
Dim NSPAMonthlyFigures_Adapter As NSPA.blaSp_GetNSPAMonthlyFiguresDataTable = NSPAMonthlyFiguresTable_Adapter.GetData()
PlotChart(Chart1, NSPAMonthlyFigures_Adapter, "NSPA Monthly Figures", "Month", "Count Of PO's", 1024, 768)
End Sub
问题:当我运行它时,它工作正常,它绘制图形,并有类似8系列的东西。但是,如果我告诉它在回发后绘制图形,图形将不会显示。
常规图表http://i.stack.imgur.com/xcGQK.png
回发后的图表http://i.stack.imgur.com/gu1Q9.png
你可以看到第二个图表确实显示了图表标题,它正在做其他事情,因为你可以看到屏幕底部的复选框的位移,它只是没有给我一个错误信息或我可以跟踪的任何内容解决这个问题。
我已经被困在这几天了,我离开了几天然后又回到了它,但我仍然无法理解这一点。我已经在谷歌和堆栈中进行了多次搜索,我找不到任何有用的信息。
本质上,我正在尝试动态绑定图表,并使用一组复选框替换图例,这些复选框可以打开和关闭系列的可见性,以便其他开发人员可以绑定图表并通过仅传入数据来实现此功能源代码而不必在屏幕上有大量的控件来操作SQL。
感谢您的时间和耐心
Dave H