Results 1 to 11 of 11

Thread: How to export chart data to a .txt/.csv?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    10

    How to export chart data to a .txt/.csv?

    Hello there (again),

    Now that I've got the voltmeter working very well, and the graphs updating as they should, I've got one other question for you. I'm trying to export the x/y values I get from the voltmeter and export them to either a txt or csv file.

    Code:
     Private Sub VoltTimer_Tick(sender As System.Object, e As System.EventArgs) Handles VoltTimer.Tick
            'monitors both FID and TCD at the same time
            If TCDCheckBox.Checked And FIDCheckBox.Checked Then
                'creates x-coordinate for the T,V plot
                Runtime = Runtime + 1
                'Changes channel to TCD, reads voltage, puts it on the graph, and displays it in the textbox.
                myDevice.Write(":ROUT:CLOS (@" & 1 & ")")
                myDevice.Write("READ?")
                Dim TCDVoltage = myDevice.ReadString()
                TCDChart.Series("Series1").Points.AddXY((Runtime / 60), TCDVoltage)
                TCDVoltageDisplay.Text = TCDVoltage * 1000
                'switches channel to FID, does the same as for the TCD
                myDevice.Write(":ROUT:CLOS (@" & 2 & ")")
                myDevice.Write("READ?")
                Dim FIDVoltage = myDevice.ReadString()
                FIDChart.Series("Series1").Points.AddXY((Runtime / 60), FIDVoltage)
                FIDVoltageDisplay.Text = FIDVoltage * 1000
    
                'Monitors TCD only
            ElseIf TCDCheckBox.Checked Then
                Runtime = Runtime + 1
                myDevice.Write("READ?")
                Dim TCDVoltage = myDevice.ReadString()
                TCDChart.Series("Series1").Points.AddXY((Runtime / 60), TCDVoltage)
                TCDVoltageDisplay.Text = TCDVoltage * 1000
    
    
                'Monitors FID only
            ElseIf FIDCheckBox.Checked Then
                Runtime = Runtime + 1
                myDevice.Write(":READ?")
                Dim FIDVoltage = myDevice.ReadString()
                FIDChart.Series("Series1").Points.AddXY((Runtime / 60), FIDVoltage)
                FIDVoltageDisplay.Text = FIDVoltage * 1000
    
            End If
    
        End Sub
    This is what essentially runs my program.

    Where do the X,Y values used in creating the chart go? If I could find those I could rather easily export the results as a .txt file, in the format

    x1,y1
    x2,y2
    x3,y3

    you get the idea.

    Thanks, NSEasternShoreChemist.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: How to export chart data to a .txt/.csv?

    Well,you're adding those points with AddXY, so at the same time, how about storing them somewhere else? For example, you could make up a datatable with two columns, and every time you call AddXY, you can also add a row to the datatable. This would also make exporting the data SUPER easy, as you could just use the WriteXML method of the datatable to write them out to an XML file. That isn't quite the same as a CSV, of course, but it would probably work about as well. XML is just text with some tags to format it. I'd expect that Excel could read it about as easily as a CSV, too, and other text editors like NotePad certainly can. Also, you could read the data back into the datatable with ReadXML, if that mattered.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    10

    Re: How to export chart data to a .txt/.csv?

    Well yes, being able to reopen old runs would be kind of nice...

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    10

    Re: How to export chart data to a .txt/.csv?

    Trouble is, I can't figure out how to create a data table. What are the commands for creating a basic data table with three columns? One for runtime, the other for the TCD voltage, another for FID voltage?

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    10

    Re: How to export chart data to a .txt/.csv?

    OK. Here's a table, which is generated when the StopRun button is clicked (just for convenience of testing)

    Code:
    Private Sub StopRun_Click(sender As System.Object, e As System.EventArgs) Handles StopRun.Click
            Dim dtRun As New DataTable
            dtRun.Columns.Add("Runtime2", GetType(String))
            dtRun.Columns.Add("TCDVoltage2", GetType(String))
            dtRun.Columns.Add("FIDVoltage2", GetType(String))
            ' simulate adding rows
            dtRun.Rows.Add(New Object() _
            {"hello", "what's", "this"})
            DataGridView1.DataSource = dtRun
            VoltTimer.Stop()
        End Sub
    This works fine to create a table and add entries to it. But trouble is that in order for this to work as is the table would have to be in the same sub as the voltmeter tick sub... doesn't work well because that would result in the table being re-defined every second. What I'd like to do is define the table by clicking on the "start run" button, then have the voltmeter tick program (in the original post) add new data to the table every time that elapses.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: How to export chart data to a .txt/.csv?

    Right, so rather than creating the datatable as a local variable, create it at form scope. Have a method that creates the table, which probably has to happen REALLY early on for it to be useful. If you are going to read the data back from a file, you don't have to do much to create the table, because it will get the schema from the file it reads.

    So, if you are going to read a file, then read a file. If you are not going to start by reading a file, then call a method that creates a datatable just as you have shown, but put the datatable at form scope (not in any method). You might call the method to create the table in the form constructor, or at the beginning of the Load event handler. The table will then be available for you to add rows to it wherever in the form, and whenever, you want.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    10

    Re: How to export chart data to a .txt/.csv?

    OK. The voltages are being exported to the table correctly.

    I've got the following code for saving things to XML. Now how do I choose the filepath for this?
    Code:
     Private Sub SaveRunToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SaveRunToolStripMenuItem.Click
            dtRun.WriteXml("Z:\test.xml")
        End Sub
    It is giving an error, namely DataTable name is not set.
    Last edited by NSEasternShoreChem; Oct 4th, 2017 at 09:09 AM.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: How to export chart data to a .txt/.csv?

    Where's that error showing up? Shouldn't be on the line shown, because there isn't a datatable name necessary in that line.

    What you have shown is WriteXML with a hard coded path. If you want to let the user select a path, then you'd need to use the SaveFileDialog (or you could write your own, but that would be MUCH harder). There are plenty of options with SaveFileDialog. A simple tutorial might be this one:

    https://www.tutorialspoint.com/vb.ne...ile_dialog.htm

    though this one has more details:

    https://docs.microsoft.com/en-us/dot...alog-component
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    10

    Re: How to export chart data to a .txt/.csv?

    I've used the save filedialog in the past, and have one in my program. This was just for testing purposes.

    Yes, the error is showing up on that particular line. The writexml line.

    Here's all the program code.

    Code:
    Imports System.Windows.Forms.DataVisualization.Charting
    Imports System.IO
    
    
    Public Class GCProgram
        'chromatogram run time definition
        Dim Runtime
        'defines datatable
        Dim dtRun As DataTable
        'GBPB address of multimeter
        Private myDevice As New Device(0, 4)
    
        'event is raised after measurements have been taken
        Public Event readComplete()
    
        Dim writer As New System.IO.StringWriter
    
        'This is an Easter Egg
        Private Sub WhatsThisToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles WhatsThisToolStripMenuItem.Click
            MessageBox.Show("La la la! (Jeff told me to make the GC sing, so here you go!)", "GC's song")
        End Sub
    
    
        Private Sub NewRunToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles NewRunToolStripMenuItem.Click
            'configuring the device and open all channels (none are connected)
            myDevice.Write("CONF:VOLT:DC")
            myDevice.Write(":ROUTe:MULT:CLOS (@ 11)")
            'Clear out the old chart data; re-define new series
            FIDChart.Series.Clear()
            TCDChart.Series.Clear()
            FIDVoltageDisplay.Clear()
            TCDVoltageDisplay.Clear()
            FIDChart.Series.Add("Series1")
            FIDChart.Series("Series1").ChartType = SeriesChartType.Line
            TCDChart.Series.Add("Series1")
            TCDChart.Series("Series1").ChartType = SeriesChartType.Line
            Runtime = 0
            'clears out the data table
            dtRun.Clear()
        End Sub
    
        Private Sub StartRun_Click(sender As System.Object, e As System.EventArgs) Handles StartRun.Click
            'starts the main voltage timer that drives the entire program.
            VoltTimer.Start()
            'Channel 1 on the Keithley is for the TCD, Channel 2 for the FID
            If FIDCheckBox.Checked Then
                myDevice.Write(":ROUT:CLOS (@" & 2 & ")")
    
            ElseIf TCDCheckBox.Checked Then
                myDevice.Write(":ROUT:CLOS (@" & 1 & ")")
            End If
        End Sub
    
        Public Sub GCProgram_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
            'configuring the device and open all channels (none are connected)
            myDevice.Write("CONF:VOLT:DC")
            myDevice.Write(":ROUTe:MULT:CLOS (@ 11)")
            dtRun = New DataTable
            dtRun.Columns.Add("Runtime", GetType(String))
            dtRun.Columns.Add("TCD Voltage", GetType(String))
            dtRun.Columns.Add("FID Voltage", GetType(String))
    
        End Sub
    
        Private Sub StopRun_Click(sender As System.Object, e As System.EventArgs) Handles StopRun.Click
            VoltTimer.Stop()
        End Sub
    
        Private Sub VoltTimer_Tick(sender As System.Object, e As System.EventArgs) Handles VoltTimer.Tick
            'monitors both FID and TCD at the same time
            If TCDCheckBox.Checked And FIDCheckBox.Checked Then
                'creates x-coordinate for the T,V plot
                Runtime = Runtime + 0.2
                'Changes channel to TCD, reads voltage, puts it on the graph, and displays it in the textbox.
                myDevice.Write(":ROUT:CLOS (@" & 1 & ")")
                myDevice.Write("READ?")
                Dim TCDVoltage = myDevice.ReadString()
                TCDChart.Series("Series1").Points.AddXY((Runtime / 60), TCDVoltage)
                TCDVoltageDisplay.Text = TCDVoltage * 1000
                'switches channel to FID, does the same as for the TCD
                myDevice.Write(":ROUT:CLOS (@" & 2 & ")")
                myDevice.Write("READ?")
                Dim FIDVoltage = myDevice.ReadString()
                FIDChart.Series("Series1").Points.AddXY((Runtime / 60), FIDVoltage)
                FIDVoltageDisplay.Text = FIDVoltage * 1000
                dtRun.Rows.Add(New Object() _
            {Runtime, TCDVoltage, FIDVoltage})
    
    
                'Monitors TCD only
            ElseIf TCDCheckBox.Checked Then
                Runtime = Runtime + 0.2
                myDevice.Write("READ?")
                Dim TCDVoltage = myDevice.ReadString()
                TCDChart.Series("Series1").Points.AddXY((Runtime / 60), TCDVoltage)
                TCDVoltageDisplay.Text = TCDVoltage * 1000
                dtRun.Rows.Add(New Object() _
            {Runtime, TCDVoltage, "0"})
    
    
                'Monitors FID only
            ElseIf FIDCheckBox.Checked Then
                Runtime = Runtime + 0.2
                myDevice.Write(":READ?")
                Dim FIDVoltage = myDevice.ReadString()
                FIDChart.Series("Series1").Points.AddXY((Runtime / 60), FIDVoltage)
                FIDVoltageDisplay.Text = FIDVoltage * 1000
                dtRun.Rows.Add(New Object() _
            {Runtime, "0", FIDVoltage})
    
    
            End If
    
        End Sub
    
        Private Sub SaveRunAsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SaveRunAsToolStripMenuItem.Click
    
        End Sub
    
        Private Sub SaveRunToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SaveRunToolStripMenuItem.Click
            dtRun.WriteXml("Z:\test.xml")
        End Sub
    
        Private Sub OpenRunToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OpenRunToolStripMenuItem.Click
            dtRun.ReadXml("Z:\test.xml")
        End Sub
    End Class

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: How to export chart data to a .txt/.csv?

    I'm not seeing anything in that code that should give that error. It's all pretty simple, and I've done this kind of thing several times. The only thing that is different from what I've done and what you've done is about adding that object array to the table. What I've done is more laborious:
    Code:
    Dim dr = dtRun.NewRow()
    dtRun.AddRows(dr)
    dr(0) = first value
    dr(1) = second value
    dr(2) = third value
    frankly, there shouldn't be all that much difference between the two, though it seems like what you've done might not work right with Option Strict ON. I might be wrong about that, too, and either way it shouldn't result in the error you are showing.

    Oddly, I found what looks like a solution: Set the TableName of the datatable to something. I can understand why this would be an issue, because the XML will write the table, and all the rows in the table, so it could be requiring the table name be set so that it can do something with that tag....still, I really don't think I ever needed to do that. It may be that, just by chance, the only times I've created a datatable the way you have, I never serialized it, and every table I have serialized I created using a different technique that gave it a name automatically. Not sure about that, but try that solution: Set the TableName property of the datatable to something.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    10

    Re: How to export chart data to a .txt/.csv?

    Well that seemed to solve the problem. And now the file is saving properly as an XML.

    Now for another question; how to make the program display old runs from the data table on the appropriate graphs? Here's the code that I've got so far:

    Code:
     Private Sub OpenRunToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OpenRunToolStripMenuItem.Click
            OpenFileDialog1.Filter = "XML Files (*.xml*)|*.xml"
            OpenFileDialog1.ShowDialog()
            FileName = OpenFileDialog1.FileName
            If FileName IsNot Nothing Then
                dtRun.ReadXml(FileName)
                DataGridView1.DataSource = dtRun
                TCDChart.DataSource = dtRun
                TCDChart.Series("Series1").XValueMember = "Runtime"
                TCDChart.Series("Series1").YValueMembers = "TCD Voltage"
                FIDChart.DataSource = dtRun
                FIDChart.Series("Series1").XValueMember = "Runtime"
                FIDChart.Series("Series1").YValueMembers = "FID Voltage"
            End If
        End Sub
    This doesn't display anything in the graphs, although it displays fine in a data table on screen.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width