Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. GraphsView: Labels in Bars in a BarSeries stay after clearing the series
Forum Update on Monday, May 27th 2025

GraphsView: Labels in Bars in a BarSeries stay after clearing the series

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
graphsgraphsviewbarseriesqml
1 Posts 1 Posters 727 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dizarc
    wrote on last edited by Dizarc
    #1

    Currently I have a GraphsView with a BarSeries inside it.
    The graph has Values on the Y axis and Dates on the X axis and It supports filtering by different variables such as dates.

    Since i want the value to be clearly viewed above the bars i have used the properties regarding labels inside the BarSeries. But every time I filter the data some of the old values still show. I have tried using clear and append for the new data, I have tried turning the labels visibility off and on, I have also tried removing the series from the GraphView and adding it back with the new data which works but adds a different type of issue where the series is shown for a split second and gets removed after.
    The weird thing I have seen though is that the data is shown normally every time i filter the default launch way and any old labels get removed.

    I have also attached screenshots to show the issue.

    Using QtQuick 6.8 and QtGraphs Windows 11 MinGW

    Graph used in the application:

    GraphsView {
          id: graphsView
    
          antialiasing: true
    
          Layout.fillHeight: true
          Layout.fillWidth: true
    
          axisX: BarCategoryAxis{
            categories: LogData.categories
            subGridVisible : false
            gridVisible: false
          }
    
          axisY: ValueAxis {
            min: 0
            max: 200
          }
    
          theme: GraphsTheme {
            backgroundVisible: false
    
            seriesColors: Style.inputBoxColor
            borderWidth: 2
            borderColors: Style.borderColor
    
            grid.mainColor: Style.inputBoxColor
            plotAreaBackgroundColor: Style.backgroundColor
    
            axisXLabelFont.pointSize: 10
            axisX.labelTextColor: Style.textColor
            axisX.mainColor: "transparent"
            axisX.subColor: "transparent"
    
            axisYLabelFont.pointSize: 10
            axisY.labelTextColor: Style.textColor
            axisY.mainColor: Style.textColor
          }
    
          BarSeries{
            id: bars
    
            labelsVisible: true
            labelsPrecision: 0
            labelsFormat: '<font color="' + Style.textColor + '" size="4">@value</font>'
            labelsPosition: BarSeries.LabelsPosition.OutsideEnd
    
            Component.onCompleted: bars.append(LogData.generateSeries("all", -1, -1, "day"))
          }
        }
    

    I have filtering done with other variables but here is simply filtering done by dates:

    ButtonGroup{
          id: radioGroup
    
          onClicked: {
            graphColumn.sizeSelected = -1
            sizesComboBox.displayText = qsTr("All")
    
            typesComboBox.currentIndex = -1
            typesComboBox.displayText = qsTr("All")
    
            clothesComboBox.displayText = qsTr("")
    
            bars.clear()
            bars.insert(0, LogData.generateSeries("all", -1, graphColumn.sizeSelected, radioGroup.checkedButton.text))
          }
        }
    
        RowLayout{
          Layout.alignment: Qt.AlignVCenter
    
          Text {
            text: qsTr("Filter by date:")
    
            color: Style.textColor
            font.pointSize: 12
          }
    
          CustomRadioButton {
            checked: true
            text: qsTr("day")
            ButtonGroup.group: radioGroup
          }
    
          CustomRadioButton {
            text: qsTr("month")
            ButtonGroup.group: radioGroup
          }
    
          CustomRadioButton {
            text: qsTr("year")
            ButtonGroup.group: radioGroup
          }
        }
    

    Getting the data works normally as intended but I am putting it out here just incase:
    LogData.h:

    class LogData : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QStringList categories READ categories NOTIFY categoriesChanged)
    public:
        explicit LogData(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase());
    
        QStringList categories() const;
    
    public slots:
        bool log(const int &cId, const int &tId, const QString &sName, const int &value);
        QBarSet* generateSeries(const QString &filterType, const int &filterId, const int &sizeId, const QString& dateFormat);
    
    signals:
        void categoriesChanged();
    
    private:
        QStringList m_categories;
    };
    

    LogData.cpp:

    QBarSet *LogData::generateSeries(const QString &filter, const int &filterId, const int &sizeId, const QString &dateFilter)
    {
        QBarSet* barSet = new QBarSet("Count");
    
        //SQL date grouping format
        QString dateFormat;
        if(dateFilter == "day")
            dateFormat = "strftime('%Y-%m-%d', changeDate)";
        else if(dateFilter == "month")
            dateFormat = "strftime('%Y-%m', changeDate)";
        else if(dateFilter == "year")
            dateFormat = "strftime('%Y', changeDate)";
    
        QString queryStr = "SELECT " + dateFormat + " as period, SUM(changeCount) FROM ChangeLog ";
    
        QStringList whereClause;
        if(sizeId != -1)
            whereClause.append("sizeId = :sizeId");
    
        if(filter != "all"){
            if(filter == "type")
                whereClause.append("typeId = :typeId");
            else if(filter == "item")
                whereClause.append("clothingId = :clothingId");
        }
    
        if(!whereClause.isEmpty())
            queryStr += "WHERE " + whereClause.join(" AND ");
    
        queryStr += " GROUP BY period";
    
        QSqlQuery query(queryStr);
        query.prepare(queryStr);
    
        if(sizeId != -1)
            query.bindValue(":sizeId", sizeId);
    
        if(filter == "type")
            query.bindValue(":typeId", filterId);
        if(filter == "item")
            query.bindValue(":clothingId", filterId);
    
        //dates for X axis
        QStringList xDates;
        if(query.exec()){
            while(query.next()){
                QString period = query.value(0).toString();
                int count = query.value(1).toInt();
    
                xDates.append(period);
                *barSet << count;
    
                qDebug()<< period << " " << count;
            }
            qDebug()<< "================";
        }else{
            barSet->deleteLater();
            qWarning()<< "Error creating series...";
            return NULL;
        }
    
        m_categories.clear();
        m_categories.append(xDates);
    
        emit categoriesChanged();
    
        return barSet;
    }
    
    QStringList LogData::categories() const
    {
        return m_categories;
    }
    

    Screenshots for reference:
    on Launch(filter by days):
    bde88be7-8408-4965-9aa3-3d43092877b1-image.png

    filter by month(one wrong label shown):
    1c419608-fcac-4ae4-8bb4-b29d5442d9e0-image.png

    filter by year(many old labels shown):
    d4cf3c58-9c92-4675-971b-e0de7a24d449-image.png

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved