Qt QML BarSeries中的y轴最大值未更新

Qt QML BarSeries中的y轴最大值未更新,qt,qml,qtcharts,Qt,Qml,Qtcharts,我使用a显示一些数据时遇到了一个问题:BarSeries的y轴不会自动更新 以下是从BarSeries文档复制和修改的mcve。当用户单击背景时,它会更新条形图系列 // main.qml import QtQuick 2.6 import QtQuick.Window 2.2 import QtCharts 2.2 Window { visible: true width: 640 height: 480 title: qsTr("Hello World"

我使用a显示一些数据时遇到了一个问题:BarSeries的y轴不会自动更新

以下是从BarSeries文档复制和修改的mcve。当用户单击背景时,它会更新条形图系列

//  main.qml

import QtQuick 2.6
import QtQuick.Window 2.2
import QtCharts 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    ChartView {
        id: chartView
        title: "Bar series"
        anchors.fill: parent
        legend.alignment: Qt.AlignBottom
        antialiasing: true

        BarSeries {
            id: mySeries
            axisX: BarCategoryAxis { categories: ["2007", "2008", "2009", "2010", "2011", "2012" ] }

            BarSet { label: "Bob"; values: [2, 2, 3, 4, 5, 6] }
            BarSet { label: "Susan"; values: [5, 1, 2, 4, 1, 7] }
            BarSet { label: "James"; values: [3, 5, 8, 13, 5, 8] }
        }

    }

    MouseArea {
        anchors.fill: parent
        onClicked: {
            mySeries.clear();  //  clear previous sets

            //  update with new sets
            mySeries.append("Bob", [3, 5, 8, 13, 5, 8]);
            mySeries.append("Susan", [2, 2, 3, 4, 5, 200]);
            mySeries.append("James", [5, 1, 2, 4, 1, 7]);

        }
    }
}
从代码中,我们可以看到,单击鼠标区域应该会更新序列,使其y轴最多为200(由于Susan的新值集)

下面的屏幕截图显示了更新的列,但没有显示y轴。(注意,我希望y轴最大值更新为200。

鼠标单击前:

鼠标单击后:

我应该做哪些更改来更新图表y轴的最大值?

在点击
MouseArea::onClicked
中的多个
mySeries.append
语句之后,我尝试执行
chartView.update()
,但没有效果

我找了又找,但什么也没找到。大多数来自Web的答案只涉及QtTalk从C++运行或描述不同的问题(除非我用错误的关键字搜索)。
为了完整起见,这里是
main.cpp
文件:

#include <QApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc, argv);  // needs QT += widgets in qmake

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}
#包括
#包括
int main(int argc,char*argv[])
{
QCoreApplication::setAttribute(Qt::AA_enableHighdDiscaling);
QApplication应用程序(argc,argv);//需要qtake中的QT+=小部件
qqmlaplicationengine;
engine.load(QUrl(QStringLiteral(“qrc:/main.qml”));
if(engine.rootObjects().isEmpty())
返回-1;
返回app.exec();
}

我能够通过将自定义项附加到BarSeries并手动、以编程方式使用属性更新新的最大值来解决此问题

import QtQuick 2.6
import QtQuick.Window 2.2
import QtCharts 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    
    ChartView {
        id: chartView
        title: "Bar series"
        anchors.fill: parent
        legend.alignment: Qt.AlignBottom
        antialiasing: true
    
        BarSeries {
            id: mySeries
            axisX: BarCategoryAxis { categories: ["2007", "2008", "2009", "2010", "2011", "2012" ] }

            axisY: ValueAxis {    //  <- custom ValueAxis attached to the y-axis
                id: valueAxis
            }

            BarSet { label: "Bob"; values: [2, 2, 3, 4, 5, 6] }
            BarSet { label: "Susan"; values: [5, 1, 2, 4, 1, 7] }
            BarSet { label: "James"; values: [3, 5, 8, 13, 5, 8] }
        }
        
    }
    
    MouseArea {
        anchors.fill: parent
        onClicked: {
            mySeries.clear();
            mySeries.append("Bob", [3, 5, 8, 13, 5, 8]);
            mySeries.append("Susan", [2, 2, 3, 4, 5, 200]);
            mySeries.append("James", [5, 1, 2, 4, 1, 7]);
            
            valueAxis.max = 200;  //  hard-code a new maximum
        }
    }
}
当然,最小值可以忽略不计,但这完全取决于您的数据和情况

onClicked: {
    mySeries.clear();
    mySeries.append("Bob", [3, 5, 8, 13, 5, 8]);
    mySeries.append("Susan", [2, 2, 3, 4, 5, 200]);
    mySeries.append("James", [5, 1, 2, 4, 1, 7]);
    
    //  deduce the new min and max
    var min = 1e8, max = -1e8;
    for (var i = 0; i < mySeries.count; i++) {
        //  min = Math.min(min, ...mySeries.at(i).values);  //  modern js not yet supported?
        //  max = Math.min(max, ...mySeries.at(i).values);

        min = Math.min(min, mySeries.at(i).values.reduce(function(a,b) {
            return Math.min(a, b);
        }));
        max = Math.max(max, mySeries.at(i).values.reduce(function(a,b) {
            return Math.max(a, b);
        }));
    }

    //  set the new min and max
    valueAxis.min = min;
    valueAxis.max = max;

    // valueAxis.max = max * 1.05; // or give a little margin?
    
}