Adding data series to chart at runtime

Hello,
In the application that I am developing, I want users to select a metric and then be able to select one or more datasets that will be used populate their respective data series on the cihart.  I am only able to get one data series populated.  Below is the code I have come up with.  Thanks
Try
            MessageBox.Show(graphs.Count.ToString)
            For Each g As GraphReport In graphs
                Dim dt As New DataTable
                dt = ReportDB.GetGraphData(g.DataVersionId, g.MetricName)
                'Chart1.DataBindTable(dt)
                Chart1.DataSource = dt
                Chart1.Series.RemoveAt((Chart1.Series.Count - 1))

                Dim series As Series = Chart1.Series.Add(g.TechnologyCode.ToString & "-" & g.Budget.ToString)
                series.Name = g.TechnologyCode.ToString & "-" & g.Budget.ToString
                series.ChartArea = "Default"
                series.ChartType = SeriesChartType.Spline
                series.BorderWidth = 2

                Chart1.Series(series.Name.ToString).XValueMember = "ForecastYear"
                Chart1.Series(series.Name.ToString).YValueMembers = "MetricValue"
                Chart1.DataBind()


                strmessage = strmessage & g.DataVersionId.ToString & vbTab & g.MetricName.ToString & vbTab & g.GraphType.ToString & vbTab & g.GraphTitle.ToString & vbCrLf
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString)
        End Try    'MessageBox.Show(strmessage)

Open in new window

Juan VelasquezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
Use the same code to add the other series

Dim series As Series = Chart1.Series.Add(g.TechnologyCode.ToString & "-" & g.Budget.ToString)
                series.Name = g.TechnologyCode.ToString & "-" & g.Budget.ToString
                series.ChartArea = "Default"
                series.ChartType = SeriesChartType.Spline
                series.BorderWidth = 2

                Chart1.Series(series.Name.ToString).XValueMember = "ForecastYear"
                Chart1.Series(series.Name.ToString).YValueMembers = "MetricValue"
0
Juan VelasquezAuthor Commented:
I've done a little more debuging and in portion of the code where I am applying the filter, that will be latet used to get the selected metric names, I have found that I am always returning the first row in the list instead of the selected row.  I need to fix that first
If lstMetricName.SelectedIndex > -1 Then
            For i As Integer = 0 To lstMetricName.SelectedItems.Count - 1
                Dim dv As DataRowView = DirectCast(Me.lstMetricName.Items(i), DataRowView)
                strMetricName = dv.Row(1).ToString
            Next
        End If
0
Juan VelasquezAuthor Commented:
Hello,
I've changed the code to the example shown below, and I am now picking up the selected item,
 If lstMetricName.SelectedIndex > -1 Then
            For i As Integer = 0 To lstMetricName.SelectedItems.Count - 1
                Dim dv As DataRowView = DirectCast(Me.lstMetricName.SelectedItems(i), DataRowView)
                strMetricName = dv.Row(1).ToString
            Next
        End If

However I am getting the error below on the following line of code.  This BillByUserSelections method calls a stored procedure that populates the datasetlist view from which the user will select which datasets will be graphed
Me.CommentsTableAdapter.FillByUserSelections(Me.BADSDataSet.Comments, strMetricName, strInputCode, strTechnologySet, intSelectedYear)
 


System.Data.ConstraintException was unhandled
  Message="Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."
  Source="System.Data"
  StackTrace:
       at System.Data.DataSet.FailedEnableConstraints()
       at System.Data.DataSet.EnableConstraints()
       at System.Data.DataSet.set_EnforceConstraints(Boolean value)
       at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
       at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
       at BADS.BADSDataSetTableAdapters.CommentsTableAdapter.FillByUserSelections(CommentsDataTable dataTable, String MetricName, String InputCode, String TechnologySetCode, Nullable`1 BudgetYear) in C:\Documents and Settings\jvelasqu\My Documents\Visual Studio 2008\Projects\BADS\BADS\BADSDataSet.Designer.vb:line 16509
       at BADS.frmGraphDesigner.SetFilter() in C:\Documents and Settings\jvelasqu\My Documents\Visual Studio 2008\Projects\BADS\BADS\frmGraphDesigner.vb:line 189
       at BADS.frmGraphDesigner.btnSetFilter_Click(Object sender, EventArgs e) in C:\Documents and Settings\jvelasqu\My Documents\Visual Studio 2008\Projects\BADS\BADS\frmGraphDesigner.vb:line 212
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.RunDialog(Form form)
       at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
       at System.Windows.Forms.Form.ShowDialog()
       at BADS.frmGraph.btnAddGraph_Click(Object sender, EventArgs e) in C:\Documents and Settings\jvelasqu\My Documents\Visual Studio 2008\Projects\BADS\BADS\frmGraph.vb:line 67
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.RunDialog(Form form)
       at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
       at System.Windows.Forms.Form.ShowDialog()
       at BADS.frmReports.tvNav_AfterSelect(Object sender, TreeViewEventArgs e) in C:\Documents and Settings\jvelasqu\My Documents\Visual Studio 2008\Projects\BADS\BADS\frmReports.vb:line 59
       at System.Windows.Forms.TreeView.OnAfterSelect(TreeViewEventArgs e)
       at System.Windows.Forms.TreeView.TvnSelected(NMTREEVIEW* nmtv)
       at System.Windows.Forms.TreeView.WmNotify(Message& m)
       at System.Windows.Forms.TreeView.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
       at System.Windows.Forms.Control.WmNotify(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
       at System.Windows.Forms.Control.DefWndProc(Message& m)
       at System.Windows.Forms.TreeView.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.TreeView.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.RunDialog(Form form)
       at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
       at System.Windows.Forms.Form.ShowDialog()
       at BADS.frmStartUp.btnReports_Click(Object sender, EventArgs e) in C:\Documents and Settings\jvelasqu\My Documents\Visual Studio 2008\Projects\BADS\BADS\frmStartup.vb:line 17
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at BADS.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
       at System.Activator.CreateInstance(ActivationContext activationContext)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Juan VelasquezAuthor Commented:
I added the following code
 Me.BADSDataSet.EnforceConstraints = False before the fill method and I got rid of the error.  I'm still having trouble to get the graph to show more than one data series
0
Juan VelasquezAuthor Commented:
Below is my latest attached code.  The chart still displays only on data series at a time

        Try
            MessageBox.Show(graphs.Count.ToString)
            For Each g As GraphReport In graphs
                Dim dt As New DataTable
                dt = ReportDB.GetGraphData(g.DataVersionId, g.MetricName)
                'Chart1.DataBindTable(dt)
                Chart1.DataSource = dt
                'Chart1.Series.RemoveAt((Chart1.Series.Count - 1))
                Dim strSeriesName As String = g.TechnologyCode.ToString & "-" & g.Budget.ToString
                Dim series As Series = Chart1.Series.Add(strSeriesName)
                series.Name = ""
                series.Name = strSeriesName
                series.ChartArea = "Default"
                series.ChartType = SeriesChartType.Spline
                series.BorderWidth = 2
                'Dim strSeriesName As String = series.Name.ToString
                Chart1.Series(strSeriesName).XValueMember = "ForecastYear"
                Chart1.Series(strSeriesName).YValueMembers = "MetricValue"
                Chart1.DataBind()

                'strmessage = strmessage & g.DataVersionId.ToString & vbTab & g.MetricName.ToString & vbTab & g.GraphType.ToString & vbTab & g.GraphTitle.ToString & vbCrLf
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString)
            'Chart1.Series.RemoveAt((Chart1.Series.Count - 1))
        End Try    'MessageBox.Show(strmessage)

Open in new window

0
CodeCruiserCommented:
If you set the dataset as a datasource to a grid, do you see the values in dataset as expected?
0
Juan VelasquezAuthor Commented:
Yes I do.  
0
Juan VelasquezAuthor Commented:
There must be a way for me to add more than one dataseries to the chart at runtime. I'm using
For Each g As GraphReport In graphs to loop through the returned datasetversionid and metricname and add them as a new data series to the chart
0
CodeCruiserCommented:
What does

MessageBox.Show(graphs.Count.ToString)

show you?
0
Juan VelasquezAuthor Commented:
It shows the number of datasets I selected.  For example, if I selected three datasets, the messagebox shows 3
0
CodeCruiserCommented:
I think you need to move this portion outside of loop

                Dim dt As New DataTable
                dt = ReportDB.GetGraphData(g.DataVersionId, g.MetricName)
                'Chart1.DataBindTable(dt)
                Chart1.DataSource = dt


and then leave the code which adds series to the chart inside the loop.
0
Juan VelasquezAuthor Commented:
I don't think that will work as g is then now longer declared.  I've created a class called graphreports I create an instance of each class containged the selected dataversiond, and metric name. I then store each instance in a list.  Later on I iterate through the list in order to use the value as parameters to the stored procedure in ReportDB.GetGraphData.  
0
CodeCruiserCommented:
But you can only set the datasource once. So everytime in the loop

Chart1.DataSource = dt

you replace the previously set datasource!
0
Juan VelasquezAuthor Commented:
Basically yes, since each datasource represents a different datataversionid that the user selected from the dataversionid listbox- the user can make multiple selections with each selection being a different data series.  Oh I see, I was changing the datasource of the chart.  I needed to change the data source of the series instead.  Is that where I made my error?
0
CodeCruiserCommented:
I think that is the problem.
0
Juan VelasquezAuthor Commented:
To be honest I'm not sure how to go about solving this problem.  Well let's looks at the problem in steps.
I need to set the datasource of the chart to something that corresponds to the names of the metrics I want to collect.  I then have to add the dataseries that are created from the data retreived by ReportDB.GetGraphData.   which should realy be renamed ReportDB.GetSeriesData
0
Juan VelasquezAuthor Commented:
I am going to place the following code outside the loop.  I have renamed  ReportDB.GetGraphData to  ReportDB.GetSeriesData and have left it in the loop.  I am going to modify GetGraphData(g.DataVersionId, g.MetricName) to GetGraphData(MetricName)


 Dim dt As New DataTable
                dt = ReportDB.GetGraphData(g.DataVersionId, g.MetricName)
                'Chart1.DataBindTable(dt)
                Chart1.DataSource = dt
0
Juan VelasquezAuthor Commented:
No I don't think that will work.
0
Juan VelasquezAuthor Commented:
One of the reasons that it won't work is that I would end up, after a while needing to retrieve hundreds of thouands of records across the network.
0
CodeCruiserCommented:
Why would it be that many rows?
0
Juan VelasquezAuthor Commented:
Actually its only about 54 rows.  My mistake. I've been at this so long, I started to go in circles.  Below is the code at present.  As you can see, I am using dg as the data source for the chart outside of the loop.  Dim dg As New DataTable
 dg = ReportDB.GetGraphData(Me.lstMetricName.SelectedValue.ToString)
        MessageBox.Show(dg.Rows.Count.ToString)
        Chart1.DataSource = dg

Now within the loop, I need to set series data source to ds = ReportDB.GetSeriesData(g.DataVersionId, g.MetricName) which returns a subset of the previous records as defined by g.DataVersionId,

Private Sub btnPreviewGraph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreviewGraph.Click

        Dim graphs As New List(Of GraphReport)
        For i As Integer = 0 To lstDataVersionIDListBox.SelectedItems.Count - 1
            Dim objgraph As New GraphReport
            
            If rdLine.Checked Then
                objgraph.GraphType = GraphType.Line
            ElseIf rdBar.Checked Then
                objgraph.GraphType = GraphType.column
            ElseIf rdStacked.Checked Then
                objgraph.GraphType = GraphType.Stacked
            Else
                MessageBox.Show("You must make a selection")
                Exit Sub
            End If

            Dim dv As DataRowView = DirectCast(Me.lstDataVersionIDListBox.SelectedItems(i), DataRowView)
            objgraph.DataVersionId = dv.Row(0).ToString
            objgraph.ParseDVID(objgraph.DataVersionId.ToString, "-")

            For j As Integer = 0 To lstMetricName.SelectedItems.Count - 1
                Dim mn As DataRowView = DirectCast(Me.lstMetricName.SelectedItems(j), DataRowView)
                objgraph.MetricName = mn.Row(1).ToString

            Next

            objgraph.GraphTitle = txtGraphTitle.Text.ToString

            graphs.Add(objgraph)
            Dim strmessage As String = ""
            strmessage = Me.lstMetricName.SelectedValue.ToString
            MessageBox.Show(strmessage)

        Next

        Dim dg As New DataTable
        dg = ReportDB.GetGraphData(Me.lstMetricName.SelectedValue.ToString)
        MessageBox.Show(dg.Rows.Count.ToString)
        Chart1.DataSource = dg
        'Chart1.DataBindCrossTable()

        Try
            MessageBox.Show(graphs.Count.ToString)

            For Each g As GraphReport In graphs

                Dim ds As New DataTable
                ds = ReportDB.GetSeriesData(g.DataVersionId, g.MetricName)

                'Chart1.DataSource = ds
                'dgv.DataSource = ds


                Dim strSeriesName As String = g.TechnologyCode.ToString & "-" & g.Budget.ToString

                Dim series As Series
                series = Chart1.Series.Add(strSeriesName)

                series.Name = ""
                series.Name = strSeriesName
                series.ChartArea = "Default"

                If g.GraphType = 0 Then
                    series.ChartType = SeriesChartType.Spline
                ElseIf g.GraphType = 1 Then
                    series.ChartType = SeriesChartType.Column
                ElseIf g.GraphType = 2 Then
                    series.ChartType = SeriesChartType.StackedArea
                End If
                series.BorderWidth = 2
                'Dim strSeriesName As String = series.Name.ToString

                Chart1.Series(strSeriesName).XValueMember = "ForecastYear"
                Chart1.Series(strSeriesName).YValueMembers = "MetricValue"



                'Chart1.ChartAreas.Add(series(strSeriesName))
                g.GraphTitle = txtGraphTitle.Text.ToString
                Chart1.DataBind()

                'strmessage = strmessage & g.DataVersionId.ToString & vbTab & g.MetricName.ToString & vbTab & g.GraphType.ToString & vbTab & g.GraphTitle.ToString & vbCrLf
            Next

        Catch ex As Exception
            'MessageBox.Show(ex.Message.ToString)
            Chart1.Series.RemoveAt((Chart1.Series.Count - 1))
        End Try    'MessageBox.Show(strmessage)

    End Sub

Open in new window

0
CodeCruiserCommented:
0
Juan VelasquezAuthor Commented:
Well when I entered the following, I would see the values in the data series duplicated in both data series

                Chart1.Series(strSeriesName).XValueMember = ds.Columns(2).ToString
                Chart1.Series(strSeriesName).YValueMembers = ds.Columns(4).ToString
0
Juan VelasquezAuthor Commented:
I now get the following result when running the attached  code.  As you can see I get both series but they have the same values
 Private Sub btnPreviewGraph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreviewGraph.Click

        Dim graphs As New List(Of GraphReport)
        For i As Integer = 0 To lstDataVersionIDListBox.SelectedItems.Count - 1
            Dim objgraph As New GraphReport
            
            If rdLine.Checked Then
                objgraph.GraphType = GraphType.Line
            ElseIf rdBar.Checked Then
                objgraph.GraphType = GraphType.column
            ElseIf rdStacked.Checked Then
                objgraph.GraphType = GraphType.Stacked
            Else
                MessageBox.Show("You must make a selection")
                Exit Sub
            End If

            Dim dv As DataRowView = DirectCast(Me.lstDataVersionIDListBox.SelectedItems(i), DataRowView)
            objgraph.DataVersionId = dv.Row(0).ToString
            objgraph.ParseDVID(objgraph.DataVersionId.ToString, "-")

            For j As Integer = 0 To lstMetricName.SelectedItems.Count - 1
                Dim mn As DataRowView = DirectCast(Me.lstMetricName.SelectedItems(j), DataRowView)
                objgraph.MetricName = mn.Row(1).ToString

            Next

            objgraph.GraphTitle = txtGraphTitle.Text.ToString

            graphs.Add(objgraph)
            Dim strmessage As String = ""
            strmessage = Me.lstMetricName.SelectedValue.ToString
            'MessageBox.Show(strmessage)

        Next

        Dim dg As New DataTable
        dg = ReportDB.GetGraphData(Me.lstMetricName.SelectedValue.ToString)
        'MessageBox.Show(dg.Rows.Count.ToString)
        Chart1.DataSource = dg
        'Chart1.DataBindCrossTable()

        Try
            'MessageBox.Show(graphs.Count.ToString)

            For Each g As GraphReport In graphs

                Dim ds As New DataTable
                ds = ReportDB.GetSeriesData(g.DataVersionId, g.MetricName)
                'MessageBox.Show(ds.Rows.Count.ToString)
                'Chart1.DataSource = ds
                'dgv.DataSource = ds


                Dim strSeriesName As String = g.TechnologyCode.ToString & "-" & g.Budget.ToString

                Dim series As Series
                series = Chart1.Series.Add(strSeriesName)

                'series.Name = ""
                series.Name = strSeriesName
                series.ChartArea = "Default"

                If g.GraphType = 0 Then
                    series.ChartType = SeriesChartType.Spline
                ElseIf g.GraphType = 1 Then
                    series.ChartType = SeriesChartType.Column
                ElseIf g.GraphType = 2 Then
                    series.ChartType = SeriesChartType.StackedArea
                End If
                series.BorderWidth = 2
                'Dim strSeriesName As String = series.Name.ToString

                For r As Integer = 0 To ds.Rows.Count - 1
                    Chart1.Series(strSeriesName).XValueMember = ds.Columns(2).ToString
                    Chart1.Series(strSeriesName).YValueMembers = ds.Columns(4).ToString
                Next




                'Chart1.ChartAreas.Add(series(strSeriesName))
                g.GraphTitle = txtGraphTitle.Text.ToString
                Chart1.DataBind()

                'strmessage = strmessage & g.DataVersionId.ToString & vbTab & g.MetricName.ToString & vbTab & g.GraphType.ToString & vbTab & g.GraphTitle.ToString & vbCrLf
            Next

        Catch ex As Exception
            'MessageBox.Show(ex.Message.ToString)
            Chart1.Series.RemoveAt((Chart1.Series.Count - 1))
        End Try    'MessageBox.Show(strmessage)

    End Sub

Open in new window

Chart.bmp
0
CodeCruiserCommented:
Why have you this code in a loop?

For r As Integer = 0 To ds.Rows.Count - 1
                    Chart1.Series(strSeriesName).XValueMember = ds.Columns(2).ToString
                    Chart1.Series(strSeriesName).YValueMembers = ds.Columns(4).ToString
                Next

I think you dont need a loop there. Also, this code is in a loop(the for each g loop) and you are running the same code for both series so obviously the series will be same.
0
Juan VelasquezAuthor Commented:
Actually, I have already removed that loop and retried the code with the same result
0
Juan VelasquezAuthor Commented:
Each time around in the loop the g.DataVersionId is a different value. while g.MetricName remains the same.  I've confirmed this using  
MessageBox.Show(g.DataVersionId.ToString & "   -  " & g.MetricName.ToString)
right after the loop starts
0
Juan VelasquezAuthor Commented:
I think I understand what you are saying.  In each iteration of the for each loop both data series are being populated by the same datatable.  Even though the contents of the datatable changes at every iteration, that same datatable is being added to the same two series.  I had thought that a new series is created and added to chart via
Dim strSeriesName As String = g.TechnologyCode.ToString & "-" & g.Budget.ToString
 Dim series As Series
series = Chart1.Series.Add(strSeriesName)

Am I reading that correctly.
0
CodeCruiserCommented:
That is my thinking. First, remove the loop and try with two series using two separate datatables.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Juan VelasquezAuthor Commented:
Well I did as your recommended.  I created two series using two differenct datatables.  I then commented out each secetion and looked at the resulting datagrid and charts.  I found tihat while the datagrid data would change the graphs would not changed.  It seemed stuck
Try


            Dim g As GraphReport
            g = graphs(0)
            Dim ds As New DataTable
            ds = ReportDB.GetSeriesData(g.DataVersionId, g.MetricName)
            MessageBox.Show(g.DataVersionId.ToString & "   -  " & g.MetricName.ToString)
            dgv.DataSource = ds
            Dim strSeriesName As String = g.TechnologyCode.ToString & "-" & g.Budget.ToString
            Dim series As Series = Chart1.Series.Add(strSeriesName)
            series.ChartArea = "Default"
            series.BorderWidth = 2
            Chart1.Series(strSeriesName).XValueMember = ds.Columns(2).ToString
            Chart1.Series(strSeriesName).YValueMembers = ds.Columns(4).ToString

            If g.GraphType = 0 Then
                series.ChartType = SeriesChartType.Spline
            ElseIf g.GraphType = 1 Then
                series.ChartType = SeriesChartType.Column
            ElseIf g.GraphType = 2 Then
                series.ChartType = SeriesChartType.StackedArea
            End If
            series.BorderWidth = 2

            Chart1.DataBind()

            '**************Second Series
            'Dim g1 As GraphReport
            'g1 = graphs(1)
            'Dim ds1 As New DataTable
            'ds1 = ReportDB.GetSeriesData(g1.DataVersionId, g1.MetricName)
            'MessageBox.Show(g1.DataVersionId.ToString & "   -  " & g1.MetricName.ToString)
            'dgv.DataSource = ds1
            'Dim strSeriesName1 As String = g1.TechnologyCode.ToString & "-" & g1.Budget.ToString
            'Dim series1 As Series = Chart1.Series.Add(strSeriesName1)
            'series1.ChartArea = "Default"
            'series1.BorderWidth = 2
            'Chart1.Series(strSeriesName1).XValueMember = ds1.Columns(2).ToString
            'Chart1.Series(strSeriesName1).YValueMembers = ds1.Columns(4).ToString

            'If g1.GraphType = 0 Then
            '    series1.ChartType = SeriesChartType.Spline
            'ElseIf g1.GraphType = 1 Then
            '    series1.ChartType = SeriesChartType.Column
            'ElseIf g1.GraphType = 2 Then
            '    series1.ChartType = SeriesChartType.StackedArea
            'End If
            'series1.BorderWidth = 2

            'g1.GraphTitle = txtGraphTitle.Text.ToString
            'Chart1.DataBind()

            'Next

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString)
            'Chart1.Series.RemoveAt((Chart1.Series.Count - 1))
        End Try    'MessageBox.Show(strmessage)

Open in new window

0
CodeCruiserCommented:
That is very strange and would require to develop a demo project. I am afraid I am not in a position to do that right now given the development burdon I have. I hope someone comes to the rescue.
0
Juan VelasquezAuthor Commented:
Thanks for your help.  
0
Juan VelasquezAuthor Commented:
Hello CodeCruiser,
I finally found the solution.  Basically I created two arraylists, one to hold the x-axis data and another to hold the y-axis data.  I then used
Chart1.Series(strSeriesName).Points.DataBindXY(xval, yval)

to populate each data series.  It works great and users can select multiple datasets at a time and assign a specific chart type to the whole group or add them one-by one and asign a different chart type to each data series
0
CodeCruiserCommented:
Great. Finally you cracked it :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.