Friday, July 4th 2008, 11:30pm UTC+1

You are not logged in.

  • Login
  • Register

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains how this page works. You must be registered before you can use all the page's features. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

crazycoder

Trainee

Posts: 128

Occupation: Software Engineer

1

Thursday, May 15th 2008, 12:56pm

how to plot a graph from database

hi all,

what i need is to plot data from my database (SQLite)..
first of all i am connecting my window to my database, then now acquiring data from it , now i need to get data which is present in the database and plot accordingly with the values present in it in the form of a graph..

i tried doing this with the inspiration from cpu plot but failed..

so can any one help me out..

thanks in advance.
  • Go to the top of the page

2

Thursday, May 15th 2008, 3:35pm

RE: how to plot a graph from database

But exactly where do you have a problem? What are you using for drawing graphs? Qwt library, or something else?
  • Go to the top of the page

crazycoder

Trainee

Posts: 128

Occupation: Software Engineer

3

Thursday, May 15th 2008, 5:19pm

RE: how to plot a graph from database

Hi TMF thanx for the reply.

I am using Qwt library for the Plotting of graph's. I tried the example programs they all worked. I used the QwtPlot Widget on the GUI form. I am able to plot graphs using a formula and random numbers. I wanted to take the data from the database and plot a graph from it. I am not able to figure out how to take the value's to plot the graph from the database. I am using this code to plot graph i call the draw function using a timer signal every one second. Now i wanted to acquire data from a database and plot the graphs. I don't know how to do this. I will have Pressure values on the Y axis and Time Stamp on the bottom X axis.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void PlottingGraph::draw()
{
	m_time++;
	memmove(m_y, &m_y[1], 99 * sizeof(double));
	m_y[99] =  10 * sin((double)m_time / 360 * 3.1415 * 10) + rand() % 4000 -2000;

	for (int i = 0; i < 100; i++)
            m_x[i]++;
	if (!m_curve)
	{      
       	        m_curve = new QwtPlotCurve();
                m_curve->setPen(QPen(Qt::red));
                m_curve->setData(m_x, m_y, 100);
	        m_curve->attach(myPlot);
	}
    m_curve->setData(m_x, m_y,100);
    myPlot->setAxisScale(QwtPlot::xBottom, m_time, m_time +100); 
}

I think this is the line which generates the values to plot the graph.

Source code

1
m_y[99] =  10 * sin((double)m_time / 360 * 3.1415 * 10) + rand() % 4000 -2000;

Now what changes i need to do so that i can connect to the SQLite database and plot the graphs with the values defined in the database. I will be using SQLite db for the backend database for this application.

Thank you so much.
Pls help me i am really stuck here. !!! ?(
  • Go to the top of the page

crazycoder

Trainee

Posts: 128

Occupation: Software Engineer

4

Friday, May 16th 2008, 8:13am

how to plot a graph from database

Hi T.M.F i am waiting for your help. !!
  • Go to the top of the page

5

Friday, May 16th 2008, 11:14am

RE: how to plot a graph from database

connect to your database with something such as

Source code

1
2
3
4
5
6
QSqlDatabase db = QSqlDatabase::addDatabase( "QMYSQL", "test" );
db.setHostName( "..." );
db.setDatabaseName( "..." );
db.setUserName( "..." );
db.setPassword( "..." );
db.open();

retrieve your data with something such as

Source code

1
2
3
4
5
6
7
8
9
10
11
12
QSqlQuery q( QSqlDatabase::database( "test" ) );
if( q.exec( "select x, y from data_table order by x;" ) )
{
    nb = q.size();
    x  = new double[nb];
    y  = new double[nb];
    for( int i = 0; q.next(); i++ )
    {
        x[i] = q.value(0).toDouble();
        y[i] = q.value(0).toDouble();
    }
}

and set up your plot
Nicolas
  • Go to the top of the page

crazycoder

Trainee

Posts: 128

Occupation: Software Engineer

6

Friday, May 16th 2008, 2:46pm

how to plot a graph from database

Hi Nicolas SOUCHON Thank you so much. You have helped me before also with some problem. :)

I will try what ever you have given. I have another doubt , I will have huge data around 10 hours or more of data in the database. I am planning to use SQlite right now. But will it be able to handle such huge data. It is not even a server. What do you think will be correct for this kinda application where in i have to acquire data and plot it and have all the acquired data saved in the back end Database. Should i use MYSQL server or ORACLE . I don't think SQLite is a server its a plugin what i am using so i was wondering what i have to use. Any Suggesion's ??


Thank You
  • Go to the top of the page

7

Friday, May 16th 2008, 3:13pm

RE: how to plot a graph from database

I can't help you for this, I never used SQlite

also 10 hours is not sufficient, what frequency, number of channel ...
Nicolas
  • Go to the top of the page

crazycoder

Trainee

Posts: 128

Occupation: Software Engineer

8

Friday, May 16th 2008, 5:18pm

Which Database. ?

Hi Nicolas SOUCHON i will have total of 46 channels. There are both temperature and pressure channels in it.At 48 MHz .I will have to acquire data on USB port from a remote system for a period of 10 hours or more. I did't know which db to use. I read a lil about sqlite and implemented it. But now i doubt if it can handle huge data. I think i need a SQL server program so that all the data i acquire goes into the database. Which data base do you suggest for this kinda application. ??

Thank You

This post has been edited 1 times, last edit by "crazycoder" (May 16th 2008, 5:31pm)

  • Go to the top of the page

9

Friday, May 16th 2008, 5:47pm

RE: Which Database. ?

46 channel at 48 MHz
--> ~ 4.1 GBytes / s
--> ~ 14 800 GBytes / h
--> ~ 148 000 GBytes / 10 h
????
Nicolas
  • Go to the top of the page

crazycoder

Trainee

Posts: 128

Occupation: Software Engineer

10

Friday, May 16th 2008, 6:11pm

RE: Which Database. ?

Hey but i will get data once in a second for each channel. the remote system is going to average 100 samples and send it every second on serial port through USB.Hey the frequency of the remote embedded system is 48 Mhz but i will get max of 10 Kbpsec .I won't be acquiring all the channels data every time.
  • Go to the top of the page

11

Friday, May 16th 2008, 6:23pm

RE: Which Database. ?

to estimate volume of data:
- nb of channel
- bytes/channel (2 bytes/channel for most analog acquisition cards, ??? for your system, depends on what you store, original or converted values)
- acquisition frequency (never mind the frequency of the remote embedded system)
- elapsed time of stored acquisitions
Nicolas
  • Go to the top of the page

crazycoder

Trainee

Posts: 128

Occupation: Software Engineer

12

Saturday, May 17th 2008, 5:04am

RE: Which Database. ?

Hi,
Actually my remote embedded system sends 2000 samples per second.. per channel so 46 channels * 2000 so my database should handle 92000 samples of data per second..

hearing all this.... in ur mind what kind of database might be able to handle this much amount of huge data which will be continuously being stored for a Maximum of 10 hours..... when the embedded systems runs..

now i think u got a complete picture about what i am doing...

keeping all these things in mind can u suggest me which database will be good enough... (I.e those database which u worked on..)

On a normal use the application may run for 4 to 5 hours max a day. When the hard disk space is full.The data will be backed up into a tape drive.


thanks in advance...

This post has been edited 1 times, last edit by "crazycoder" (May 17th 2008, 5:45am)

  • Go to the top of the page

13

Saturday, May 17th 2008, 3:41pm

RE: Which Database. ?

Problem is not which database can handle so many requests, but what kind of computer do you need to handle the database which handles so many requests J
Are you sure it must be a database? In your case you have only a couple of data streams which can easily be saved into file. And not very powerful computer can do that. Because of simplicity of your data I think that database doesn’t give you any benefits.
  • Go to the top of the page

14

Monday, May 19th 2008, 1:14pm

Mysql

Use MYSQl which is pretty powerfull.

This post has been edited 1 times, last edit by "Boss999" (May 19th 2008, 1:45pm)

  • Go to the top of the page

This post "Error in DB data Retrieval & Plotting" by "crazycoder" (Monday, May 19th 2008, 5:21pm) has been deleted by the author himself (Friday, May 23rd 2008, 1:23pm).

crazycoder

Trainee

Posts: 128

Occupation: Software Engineer

16

Friday, May 23rd 2008, 1:19pm

Error in DB data Retrieval & Plotting

Hi Nicolas SOUCHON i tried debugging the program. I am left with 2 errors. pls help me i am stuck here. I am breaking my head to get this result . ;(
-----------------plot_db.cpp---------------------------

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <QSqlQuery>
#include <QMenu>
plot_db::plot_db(QWidget *parent)
: QMainWindow(parent)
, m_curve(0)
{
ui.setupUi(this);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(draw()));

timer->start(1000);
myPlot->setTitle("Pressure ");
myPlot->setAxisTitle(QwtPlot::xBottom, " System time [h:m:s]");
myPlot->setAxisScale(QwtPlot::xBottom, 0,60 );
myPlot->setAxisTitle(QwtPlot::yLeft, "Degree");
myPlot->setAxisScale(QwtPlot::yLeft, -60,60 );
myPlot->setAutoReplot(true);

}

plot_db::~plot_db()
{

}

void plot_db::draw()
{
QSqlQuery q;
if( q.exec( "select s_x, s_y from data_table order by x;" ) )
{
nb = q.size();
s_x = new double[nb];
s_y = new double[nb];

for( int i = 0; q.size(); i++ )
{
s_x[i] = q.value(0).toDouble();
s_y[i] = q.value(0).toDouble();
}
}
m_curve = new QwtPlotCurve();
m_curve->setPen(QPen(Qt::red));
m_curve->setData(s_x, s_y, 100);
m_curve->attach(myPlot);
}


---------plot_db.h-----------------

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "qwt_plot.h"
#include <qwt_plot_curve.h>

using namespace Ui;
class plot_db : public QMainWindow, public plot_dbClass
{
Q_OBJECT

public:
plot_db(QWidget *parent = 0);
~plot_db();

private slots:

void draw();

private:
int nb;
QwtPlotCurve *m_curve;
double s_x[100];
double s_y[100];
Ui::plot_dbClass ui;
};

--------------------------database connection---------------------

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef CONNECTION_H
#define CONNECTION_H

#include <QtGui/QMainWindow>
#include <QtSql>
#include <QMessageBox>
#include <QTableView>


inline bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(" Plot_DB ");
if (!db.open()) 
{
QMessageBox::critical(0, qApp->tr("Cannot open database"), 
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
QSqlQuery query;

if (!query.exec("create table if not exists data_table(s_x varchar(20) primary key, "
"s_y varchar(20))"))
{
QMessageBox::critical(0, "Failed", "Failed create Te table");
return false;
}
query.exec("insert into data_table values(0,-60 )");
query.exec("insert into Acquisition values(10, 0)");
query.exec("insert into Acquisition values(20, 40)");
query.exec("insert into Acquisition values(28, 0)");
query.exec("insert into Acquisition values(30, -30)");
query.exec("insert into Acquisition values(38, 0)");
query.exec("insert into Acquisition values(40, 38 )");
query.exec("insert into Acquisition values(50, 0 )");
query.exec("insert into Acquisition values(52,-30 )");
query.exec("insert into Acquisition values(55, 0)");
query.exec("insert into Acquisition values(60,50)");
return true;
}
#endif


----------errors----

Source code

1
2
3
4
5
.\plot_db.cpp(44) : error C2440: '=' : cannot convert from 'double *' to 'double [100]'
There are no conversions to array types, although there are conversions to references or pointers to arrays
.\plot_db.cpp(45) : error C2440: '=' : cannot convert from 'double *' to 'double [100]'
There are no conversions to array types, although there are conversions to references or pointers to arrays
main.cpp

what is going wrong in this program..
Thank You.
[/i][/i]
  • Go to the top of the page

crazycoder

Trainee

Posts: 128

Occupation: Software Engineer

17

Sunday, May 25th 2008, 8:45am

Problem with retreving data from db

Hi Nicolas SOUCHON i have a problem in completing my program. I am stuck with a few erros just not able to clear those errors, Pls help me out with this error i did not get any example related to this topic. I wanted to retrive data from a database and print a graph with it. I have attached the zip files ,pls tell me the changes i have to do in the program. I really need to clear this error, and get it working. ;(



Thank You :)
crazycoder has attached the following file:
  • test_plotdb.zip (3.76 kB - 196 times downloaded - Last download: Jun 23rd 2008, 12:44pm)
  • Go to the top of the page

18

Sunday, May 25th 2008, 2:20pm

The errors in lines 44 and 45 (I think it should be 37 and 38) are due to you try to convert from pointer to double array to array of double. So try to replace lines 37 & 38:
*s_x = q.value(0).toDouble();
*s_y[i] = q.value(0).toDouble();
  • Go to the top of the page

crazycoder

Trainee

Posts: 128

Occupation: Software Engineer

19

Monday, May 26th 2008, 6:08am

Re: How to plot a graph from database ???

Hi TMF,

Thanks for the reply...
Actually all the errors has been removed now..
the graph is not being plotted..
i am using timer for drawing the plot... which u might have seen...
so what shall i do to see my graph plotting the values from the database....


This is the code which i am using to retrieve values to plot...
so which part of the code do u think i made a wrong step...

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* *****************test_plotdb.cpp****************** */


m_sqlModel = new QSqlQueryModel(this);
QSqlQuery q("SELECT xval,yval FROM data_table1");
if (q.exec())
{
        for(int i=q.first();i<q.last();i++) 
{
*s_x=q.value(0).toDouble();
*s_y=q.value(1).toDouble();
m_curve = new QwtPlotCurve();
m_curve->setPen(QPen(Qt::red));
m_curve->setData((const double*)s_x, (const double*)s_y, 100);
//m_curve->setRawData(s_x,s_y,Size);
m_curve->attach(myPlot);
myPlot->replot();
}
}
else
{
QMessageBox::critical(this, "Failure", "Query failure");
}



Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* *****************test_plotdb.h****************** */
 

class test_plotdb : public QDialog,public Ui::test_plotdbClass
{
	Q_OBJECT

public:
	test_plotdb(QWidget *parent = 0);
	~test_plotdb();

private slots:
	void draw();
//	Ui::test_plotdbClass ui;
private:
	QwtPlotCurve *m_curve;
	QSqlQueryModel *m_sqlModel;
	int nb;
	double *s_x;
	double *s_y;

private slots:
	void on_plotButton_clicked();
};


Thanks in advance..
  • Go to the top of the page

Rate this thread