-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchartwidget.cpp
113 lines (91 loc) · 3.21 KB
/
chartwidget.cpp
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
#ifdef CREATE_CHARTS
#include "chartwidget.h"
#include "core.h"
ChartWidget::ChartWidget(QWidget* parent) : QWidget(parent)
{
slow_down_chart = new SlowDownChart();
speed_chart = new SpeedChart();
slow_down_chart_view.setChart(slow_down_chart);
tabs.addTab(&slow_down_chart_view, tr("Slow Down Influence"));
speed_chart_view.setChart(speed_chart);
tabs.addTab(&speed_chart_view, tr("Speed Distribution"));
main_layout.addWidget(&tabs);
this->setLayout(&main_layout);
set_slow_down_data(0, 0, 0);
set_car_speed_data(std::vector<std::size_t>());
}
void ChartWidget::set_slow_down_data(std::size_t slow_down_chance, double avg_speed, std::size_t time)
{
slow_down_chart->add_data(slow_down_chance, avg_speed, time);
update();
}
void ChartWidget::set_car_speed_data(const std::vector<std::size_t>& amount_of_car_at_speed)
{
speed_chart->add_data(amount_of_car_at_speed);
update();
}
SlowDownChart::SlowDownChart()
{
this->setTheme(QChart::ChartThemeDark);
}
SlowDownChart::~SlowDownChart()
{
this->removeAllSeries();
}
void SlowDownChart::add_data(std::size_t slow_down_chance, double avg_speed, std::size_t time)
{
if(!slow_down_series.count(slow_down_chance))
slow_down_series[slow_down_chance] = new QLineSeries();
QLineSeries* line = slow_down_series[slow_down_chance];
if(!this->series().contains(line))
this->addSeries(line);
// clear old line
if(time < line->at(line->count() - 1).x())
{
line->clear();
this->removeSeries(line);
}
line->setName(QString::number(slow_down_chance));
line->append(time, avg_speed);
this->createDefaultAxes();
QValueAxis* axis_x = static_cast<QValueAxis*>(this->axes(Qt::Horizontal).back());
if(time >= axis_x->max())
axis_x->setRange(0, std::ceil(time / 5.) * 5);
else if(static_cast<QValueAxis*>(axis_x)->max() < 5)
axis_x->setRange(0, 5);
axis_x->setLabelFormat("%d");
axis_x->applyNiceNumbers();
// static_cast<QValueAxis*>(this->axisX())->setTickCount(6);
QValueAxis* axis_y = static_cast<QValueAxis*>(this->axes(Qt::Vertical).back());
axis_y->setRange(0, static_cast<int>(Core::get_max_speed()));
axis_y->setTickCount(static_cast<int>(Core::get_max_speed() + 1));
}
SpeedChart::SpeedChart() : speed_bar_set("")
{
this->setTheme(QChart::ChartThemeDark);
}
void SpeedChart::add_data(const std::vector<std::size_t>& amount_of_car_at_speed)
{
speed_bar_set.remove(0, speed_bar_set.count());
speed_bar_set.setLabelColor(QColor(Qt::transparent));
/* if(speed_axis.count() != Core::get_max_speed())
{
this->removeAxis(&speed_axis);
speed_axis.clear();
}*/
for(std::size_t i = 0; i < amount_of_car_at_speed.size(); ++i)
speed_bar_set.insert(static_cast<int>(i), amount_of_car_at_speed[i]);
//speed_bar_set.append();
speed_series.append(&speed_bar_set);
if(!this->series().contains(&speed_series))
this->addSeries(&speed_series);
// if(speed_axis.count() != Core::get_max_speed())
// this->setAxisX(&speed_axis, &speed_series);
this->createDefaultAxes();
QAbstractAxis* axis_x = this->axes(Qt::Horizontal).back();
QValueAxis* axis_y = static_cast<QValueAxis*>(this->axes(Qt::Vertical).back());
axis_y->setLabelFormat("%d");
axis_x->setRange(0, static_cast<int>(Core::get_max_speed() + 1));
axis_x->setRange(0, static_cast<int>(Core::get_car_amount()));
}
#endif