Everything compiles with no errors. Basically I copied the Histogram example from the Qwt site and slimmed it down for speed. a few other tweaks were made as well, nothing major though. I read in values from a .txt file where every line is the y value. the x value is the line number. For small text files with small numbers, I am able to clearly see the graph (ie x.interval = 1-10, y.interval = 1-10 respectively, so the text file would just have the 10 numbers in it, separated by a carriage return) but when i try to expand to larger files with 10,000 entries, i get a flat bar across the bottom and the last few entries have infinite y values. when i tried a file with 1 million entries, it crashed (probably having to do with using int). below is the code. any help or suggestions will be happily welcomed!
histogram.cpp
#include "histogram_item.h"
class HistogramItem::PrivateData
{
public:
int attributes;
double reference;
};
HistogramItem
::HistogramItem(const QwtText &title
):{
init();
}
HistogramItem
::HistogramItem(const QString &title
):{
init();
}
HistogramItem::~HistogramItem()
{
delete d_data;
}
void HistogramItem::init()
{
d_data = new PrivateData();
setZ(20);
}
double HistogramItem::baseline() const
{
return d_data->reference;
}
{
d_data->data = data;
itemChanged();
}
void HistogramItem
::setColor(const QColor &color
) {
if ( d_data->color != color )
{
d_data->color = color;
itemChanged();
}
}
QwtDoubleRect HistogramItem::boundingRect() const
{
QwtDoubleRect rect = d_data->data.boundingRect();
return rect;
}
int HistogramItem::rtti() const
{
}
{
painter
->setPen
(QPen(d_data
->color
));
// const int x0 = xMap.transform(baseline());
const int y0 = yMap.transform(baseline());
for ( int i = 0; i < (int)iData.size(); i++ )
{
const int y2 = yMap.transform(iData.value(i));
int x1 = xMap.transform(iData.interval(i).minValue());
int x2 = xMap.transform(iData.interval(i).maxValue());
drawBar(painter, Qt::Vertical,
QRect(x1, y0, x2
- x1, y2
- y0
) );
}
}
void HistogramItem
::drawBar(QPainter *painter,
Qt::Orientation, const QRect& rect) const
{
painter->save();
const QColor color
(painter
->pen
().
color());
#if QT_VERSION >= 0x040000
const QRect r
= rect.
normalized();
#else
const QRect r
= rect.
normalize();
#endif
const int factor = 125;
const QColor light
(color.
light(factor
));
const QColor dark
(color.
dark(factor
));
painter->setBrush(color);
r.width()-3, r.height()-3);
painter->restore();
}
#include "histogram_item.h"
class HistogramItem::PrivateData
{
public:
int attributes;
QwtIntervalData data;
QColor color;
double reference;
};
HistogramItem::HistogramItem(const QwtText &title):
QwtPlotItem(title)
{
init();
}
HistogramItem::HistogramItem(const QString &title):
QwtPlotItem(QwtText(title))
{
init();
}
HistogramItem::~HistogramItem()
{
delete d_data;
}
void HistogramItem::init()
{
d_data = new PrivateData();
setZ(20);
}
double HistogramItem::baseline() const
{
return d_data->reference;
}
void HistogramItem::setData(const QwtIntervalData &data)
{
d_data->data = data;
itemChanged();
}
void HistogramItem::setColor(const QColor &color)
{
if ( d_data->color != color )
{
d_data->color = color;
itemChanged();
}
}
QwtDoubleRect HistogramItem::boundingRect() const
{
QwtDoubleRect rect = d_data->data.boundingRect();
return rect;
}
int HistogramItem::rtti() const
{
return QwtPlotItem::Rtti_PlotHistogram;
}
void HistogramItem::draw(QPainter *painter, const QwtScaleMap &xMap,
const QwtScaleMap &yMap, const QRect &) const
{
const QwtIntervalData &iData = d_data->data;
painter->setPen(QPen(d_data->color));
// const int x0 = xMap.transform(baseline());
const int y0 = yMap.transform(baseline());
for ( int i = 0; i < (int)iData.size(); i++ )
{
const int y2 = yMap.transform(iData.value(i));
int x1 = xMap.transform(iData.interval(i).minValue());
int x2 = xMap.transform(iData.interval(i).maxValue());
drawBar(painter, Qt::Vertical,
QRect(x1, y0, x2 - x1, y2 - y0) );
}
}
void HistogramItem::drawBar(QPainter *painter,
Qt::Orientation, const QRect& rect) const
{
painter->save();
const QColor color(painter->pen().color());
#if QT_VERSION >= 0x040000
const QRect r = rect.normalized();
#else
const QRect r = rect.normalize();
#endif
const int factor = 125;
const QColor light(color.light(factor));
const QColor dark(color.dark(factor));
painter->setBrush(color);
QwtPainter::drawRect(painter, r.x()+1 , r.y()+1,
r.width()-3, r.height()-3);
painter->restore();
}
To copy to clipboard, switch view to plain text mode
histogram.h
#ifndef HISTOGRAM_ITEM_H
#define HISTOGRAM_ITEM_H
#include <qglobal.h>
#include <qcolor.h>
#include <qapplication.h>
#include <qmainwindow.h>
#include <qwt_counter.h>
#include <qtoolbar.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qfile.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <QMessageBox>
#include <QMainWindow>
#include <QTextStream>
#include <QTextOStream>
#include <stdlib.h>
#include <qpen.h>
#include <qwt_plot.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_marker.h>
#include <qwt_interval_data.h>
#include <qstring.h>
#include <qpainter.h>
#include <qwt_painter.h>
#include <qwt_scale_map.h>
#include "qwt_plot_item.h"
{
public:
explicit HistogramItem
(const QwtText &title
);
virtual ~HistogramItem();
void setColor
(const QColor &);
virtual QwtDoubleRect boundingRect() const;
virtual int rtti() const;
void setBaseline(double reference);
double baseline() const;
enum HistogramAttribute
{
Auto = 0,
Xfy = 1
};
void setHistogramAttribute(HistogramAttribute, bool on = true);
bool testHistogramAttribute(HistogramAttribute) const;
protected:
Qt
::Orientation o,
const QRect &) const;
private:
void init();
class PrivateData;
PrivateData *d_data;
};
#endif
#ifndef HISTOGRAM_ITEM_H
#define HISTOGRAM_ITEM_H
#include <qglobal.h>
#include <qcolor.h>
#include <qapplication.h>
#include <qmainwindow.h>
#include <qwt_counter.h>
#include <qtoolbar.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qfile.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <QMessageBox>
#include <QMainWindow>
#include <QTextStream>
#include <QTextOStream>
#include <stdlib.h>
#include <qpen.h>
#include <qwt_plot.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_marker.h>
#include <qwt_interval_data.h>
#include <qstring.h>
#include <qpainter.h>
#include <qwt_painter.h>
#include <qwt_scale_map.h>
#include "qwt_plot_item.h"
class QwtIntervalData;
class QString;
class HistogramItem: public QwtPlotItem
{
public:
explicit HistogramItem(const QString &title = QString::null);
explicit HistogramItem(const QwtText &title);
virtual ~HistogramItem();
void setData(const QwtIntervalData &data);
const QwtIntervalData &data() const;
void setColor(const QColor &);
QColor color() const;
virtual QwtDoubleRect boundingRect() const;
virtual int rtti() const;
virtual void draw(QPainter *, const QwtScaleMap &xMap,
const QwtScaleMap &yMap, const QRect &) const;
void setBaseline(double reference);
double baseline() const;
enum HistogramAttribute
{
Auto = 0,
Xfy = 1
};
void setHistogramAttribute(HistogramAttribute, bool on = true);
bool testHistogramAttribute(HistogramAttribute) const;
protected:
virtual void drawBar(QPainter *,
Qt::Orientation o, const QRect &) const;
private:
void init();
class PrivateData;
PrivateData *d_data;
};
#endif
To copy to clipboard, switch view to plain text mode
main.cpp
#include "histogram_item.h"
int main(int argc, char **argv)
{
plot.
setCanvasBackground(QColor(Qt
::white));
plot.setTitle("Histogram");
grid->enableXMin(true);
grid->enableYMin(true);
grid
->setMajPen
(QPen(Qt
::black,
0, Qt
::DotLine));
grid
->setMinPen
(QPen(Qt
::gray,
0 , Qt
::DotLine));
grid->attach(&plot);
HistogramItem *histogram = new HistogramItem();
histogram->setColor(Qt::darkCyan);
const int numValues = 10000;
QwtArray<QwtDoubleInterval> intervals(numValues);
QwtArray<double> values(numValues);
QFile file( "C:\\path\\to\\file\\file.txt" );
int count[10000];
if(file.
open(QFile::ReadOnly)) {
int i = 1;
line = stream.readLine();
while(!line.isNull())
{
line = stream.readLine();
if(line.toInt() != 0)
{
lines += line;
count[i-1] = line.toInt();
i++;
}
else{
lines+=line;
i++;
}
}
}
file.close();
double pos = 0.0;
for ( int i = 0; i < (int)intervals.size(); i++ )
{
int width = 1;
int value = count[i];
values[i] = value;
pos += width;
}
histogram->attach(&plot);
plot.
setAxisScale(QwtPlot::yLeft,
0.0,
10000.0);
plot.
setAxisScale(QwtPlot::xBottom,
-1, pos
);
plot.replot();
#if QT_VERSION < 0x040000
a.setMainWidget(&plot);
#endif
plot.resize(800,600);
plot.show();
return a.exec();
}
#include "histogram_item.h"
int main(int argc, char **argv)
{
QApplication a(argc, argv);
QwtPlot plot;
plot.setCanvasBackground(QColor(Qt::white));
plot.setTitle("Histogram");
QwtPlotGrid *grid = new QwtPlotGrid;
grid->enableXMin(true);
grid->enableYMin(true);
grid->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
grid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));
grid->attach(&plot);
HistogramItem *histogram = new HistogramItem();
histogram->setColor(Qt::darkCyan);
const int numValues = 10000;
QwtArray<QwtDoubleInterval> intervals(numValues);
QwtArray<double> values(numValues);
QStringList lines;
QFile file( "C:\\path\\to\\file\\file.txt" );
int count[10000];
if(file.open(QFile::ReadOnly))
{
QTextStream stream(&file);
QString line;
int i = 1;
line = stream.readLine();
while(!line.isNull())
{
line = stream.readLine();
if(line.toInt() != 0)
{
lines += line;
count[i-1] = line.toInt();
i++;
}
else{
lines+=line;
i++;
}
}
}
file.close();
double pos = 0.0;
for ( int i = 0; i < (int)intervals.size(); i++ )
{
int width = 1;
int value = count[i];
intervals[i] = QwtDoubleInterval(pos-.5*double(width), pos + .5*double(width));
values[i] = value;
pos += width;
}
histogram->setData(QwtIntervalData(intervals, values));
histogram->attach(&plot);
plot.setAxisScale(QwtPlot::yLeft, 0.0, 10000.0);
plot.setAxisScale(QwtPlot::xBottom, -1, pos);
plot.replot();
#if QT_VERSION < 0x040000
a.setMainWidget(&plot);
#endif
plot.resize(800,600);
plot.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks