#include "qwt_scale_widget.h"
#include "qwt_legend.h"
#include "qwt_legend_item.h"
#include "qwt_plot_canvas.h"
#include "qwt_plot_grid.h"
#include "qwt_plot.h"
#include <stdio.h>
#include <iostream>
#include<string.h>
#include <QString>
#include <QTcpSocket>
#include <QtNetwork>
#include <QMutex>
#include <QSemaphore>
Q_OBJECT
public:
/* constructor */
NodePlot
(QWidget *parent ,
int portnumber ,
char* hostname,
int node_id
);
{ return data.curve; }
int getHistory(){ return this->HISTORY; }
void set_params();
struct
{
double* data;
} data;
unsigned int ts; // timestamp
char pack_str[40]; // package storage
char hostname[30];
int portnumber; // portnumber of data communication with the server
double* timeData; // contains the values of the x-axis
int node_id; // id of sender node
unsigned char val; // utility var to control package ssequence in control_sequence function
int dataCount; // take trace of the amount of points to plot
QMutex mutex;
// mutex to handle array of data to plot
protected:
private:
int HISTORY;
int YMAX;
int YMIN;
char xTitle[30];
char yTitle[30];
};
#endif // NODEPLOT_H
#include "qwt_scale_widget.h"
#include "qwt_legend.h"
#include "qwt_legend_item.h"
#include "qwt_plot_canvas.h"
#include "qwt_plot_grid.h"
#include "qwt_plot.h"
#include <stdio.h>
#include <iostream>
#include<string.h>
#include <QString>
#include <QTcpSocket>
#include <QtNetwork>
#include <QMutex>
#include <QSemaphore>
class QwtPlotCurve;
class NodePlot : public QwtPlot{
Q_OBJECT
public:
/* constructor */
NodePlot(QWidget *parent , int portnumber , char* hostname,int node_id);
const QwtPlotCurve *nodeCurve() const
{ return data.curve; }
int getHistory(){ return this->HISTORY; }
void set_params();
struct
{
QwtPlotCurve *curve;
double* data;
} data;
unsigned int ts; // timestamp
char pack_str[40]; // package storage
char hostname[30];
int portnumber; // portnumber of data communication with the server
double* timeData; // contains the values of the x-axis
int node_id; // id of sender node
unsigned char val; // utility var to control package ssequence in control_sequence function
int dataCount; // take trace of the amount of points to plot
QMutex mutex; // mutex to handle array of data to plot
protected:
void timerEvent(QTimerEvent *e);
private:
int HISTORY;
int YMAX;
int YMIN;
char xTitle[30];
char yTitle[30];
};
#endif // NODEPLOT_H
To copy to clipboard, switch view to plain text mode
#include "nodeplot.h"
using namespace std;
{
public:
{
#if QT_VERSION >= 0x040000
//setRenderHint(QwtPlotItem::RenderAntialiased);
#endif
}
void setColor
(const QColor &color
) {
#if QT_VERSION >= 0x040000
c.setAlpha(150);
setPen(c);
//setBrush(c);
#else
setPen(color);
setBrush
(QBrush(color, Qt
::Dense4Pattern));
#endif
}
};
/*
class to set eventually the x-axis to time scale
*/
{
public:
TimeScaleDraw
(const QTime &base
): baseTime(base)
{
}
virtual QwtText label
(double v
) const {
QTime upTime
= baseTime.
addSecs((int)v
);
return upTime.toString();
}
private:
};
NodePlot
::NodePlot(QWidget *parent,
int portnumber,
char *hostname,
int node_id
): dataCount(0)
{
this->HISTORY=60;
set_params();
this->data.data=(double*)malloc(HISTORY*sizeof(double));
this->timeData=(double*)malloc(HISTORY*sizeof(double));
setAutoReplot(false);
/*
setting the vars for the tcp connection
*/
strcpy(this->hostname,hostname);
this->portnumber=portnumber;
this->node_id= node_id;
this->val=0;
plotLayout()->setAlignCanvasToScales(true);
/* set the x/y axis */
//setAxisTitle(QwtPlot::xBottom, " System Uptime [h:m:s]");
//setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw(cpuStat.upTime()));
setAxisTitle
(QwtPlot::xBottom, xTitle
);
setAxisScale
(QwtPlot::xBottom,
-HISTORY,
0);
setAxisLabelRotation
(QwtPlot::xBottom,
-50.0);
setAxisLabelAlignment
(QwtPlot::xBottom, Qt
::AlignLeft | Qt
::AlignBottom);
/*
set the visual aspect of timestamp axis
*/
scaleWidget->setMinBorderDist(0, fmh / 2);
/* y-axis settings */
setAxisTitle
(QwtPlot::yLeft, yTitle
);
setAxisScale
(QwtPlot::yLeft, YMIN, YMAX
);
/*set the canvas background color*/
/*
draw a grid into canvas
*/
grid
->setMajPen
(QPen(Qt
::white,
0, Qt
::DotLine));
grid->attach(this);
/* curve to plot*/
NodeCurve *curve;
QPen pen
(Qt
::green);
//curve color pen.setWidth(1);
curve = new NodeCurve("Node curve");
//curve->setColor(Qt::green);
curve->setPen(pen);
curve->attach(this);
data.curve = curve;
/*
init x-axis scale
*/
for ( int i = 0; i < HISTORY; i++ )
timeData[HISTORY - 1 - i] = i - HISTORY + 1;
//(void)startTimer(50); // every 50 -millisecond
}
/*
timer event function: should be called every 50ms ( 20 hz refresh frequency)
*/
{
mutex.lock();
replot();
mutex.unlock();
}
void NodePlot::set_params(){
char str[100];
char str2[100];
char str3[100];
FILE* file_conf=fopen(".\\Win_QT\\config_canvas.txt","r");
while(!feof(file_conf)){
fgets(str,100,file_conf);
sscanf(str,"%s%s",str2,str3);
if(str[0]== '#') continue;
if(strcmp(str2,"HISTORY") == 0){
this->HISTORY=atoi(str3);
}
else if(strcmp(str2,"YMAX") == 0){
this->YMAX=atoi(str3);
}
else if(strcmp(str2,"YMIN") == 0){
this->YMIN=atoi(str3);
}
else if(strcmp(str2,"XTITLE") == 0){
strcpy(this->xTitle,str3);
}
else if(strcmp(str2,"YTITLE") == 0){
strcpy(this->yTitle,str3);
}
}
fclose(file_conf);
}
#include "nodeplot.h"
using namespace std;
class NodeCurve: public QwtPlotCurve
{
public:
NodeCurve(const QString &title):
QwtPlotCurve(title)
{
#if QT_VERSION >= 0x040000
//setRenderHint(QwtPlotItem::RenderAntialiased);
#endif
this->setCurveType(QwtPlotCurve::Yfx);
}
void setColor(const QColor &color)
{
#if QT_VERSION >= 0x040000
QColor c = color;
c.setAlpha(150);
setPen(c);
//setBrush(c);
#else
setPen(color);
setBrush(QBrush(color, Qt::Dense4Pattern));
#endif
}
};
/*
class to set eventually the x-axis to time scale
*/
class TimeScaleDraw: public QwtScaleDraw
{
public:
TimeScaleDraw(const QTime &base):
baseTime(base)
{
}
virtual QwtText label(double v) const
{
QTime upTime = baseTime.addSecs((int)v);
return upTime.toString();
}
private:
QTime baseTime;
};
NodePlot::NodePlot(QWidget *parent, int portnumber, char *hostname,int node_id):
QwtPlot(parent),
dataCount(0)
{
this->HISTORY=60;
set_params();
this->data.data=(double*)malloc(HISTORY*sizeof(double));
this->timeData=(double*)malloc(HISTORY*sizeof(double));
setAutoReplot(false);
/*
setting the vars for the tcp connection
*/
strcpy(this->hostname,hostname);
this->portnumber=portnumber;
this->node_id= node_id;
this->val=0;
plotLayout()->setAlignCanvasToScales(true);
/* set the x/y axis */
//setAxisTitle(QwtPlot::xBottom, " System Uptime [h:m:s]");
//setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw(cpuStat.upTime()));
setAxisTitle(QwtPlot::xBottom, xTitle);
setAxisScale(QwtPlot::xBottom, -HISTORY, 0);
setAxisLabelRotation(QwtPlot::xBottom, -50.0);
setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom);
/*
set the visual aspect of timestamp axis
*/
QwtScaleWidget *scaleWidget = axisWidget(QwtPlot::xBottom);
const int fmh = QFontMetrics(scaleWidget->font()).height();
scaleWidget->setMinBorderDist(0, fmh / 2);
/* y-axis settings */
setAxisTitle(QwtPlot::yLeft, yTitle);
setAxisScale(QwtPlot::yLeft, YMIN, YMAX);
/*set the canvas background color*/
setCanvasBackground(QColor(QColor(Qt::black)));
/*
draw a grid into canvas
*/
QwtPlotGrid *grid = new QwtPlotGrid;
grid->setMajPen(QPen(Qt::white, 0, Qt::DotLine));
grid->attach(this);
/* curve to plot*/
NodeCurve *curve;
QPen pen(Qt::green);//curve color
pen.setWidth(1);
curve = new NodeCurve("Node curve");
//curve->setColor(Qt::green);
curve->setPen(pen);
curve->attach(this);
data.curve = curve;
canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
/*
init x-axis scale
*/
for ( int i = 0; i < HISTORY; i++ )
timeData[HISTORY - 1 - i] = i - HISTORY + 1;
//(void)startTimer(50); // every 50 -millisecond
}
/*
timer event function: should be called every 50ms ( 20 hz refresh frequency)
*/
void NodePlot::timerEvent(QTimerEvent *)
{
mutex.lock();
replot();
mutex.unlock();
}
void NodePlot::set_params(){
char str[100];
char str2[100];
char str3[100];
FILE* file_conf=fopen(".\\Win_QT\\config_canvas.txt","r");
while(!feof(file_conf)){
fgets(str,100,file_conf);
sscanf(str,"%s%s",str2,str3);
if(str[0]== '#') continue;
if(strcmp(str2,"HISTORY") == 0){
this->HISTORY=atoi(str3);
}
else if(strcmp(str2,"YMAX") == 0){
this->YMAX=atoi(str3);
}
else if(strcmp(str2,"YMIN") == 0){
this->YMIN=atoi(str3);
}
else if(strcmp(str2,"XTITLE") == 0){
strcpy(this->xTitle,str3);
}
else if(strcmp(str2,"YTITLE") == 0){
strcpy(this->yTitle,str3);
}
}
fclose(file_conf);
}
To copy to clipboard, switch view to plain text mode
#include"nodeplot.h"
#include<stdio.h>
#include<stdlib.h>
#include <QApplication>
#include <QLabel>
#include <QVBoxLayout>
int main(int argc, char* argv[]){
#if QT_VERSION >= 0x040000
vBox.setWindowTitle("Sensor Plot");
#else
vBox.setCaption("Sensor Plot");
#endif
NodePlot plot(&vBox,5000,"localhost",2);
layout->addWidget(&plot);
#if QT_VERSION < 0x040000
a.setMainWidget(&vBox);
#endif
vBox.resize(600,400);
vBox.show();
return a.exec();
}
#include"nodeplot.h"
#include<stdio.h>
#include<stdlib.h>
#include <QApplication>
#include <QLabel>
#include <QVBoxLayout>
int main(int argc, char* argv[]){
QApplication a(argc, argv);
QWidget vBox;
#if QT_VERSION >= 0x040000
vBox.setWindowTitle("Sensor Plot");
#else
vBox.setCaption("Sensor Plot");
#endif
NodePlot plot(&vBox,5000,"localhost",2);
QVBoxLayout *layout = new QVBoxLayout(&vBox);
layout->addWidget(&plot);
#if QT_VERSION < 0x040000
a.setMainWidget(&vBox);
#endif
vBox.resize(600,400);
vBox.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks