|
1 #pragma once |
|
2 |
|
3 #include <iostream> |
|
4 |
|
5 #include <QWidget> |
|
6 #include <QGridLayout> |
|
7 #include <QLabel> |
|
8 |
|
9 #include <QChart> |
|
10 #include <QChartView> |
|
11 #include <QStackedBarSeries> |
|
12 #include <QBarCategoryAxis> |
|
13 #include <QVBarModelMapper> |
|
14 |
|
15 |
|
16 #include <relpipe/reader/typedefs.h> |
|
17 #include <relpipe/reader/TypeId.h> |
|
18 #include <relpipe/reader/handlers/AttributeMetadata.h> |
|
19 |
|
20 #include "RelpipeTableModel.h" |
|
21 |
|
22 using namespace relpipe::reader; |
|
23 using namespace relpipe::reader::handlers; |
|
24 |
|
25 QT_CHARTS_USE_NAMESPACE |
|
26 |
|
27 class RelpipeChartWidget : public QWidget { |
|
28 Q_OBJECT |
|
29 private: |
|
30 RelpipeTableModel* model; |
|
31 |
|
32 QGridLayout* layout; |
|
33 QChart* chart; |
|
34 QChartView* chartView; |
|
35 QVBarModelMapper* mapper; |
|
36 QStackedBarSeries* series; |
|
37 QBarCategoryAxis* axis; |
|
38 |
|
39 boolean_t isNumeric(TypeId typeId) { |
|
40 return typeId == TypeId::INTEGER; |
|
41 } |
|
42 |
|
43 int firstValueColumn() { |
|
44 int first = lastValueColumn(); |
|
45 for (int i = first; i > 0; i--) if (isNumeric(model->attributeType(i))) first = i; else break; |
|
46 return first; |
|
47 } |
|
48 |
|
49 int lastValueColumn() { |
|
50 return model->columnCount() - 1; |
|
51 } |
|
52 |
|
53 public: |
|
54 |
|
55 RelpipeChartWidget(RelpipeTableModel* model, QWidget* parent = Q_NULLPTR) : model(model), QWidget(parent) { |
|
56 layout = new QGridLayout(); |
|
57 setLayout(layout); |
|
58 } |
|
59 |
|
60 void endOfRelation() { |
|
61 if (hasChartData()) { |
|
62 chart = new QChart(); |
|
63 chartView = new QChartView(chart, this); |
|
64 mapper = new QVBarModelMapper(chartView); |
|
65 series = new QStackedBarSeries(mapper); |
|
66 mapper->setFirstBarSetColumn(firstValueColumn()); |
|
67 mapper->setLastBarSetColumn(lastValueColumn()); |
|
68 mapper->setSeries(series); |
|
69 mapper->setModel(model); |
|
70 |
|
71 |
|
72 axis = new QBarCategoryAxis(chartView); |
|
73 for (int i = 0; i < model->rowCount(); i++) axis->append(model->data(model->index(i, 0)).toString()); |
|
74 chart->addSeries(series); |
|
75 chart->createDefaultAxes(); |
|
76 chart->setAxisX(axis, series); |
|
77 layout->addWidget(chartView); |
|
78 } else { |
|
79 layout->addWidget(new QLabel("This relation can't be displayed as a chart.", this)); |
|
80 } |
|
81 } |
|
82 |
|
83 /** |
|
84 * @return whether drawing a chart makes sense for this relation |
|
85 */ |
|
86 boolean_t hasChartData() { |
|
87 return model->columnCount() >= 2 // first = category, last = numeric value |
|
88 && isNumeric(model->attributeType(lastValueColumn())); // at least the last attribute must be numeric |
|
89 } |
|
90 }; |