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();
171 auto generator = [&,
this, i = range.startX]()
mutable ->
QList<BarData> {
174 for (
int j = 0; j < sources.count(); ++j) {
175 auto value = (sources.at(j)->item(i).toReal() - range.startY) / range.distanceY;
176 auto color = colors->item(colorIndex).value<
QColor>();
178 if (highlightIndex >= 0 && highlightIndex != colorIndex) {
182 colorInfos << BarData{value, color};
191 for (
auto &[colorVal, _] : colorInfos) {
192 colorVal += previous;
208 std::generate_n(m_barDataItems.
begin(), range.distanceX, generator);
210 std::generate_n(m_barDataItems.
rbegin(), range.distanceX, generator);
224 float w = m_barWidth;
228 w = targetWidth / totalItemCount - m_spacing;
230 auto x = float(m_spacing / 2);
231 const auto itemSpacing = w + m_spacing;
233 for (
const auto &items : std::as_const(m_barDataItems)) {
236 std::transform(items.crbegin(), items.crend(), std::back_inserter(result), [
x, w](
const BarData &entry) {
237 return Bar{x, w, float(entry.value), entry.color};
241 std::transform(items.cbegin(), items.cend(), std::back_inserter(result), [&
x, itemSpacing, w](
const BarData &entry) {
242 Bar bar{x, w, float(entry.value), entry.color};
249 const auto itemSpacing = targetWidth / m_barDataItems.size();
251 auto x = float(itemSpacing / 2 - m_barWidth / 2);
253 for (
const auto &items : std::as_const(m_barDataItems)) {
255 std::transform(items.crbegin(), items.crend(), std::back_inserter(result), [x, w](
const BarData &entry) {
256 return Bar{x, w, float(entry.value), entry.color};
262 const auto totalWidth = m_barWidth * valueSources().count() + m_spacing * (valueSources().count() - 1);
264 auto x = float(itemSpacing / 2 - totalWidth / 2);
266 for (
const auto &items : std::as_const(m_barDataItems)) {
268 for (
int i = 0; i < items.count(); ++i) {
269 auto entry = items.at(i);
270 result << Bar{float(x + i * (m_barWidth + m_spacing)), w, float(entry.value), entry.color};
280#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.
QColor desaturate(const QColor &input)
Desaturate and de-emphasise a color.
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.
int highlight
The index of a value source to highlight.
@ 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
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