Customizing BarSet in QML Charts

1.4k Views Asked by At

I am trying to change the look of a BarChart using QtCharts 2.1. At the moment I apply the desired color and styling to the single QBarSets like this:

import QtQuick 2.8
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import QtCharts 2.1

import QtQuick.Window 2.2

ChartView {
    id: root
    width: 600
    height: 400
    antialiasing: true
    backgroundColor: "transparent"
    legend.visible: false

    property var histogramProvider

    Connections {
        target: histogramProvider
        onHistogramChanged: {
            console.log("histogram changed")
            histogramProvider.updateSeries(histogramSeries)
            console.log(histogramSeries)
            console.log(histogramSeries.at(0))
            histogramSeries.at(0).color = Qt.rgba(0.9, 0.2, 0.1, 0.5)
        }
        onAxesChanged: {
            xAxis.min = xMin
            xAxis.max = xMax
            yAxis.min = yMin
            yAxis.max = yMax
        }
    }

    ValueAxis {
            id: yAxis
            min: 0
            max: 100
            tickCount: 3
            gridVisible: false
            labelsColor: Style.fontColorLight
            labelsFont: Qt.font({pointSize: 8})
            titleText: qsTr("Count")
            labelFormat: "%.1f"
    }

    ValueAxis {
        id: xAxis
        tickCount: 3
        gridVisible: false
        labelsColor: "white"
        labelFormat: "%i"
        min: 0
        max: 100
        titleText: qsTr("Elongation")
    }

    StackedBarSeries {
        id: histogramSeries
        axisX: xAxis
        axisY: yAxis
    }
}

I can see the BarSets with the default colors in the chart, hwever I can not change the colors. The code produces the following console output from onHistogramChanged:

qml: QtCharts::DeclarativeStackedBarSeries(0x1df550cd830)
qml: null
qrc:/qml/Histogram.qml:27: TypeError: Type error

I update the BarSeries from C++ like this:

void HistogramProvider::updateSeries(QStackedBarSeries *barSeries)
{
    [...]
    barSeries->clear();
    QBarSet* set = new QBarSet("Histogram", barSeries);
    set->setBorderColor(QColor("transparent"));
    set->setColor(QColor("#6f74dd"));
    for(int i = 0; i < bins.size(); i++) {
        set->append(bins.at(i));
    }
    qDebug() << "blubb";
    barSeries->append(set);
}

What am I doing wrong here?

0

There are 0 best solutions below