francois P
francois P

Reputation: 392

how could I replace gnuplot output to image (keep it open) output from inside PSQL?

how could I replace gnuplot output to image (keep it open in its own window) output from inside PSQL ?

So I would like to output that on graphical graphic to make it more readable by my users,

I mean when they start the script they get a popup Xorg window with the graph until they close themselves that window.

currently, I use this kind of test :

francois@zaphod:~/Documents/DEV/sh/pgsqlplot$ cat commands                                                                   
\t                                                                                                                           
\a                                                                                                                           
\f ' '                                                                                                                       
\o | /usr/bin/gnuplot                                                                                                        
select 'set title "My test Graph"; set terminal dumb 128 52; set key off; set ylabel "something"; set xlabel "temp";' || 'plot ''-'' with lines;' ;
select something from temp where date >= '2018-01-01' order by date ;                                                        
\o                                                                                                                           
francois@zaphod:~/Documents/DEV/sh/pgsqlplot$ psql perso < commands

So I cat get a terminal pseudo graph like that :

  3000 +-+--------------------+-----------------------+----------------------+-----------------------+--------------------+-+
       +                      +                       +                      +                       +                      +
       |                                                       *                                                            |
       |                                                       **                                                           |
       |                                                       * *                                                          |
       |                                                       *  *                                                         |
       |                                                       *  *                                                         |
  2500 +-+                                            *       *    *                                                      +-+

[...etc...]

I did not find the good help pages neither in gnuplot neither in gluplot documents. Could you help me to adapt the commands file to use proper options around gnuplot & postgres ?

I tried to use set terminal jpeg large font arial size 2048,768 & \o | /usr/bin/gnuplot --persist but then I get the jpeg binary content on the terminal then

Upvotes: 0

Views: 130

Answers (1)

maij
maij

Reputation: 4218

First, choose a window terminal. Normally I would expect the default terminal, the terminal which is selected without an explicit set terminal command, to be a window terminal. In my case it is the qt terminal, but there are other options like set terminal x11. You can get a list with the set terminal command from within gnuplot. As a first attempt, I would just remove the set terminal dumb command.

Second, to keep the window open, I see two possibilities:

  1. Adding the -persist commandline option when calling gnuplot.

    This closes gnuplot and finishes your script.

  2. Using the pause mouse close command in the gnuplot script.

    This does not close gnuplot but keeps the possibility to use the mouse and keyboard shortcuts for zooming, adding a grid etc.

Using option 2, we arrive at this script (I have rearranged the lines to avoid scrolling):

\t
\a
\f ' ' 
\o | /usr/bin/gnuplot
select 'show terminal';
select 'set title "My test Graph"';
select 'set key off';
select 'set ylabel "something"'; 
select 'set xlabel "temp"'; 
select 'plot ''-'' with lines';
select something from temp where date >= '2018-01-01' order by date ;
select 'e';
select 'pause mouse close';
\o

The line select 'show terminal'; prints qt in my case. The line select 'e'; indicates that there is no more data to plot.

With option 1, the line which calls gnuplot would be \o | /usr/bin/gnuplot -persist.


Just in case you want to switch to a file output, you have to add the name of the output file:

select 'set terminal jpeg';
select 'set output "filename.jpg"';
...
select 'plot ...

Upvotes: 1

Related Questions