[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Adding data series to chart at runtime

Posted on 2010-04-05
34
Medium Priority
?
1,012 Views
Last Modified: 2012-05-09
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

0
Comment
Question by:chtullu135
  • 22
  • 12
34 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 29815901
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
 

Author Comment

by:chtullu135
ID: 29825121
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
 

Author Comment

by:chtullu135
ID: 29826061
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:chtullu135
ID: 29827640
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
 

Author Comment

by:chtullu135
ID: 29834043
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 29834094
If you set the dataset as a datasource to a grid, do you see the values in dataset as expected?
0
 

Author Comment

by:chtullu135
ID: 29835191
Yes I do.  
0
 

Author Comment

by:chtullu135
ID: 29835394
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 29885111
What does

MessageBox.Show(graphs.Count.ToString)

show you?
0
 

Author Comment

by:chtullu135
ID: 29906079
It shows the number of datasets I selected.  For example, if I selected three datasets, the messagebox shows 3
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 29921109
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
 

Author Comment

by:chtullu135
ID: 29923762
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 29924412
But you can only set the datasource once. So everytime in the loop

Chart1.DataSource = dt

you replace the previously set datasource!
0
 

Author Comment

by:chtullu135
ID: 29925178
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 29925248
I think that is the problem.
0
 

Author Comment

by:chtullu135
ID: 29925891
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
 

Author Comment

by:chtullu135
ID: 29928971
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
 

Author Comment

by:chtullu135
ID: 29929152
No I don't think that will work.
0
 

Author Comment

by:chtullu135
ID: 29934791
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 29948584
Why would it be that many rows?
0
 

Author Comment

by:chtullu135
ID: 29949521
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 29949866
0
 

Author Comment

by:chtullu135
ID: 29952695
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
 

Author Comment

by:chtullu135
ID: 29953683
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 29953999
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
 

Author Comment

by:chtullu135
ID: 29954219
Actually, I have already removed that loop and retried the code with the same result
0
 

Author Comment

by:chtullu135
ID: 29954678
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
 

Author Comment

by:chtullu135
ID: 30021096
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 30022746
That is my thinking. First, remove the loop and try with two series using two separate datatables.
0
 

Author Comment

by:chtullu135
ID: 30031877
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 30032148
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
 

Author Closing Comment

by:chtullu135
ID: 31711011
Thanks for your help.  
0
 

Author Comment

by:chtullu135
ID: 30053539
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 30054828
Great. Finally you cracked it :-)
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Jorge
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question