BioDynaMo  v1.05.120-25dc9790
line_graph.cc
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
16 #include <TCanvas.h>
17 #include <TFrame.h>
18 #include <TGraph.h>
19 #include <TGraphAsymmErrors.h>
20 #include <TLegend.h>
21 #include <TMultiGraph.h>
22 #include <TStyle.h>
24 #include "core/util/log.h"
25 #include "core/util/string.h"
26 
27 namespace bdm {
28 namespace experimental {
29 
30 // -----------------------------------------------------------------------------
31 LineGraph::LineGraph(const TimeSeries* ts, const std::string& title,
32  const std::string& xaxis_title,
33  const std::string& yaxis_title, bool legend, TStyle* style,
34  int width, int height)
35  : ts_(ts), s_(style) {
36  if (s_) {
37  s_->cd();
38  }
39  c_ = new TCanvas();
40  mg_ = new TMultiGraph();
41  c_->SetCanvasSize(width, height);
42  c_->SetGrid();
43  mg_->SetTitle(Concat(title, ";", xaxis_title, ";", yaxis_title).c_str());
44  if (legend) {
45  l_ = new TLegend();
46  }
47 }
48 
49 // -----------------------------------------------------------------------------
51  delete c_;
52  delete mg_;
53  if (l_) {
54  delete l_;
55  }
56 }
57 
58 // -----------------------------------------------------------------------------
59 TGraph* LineGraph::Add(const std::string& ts_name,
60  const std::string& legend_label,
61  const char* add_mg_options, short line_color,
62  float line_color_alpha, short line_style,
63  short line_width, short marker_color,
64  float marker_color_alpha, short marker_style,
65  float marker_size, short fill_color,
66  float fill_color_alpha, short fill_style) {
67  if (s_) {
68  s_->cd();
69  }
70 
71  if (!ts_->Contains(ts_name)) {
72  Log::Warning("LineGraph::Add",
73  "The time series stored in this line graph does not contain "
74  "an entry for (",
75  ts_name, "). Operation aborted.");
76  return nullptr;
77  }
78  const auto& xvals = ts_->GetXValues(ts_name);
79  const auto& yvals = ts_->GetYValues(ts_name);
80  const auto& el = ts_->GetYErrorLow(ts_name);
81  const auto& eh = ts_->GetYErrorHigh(ts_name);
82  TGraph* gr = nullptr;
83  if (el.size() == 0) {
84  gr = new TGraph(xvals.size(), xvals.data(), yvals.data());
85  } else {
86  gr = new TGraphAsymmErrors(xvals.size(), xvals.data(), yvals.data(),
87  nullptr, nullptr, el.data(), eh.data());
88  }
89  gr->SetTitle(legend_label.c_str());
90  gr->InvertBit(TGraph::EStatusBits::kNotEditable);
91  gr->SetLineColorAlpha(line_color, line_color_alpha);
92  gr->SetLineStyle(line_style);
93  gr->SetLineWidth(line_width);
94  gr->SetMarkerColorAlpha(marker_color, marker_color_alpha);
95  gr->SetMarkerStyle(marker_style);
96  gr->SetMarkerSize(marker_size);
97  gr->SetFillColorAlpha(fill_color, fill_color_alpha);
98  gr->SetFillStyle(fill_style);
99 
100  mg_->Add(gr, add_mg_options);
101  if (l_ && legend_label != "") {
102  l_->AddEntry(gr, legend_label.c_str());
103  }
104  id_tgraphs_map_[ts_name].push_back(gr);
105  return gr;
106 }
107 
108 // -----------------------------------------------------------------------------
109 void LineGraph::Draw(const char* canvas_draw_option) {
110  Update();
111  c_->Draw(canvas_draw_option);
112 }
113 
114 // -----------------------------------------------------------------------------
116  if (l_) {
117  Update();
118  l_->SetX1(x1);
119  l_->SetY1(y1);
120  l_->SetX2(x2);
121  l_->SetY2(y2);
122  } else {
123  Log::Warning(
124  "LineGraph::SetLegendPos",
125  "This LineGraph was created without legend. Operation aborted.");
126  }
127 }
128 
129 // -----------------------------------------------------------------------------
131  if (l_) {
132  Update();
133  l_->SetX1NDC(x1);
134  l_->SetY1NDC(y1);
135  l_->SetX2NDC(x2);
136  l_->SetY2NDC(y2);
137  } else {
138  Log::Warning(
139  "LineGraph::SetLegendPosNDC",
140  "This LineGraph was created without legend. Operation aborted.");
141  }
142 }
143 
144 // -----------------------------------------------------------------------------
145 void LineGraph::SaveAs(const std::string& filenpath_wo_extension,
146  const std::vector<std::string>& extensions) {
147  Update();
148  for (auto& ext : extensions) {
149  auto full_path = Concat(filenpath_wo_extension, ext);
150  Log::Info("LineGraph::SaveAs", "Saved LineGraph at: ", full_path);
151  c_->SaveAs(full_path.c_str());
152  }
153 }
154 
155 // -----------------------------------------------------------------------------
156 void LineGraph::SetMultiGraphDrawOption(const std::string& s) {
157  mg_draw_option_ = s;
158 }
159 
160 // -----------------------------------------------------------------------------
161 TCanvas* LineGraph::GetTCanvas() { return c_; }
162 
163 // -----------------------------------------------------------------------------
164 TMultiGraph* LineGraph::GetTMultiGraph() { return mg_; }
165 
166 // -----------------------------------------------------------------------------
167 TLegend* LineGraph::GetTLegend() { return l_; }
168 
169 // -----------------------------------------------------------------------------
170 const std::vector<TGraph*>& LineGraph::GetTGraphs(
171  const std::string& ts_name) const {
172  auto it = id_tgraphs_map_.find(ts_name);
173  if (it == id_tgraphs_map_.end()) {
174  static std::vector<TGraph*> kEmpty;
175  return kEmpty;
176  }
177  return it->second;
178 }
179 
180 // -----------------------------------------------------------------------------
181 TStyle* LineGraph::GetTStyle() { return s_; }
182 
183 // -----------------------------------------------------------------------------
185  if (s_) {
186  s_->cd();
187  }
188  mg_->Draw(mg_draw_option_.c_str());
189  if (l_) {
190  l_->Draw();
191  }
192  c_->Update();
193  gPad->Modified();
194  gPad->Update();
195  c_->Modified();
196  c_->cd(0);
197 }
198 
199 } // namespace experimental
200 } // namespace bdm
bdm::experimental::LineGraph::SaveAs
void SaveAs(const std::string &filenpath_wo_extension, const std::vector< std::string > &extensions)
Definition: line_graph.cc:145
bdm::experimental::LineGraph::Add
TGraph * Add(const std::string &ts_name, const std::string &legend_name="", const char *add_mg_options="L", short line_color=1, float line_color_alpha=1.0, short line_style=1, short line_width=1, short marker_color=1, float marker_color_alpha=1.0, short marker_style=1, float marker_size=1, short fill_color=0, float fill_color_alpha=1.0, short fill_style=1000)
Definition: line_graph.cc:59
bdm::experimental::LineGraph::LineGraph
LineGraph(const TimeSeries *ts, const std::string &title="", const std::string &xaxis_title="", const std::string &yaxis_title="", bool legend=true, TStyle *style=nullptr, int width=700, int height=500)
Definition: line_graph.cc:31
bdm::experimental::TimeSeries::GetYErrorHigh
const std::vector< real_t > & GetYErrorHigh(const std::string &id) const
Definition: time_series.cc:522
bdm::experimental::LineGraph::mg_draw_option_
std::string mg_draw_option_
Definition: line_graph.h:84
bdm::experimental::LineGraph::l_
TLegend * l_
Definition: line_graph.h:87
bdm
Definition: agent.cc:39
string.h
bdm::experimental::LineGraph::c_
TCanvas * c_
Definition: line_graph.h:85
bdm::experimental::LineGraph::GetTStyle
TStyle * GetTStyle()
Definition: line_graph.cc:181
bdm::real_t
double real_t
Definition: real_t.h:21
line_graph.h
bdm::experimental::LineGraph::GetTLegend
TLegend * GetTLegend()
Definition: line_graph.cc:167
bdm::experimental::LineGraph::SetMultiGraphDrawOption
void SetMultiGraphDrawOption(const std::string &s)
Definition: line_graph.cc:156
bdm::experimental::LineGraph::SetLegendPos
void SetLegendPos(real_t x1, real_t y1, real_t x2, real_t y2)
Definition: line_graph.cc:115
bdm::experimental::LineGraph::GetTCanvas
TCanvas * GetTCanvas()
Definition: line_graph.cc:161
bdm::experimental::LineGraph::SetLegendPosNDC
void SetLegendPosNDC(real_t x1, real_t y1, real_t x2, real_t y2)
Definition: line_graph.cc:130
bdm::experimental::TimeSeries::GetYErrorLow
const std::vector< real_t > & GetYErrorLow(const std::string &id) const
Definition: time_series.cc:516
bdm::experimental::LineGraph::s_
TStyle * s_
Definition: line_graph.h:88
bdm::Log::Warning
static void Warning(const std::string &location, const Args &... parts)
Prints warning message.
Definition: log.h:67
time_series.h
bdm::Log::Info
static void Info(const std::string &location, const Args &... parts)
Prints information message.
Definition: log.h:55
bdm::experimental::LineGraph::Update
void Update()
Definition: line_graph.cc:184
bdm::experimental::LineGraph::GetTGraphs
const std::vector< TGraph * > & GetTGraphs(const std::string &ts_name) const
Definition: line_graph.cc:170
bdm::Concat
std::string Concat(const Args &... parts)
Concatenates all arguments into a string. Equivalent to streaming all arguments into a stringstream a...
Definition: string.h:70
bdm::experimental::TimeSeries::GetYValues
const std::vector< real_t > & GetYValues(const std::string &id) const
Definition: time_series.cc:511
bdm::experimental::TimeSeries
Definition: time_series.h:123
log.h
bdm::experimental::TimeSeries::GetXValues
const std::vector< real_t > & GetXValues(const std::string &id) const
Definition: time_series.cc:506
bdm::experimental::LineGraph::ts_
const TimeSeries * ts_
Definition: line_graph.h:83
bdm::experimental::LineGraph::id_tgraphs_map_
std::unordered_map< std::string, std::vector< TGraph * > > id_tgraphs_map_
Definition: line_graph.h:89
bdm::experimental::LineGraph::Draw
void Draw(const char *canvas_draw_option="")
Definition: line_graph.cc:109
bdm::experimental::LineGraph::GetTMultiGraph
TMultiGraph * GetTMultiGraph()
Definition: line_graph.cc:164
bdm::experimental::LineGraph::~LineGraph
~LineGraph()
Definition: line_graph.cc:50
bdm::experimental::LineGraph::mg_
TMultiGraph * mg_
Definition: line_graph.h:86
bdm::experimental::TimeSeries::Contains
bool Contains(const std::string &id) const
Returns whether a times series with given id exists in this object.
Definition: time_series.cc:498