14 Replies - 3830 Views - Last Post: 23 February 2012 - 11:12 AM Rate Topic: -----

#1 rg2720  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 14-February 12

Logging data (i.e. temperature over time) to database and draw graph

Posted 15 February 2012 - 12:58 AM

Hi,

First of all, I am not asking for a complete solution - merely a hint in the right direction.

I have been Googling around for hours but I am still very confused. I want to log a set of data like temperature inside, outside etc with a timestamp. Currently I am logging this to a .txt file, but this makes it very hard to present the data in graphs in order to show temperatures from i.e. last 24 hours, or last week, last month etc.

Correct me if I am wrong, but I have come to the conclusion that I need to learn how to do this using database (SQL?) and then when the data are stored in the database I will need to figure out a way to draw the graphs from there.

So what I am wondering:

1) Will this be the best/easiest way to attack this problem, store the data in an SQL database and then draw graphs?

2) If yes, where should I begin - are there any good tutorials or other resources you guys could recommend?


Thanks for all your help.


-Rg

Is This A Good Question/Topic? 0
  • +

Replies To: Logging data (i.e. temperature over time) to database and draw graph

#2 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Logging data (i.e. temperature over time) to database and draw graph

Posted 15 February 2012 - 02:15 AM

Why don't you use a keyValue pair inside a Dictionary<T> ?

Dictionary<timeDate, string> readings = new Dictionary<timeDate, String>();

Then you could have a key being the timeDate and the value being the temperature. This would allow you to find any temperature by its date, by searching the keys, or allow you to find all dates where it was a certain temperature by searching the values.

http://www.dotnetperls.com/dictionary

I use Dictionaries pretty much any time I need to use any kind or Array or List because they are so versatile.

This post has been edited by negligible: 15 February 2012 - 02:15 AM

Was This Post Helpful? 1
  • +
  • -

#3 rg2720  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 14-February 12

Re: Logging data (i.e. temperature over time) to database and draw graph

Posted 15 February 2012 - 02:49 AM

Thanks, that's a good idea. However, how would this be for keeping the data between each time the program is closed and then opened?

I wish to store the temperature over time, if I loose all data when i close the application then I have to think of another way.

This post has been edited by tlhIn`toq: 15 February 2012 - 06:39 AM
Reason for edit:: No need to quote the entire immediately preceding post

Was This Post Helpful? 0
  • +
  • -

#4 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Logging data (i.e. temperature over time) to database and draw graph

Posted 15 February 2012 - 03:43 AM

You can write the data stored in your dictionaries to a textfile (XML files allow you to load data from them directly into your class structure) and load the data from the text file when the application is started, this is a very common way of doing configuration files.

The process is called Serialization. Not hard to find tutorials on the subject, learnt it myself the other day.

To save settings after you've closed a WinForms application you have to have written something to the actual hard drive rather than just memory. So yeah you could also write it to a database, just personally I don't see the point unless you need to use that database for something outside of a .NET application.

This post has been edited by negligible: 15 February 2012 - 03:47 AM

Was This Post Helpful? 1
  • +
  • -

#5 rg2720  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 14-February 12

Re: Logging data (i.e. temperature over time) to database and draw graph

Posted 15 February 2012 - 05:35 AM

Thanks again! I am an electric engineer and even if I have programmed a fair share of C# in my spare time this is completely new territory for me! Im very excited to learn this. I'll get well known with Dictionary before I go further ahead and learn Serialization though.

I have made some test code:

        private void button1_Click(object sender, EventArgs e)
        {
            Dictionary<DateTime, int> dictionary = new Dictionary<DateTime, int>();
            dictionary.Add(DateTime.Now, 2);
            DateTime naa = DateTime.Now;
            textBox1.Text = naa.
            Thread.Sleep(1000);

            dictionary.Add(DateTime.Now, 3);
            textBox2.Text = DateTime.Now.ToString();
            Thread.Sleep(1000);

            dictionary.Add(DateTime.Now, 4);
            textBox3.Text = DateTime.Now.ToString();
            Thread.Sleep(1000);

            dictionary.Add(DateTime.Now, 5);
            textBox4.Text = DateTime.Now.ToString();
            Thread.Sleep(1000);

            textBox5.Text = DateTime.Now.ToString(); 
            dictionary.Add(DateTime.Now, 6);
            textBox6.Text = DateTime.Now.ToString();
            Thread.Sleep(1000);

            string temp = DateTime.Now.ToString();
            if (dictionary.ContainsKey(naa))
            {
                textBox7.Text= dictionary[naa].ToString();
            }
            else textBox7.Text= "Not in dictionary.";
        }



Horribly messy, but still: The DateTime variable are so accurate (down to milliseconds I think), so I wonder if I wanted to pick out values from time A to time B (i.e. Feb 1st to Feb 2nd) I wonder how to achieve this.

But anyway your help has brought me closer to what I am trying to achieve!

This post has been edited by tlhIn`toq: 15 February 2012 - 06:40 AM
Reason for edit:: No need to quote the entire immediately preceding post

Was This Post Helpful? 0
  • +
  • -

#6 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Logging data (i.e. temperature over time) to database and draw graph

Posted 15 February 2012 - 05:45 AM

Learn a little about LINQ. You can use it to querry against collections as if they are databases.
            Dictionary<DateTime, int> dic = new Dictionary<DateTime, int>();
            dic.Add(new DateTime(2012, 01, 31), 1);
            dic.Add(new DateTime(2012, 02, 01), 2);
            dic.Add(new DateTime(2012, 02, 02), 3);
            dic.Add(new DateTime(2012, 02, 03), 4);

            DateTime startDate = new DateTime(2012, 02, 01);
            DateTime endDate = new DateTime(2012, 02, 02);

            var dates = from dt in dic
                        where
                            (dt.Key >= startDate) &&
                            (dt.Key <= endDate)
                        select dt;

            foreach (var date in dates)
                Console.WriteLine(date.Value);

This post has been edited by Robin19: 15 February 2012 - 05:46 AM

Was This Post Helpful? 3
  • +
  • -

#7 rg2720  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 14-February 12

Re: Logging data (i.e. temperature over time) to database and draw graph

Posted 15 February 2012 - 05:57 AM

Wow, haha. C# is so powerful. And yes I will look into LINQ as well, now I have a lot of hours work figure out of these new areas. Can't wait! :D

And when I finally get the hang of that I need to figure our the best way to plot the values in a graph!

This post has been edited by tlhIn`toq: 15 February 2012 - 06:40 AM
Reason for edit:: No need to quote the entire immediately preceding post

Was This Post Helpful? 0
  • +
  • -

#8 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: Logging data (i.e. temperature over time) to database and draw graph

Posted 16 February 2012 - 04:51 AM

I wouldn't concern myself with making a graph library.

Instead use some of the already proven libraries:

QuickGraph
Graph#

And if those don't suit your needs just do a quick google of it:
like this, and I'm sure that you will find one that suits you.

NOTE: Most of them are Open-Source so you can ponder in it and see how does it actually draw a graph.

Quote

so I wonder if I wanted to pick out values from time A to time B (i.e. Feb 1st to Feb 2nd) I wonder how to achieve this.


This could be achieved using the DateTime.Subtract method. You can subtract one dateTime from another.

NOTE: You need to subtract the earlier dateTime from the newer. Obvious of course, but sometimes people forget.

Some Reference:
DateTime.Subtract Method()

This post has been edited by RexGrammer: 16 February 2012 - 04:51 AM

Was This Post Helpful? 1
  • +
  • -

#9 rg2720  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 14-February 12

Re: Logging data (i.e. temperature over time) to database and draw graph

Posted 16 February 2012 - 06:18 AM

View PostRexGrammer, on 16 February 2012 - 11:51 AM, said:

I wouldn't concern myself with making a graph library.

Instead use some of the already proven libraries:

QuickGraph
Graph#

And if those don't suit your needs just do a quick google of it:
like this, and I'm sure that you will find one that suits you.


Thank you very much! I have got the subtract thing working and I have got both dictionary and serialization to file/reading to file etc. It's now all about graphing.

I checked the links you gave to Graph# and Quickgraph, but I couldn't really find any pictures showing example graphs. Do you have personal experience with a graphing library that you would recommend?

Thanks again.

-Rg
Was This Post Helpful? 0
  • +
  • -

#10 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: Logging data (i.e. temperature over time) to database and draw graph

Posted 16 February 2012 - 12:03 PM

I use ZedGraph for all my graphing. It's very flexible, easy, and can be customized.

You can find it on SourceForge: ZedGraph
There's even an article on it on CodeProject: A flexible charting library for .NET

Also, a big plus is that it's open source. So you can see for yourself how does it actually work.
Was This Post Helpful? 2
  • +
  • -

#11 rg2720  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 14-February 12

Re: Logging data (i.e. temperature over time) to database and draw graph

Posted 16 February 2012 - 12:42 PM

View PostRexGrammer, on 16 February 2012 - 07:03 PM, said:

I use ZedGraph for all my graphing. It's very flexible, easy, and can be customized.

You can find it on SourceForge: ZedGraph
There's even an article on it on CodeProject: A flexible charting library for .NET

Also, a big plus is that it's open source. So you can see for yourself how does it actually work.


Funny you should say that, I googled around (using your google link) and ended up with ZedGraph myself. Im already graphing up things as we speak, it seems very easy to use.

Could I just ask you though, would you serialize as XML (what Im currently doing) or as binary code? Each year I will do 500 thousand measurements, will this bee too much for a dictionary in your opinion?

Thank you very much for your help, much appreciated!
Was This Post Helpful? 0
  • +
  • -

#12 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: Logging data (i.e. temperature over time) to database and draw graph

Posted 16 February 2012 - 01:45 PM

I would export it to XML so that you can easily modify it later on, or when you plan on upgrading the system, and so that you can use it in other applications too. (You could use binary format in other applications, but this way it's much easier to get the data you're after).

This way when you plan on changing the name of a variable you don't have to rewrite the code, just to rename your node in XML.

And 500 entries is nothing. I've loaded thousands and thousands of entries from a dataBase and it went fine.
Was This Post Helpful? 1
  • +
  • -

#13 rg2720  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 14-February 12

Re: Logging data (i.e. temperature over time) to database and draw graph

Posted 23 February 2012 - 01:53 AM

Thanks for all your help people!

I just wanted to post the result, showing temperature inside and outside for the last 24 hours, week, month and year.
If anyone needs help with something similar I would be happy to contribute back to the community by helping others.



Posted Image
Was This Post Helpful? 2
  • +
  • -

#14 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 22-December 09

Re: Logging data (i.e. temperature over time) to database and draw graph

Posted 23 February 2012 - 11:10 AM

I realize that I am a little late on this topic, but another option to consider for future projects is to use a database. If so, you can always grab MySQL for free. Once you have that, you can use their connector to pull the data. In addition, check out Fluent NHibernate as a data mapper. It makes life very easy when dealing with a database.

For Fluent help, you can check out these tutorials:

By FlashM
There is also a part two that you can get to easily enough.

Regards,

Ed
Was This Post Helpful? 0
  • +
  • -

#15 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: Logging data (i.e. temperature over time) to database and draw graph

Posted 23 February 2012 - 11:12 AM

Personally, I like to stick with MS technologies. Sql Server Express is free. And for the ORM, you can use LINQ to SQL or Entity Framework. All are MS 1st party tools.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1