help with applying a filter to a dataview for chart data grouping

Status
Not open for further replies.

bones

Well-known member
Joined
Aug 23, 2014
Messages
143
Programming Experience
Beginner
Having a go at this for a while not and gaining ground....just can't seem to bring it home to what I need it to be.

Scenario:

In memory datatable.
I have coded a new dataset and added the datatable to it.
I created a dataview and coded it to filter the data.

What I need to do is filter the data, then chart a series, then change the filter, then chart the next series. I can't get that sorted out.

To prove the filtering is working I added several other dgv's to the form and use the filtered table results as their data source. The data is being filtered correctly in each of the dgvs.

What's not working is the charting portion. As I add each new filter for the next series, only the last filtered data is actually getting drawn to the chart.

Here's that part of my code. It's in a checkbox checkchanged sub. I have several more to do. If I can get this filtering working then the rest will all fall into place without assistance.

VB.NET:
'**********************************Captured Data Chart Setup************************************************
        'load datagridview1 with data
        DataGridView1.DataSource = SourceTable


        If CheckBox1.Checked = True Then


            With Me.Chart1.ChartAreas(0)
                Chart1.DataSource = SourceTable
                .AxisX.Interval = 0.1
                .AxisY.Interval = 15
                .AxisX.IsLabelAutoFit = True
                .AxisX.LabelAutoFitStyle = DataVisualization.Charting.LabelAutoFitStyles.DecreaseFont
                .AxisY.LabelAutoFitStyle = DataVisualization.Charting.LabelAutoFitStyles.DecreaseFont
                .AxisX.LabelStyle.Angle = -45
                .AxisX.Title = "Lift"
                .AxisY.Title = "CFM"


            End With


            'build a new dataset
            Dim andata = New DataSet("andata")
            andata.Tables.Add(SourceTable)


            'create and filter dataview
            Dim dv As DataView
            dv = New DataView(andata.Tables(0), "port = 'port 1' ", "port", DataViewRowState.CurrentRows)
            DataGridView2.DataSource = dv


            Chart1.DataSource = DataGridView2
            Chart1.Series(0).Points.Clear()
            For Count As Integer = 0 To Me.DataGridView2.Rows.Count - 2
                Chart1.Series(1).Points.AddXY(DataGridView2.Item(0, Count).Value, DataGridView2.Item(2, Count).Value)
            Next


            dv = New DataView(andata.Tables(0), "port = 'port 2' ", "port", DataViewRowState.CurrentRows)
            DataGridView3.DataSource = dv


            Chart1.DataSource = DataGridView3
            Chart1.Series(1).Points.Clear()
            For Count As Integer = 0 To Me.DataGridView3.Rows.Count - 2
                Chart1.Series(1).Points.AddXY(DataGridView3.Item(0, Count).Value, DataGridView3.Item(2, Count).Value)
            Next

Pic to show the filtered data in the DGV's is correct

New Picture (52).jpg
 
You're clearing the chart series each time. Surely you have to add new ones. I would think that you'd create multiple DataViews, or simply call Select on the DataTable multiple times, and then add a new series to the chart for each one.
 
You're clearing the chart series each time. Surely you have to add new ones. I would think that you'd create multiple DataViews, or simply call Select on the DataTable multiple times, and then add a new series to the chart for each one.

I fixed it......Found a typo in my code..... was charting series 1 twice....Can't believe I didn't see that before....BUT THANK YOU for your time and offerings....much appreciated.
 
Last edited:
The charting works great now but I have another issue that I don't know how to address.

If I uncheck the checkbox I get this error

System.ArgumentException was unhandled HResult=-2147024809
Message=DataTable already belongs to another DataSet.
Source=System.Data

The debugger points to this line andata.Tables.Add(SourceTable)

My code for the unchecked state of the checkbox in the if statement is this

VB.NET:
 If CheckBox1.Checked = False Then
            Me.Chart1.Series(0).Points.Clear()
            Me.Chart1.Series(1).Points.Clear()
            Me.Chart1.Series(2).Points.Clear()
            Me.Chart1.Series(3).Points.Clear()
            Me.Chart1.Series(4).Points.Clear()
            Me.Chart1.Series(5).Points.Clear()


            Exit Sub

Do I need an If statement here that handles the situation if the table is already added....go to another point in the code?
 
The charting works great now but I have another issue that I don't know how to address.

If I uncheck the checkbox I get this error

System.ArgumentException was unhandled HResult=-2147024809
Message=DataTable already belongs to another DataSet.
Source=System.Data

The debugger points to this line andata.Tables.Add(SourceTable)

My code for the unchecked state of the checkbox in the if statement is this

VB.NET:
 If CheckBox1.Checked = False Then
            Me.Chart1.Series(0).Points.Clear()
            Me.Chart1.Series(1).Points.Clear()
            Me.Chart1.Series(2).Points.Clear()
            Me.Chart1.Series(3).Points.Clear()
            Me.Chart1.Series(4).Points.Clear()
            Me.Chart1.Series(5).Points.Clear()


            Exit Sub

Do I need an If statement here that handles the situation if the table is already added....go to another point in the code?

If the charting is working then the current issue is resolved, so this thread should be marked as answered. If you have another question then you should start a new thread and explain the new issue. Otherwise, you make it hard to work out what's currently at issue and what information is relevant.

That said, what's the DataSet for at all? Just get rid of it.
 
If the charting is working then the current issue is resolved, so this thread should be marked as answered. If you have another question then you should start a new thread and explain the new issue. Otherwise, you make it hard to work out what's currently at issue and what information is relevant.

That said, what's the DataSet for at all? Just get rid of it.

Thank you... this code fixed the problem

VB.NET:
Do While (andata.Tables.Count > 0)
                Dim table As DataTable = andata.Tables(0)
                If (andata.Tables.CanRemove(table)) Then
                    andata.Tables.Remove(table)
                End If
            Loop
 
Everything is sorted out now. There's one more thing I'm trying to find information / sample code to accomplish. I'd like to build in an option for the end user to be able to select line colors of their choice. Is there something out there regarding that?

I did find this Manipulating the Chart Appearance but I don't know if it will apply to datavisualization charts.
 
Last edited:
Everything is sorted out now. There's one more thing I'm trying to find information / sample code to accomplish. I'd like to build in an option for the end user to be able to select line colors of their choice. Is there something out there regarding that?

I did find this Manipulating the Chart Appearance but I don't know if it will apply to datavisualization charts.

You must have missed the part where I said that you should start a new thread to ask a new question.
 
Status
Not open for further replies.

Latest posts

Back
Top