12#include "RangeGroup.h"
13#include "datasource/ChartDataSource.h"
14#include "scenegraph/BarChartNode.h"
26void BarChart::setSpacing(qreal newSpacing)
28 if (newSpacing == m_spacing) {
32 m_spacing = newSpacing;
42void BarChart::setBarWidth(qreal newBarWidth)
44 if (newBarWidth == m_barWidth) {
48 m_barWidth = newBarWidth;
58void BarChart::setRadius(qreal newRadius)
60 if (newRadius == m_radius) {
76 if (newOrientation == m_orientation) {
80 m_orientation = newOrientation;
81 m_orientationChanged =
true;
83 Q_EMIT orientationChanged();
88 return m_backgroundColor;
91void BarChart::setBackgroundColor(
const QColor &newBackgroundColor)
93 if (newBackgroundColor == m_backgroundColor) {
97 m_backgroundColor = newBackgroundColor;
99 Q_EMIT backgroundColorChanged();
104 BarChartNode *barNode =
nullptr;
106 if (m_orientationChanged) {
109 m_orientationChanged =
false;
113 barNode =
new BarChartNode{};
121 matrix.
rotate(90.0, 0.0, 0.0, 1.0);
122 transformNode->setMatrix(matrix);
123 node = transformNode;
127 barNode =
static_cast<BarChartNode *
>(node);
129 barNode =
static_cast<BarChartNode *
>(node->
childAtIndex(0));
138 matrix.
rotate(90.0, 0.0, 0.0, 1.0);
142 barNode->setBars(calculateBars());
143 barNode->setRadius(m_radius);
144 barNode->setBackgroundColor(m_backgroundColor);
157 m_barDataItems.
clear();
169 auto generator = [&,
this, i = range.startX]()
mutable ->
QList<BarData> {
172 for (
int j = 0; j < sources.count(); ++j) {
173 auto value = (sources.at(j)->item(i).toReal() - range.startY) / range.distanceY;
174 colorInfos << BarData{value, colors->item(colorIndex).
value<
QColor>()};
183 for (
auto &[colorVal, _] : colorInfos) {
184 colorVal += previous;
200 std::generate_n(m_barDataItems.
begin(), range.distanceX, generator);
202 std::generate_n(m_barDataItems.
rbegin(), range.distanceX, generator);
216 float w = m_barWidth;
220 w = targetWidth / totalItemCount - m_spacing;
222 auto x = float(m_spacing / 2);
223 const auto itemSpacing = w + m_spacing;
225 for (
const auto &items :
std::as_const(m_barDataItems)) {
228 std::transform(items.crbegin(), items.crend(), std::back_inserter(result), [
x, w](
const BarData &entry) {
229 return Bar{x, w, float(entry.value), entry.color};
233 std::transform(items.cbegin(), items.cend(), std::back_inserter(result), [&
x, itemSpacing, w](
const BarData &entry) {
234 Bar bar{x, w, float(entry.value), entry.color};
241 const auto itemSpacing = targetWidth / m_barDataItems.size();
243 auto x = float(itemSpacing / 2 - m_barWidth / 2);
245 for (
const auto &items :
std::as_const(m_barDataItems)) {
247 std::transform(items.crbegin(), items.crend(), std::back_inserter(result), [x, w](
const BarData &entry) {
248 return Bar{x, w, float(entry.value), entry.color};
254 const auto totalWidth = m_barWidth * valueSources().count() + m_spacing * (valueSources().count() - 1);
256 auto x = float(itemSpacing / 2 - totalWidth / 2);
258 for (
const auto &items :
std::as_const(m_barDataItems)) {
260 for (
int i = 0; i < items.count(); ++i) {
261 auto entry = items.at(i);
262 result << Bar{float(x + i * (m_barWidth + m_spacing)), w, float(entry.value), entry.color};
272#include "moc_BarChart.cpp"
Orientation orientation
The orientation of bars in the chart.
void onDataChanged() override
Reimplemented from Chart.
QColor backgroundColor
The background color of bars in the chart.
QSGNode * updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *) override
Reimplemented from QQuickItem.
qreal spacing
The spacing between bars for each value source.
qreal barWidth
The width of individual bars in the chart.
@ VerticalOrientation
Bars are oriented vertically, with low values at the bottom and high values at the top.
qreal radius
The radius of the ends of bars in the chart in pixels.
QQmlListProperty< ChartDataSource > valueSources
The data sources providing the data this chart needs to render.
ChartDataSource * colorSource
The data source to use for colors of chart items.
@ IndexSourceValues
Index each value, restart indexing for each value source.
@ IndexEachSource
Index each value source, never index individual values.
IndexingMode indexingMode
The indexing mode used for indexing colors and names.
A base class for Charts that are based on an X/Y grid.
virtual void updateComputedRange()
Re-calculate the chart's range.
Direction direction
Which direction this chart's X axis runs.
bool stacked
Whether the values of each value source should be stacked.
ComputedRange computedRange() const
Get the complete, calculated range for this chart.
@ ZeroAtStart
Zero is at the beginning of the chart, values run from begin to end.
QList< T > & fill(parameter_type value, qsizetype size)
reverse_iterator rbegin()
void reserve(qsizetype size)
qsizetype size() const const
T value(qsizetype i) const const
void rotate(const QQuaternion &quaternion)
void translate(const QVector3D &vector)
virtual QRectF boundingRect() const const
QSizeF size() const const
QPointF topLeft() const const
void appendChildNode(QSGNode *node)
QSGNode * childAtIndex(int i) const const