make_graph.py 3.16 KB
Newer Older
1
#!/usr/bin/python3
2 3
# -*- coding: utf-8 -*-
#
4
# Copyright 2009-2019 Holger Levsen (holger@layer-acht.org)
5 6 7 8 9 10 11 12 13
#
# based on similar code taken from piuparts-reports.py written by me

import os
import sys
from rpy2 import robjects
from rpy2.robjects.packages import importr

def main():
14
    if len(sys.argv) != 8:
15
        print("we need exactly seven params: csv-file-in, png-out-file, color, mainlabel, ylabl, width, height")
16 17 18 19 20 21 22
        return
    filein = sys.argv[1]
    fileout = sys.argv[2]
    colors = sys.argv[3]
    columns = str(int(colors)+1)
    mainlabel = sys.argv[4]
    ylabel = sys.argv[5]
23 24
    width = int(sys.argv[6])
    height = int(sys.argv[7])
25 26 27
    countsfile = os.path.join(filein)
    pngfile = os.path.join(fileout)
    grdevices = importr('grDevices')
28
    grdevices.png(file=pngfile, width=width, height=height, pointsize=10, res=100, antialias="none")
29 30 31
    r = robjects.r
    r('t <- (read.table("'+countsfile+'",sep=",",header=1,row.names=1))')
    r('cname <- c("date",rep(colnames(t)))')
32
    # thanks to http://tango.freedesktop.org/Tango_Icon_Theme_Guidelines for those nice colors
Holger Levsen's avatar
Holger Levsen committed
33
    if int(colors) < 6:
34
        r('palette(c("#73d216", "#f57900", "#cc0000", "#2e3436", "#888a85"))')
35 36
    elif int(colors) == 6:
        r('palette(c("#73d216", "#f57900", "#cc0000", "#2e3436", "#ffdd00", "#aaaaaa"))')
37 38 39 40 41
    elif int(colors) == 16:
        r('palette(c("#4e9a06", "#57a231", "#73d216", "#8ae234", \
                     "#ce5c00", "#f57900", "#fcaf3e", "#fcda27", \
                     "#a40000", "#cc0000", "#ef2929", "#fc2a1a", \
                     "#2e3436", "#555753", "#888a85", "#b8bbb4" ))')
42
    elif int(colors) < 39:
43 44
        r('palette(c("#fce94f", "#c4a000", "#eeeeec", "#babdb6", \
                     "#fcaf3e", "#ce5c00", "#ad7fa8", "#5c3566", \
45
                     "#e9b96e", "#8f5902", "#8ae234", "#4e9a06", \
46
                     "#729fcf", "#204a87", "#ef2929", "#a40000", \
47
                     "#888a85", "#2e3436", "#75507b", "#cc0000", \
48
                     "#ce5c00", "#73d216", "#edd400", "#f57900", \
49
                     "#c17d11", "#3465a4", "#666666", "#aaaaaa", \
50
                     "#aa00aa", "#ff55ff", "#123456", "#7890ab" ))')
51
    elif int(colors) == 40:
52
        r('palette(c("#4e9a06", "#000000"))')
53
    elif int(colors) == 41:
54
        r('palette(c("#57a231", "#000000"))')
55
    elif int(colors) == 42:
56 57
        r('palette(c("#73d216", "#000000"))')
    elif int(colors) == 43:
58
        r('palette(c("#8ae234", "#000000"))')
59 60 61
    # "revert the hack" (it's still a hack :)
    if int(colors) >= 40:
        colors='1'
62 63
    r('v <- t[0:nrow(t),0:'+colors+']')
    # make graph since day 1
64
    r('barplot(t(v),col = 1:'+columns+', main="'+mainlabel+'", xlab="", ylab="'+ylabel+'", space=0, border=NA, names.arg=rownames(t))')
65
    if int(colors) < 10:
66
        r('legend(x="bottom",legend=colnames(t), ncol=2,fill=1:'+columns+',xjust=0.5,yjust=0,bty="n")')
67
    elif int(colors) == 16:
68
        r('legend(x="bottom",legend=colnames(t), ncol=4,fill=1:'+columns+',xjust=0.5,yjust=0,bty="n")')
69
    else:
70
        r('legend(x="bottom",legend=colnames(t), ncol=7,fill=1:'+columns+',xjust=0.5,yjust=0,bty="n")')
71 72 73 74 75 76
    grdevices.dev_off()

if __name__ == "__main__":
    main()

# vi:set et ts=4 sw=4 :