Debian Codegraph @

examples/charts/qmlchartsgallery/qml/f1legends/F1Legends.qml

raw
1 // Copyright (C) 2023 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4 import QtQuick
5 import QtCharts
6
7 Item {
8 id: root
9 width: 400
10 height: 300
11 property int currentIndex: -1
12
13 //![1]
14 ChartView {
15 id: chartView
16 title: "Driver Speeds, lap 1"
17 anchors.fill: parent
18 legend.alignment: Qt.AlignTop
19 animationOptions: ChartView.SeriesAnimations
20 antialiasing: true
21 }
22 //![1]
23
24 //![2]
25 // An example ListModel containing F1 legend drivers' speeds at speed traps
26 SpeedsList {
27 id: speedsList
28 Component.onCompleted: {
29 timer.start();
30 }
31 }
32 //![2]
33
34 //![3]
35 // A timer to mimic refreshing the data dynamically
36 Timer {
37 id: timer
38 interval: 700
39 repeat: true
40 triggeredOnStart: true
41 running: false
42 onTriggered: {
43 root.currentIndex++;
44 if (root.currentIndex < speedsList.count) {
45 // Check if there is a series for the data already
46 // (we are using driver name to identify series)
47 var lineSeries = chartView.series(speedsList.get(root.currentIndex).driver);
48 if (!lineSeries) {
49 lineSeries = chartView.createSeries(ChartView.SeriesTypeLine,
50 speedsList.get(root.currentIndex).driver);
51 chartView.axisY().min = 0;
52 chartView.axisY().max = 250;
53 chartView.axisY().tickCount = 6;
54 chartView.axisY().titleText = "speed (kph)";
55 chartView.axisX().titleText = "speed trap";
56 chartView.axisX().labelFormat = "%.0f";
57 }
58 lineSeries.append(speedsList.get(root.currentIndex).speedTrap,
59 speedsList.get(root.currentIndex).speed);
60
61 if (speedsList.get(root.currentIndex).speedTrap > 3) {
62 chartView.axisX().max = Number(speedsList.get(root.currentIndex).speedTrap) + 1;
63 chartView.axisX().min = chartView.axisX().max - 5;
64 } else {
65 chartView.axisX().max = 5;
66 chartView.axisX().min = 0;
67 }
68 chartView.axisX().tickCount = chartView.axisX().max - chartView.axisX().min + 1;
69 } else {
70 // No more data, change x-axis range to show all the data
71 timer.stop();
72 chartView.animationOptions = ChartView.AllAnimations;
73 chartView.axisX().min = 0;
74 chartView.axisX().max = speedsList.get(root.currentIndex - 1).speedTrap;
75 }
76 }
77 }
78 //![3]
79 }