Files
@ 3f00e674e822
Branch filter:
Location: tempo-plotter/src/barchart.cpp
3f00e674e822
3.1 KiB
text/x-c++hdr
show barplot at minimum size
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | /*
Copyright © 2017 Hasan Yavuz Özderya
This file is part of serialplot.
serialplot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
serialplot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with serialplot. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QPalette>
#include <qwt_scale_map.h>
#include "barchart.h"
BarChart::BarChart(ChannelManager* channelMan)
{
_channelMan = channelMan;
setSpacing(0);
}
void BarChart::resample()
{
setSamples(chartData());
}
void BarChart::setBorderColor(QColor color)
{
borderColor = color;
}
QVector<double> BarChart::chartData() const
{
unsigned numChannels = _channelMan->numOfChannels();
unsigned numOfSamples = _channelMan->numOfSamples();
QVector<double> data(numChannels);
for (unsigned i = 0; i < numChannels; i++)
{
data[i] = _channelMan->channelBuffer(i)->sample(numOfSamples-1);
}
return data;
}
QwtColumnSymbol* BarChart::specialSymbol(int sampleIndex, const QPointF& sample) const
{
unsigned numChannels = _channelMan->numOfChannels();
if (sampleIndex < 0 || sampleIndex > (int) numChannels)
{
return NULL;
}
auto info = _channelMan->infoModel();
auto color = info->color(sampleIndex);
QwtColumnSymbol* symbol = new QwtColumnSymbol(QwtColumnSymbol::Box);
symbol->setLineWidth(1);
symbol->setFrameStyle(QwtColumnSymbol::Plain);
QPalette pal(color);
pal.setColor(QPalette::Dark, borderColor);
symbol->setPalette(pal);
return symbol;
}
void BarChart::drawSample(
QPainter *painter, const QwtScaleMap &xMap, const QwtScaleMap &yMap,
const QRectF &canvasRect, const QwtInterval &boundingInterval,
int index, const QPointF &sample ) const
{
QwtPlotBarChart::drawSample(painter, xMap, yMap, canvasRect, boundingInterval, index, sample);
// calculate bar size
const double barWidth = sampleWidth(xMap, canvasRect.width(),
boundingInterval.width(), sample.y());
const double x = xMap.transform( sample.x() );
const double x1 = x - 0.5 * barWidth;
// const double x2 = x + 0.5 * barWidth;
const double y1 = yMap.transform(baseline());
const double y2 = yMap.transform(sample.y());
const double barHeight = fabs(y2 - y1);
// create and calculate text size
const auto valueStr = QString::number(sample.y());
auto valueText = QwtText(valueStr, QwtText::PlainText);
const auto textSize = valueText.textSize();
auto r = QRectF(x1, y2, barWidth, textSize.height());
if (y2 > y1) r.moveBottom(y2);
// display text if there is enough space
if (barHeight >= textSize.height() && barWidth >= textSize.width())
{
valueText.draw(painter, r);
}
}
|