mirror of https://github.com/qt/qtgraphs.git
Add support for printf style formatting for valueAxisLabels
Task-number: QTBUG-133359 Pick-to: 6.9 6.8 Change-Id: Ice8c69dbb98874617de126b945361b23bc4e1b97 Reviewed-by: Tomi Korpipää <tomi.korpipaa@qt.io>
This commit is contained in:
parent
22649aac4e
commit
6090cb94f1
|
|
@ -965,8 +965,14 @@ void AxisRenderer::updateValueYAxisLabels(QValueAxis *axis, const QRectF rect)
|
||||||
if (decimals < 0)
|
if (decimals < 0)
|
||||||
decimals = getValueDecimalsFromRange(m_axisVerticalValueRange);
|
decimals = getValueDecimalsFromRange(m_axisVerticalValueRange);
|
||||||
const QString f = axis->labelFormat();
|
const QString f = axis->labelFormat();
|
||||||
|
QString label;
|
||||||
|
if (f.length() <= 1) {
|
||||||
char format = f.isEmpty() ? 'f' : f.front().toLatin1();
|
char format = f.isEmpty() ? 'f' : f.front().toLatin1();
|
||||||
QString label = QString::number(number, format, decimals);
|
label = QString::number(number, format, decimals);
|
||||||
|
} else {
|
||||||
|
QByteArray array = f.toLatin1();
|
||||||
|
label = QString::asprintf(array.constData(), number);
|
||||||
|
}
|
||||||
if (m_verticalAxisOnRight) {
|
if (m_verticalAxisOnRight) {
|
||||||
setLabelTextProperties(textItem, label, false,
|
setLabelTextProperties(textItem, label, false,
|
||||||
QQuickText::HAlignment::AlignLeft,
|
QQuickText::HAlignment::AlignLeft,
|
||||||
|
|
@ -1024,8 +1030,14 @@ void AxisRenderer::updateValueXAxisLabels(QValueAxis *axis, const QRectF rect)
|
||||||
if (decimals < 0)
|
if (decimals < 0)
|
||||||
decimals = getValueDecimalsFromRange(m_axisHorizontalValueRange);
|
decimals = getValueDecimalsFromRange(m_axisHorizontalValueRange);
|
||||||
const QString f = axis->labelFormat();
|
const QString f = axis->labelFormat();
|
||||||
|
QString label;
|
||||||
|
if (f.length() <= 1) {
|
||||||
char format = f.isEmpty() ? 'f' : f.front().toLatin1();
|
char format = f.isEmpty() ? 'f' : f.front().toLatin1();
|
||||||
QString label = QString::number(number, format, decimals);
|
label = QString::number(number, format, decimals);
|
||||||
|
} else {
|
||||||
|
QByteArray array = f.toLatin1();
|
||||||
|
label = QString::asprintf(array.constData(), number);
|
||||||
|
}
|
||||||
if (m_horizontalAxisOnTop) {
|
if (m_horizontalAxisOnTop) {
|
||||||
setLabelTextProperties(textItem, label, true,
|
setLabelTextProperties(textItem, label, true,
|
||||||
QQuickText::HAlignment::AlignHCenter,
|
QQuickText::HAlignment::AlignHCenter,
|
||||||
|
|
|
||||||
|
|
@ -96,9 +96,18 @@ void tst_valueaxis::initializeProperties()
|
||||||
QCOMPARE(m_axis->zoom(), 2.0);
|
QCOMPARE(m_axis->zoom(), 2.0);
|
||||||
QCOMPARE(m_axis->pan(), 1.0);
|
QCOMPARE(m_axis->pan(), 1.0);
|
||||||
|
|
||||||
|
m_axis->setLabelFormat("%.2f cakes");
|
||||||
|
QCOMPARE(m_axis->labelFormat(), "%.2f cakes");
|
||||||
|
|
||||||
|
//Constuct a string same way we do in axisRenderer.
|
||||||
|
QByteArray format = m_axis->labelFormat().toLatin1();
|
||||||
|
QString formatTest = QString::asprintf(format.constData(), m_axis->min());
|
||||||
|
|
||||||
|
QCOMPARE(formatTest, "5.00 cakes");
|
||||||
|
|
||||||
QCOMPARE(spy0.size(), 1);
|
QCOMPARE(spy0.size(), 1);
|
||||||
QCOMPARE(spy1.size(), 1);
|
QCOMPARE(spy1.size(), 1);
|
||||||
QCOMPARE(spy2.size(), 1);
|
QCOMPARE(spy2.size(), 2);
|
||||||
QCOMPARE(spy3.size(), 1);
|
QCOMPARE(spy3.size(), 1);
|
||||||
QCOMPARE(spy4.size(), 1);
|
QCOMPARE(spy4.size(), 1);
|
||||||
QCOMPARE(spy5.size(), 1);
|
QCOMPARE(spy5.size(), 1);
|
||||||
|
|
|
||||||
|
|
@ -189,6 +189,9 @@ Item {
|
||||||
compare(initialized.tickAnchor, 2)
|
compare(initialized.tickAnchor, 2)
|
||||||
compare(initialized.tickInterval, 3.0)
|
compare(initialized.tickInterval, 3.0)
|
||||||
|
|
||||||
|
initialized.labelFormat = "%.1f test"
|
||||||
|
compare(initialized.labelFormat, "%.1f test")
|
||||||
|
|
||||||
// Common properties from AbstractAxis
|
// Common properties from AbstractAxis
|
||||||
compare(initialized.gridVisible, true)
|
compare(initialized.gridVisible, true)
|
||||||
compare(initialized.labelsAngle, 45)
|
compare(initialized.labelsAngle, 45)
|
||||||
|
|
@ -206,7 +209,7 @@ Item {
|
||||||
// Signals
|
// Signals
|
||||||
compare(minSpy.count, 1)
|
compare(minSpy.count, 1)
|
||||||
compare(maxSpy.count, 1)
|
compare(maxSpy.count, 1)
|
||||||
compare(labelFormatSpy.count, 1)
|
compare(labelFormatSpy.count, 2)
|
||||||
compare(labelDecimalsSpy.count, 1)
|
compare(labelDecimalsSpy.count, 1)
|
||||||
compare(tickSpy.count, 1)
|
compare(tickSpy.count, 1)
|
||||||
compare(subTickSpy.count, 1)
|
compare(subTickSpy.count, 1)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue