Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qchart formatLabel QML



  • Hello everybody,

    I'm developing a linear chart using qml with qt creator, and I'd like to append a character to every Y axis label. For example I want append "°C" to the number 15: the final resul should be 15°C. I tried to use the following QML code, but the resul is 15?C.

    ValueAxis {
    min: 0
    max: 100
    labelFormat: "%d%1".arg("°C)
    }

    Does someone help me?
    Many thanks in advance.

    Best Regards,
    Chobin



  • @Chobin
    As the documentation states, this is not supported by the ValueAxis of the QtCharts module.
    https://doc.qt.io/qt-5/qml-qtcharts-valueaxis.html#labelFormat-prop

    Whenever you try to use a second format specifier in .arg(), you will get a message like:

    QString::arg: Argument missing
    

    This is unlike the labelFormat property in the QtDataVisualization module, see for example
    https://doc.qt.io/qt-5.11/qtdatavisualization-qmlbars-example.html, where the following does work:

    labelFormat: "%d \u00B0C"
    

    In your case, an annoying workaround would be to use a CategoryAxis instead.

    e.g.

                axisX: CategoryAxis {
                    min: 0
                    max: 4
                    labelsPosition: CategoryAxis.AxisLabelsPositionOnValue
                    CategoryRange {
                        label: "1 \u00B0C"
                        endValue: 1
                    }
                    CategoryRange {
                        label: "2 \u00B0C"
                        endValue: 2
                    }
                    CategoryRange {
                        label: "3 \u00B0C"
                        endValue: 3
                    }
                    CategoryRange {
                        label: "4 \u00B0C"
                        endValue: 4
                    }
                }
    

    To be practical, you may need to create this one dynamically instead...


Log in to reply