Skip to content

Commit a900689

Browse files
committed
adding =title and =ptitle markers for MULTIPLOT to enable generating legend title using SQL
1 parent cbcc9a7 commit a900689

1 file changed

Lines changed: 48 additions & 9 deletions

File tree

src/latex.cpp

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,33 @@ void SpLatex::multiplot(size_t ln, size_t indent, const std::string& cmdline)
200200
std::string multiplot = rm_multiplot[1].str();
201201
std::string query = rm_multiplot[2].str();
202202

203-
query = replace_all(query, "MULTIPLOT", multiplot);
204-
205203
std::vector<std::string> groupfields = split(multiplot, ',');
206204
std::for_each(groupfields.begin(), groupfields.end(), trim_inplace_ws);
207205

206+
bool title_mark = false;
207+
bool ptitle_mark = false;
208+
209+
if (!groupfields.empty() && is_suffix(groupfields.back(), "=title")) {
210+
if (groupfields.size() != 1) {
211+
OUT_THROW("MULTIPLOT() has =title mark, but contains multiple columns.");
212+
}
213+
// remove =title from multiplot string
214+
groupfields.back().resize(groupfields.back().size() - 6);
215+
multiplot.resize(multiplot.size() - 6);
216+
title_mark = true;
217+
}
218+
else if (!groupfields.empty() && is_suffix(groupfields.back(), "=ptitle")) {
219+
if (groupfields.size() != 1) {
220+
OUT_THROW("MULTIPLOT() has =ptitle mark, but contains multiple columns.");
221+
}
222+
// remove =title from multiplot string
223+
groupfields.back().resize(groupfields.back().size() - 7);
224+
multiplot.resize(multiplot.size() - 7);
225+
ptitle_mark = true;
226+
}
227+
208228
// execute query
229+
query = replace_all(query, "MULTIPLOT", multiplot);
209230
SqlQuery sql = g_db->query(query);
210231

211232
// read column names
@@ -218,7 +239,17 @@ void SpLatex::multiplot(size_t ln, size_t indent, const std::string& cmdline)
218239
if (!sql->exist_col("y"))
219240
OUT_THROW("MULTIPLOT failed: result contains no 'y' column.");
220241

221-
unsigned int colx = sql->find_col("x"), coly = sql->find_col("y");
242+
if (title_mark && !sql->exist_col("title"))
243+
OUT_THROW("MULTIPLOT failed: title mark set but result contains no 'title' column.");
244+
245+
if (ptitle_mark && !sql->exist_col("title"))
246+
OUT_THROW("MULTIPLOT failed: ptitle mark set but result contains no 'title' column.");
247+
248+
unsigned int col_x = sql->find_col("x"), col_y = sql->find_col("y");
249+
250+
unsigned int col_title = 0;
251+
if (title_mark || ptitle_mark)
252+
col_title = sql->find_col("title");
222253

223254
// check existance of group fields and save ids
224255
std::vector<int> groupcols;
@@ -245,11 +276,11 @@ void SpLatex::multiplot(size_t ln, size_t indent, const std::string& cmdline)
245276
{
246277
unsigned int row = sql->current_row();
247278

248-
if (sql->isNULL(colx)) {
279+
if (sql->isNULL(col_x)) {
249280
OUT("MULTIPLOT warning: 'x' is NULL in row " << row << ".");
250281
continue;
251282
}
252-
if (sql->isNULL(coly)) {
283+
if (sql->isNULL(col_y)) {
253284
OUT("MULTIPLOT warning: 'y' is NULL in row " << row << ".");
254285
continue;
255286
}
@@ -274,15 +305,23 @@ void SpLatex::multiplot(size_t ln, size_t indent, const std::string& cmdline)
274305
std::ostringstream os;
275306
for (size_t i = 0; i < groupcols.size(); ++i) {
276307
if (i != 0) os << ',';
277-
os << escape_latex(groupfields[i])
278-
<< '=' << escape_latex(rowgroup[i]);
308+
if (title_mark) {
309+
os << escape_latex(sql->text(col_title));
310+
}
311+
else if (ptitle_mark) {
312+
os << sql->text(col_title);
313+
}
314+
else {
315+
os << escape_latex(groupfields[i]) << '='
316+
<< escape_latex(rowgroup[i]);
317+
}
279318
}
280319
legendlist.push_back(os.str());
281320
}
282321

283322
// group fields match with last row -> append coordinates.
284-
coord << " (" << str_reduce(sql->text(colx))
285-
<< ',' << str_reduce(sql->text(coly))
323+
coord << " (" << str_reduce(sql->text(col_x))
324+
<< ',' << str_reduce(sql->text(col_y))
286325
<< ')';
287326
}
288327

0 commit comments

Comments
 (0)