Nasty hack to get a useful Y-axis label @ bw chart
This commit is contained in:
parent
e7a73a6b5a
commit
5e0248c282
|
@ -1,7 +1,7 @@
|
|||
import QtQuick 2.0
|
||||
import QtQuick.Layouts 1.3
|
||||
import QtQuick.Controls 2.0
|
||||
import QtCharts 2.0
|
||||
import QtCharts 2.1
|
||||
|
||||
import QClipboard 1.0
|
||||
import BandwidthChartData 1.0
|
||||
|
@ -11,6 +11,8 @@ Container {
|
|||
property var down: 0
|
||||
property var up: 0
|
||||
|
||||
property var lastCategoryLabel: null
|
||||
|
||||
BandwidthChartData {
|
||||
id: chartData
|
||||
}
|
||||
|
@ -25,6 +27,7 @@ Container {
|
|||
// console.log("new 'down' value: "+ down);
|
||||
chartData.addDownloadSample(down);
|
||||
chartData.updateDownloadSeries(rxData.upperSeries);
|
||||
recalculateGraphScale(chartData.getHighestSample());
|
||||
}
|
||||
|
||||
Layout.preferredHeight: 249
|
||||
|
@ -34,6 +37,24 @@ Container {
|
|||
color: Style.panelBackgroundColor
|
||||
}
|
||||
|
||||
function recalculateGraphScale(highestSample) {
|
||||
yAxis.max = highestSample;
|
||||
const label = makeRate(highestSample);
|
||||
|
||||
// if we need a new label, remove and re-add. ughly hack.
|
||||
// essentially, we use a single CategoryAxis category because this lets us position
|
||||
// a tick interval at a specific location with a specific label.
|
||||
// ValueAxis has no way of doing this.
|
||||
if (! lastCategoryLabel) {
|
||||
yAxis.append(label, highestSample);
|
||||
lastCategoryLabel = label;
|
||||
} else if (lastCategoryLabel != label) {
|
||||
yAxis.remove(lastCategoryLabel);
|
||||
yAxis.append(label, highestSample);
|
||||
lastCategoryLabel = label;
|
||||
}
|
||||
}
|
||||
|
||||
function makeRate(value)
|
||||
{
|
||||
var unit_idx = 0;
|
||||
|
@ -113,7 +134,7 @@ Container {
|
|||
|
||||
ValueAxis {
|
||||
id: xAxis
|
||||
labelFormat: "%d"
|
||||
labelFormat: "%e"
|
||||
min: 0
|
||||
max: 128
|
||||
labelsVisible: false
|
||||
|
@ -121,13 +142,11 @@ Container {
|
|||
titleVisible: false
|
||||
}
|
||||
|
||||
ValueAxis {
|
||||
CategoryAxis {
|
||||
id: yAxis
|
||||
min: 0
|
||||
max: 10000
|
||||
gridVisible: false
|
||||
labelFormat: "%d"
|
||||
titleVisible: false
|
||||
labelsPosition: CategoryAxis.AxisLabelsPositionOnValue
|
||||
}
|
||||
|
||||
AreaSeries {
|
||||
|
|
|
@ -24,27 +24,32 @@ BandwidthChartData::BandwidthChartData(QObject *parent)
|
|||
}
|
||||
}
|
||||
|
||||
static void appendSample(QVector<QPointF>& samples, uint64_t maxSamples, qreal sample) {
|
||||
static void appendSample(QVector<QPointF>& samples, uint64_t maxSamples, int64_t& highest, qreal sample) {
|
||||
highest = 0;
|
||||
if ((uint64_t)samples.size() >= maxSamples) {
|
||||
// shift entire dataset, discarding first (oldest), and append new sample
|
||||
for (uint64_t i=0; i<maxSamples-1; ++i) {
|
||||
// TODO: use timestamps instead of indices
|
||||
auto next = samples[i+1];
|
||||
samples[i].setY(next.y());
|
||||
|
||||
highest = std::max(highest, (int64_t)next.y());
|
||||
}
|
||||
samples[maxSamples-1].setY(sample);
|
||||
} else {
|
||||
QPointF point(samples.size(), sample);
|
||||
samples.append(point);
|
||||
}
|
||||
|
||||
highest = std::max(highest, (int64_t)sample);
|
||||
}
|
||||
|
||||
void BandwidthChartData::addUploadSample(qreal sample) {
|
||||
appendSample(m_uploadSamples, m_numSamples, sample);
|
||||
appendSample(m_uploadSamples, m_numSamples, m_highestUploadSample, sample);
|
||||
}
|
||||
|
||||
void BandwidthChartData::addDownloadSample(qreal sample) {
|
||||
appendSample(m_downloadSamples, m_numSamples, sample);
|
||||
appendSample(m_downloadSamples, m_numSamples, m_highestDownloadSample, sample);
|
||||
}
|
||||
|
||||
void BandwidthChartData::updateUploadSeries(QtCharts::QAbstractSeries *series) {
|
||||
|
@ -59,3 +64,15 @@ void BandwidthChartData::updateDownloadSeries(QtCharts::QAbstractSeries *series)
|
|||
xySeries->replace(m_downloadSamples);
|
||||
}
|
||||
|
||||
int BandwidthChartData::getHighestUploadSample() {
|
||||
return m_highestUploadSample;
|
||||
}
|
||||
|
||||
int BandwidthChartData::getHighestDownloadSample() {
|
||||
return m_highestDownloadSample;
|
||||
}
|
||||
|
||||
int BandwidthChartData::getHighestSample() {
|
||||
return std::max(m_highestUploadSample, m_highestDownloadSample);
|
||||
}
|
||||
|
||||
|
|
|
@ -31,12 +31,26 @@ public:
|
|||
Q_INVOKABLE void updateUploadSeries(QtCharts::QAbstractSeries *series);
|
||||
Q_INVOKABLE void updateDownloadSeries(QtCharts::QAbstractSeries *series);
|
||||
|
||||
/**
|
||||
* Get the highest samples currently being tracked
|
||||
*/
|
||||
Q_INVOKABLE int getHighestUploadSample();
|
||||
Q_INVOKABLE int getHighestDownloadSample();
|
||||
|
||||
/**
|
||||
* Get the greater of the highest upload or highest download samples
|
||||
*/
|
||||
Q_INVOKABLE int getHighestSample();
|
||||
|
||||
private:
|
||||
|
||||
uint64_t m_numSamples;
|
||||
QVector<QPointF> m_uploadSamples;
|
||||
QVector<QPointF> m_downloadSamples;
|
||||
|
||||
int64_t m_highestUploadSample = 0;
|
||||
int64_t m_highestDownloadSample = 0;
|
||||
|
||||
};
|
||||
|
||||
#endif // BANDWIDTH_CHART_DATA_H
|
||||
|
|
Loading…
Reference in New Issue