aboutsummaryrefslogtreecommitdiffstats
path: root/examples/demos/graphs_csv/Main.qml
blob: 2ce1adb3fdd116a7495086bf5863ee49a4e7b37f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

pragma ComponentBehavior: Bound

import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Basic
import QtQml.Models
import QtGraphs
import qtgraphscsv

Window {
    id: mainWindow
    width: 1200
    height: 720
    visible: true
    title: qsTr("QtGraphs with CSV Demo")

    Binding {
        target: Units
        property: "window"
        value: mainWindow
    }

    property var seriesBaseColors: ["#99A500", "#361EAB", "#1F9B5D", "#BC7A19", "#B00F36", "#5A1FAA", "#077947", "#4B523C"]
    property var allSeriesColors: ["#99A500", "#361EAB", "#1F9B5D", "#BC7A19", "#B00F36", "#5A1FAA", "#077947", "#4B523C"]

    property var activeTheme: themes.lightTheme

    Item {
        id: themes
        property alias lightTheme: lightTheme
        property alias graphsLightTheme: lightTheme.graphsTheme
        property alias darkTheme: darkTheme
        property alias graphsDarkTheme: darkTheme.graphsTheme

        Item {
            id: lightTheme
            property color primaryTextColor: "#2D2D2D"
            property color secondaryTextColor: "#595959"
            property color backgroundColor: "#FCFCFC"
            property color selectionColor: "#E3E3E3"
            property color borderColor: "#CDCDCD"
            property color legendLabelTextColor: "#FCFCFC"
            property alias graphsTheme: graphsLightTheme

            GraphsTheme {
                id: graphsLightTheme

                backgroundColor: "transparent"
                colorScheme: GraphsTheme.ColorScheme.Light
                labelTextColor: lightTheme.primaryTextColor
            }
        }

        Item {
            id: darkTheme
            property color primaryTextColor: "#F2F2F2"
            property color secondaryTextColor: "#AEAEAE"
            property color backgroundColor: "#1F1F1F"
            property color selectionColor: "#353535"
            property color borderColor: "#3F3F3F"
            property color legendLabelTextColor: "#FCFCFC"
            property alias graphsTheme: graphsDarkTheme

            GraphsTheme {
                id: graphsDarkTheme

                backgroundColor: "transparent"
                colorScheme: GraphsTheme.ColorScheme.Dark
                labelTextColor: darkTheme.primaryTextColor
            }
        }
        Component.onCompleted: {
            // Generate more colors for the series
            for (var i = 0; i < mainWindow.seriesBaseColors.length; ++i) {
                mainWindow.allSeriesColors[i + 8] = Qt.lighter(mainWindow.seriesBaseColors[i]);
                mainWindow.allSeriesColors[i + 16] = Qt.darker(mainWindow.seriesBaseColors[i]);
                mainWindow.allSeriesColors[i + 24] = Qt.lighter(Qt.lighter(mainWindow.seriesBaseColors[i]));
                mainWindow.allSeriesColors[i + 32] = Qt.darker(Qt.darker(mainWindow.seriesBaseColors[i]));
            }
            graphsDarkTheme.seriesColors = mainWindow.allSeriesColors;
            graphsLightTheme.seriesColors = mainWindow.allSeriesColors;
        }
    }

    Rectangle {
        id: background
        property alias dataModel: dataView.dataModel

        anchors.fill: parent
        color: mainWindow.activeTheme.backgroundColor

        Item {
            id: dataView

            property alias dataModel: dataModel
            anchors.top: background.top
            anchors.bottom: background.bottom
            anchors.left: background.left
            implicitWidth: 400 * Units.px
            implicitHeight: 719 * Units.px

            CustomTableView {
                id: customTableView

                implicitHeight: 320 * Units.px
                tableViewModel: dataModel
                horizontalHeaderViewModel: hHeaderModel
                anchors.top: dataView.top
                anchors.topMargin: 40
                anchors.left: dataView.left
                anchors.right: dataView.right
                anchors.leftMargin: 24 * Units.px
                anchors.bottom: dataView.bottom
                anchors.bottomMargin: 294 * Units.px

                selectionColor: mainWindow.activeTheme.selectionColor
                backgroundColor: mainWindow.activeTheme.backgroundColor
                primaryTextColor: mainWindow.activeTheme.primaryTextColor
                secondaryTextColor: mainWindow.activeTheme.secondaryTextColor
                borderColor: mainWindow.activeTheme.borderColor
                title: qsTr("Medals")

                Connections {
                    target: customTableView.dataSelectionModel
                    function onSelectionChanged(selected, deselected) {
                        if (customTableView.dataSelectionModel.selectedIndexes.length < 1) {
                            graphView.graphsItem.clearGraph();
                            return;
                        }

                        const first = customTableView.mainTableView.cellAtIndex(customTableView.dataSelectionModel.selectedIndexes[0]);
                        const last = customTableView.mainTableView.cellAtIndex(
                            customTableView.dataSelectionModel.selectedIndexes[
                                customTableView.dataSelectionModel.selectedIndexes.length - 1]);

                        graphView.graphsItem.updateModelMapper(first, last);

                        const firstCategoryIndex = graphView.graphsItem.modelMapper.first;
                        const lastCategoryIndex = graphView.graphsItem.modelMapper.count;

                        graphView.graphsItem.categoryAxis.categories = customTableView.extractBarSetGategories(
                            firstCategoryIndex, lastCategoryIndex);
                    }
                }
            }

            SelectionRectangle {
                id: selectionRectangle
                target: customTableView.mainTableView
            }

            ListModel {
                id: hHeaderModel
            }

            LegendItem {
                id: legendItem
                anchors.top: customTableView.bottom
                anchors.left: dataView.left
                anchors.leftMargin: 24 * Units.px
                anchors.topMargin: 70 * Units.px
                labelTextColor: mainWindow.activeTheme.legendLabelTextColor
                titleTextColor: mainWindow.activeTheme.secondaryTextColor
                series: graphView.graphsItem.series
            }

            CsvDataModel {
                id: dataModel
                onModelReset: () => {
                    const first = graphView.graphsItem.modelMapper.first;
                    const count = graphView.graphsItem.modelMapper.count;
                    graphView.graphsItem.categoryAxis.categories = customTableView.extractBarSetGategories(first, count);

                    customTableView.fillHorizontalHeaderModel(dataModel.columnCount());
                }

                Component.onCompleted: () => {
                    dataModel.readCsv("qrc:/data/medals.csv");
                }
            }
        }

        Item {
            id: graphView

            property alias graphsItem: graphsItem

            anchors.left: dataView.right
            anchors.right: background.right
            anchors.top: background.top
            anchors.bottom: background.bottom
            width: 800 * Units.px

            Graph {
                id: graphsItem

                anchors.fill: graphView

                chartView.marginLeft: 71 * Units.px
                chartView.marginRight: 43 * Units.px
                chartView.marginTop: 47 * Units.px
                chartView.marginBottom: 80 * Units.px

                theme: mainWindow.activeTheme.graphsTheme
                labelDelegateTextColor: mainWindow.activeTheme.secondaryTextColor
            }
        }
    }

    Component.onCompleted: () => {
        switch (Application.styleHints.colorScheme) {
        case Qt.Light:
            activeTheme = themes.lightTheme;
            break;
        case Qt.Dark:
            activeTheme = themes.darkTheme;
            break;
        }
        graphView.graphsItem.modelMapper.model = background.dataModel;
    }
}