14 Replies - 1699 Views - Last Post: 23 February 2012 - 11:12 AM
#1
Logging data (i.e. temperature over time) to database and draw graph
Posted 15 February 2012 - 12:58 AM
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
Replies To: Logging data (i.e. temperature over time) to database and draw graph
#2
Re: Logging data (i.e. temperature over time) to database and draw graph
Posted 15 February 2012 - 02:15 AM
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
#3
Re: Logging data (i.e. temperature over time) to database and draw graph
Posted 15 February 2012 - 02:49 AM
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
#4
Re: Logging data (i.e. temperature over time) to database and draw graph
Posted 15 February 2012 - 03:43 AM
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
#5
Re: Logging data (i.e. temperature over time) to database and draw graph
Posted 15 February 2012 - 05:35 AM
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
#6
Re: Logging data (i.e. temperature over time) to database and draw graph
Posted 15 February 2012 - 05:45 AM
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
#7
Re: Logging data (i.e. temperature over time) to database and draw graph
Posted 15 February 2012 - 05:57 AM
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
#8
Re: Logging data (i.e. temperature over time) to database and draw graph
Posted 16 February 2012 - 04:51 AM
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
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
#9
Re: Logging data (i.e. temperature over time) to database and draw graph
Posted 16 February 2012 - 06:18 AM
RexGrammer, on 16 February 2012 - 11:51 AM, said:
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
#10
Re: Logging data (i.e. temperature over time) to database and draw graph
Posted 16 February 2012 - 12:03 PM
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.
#11
Re: Logging data (i.e. temperature over time) to database and draw graph
Posted 16 February 2012 - 12:42 PM
RexGrammer, on 16 February 2012 - 07:03 PM, said:
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!
#12
Re: Logging data (i.e. temperature over time) to database and draw graph
Posted 16 February 2012 - 01:45 PM
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.
#13
Re: Logging data (i.e. temperature over time) to database and draw graph
Posted 23 February 2012 - 01:53 AM
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.
#14
Re: Logging data (i.e. temperature over time) to database and draw graph
Posted 23 February 2012 - 11:10 AM
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
#15
Re: Logging data (i.e. temperature over time) to database and draw graph
Posted 23 February 2012 - 11:12 AM
|
|

New Topic/Question
Reply



MultiQuote






|