简体   繁体   中英

C# Datatable data to chart

Language: C#

Hi all,

Im a bit stuck here and was hoping someone could point me in the right direction.

I have a text file as a data source and made a datatable with all the data in it. Now i need to get data out of it create a chart.

full datatable looks like this

datestamp   |    name_X_1      |    name_X_2      |    name_Y_1      |    name_Y_2
-----------------------------------------------------------------------------------
1/1/199     |    somevalue     |    somevalue     |    somevalue     |    somevalue
1/2/199     |    somevalue     |    somevalue     |    somevalue     |    somevalue

Now i need to have all name_X_* values from the name_x_* columns from a certain time (wich will be a varible from a calander control).

This is how i need the data to be for the chart

datastamp   |    name_x_data        |    name_y_data
-------------------------------------------------------
1/1/199     |    name_X_1_value     |    name_y_1_value  
1/1/199     |    name_X_2_value     |    name_y_2_value  

To make a bit more difficult, the name_X_* and name_y_* columns can vary (X and Y are the x and y axis values from a massurement and are always the same amount of columns). In one text file it's 10 columns and a other text file it could be 20. So i can't hardcode the column names. I need to filter them on name_X_ and name_Y_ and search on a specific date.

I could also store the number of columns. So if we could select on column number 2 till 10 for X and 10 till 20 for Y it would be awesome to. Its ok to have a administration on the text file in the database here i enter the amount of colums the text file has. So if someone has a option to use count it would work for me too.

If anyone could point me in the right direction i would really appreciateit. Im stuck haha

thanks allot!

ps; Im willing to pay for a good solution :)

This is the code i made so far:

 DataTable dt = new DataTable();
        string[] columns = null;

        string FileName = "C:\\datapath\\data.dat";
        var lines = File.ReadAllLines(FileName);

        // create columns from text file and skip first few rows
        if (lines.Count() > 2)
            {

                columns = lines[1].Split(new char[] { ',' }).Select(x => x.Replace("\"", "")).ToArray();

                foreach (var column in columns)
                    dt.Columns.Add(column);
            }

        // add data to rows
        for (int i = 2; i < lines.Count(); i++)
            {
                DataRow dr = dt.NewRow();
                string[] values = lines[i].Split(new char[] { ',' }).Select(x => x.Replace("\"", "")).ToArray();

                for (int j = 0; j < values.Count() && j < columns.Count(); j++)
                    dr[j] = values[j];

                dt.Rows.Add(dr);
            }

        // Columns filter (under construction) (this is the part i need to be not hard coded)

        string[] selectedColumns = new[] { "TIMESTAMP", "sensor_X_001", "sensor_X_002" };

        DataTable dt2 = new DataView(dt).ToTable(false, selectedColumns);

        // Date filter (under construction)
        // string s = "2014-05-21 13:05:00";
        // DataRow foundRow = dt2.Tables["TIMESTAMP"].Rows.Find(s);

        // data to grid 
        this.GridView1.DataSource = dt2;
        this.GridView1.DataBind();

Found a solution and thought i should share it :)

DataTable result = new DataTable();
        result.Columns.Add("datestamp", typeof(String));
        result.Columns.Add("name_x_data", typeof(String));
        result.Columns.Add("name_y_data", typeof(String));
        DataRow drresult;
        foreach (DataRow datarow in dt.Rows)
        {
            for (int i = 1; i <= dt.Columns.Count / 2; i++)
            {
                drresult = result.NewRow();
                drresult["datestamp"] = datarow["datestamp"];
                drresult["name_x_data"] = datarow["name_X_" + i];
                drresult["name_y_data"] = datarow["name_Y_" + i];
                result.Rows.Add(drresult);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM