Personalizzazione delle visualizzazioni dei grafici con QML

Nota: questa sezione fa parte dell'esempio " Grafici con QML" della Galleria.
Qui vi mostriamo come creare una ruota della fortuna personalizzando una serie a torta.
Per prima cosa creiamo la ChartView e un paio di serie.
ChartView {
id: chartView
anchors.fill: parent
title: "Wheel of Fortune"
legend.visible: false
antialiasing: true
PieSeries {
id: wheelOfFortune
horizontalPosition: 0.3
}
SplineSeries {
id: splineSeries
}
ScatterSeries {
id: scatterSeries
}
}I dati dell’applicazione vengono generati nel metodo Component.onCompleted del rettangolo principale:
Component.onCompleted: {
__intervalCoefficient = Math.random() + 0.25;
for (var i = 0; i < 20; i++)
wheelOfFortune.append("", 1);
var interval = 1;
for (var j = 0; interval < 800; j++) {
interval = __intervalCoefficient * j * j;
splineSeries.append(j, interval);
}
chartView.axisX(scatterSeries).max = j;
chartView.axisY(scatterSeries).max = 1000;
}Le seguenti personalizzazioni vengono eseguite ripetutamente tramite un timer. Per evidenziare una delle fette del grafico a torta alla volta, modifichiamo la sua proprietà `exploded`:
wheelOfFortune.at(index).exploded = false;
root.__activeIndex++;
index = root.__activeIndex % wheelOfFortune.count;
wheelOfFortune.at(index).exploded = true;Quindi viene eseguita un'animazione utilizzando una serie a punti con un unico punto dati:
scatterSeries.clear();
scatterSeries.append(root.__activeIndex, interval);
scatterSeries.color = Qt.tint(scatterSeries.color, "#05FF0000");
scatterSeries.markerSize += 0.5;Quando la ruota della fortuna si è fermata, facciamo lampeggiare la fetta attiva modificandone i colori.
// Switch the colors of the slice and the border
wheelOfFortune.at(index).borderWidth = 2;
switchColor = wheelOfFortune.at(index).borderColor;
wheelOfFortune.at(index).borderColor = wheelOfFortune.at(index).color;
wheelOfFortune.at(index).color = switchColor;© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.