gnuplot - 用循环绘图

时间:2017-07-28 09:55:13

标签: linux gnuplot

我的gnuplot脚本有问题。

我的数据文件,格式如下,(值为例)

# timestamp        |- user1 -|       |-user2-|          |-user3-|
# ms            procs cpu% mem%   procs cpu% mem%    procs cpu% mem%
1234            10    12   13       20   22   23      30    32   33
1235            19    15   16       29   25   26      39    35   36

我的gnuplot代码是

reset

USERS="user1 user2 user3"

VAL(g,c)= (3*(g-1)) + c  

plot for [i=1:words(USERS)] "__GCRONDIR__/.data/usrstat.log" \
   u 1:VAL(i,3) t word(USERS,i)." cpu:".i  __STYLE0__, \
"" u 1:VAL(i,4) t word(USERS,i)." mem:".i  __STYLE0__

我的问题是循环只发生在最后一个用户身上, 所以如果一个有10个用户,让我们说,这个脚本,将绘制9个cpu的值, 对于前9个用户,以及最后一个用户的两个值。 ......对我来说没有任何意义吗?

这里有一个屏幕截图;)

enter image description here

2 个答案:

答案 0 :(得分:1)

似乎gnuplot只在一个循环中绘制一行,而循环的第二部分被视为一个单独的绘图。 Gnuplot首先绘制所有" cpu"循环中的行:

onComplete: function() {                                                                       
               var ctx = this.chart.ctx;
               var chart = this;
               ctx.textAlign = "center";
               ctx.textBaseline = "middle";

               var datasets = this.config.data.datasets;

               ctx.font = "15px QuickSand"; 
               ctx.fillStyle = "#303030";

               datasets.forEach(function (dataset, i) {
                    var maxValue = 0;
                    chart.getDatasetMeta(i).data.forEach(function (p, j) {
                        if(maxValue < datasets[j].data[i]) {
                            maxValue = datasets[j].data[i];
                        }
                    });

                    ctx.fillText(maxValue, datasets[i]._meta[0].data[i]._view.x, 20);        
               });
           } 

然后是最后一个&#34; mem&#34;行:

plot for [i=1:words(USERS)] "__GCRONDIR__/.data/usrstat.log" \
   u 1:VAL(i,3) t word(USERS,i)." cpu:".i  __STYLE0__, \

您可以改为使用嵌套循环:

"" u 1:VAL(i,4) t word(USERS,i)." mem:".i  __STYLE0__

enter image description here

对于你的情况,它将是这样的:

kind(k)=(k==1? "cpu: " : "mem: ")
plot for [i=1:5] for [j=1:2] (i-1)*2+j t sprintf("%s %d", kind(j), i)

答案 1 :(得分:0)

基于{p> https://stackoverflow.com/users/6401403/michael-o Michael O的回答是有效的,这是我的整个剧本。

#!/usr/bin/gnuplot

reset
set term __TERM__ transparent truecolor enhanced font "__PLOTFONT__" size __PLOTGEOMETRY__
set key noreverse outside top right Right tc rgb"#ffffff"
set grid ytics xtics back lw __PLOTGRID__ lc rgb"#ffffff"
set autoscale

set title "Users %" tc rgb"#ffffff"
set xtics ("0" 0,"." 60,"." 120,"3" 180,"." 240,"." 300,"6" 360,"." 420,"." 480,"9" 540,"." 600,"." 660,"0" 720,"." 780,"." 840,"3" 900,"." 960,"." 1020,"6" 1080,"." 1140,"." 1200,"9" 1260,"." 1320,"." 1380,"0" 1440,) tc rgb"#ffffff"
set ylabel "%" tc rgb"#ffffff"
set ytics  tc rgb"#ffffff"

DATA="__GCRONDIR__/.data/usrstat.log"
USERS=system("__GCRONDIR__/bin/getEnv 'USERS'")

VAL(g,c)=(3*(g-1)) + c  
USR(i)=word(USERS,i)

STATLABELS="pad1 procs cpu mem"

kind(k)=word(STATLABELS,k)


plot for [i=1:words(USERS)] for [j=3:4] \
"__GCRONDIR__/.data/usrstat.log" u 1:VAL(i,j) t USR(i)." ".kind(j).i  __STYLE0__