Skip to content

Commit ebf9e16

Browse files
lorenzhsByteHamster
authored andcommitted
latex: Add support for "|attr" to compute addplot attributes in SQL
MULTIPLOT(algo|attr) SELECT size AS x, avg(time) AS y, "mark=" || iif(SUBSTR(algo, 1, 4) = "foo", "o", "*") AS attr FROM data GROUP BY MULTIPLOT
1 parent b352a32 commit ebf9e16

1 file changed

Lines changed: 34 additions & 3 deletions

File tree

src/latex.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ void SpLatex::multiplot(size_t ln, size_t indent, const std::string& cmdline)
203203
std::vector<std::string> groupfields = split(multiplot, ',');
204204
std::for_each(groupfields.begin(), groupfields.end(), trim_inplace_ws);
205205

206+
bool attr_mark = false;
206207
bool title_mark = false;
207208
bool ptitle_mark = false;
208209
bool xerr = false, yerr = false;
@@ -220,6 +221,13 @@ void SpLatex::multiplot(size_t ln, size_t indent, const std::string& cmdline)
220221
ptitle_mark = true;
221222
}
222223

224+
if (!groupfields.empty() && is_suffix(groupfields.back(), "|attr")) {
225+
// remove |attr from multiplot string
226+
groupfields.back().resize(groupfields.back().size() - 5);
227+
multiplot.resize(multiplot.size() - 5);
228+
attr_mark = true;
229+
}
230+
223231
// execute query
224232
query = replace_all(query, "MULTIPLOT", multiplot);
225233
SqlQuery sql = g_db->query(query);
@@ -246,6 +254,9 @@ void SpLatex::multiplot(size_t ln, size_t indent, const std::string& cmdline)
246254
if (ptitle_mark && !sql->exist_col("ptitle"))
247255
OUT_THROW("MULTIPLOT failed: ptitle mark set but result contains no 'ptitle' column.");
248256

257+
if (attr_mark && !sql->exist_col("attr"))
258+
OUT_THROW("MULTIPLOT failed: attr mark set but result contains no 'attr' column.");
259+
249260
unsigned int col_x = sql->find_col("x"), col_y = sql->find_col("y"),
250261
col_xerr = xerr ? sql->find_col("xerr") : -1,
251262
col_yerr = yerr ? sql->find_col("yerr") : -1;
@@ -255,6 +266,9 @@ void SpLatex::multiplot(size_t ln, size_t indent, const std::string& cmdline)
255266
col_title = sql->find_col("title");
256267
if (ptitle_mark)
257268
col_title = sql->find_col("ptitle");
269+
unsigned int col_attr = 0;
270+
if (attr_mark)
271+
col_attr = sql->find_col("attr");
258272

259273
// check existance of group fields and save ids
260274
std::vector<int> groupcols;
@@ -272,6 +286,7 @@ void SpLatex::multiplot(size_t ln, size_t indent, const std::string& cmdline)
272286
// collect coordinates {...} clause groups
273287
std::vector<std::string> coordlist;
274288
std::vector<std::string> legendlist;
289+
std::vector<std::string> attrlist;
275290

276291
{
277292
std::vector<std::string> lastgroup;
@@ -322,6 +337,10 @@ void SpLatex::multiplot(size_t ln, size_t indent, const std::string& cmdline)
322337
}
323338
legendlist.push_back(os.str());
324339
}
340+
341+
if (attr_mark) {
342+
attrlist.push_back(sql->text(col_attr));
343+
}
325344
}
326345

327346
// group fields match with last row -> append coordinates.
@@ -344,6 +363,8 @@ void SpLatex::multiplot(size_t ln, size_t indent, const std::string& cmdline)
344363

345364
for (size_t i = 0; i < coordlist.size(); ++i)
346365
{
366+
if (attr_mark)
367+
OUTC(gopt_verbose >= 1, "attr {" << attrlist[i] << " }");
347368
OUTC(gopt_verbose >= 1, "coordinates {" << coordlist[i] << " }");
348369
OUTC(gopt_verbose >= 1, "legend {" << legendlist[i] << " }");
349370
}
@@ -367,7 +388,13 @@ void SpLatex::multiplot(size_t ln, size_t indent, const std::string& cmdline)
367388
// copy styles from \addplot line
368389
if (entry < coordlist.size())
369390
{
370-
out << rm[1] << coordlist[entry] << " " << rm[2] << std::endl;
391+
if (attr_mark) {
392+
// can't copy styles when an attribute is being selected
393+
out << "\\addplot[" << attrlist[entry] << "] coordinates { "
394+
<< coordlist[entry] << " " << rm[2] << std::endl;
395+
} else {
396+
out << rm[1] << coordlist[entry] << " " << rm[2] << std::endl;
397+
}
371398

372399
// check following \addlegendentry
373400
if (eln+1 < m_lines.size() &&
@@ -403,8 +430,12 @@ void SpLatex::multiplot(size_t ln, size_t indent, const std::string& cmdline)
403430
// append missing \addplot / \addlegendentry pairs
404431
while (entry < coordlist.size())
405432
{
406-
out << "\\addplot coordinates {" << coordlist[entry]
407-
<< " };" << std::endl;
433+
if (attr_mark)
434+
out << "\\addplot[" << attrlist[entry] << "] coordinates {"
435+
<< coordlist[entry] << " };" << std::endl;
436+
else
437+
out << "\\addplot coordinates {" << coordlist[entry]
438+
<< " };" << std::endl;
408439

409440
out << "\\addlegendentry{" << legendlist[entry]
410441
<< "};" << std::endl;

0 commit comments

Comments
 (0)