Add series index dynamic properties for custom delegates

Support for series index in BarSeries (barIndex) and XYSeries
(pointIndex). These will allow new customization possibilities.

Task-number: QTBUG-129382
Change-Id: I4b332efda9c630bcb0394ffa49e999a3aca4fbf1
Reviewed-by: Tomi Korpipää <tomi.korpipaa@qt.io>
Reviewed-by: Jere Tuliniemi <jere.tuliniemi@qt.io>
This commit is contained in:
Kaj Grönholm 2024-09-27 08:37:00 +03:00
parent f5f3d9b16b
commit ddab1d76dc
6 changed files with 27 additions and 3 deletions

View File

@ -262,6 +262,10 @@ QT_BEGIN_NAMESPACE
\li barSelected \li barSelected
\li This value is true when the bar is selected, meaning that the bar index \li This value is true when the bar is selected, meaning that the bar index
is in \l{QBarSet::selectedBars}. is in \l{QBarSet::selectedBars}.
\row
\li int
\li barIndex
\li Index of the bar, from 0 to the amount of bars - 1. [since 6.9]
\endtable \endtable
To use any of these, add property with the defined name into your custom component. To use any of these, add property with the defined name into your custom component.
@ -308,6 +312,10 @@ QT_BEGIN_NAMESPACE
\li barSelected \li barSelected
\li This value is true when the bar is selected, meaning that the bar index \li This value is true when the bar is selected, meaning that the bar index
is in \l{BarSet::selectedBars}. is in \l{BarSet::selectedBars}.
\row
\li int
\li barIndex
\li Index of the bar, from 0 to the amount of bars - 1. [since 6.9]
\endtable \endtable
To use any of these, add property with the defined name into your custom component. To use any of these, add property with the defined name into your custom component.

View File

@ -17,6 +17,7 @@ static const char* TAG_BAR_BORDER_WIDTH = "barBorderWidth";
static const char* TAG_BAR_SELECTED = "barSelected"; static const char* TAG_BAR_SELECTED = "barSelected";
static const char* TAG_BAR_VALUE = "barValue"; static const char* TAG_BAR_VALUE = "barValue";
static const char* TAG_BAR_LABEL = "barLabel"; static const char* TAG_BAR_LABEL = "barLabel";
static const char* TAG_BAR_INDEX = "barIndex";
BarsRenderer::BarsRenderer(QGraphsView *graph) BarsRenderer::BarsRenderer(QGraphsView *graph)
: QQuickItem(graph) : QQuickItem(graph)
@ -179,6 +180,8 @@ void BarsRenderer::updateComponents(QBarSeries *series)
barItem->setProperty(TAG_BAR_VALUE, d.value); barItem->setProperty(TAG_BAR_VALUE, d.value);
if (barItem->property(TAG_BAR_LABEL).isValid()) if (barItem->property(TAG_BAR_LABEL).isValid())
barItem->setProperty(TAG_BAR_LABEL, d.label); barItem->setProperty(TAG_BAR_LABEL, d.label);
if (barItem->property(TAG_BAR_INDEX).isValid())
barItem->setProperty(TAG_BAR_INDEX, barIndex);
} else { } else {
// Set default rectangle bars // Set default rectangle bars
auto barItem = qobject_cast<QQuickRectangle *>(barItems[barIndex]); auto barItem = qobject_cast<QQuickRectangle *>(barItems[barIndex]);

View File

@ -25,6 +25,7 @@ static const char *TAG_POINT_SELECTED_COLOR = "pointSelectedColor";
static const char *TAG_POINT_SELECTED = "pointSelected"; static const char *TAG_POINT_SELECTED = "pointSelected";
static const char *TAG_POINT_VALUE_X = "pointValueX"; static const char *TAG_POINT_VALUE_X = "pointValueX";
static const char *TAG_POINT_VALUE_Y = "pointValueY"; static const char *TAG_POINT_VALUE_Y = "pointValueY";
static const char *TAG_POINT_INDEX = "pointIndex";
PointRenderer::PointRenderer(QGraphsView *graph) PointRenderer::PointRenderer(QGraphsView *graph)
: QQuickItem(graph) : QQuickItem(graph)
@ -139,6 +140,8 @@ void PointRenderer::updatePointDelegate(
marker->setProperty(TAG_POINT_VALUE_X, point.x()); marker->setProperty(TAG_POINT_VALUE_X, point.x());
if (marker->property(TAG_POINT_VALUE_Y).isValid()) if (marker->property(TAG_POINT_VALUE_Y).isValid())
marker->setProperty(TAG_POINT_VALUE_Y, point.y()); marker->setProperty(TAG_POINT_VALUE_Y, point.y());
if (marker->property(TAG_POINT_INDEX).isValid())
marker->setProperty(TAG_POINT_INDEX, pointIndex);
marker->setX(x - marker->width() / 2.0); marker->setX(x - marker->width() / 2.0);
marker->setY(y - marker->height() / 2.0); marker->setY(y - marker->height() / 2.0);

View File

@ -790,6 +790,10 @@ QColor QXYSeries::selectedColor() const
\li qreal \li qreal
\li pointValueY \li pointValueY
\li The value of the \l{QXYPoint::y} at this position. \li The value of the \l{QXYPoint::y} at this position.
\row
\li int
\li pointIndex
\li The index of the point, from 0 to the amount of points - 1. [since 6.9]
\endtable \endtable
To use any of these, add property with the defined name into your custom component. To use any of these, add property with the defined name into your custom component.
@ -836,6 +840,10 @@ QColor QXYSeries::selectedColor() const
\li real \li real
\li pointValueY \li pointValueY
\li The value of the \l{XYPoint::y} at this position. \li The value of the \l{XYPoint::y} at this position.
\row
\li int
\li pointIndex
\li The index of the point, from 0 to the amount of points - 1. [since 6.9]
\endtable \endtable
To use any of these, add property with the defined name into your custom component. To use any of these, add property with the defined name into your custom component.

View File

@ -71,8 +71,9 @@ Rectangle {
Rectangle { Rectangle {
id: comp id: comp
property color barColor property color barColor
property int barIndex
gradient: Gradient { gradient: Gradient {
GradientStop { position: 0.0; color: "red" } GradientStop { position: 0.0; color: (comp.barIndex % 2 === 0) ? "#f02020" : "#d0d020"}
GradientStop { position: 0.4; color: Qt.darker(comp.barColor, 2.0) } GradientStop { position: 0.4; color: Qt.darker(comp.barColor, 2.0) }
GradientStop { position: 1.0; color: comp.barColor } GradientStop { position: 1.0; color: comp.barColor }
} }

View File

@ -183,6 +183,7 @@ Rectangle {
property color pointColor property color pointColor
property real pointValueX property real pointValueX
property real pointValueY property real pointValueY
property int pointIndex
width: 16 width: 16
height: 16 height: 16
Rectangle { Rectangle {
@ -190,14 +191,14 @@ Rectangle {
color: "#202020" color: "#202020"
border.width: 2 border.width: 2
border.color: pointColor border.color: pointColor
radius: width / 2 radius: (pointIndex % 2 === 0) ? width / 2 : 2
} }
Text { Text {
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.top anchors.bottom: parent.top
color: "#ffffff" color: "#ffffff"
font.pixelSize: 16 font.pixelSize: 16
text: "(" + pointValueX.toFixed(1) + ", " + pointValueY.toFixed(1) + ")" text: pointIndex + ": (" + pointValueX.toFixed(1) + ", " + pointValueY.toFixed(1) + ")"
} }
} }
XYPoint { x: 0; y: 6.6 } XYPoint { x: 0; y: 6.6 }