1

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?

Herr von Wurst
  • 2,571
  • 5
  • 32
  • 53
  • Do you have an error message, or just a plain crash? – derM - not here for BOT dreams Nov 28 '17 at 21:22
  • The error message is `TypeError: Type error` . No crash though. The reason is `histogramSeries.at(0)` being `null`, even though I can see the BarSet being displayed in the chart. – Herr von Wurst Nov 28 '17 at 21:25
  • did you make progress on this issue. If so could you then perhaps post the solution? – boojum Apr 10 '18 at 04:03
  • This is not going to be very helpfull to you, sorry. I solved the issue, but don't remember how. However the code posted in the question is identical to the code I am using today. Therefore the error must have been somewhere else. – Herr von Wurst Apr 10 '18 at 18:49

0 Answers0