Lumiera  0.pre.03
»edit your freedom«
gnuplot-gen.cpp
Go to the documentation of this file.
1 /*
2  GnuplotGen - building blocks for simplified data visualisation via Gnuplot
3 
4  Copyright (C)
5  2024, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
14 
63 #include "lib/text-template.hpp"
65 #include "lib/gnuplot-gen.hpp"
66 #include "lib/format-util.hpp"
67 
68 #include <string>
69 
70 using std::string;
71 
72 
73 namespace lib {
74 namespace gnuplot_gen {
75 
76  namespace { // Template and defaults definitions for diagram generation
77 
78 
79  const string GNUPLOT_CommonStyleDef = R"~(#
80 #---common-styles-for-plots-from-Lumiera---------
81 set style line 1 linetype 1 linewidth 2 linecolor rgb '#240CC3'
82 set style line 2 linetype 3 linewidth 2 linecolor rgb '#1149D6'
83 set style line 3 linetype 2 linewidth 2 linecolor rgb '#0B7FCC'
84 set style line 4 linetype 6 linewidth 2 linecolor rgb '#37999D'
85 set style line 5 linetype 7 linewidth 2 linecolor rgb '#248269'
86 set style line 6 linetype 13 linewidth 2 linecolor rgb '#00C443'
87 set style line 7 linetype 12 linewidth 2 linecolor rgb '#EBE83E'
88 set style line 8 linetype 5 linewidth 2 linecolor rgb '#762C14'
89 set style line 9 linetype 4 linewidth 2 linecolor rgb '#AA0519'
90 
91 set style line 10 linetype 1 linewidth 1 linecolor rgb '#303030'
92 set style line 11 linetype 0 linewidth 1 linecolor rgb '#A0A0A0' dashtype 3
93 #---(End)styles----------------------------------
94 )~";
95 
96  const string GNUPLOT_AxisGridSetup = R"~(#
97 #---axis-and-grid-setup--------------------------
98 unset border
99 set tics nomirror
100 set grid back linestyle 11
101 
102 set arrow 10 from graph 0,0 to graph 1.04,0 size screen 0.025,15,60 filled ls 10
103 set arrow 11 from graph 0,0 to graph 0,1.08 size screen 0.025,15,60 filled ls 10
104 )~";
105 
106 
107  const string GNUPLOT_BASIC_PLOT_DEF = R"~(#
108 #
109 # GNUPLOT - data plot from Lumiera
110 #
111 
112 ${if Term
113 }set term ${Term} ${
114 if TermSize}size ${TermSize}${endif}
115 ${else}${if TermSize
116 }set term wxt size ${TermSize}
117 ${endif}${endif Term}
118 
119 set datafile separator ",;"
120 
121 ####---------Data--------------------------------
122 $RunData << _End_of_Data_
123 ${CSVData}
124 _End_of_Data_
125 
126 
127 ${CommonStyleDef}
128 ${AxisGridSetup}
129 
130 ${if Xlabel
131 }set xlabel '${Xlabel}'
132 ${else
133 }stats $RunData using (abscissaName=strcol(1)) every ::0::0 nooutput
134 
135 set xlabel abscissaName
136 ${end if Xlabel
137 }${if Ylabel
138 }set ylabel '${Ylabel}' ${end if Ylabel
139 }
140 ${if Xrange}
141 set xrange [${Xrange}] ${endif
142 }${if Yrange}
143 set yrange [${Yrange}] ${endif
144 }set key autotitle columnheader tmargin
145 
146 )~";
147 
148  const string GNUPLOT_SIMPLE_DATA_PLOT = R"~(#
149 ####---------------------------------------------
150 plot for [i=2:*] $RunData using 1:i with ${DiagramKind} linestyle i-1
151 
152 )~";
153 
154 
155  const string GNUPLOT_SCATTER_REGRESSION = R"~(#
156 stats $RunData using 1:2 nooutput
157 
158 ${if RegrSlope
159 }# regression line function (given as parameter)
160 regLine(x) = ${RegrSlope} * x + ${RegrSocket}
161 ${else
162 }# regression line function derived from data
163 regLine(x) = STATS_slope * x + STATS_intercept
164 ${end if
165 }#
166 regLabel = sprintf("Model: %3.2f·p + %3.2f", STATS_slope, STATS_intercept)
167 
168 set key horizontal width -4
169 ${if Xtics
170 }set xtics ${Xtics}
171 ${else}${if Xrange}${else
172 }set xrange [0:*]
173 set xtics 1
174 ${end if}${end if Xtics
175 }
176 plots = STATS_columns - 1
177 # Adjust layout based on number of data sequences;
178 # additional sequences placed into secondary diagram
179 #
180 if (plots > 1) {
181  set multiplot layout 2,1 # 2 rows 1 column
182  set lmargin at screen 0.12 # fixed margins to align diagrams
183  set rmargin at screen 0.88
184 }
185 #
186 #
187 ####---------Scatter-Regression-Plot-------------
188 plot $RunData using 1:2 with points linestyle 1, \
189  regLine(x) with line linestyle 9 title regLabel
190 
191 if (plots > 1) {
192  # switch off decorations for secondary diagram
193  unset arrow 10
194  unset arrow 11
195  set border 2+8
196  set key bmargin vertical
197 ${if Y2range}
198  set yrange [${Y2range}]
199 ${endif}
200  unset xlabel
201  set format x ""
202 ${if Y2label
203 } set ylabel '${Y2label}' ${endif
204 }
205  if (plots <= 2) {
206  ####---------------------------------
207  plot $RunData using 1:3 with impulses linestyle 3
208 
209  } else {
210  # more than one additional data sequence
211  #
212 ${if Y3range
213 } set y2range [${Y3range}]
214 
215 ${endif
216 } set y2tics
217 ${if Y3label
218 } set y2label '${Y3label}' offset -1.5
219 ${endif}
220  ####---------------------------------------------
221  plot $RunData using 1:3 with impulses linestyle 3, \
222  for [i=4:*] $RunData using 1:i with points linestyle 5+(i-4) axes x1y2
223  }
224 }
225 )~";
226 
227 
228  }//(End)template and defaults definitions
229 
230 
231 
232 
233 
240  string
242  {
243  TextTemplate plot{GNUPLOT_BASIC_PLOT_DEF
244  +GNUPLOT_SIMPLE_DATA_PLOT};
245 
246  params.set ("CommonStyleDef", GNUPLOT_CommonStyleDef)
247  .set ("AxisGridSetup", GNUPLOT_AxisGridSetup)
248  .set (KEY_DiagramKind, "points")
249  ;
250  return plot.render (params.genNode());
251  }
252 
253 
264  string
266  {
267  TextTemplate plot{GNUPLOT_BASIC_PLOT_DEF
268  +GNUPLOT_SCATTER_REGRESSION};
269 
270  params.set ("CommonStyleDef", GNUPLOT_CommonStyleDef)
271  .set ("AxisGridSetup", GNUPLOT_AxisGridSetup)
272  ;
273  return plot.render (params.genNode());
274  }
275 
276 }} // namespace lib::gnuplot_gen
Preconfigured setup for data visualisation with Gnuplot.
Text template substitution engine.
string scatterRegression(ParamRecord params)
Generate a (X,Y)-scatter plot with regression line.
string dataPlot(ParamRecord params)
Generate a Gnuplot diagram to visualise the given data points.
Implementation namespace for support and library code.
A minimalistic text templating engine with flexible data binding.
Collection of small helpers and convenience shortcuts for diagnostics & formatting.
A complement to allow instantiation of a TextTemplate with ETD data.