~indicator-multiload/indicator-multiload/trunk

« back to all changes in this revision

Viewing changes to src/graphdata.vala

  • Committer: Michael Hofmann
  • Date: 2011-08-15 17:12:11 UTC
  • mfrom: (32.1.13 expressions)
  • Revision ID: mh21@piware.de-20110815171211-y2l29z2yan40g3cg
Merge expression branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                *
17
17
 ******************************************************************************/
18
18
 
19
 
public abstract class IconData : GLib.Object {
 
19
public class GraphData : GLib.Object {
 
20
    private string[] _traces;
20
21
    private uint _trace_length;
 
22
    private string _smooth;
 
23
 
 
24
    private uint smoothvalue;
21
25
    private double[] scalerhistory;
22
 
    private double scalerminimum;
23
 
 
24
 
    public Gdk.Color color { get; set; }
25
 
    public uint alpha { get; set; default = 0xffff; }
26
 
    public bool enabled { get; set; default = true; }
27
 
    public string id { get; private set; }
28
 
    public double scale { get; private set; default = 1; }
29
 
    public string[] menuitems { get; protected set; default = {}; }
30
 
    public IconTraceData[] traces {get; private set; }
 
26
 
 
27
    public string minimum { get; set; }
 
28
    public string maximum { get; set; }
 
29
    public string smooth { 
 
30
        get {
 
31
            return this._smooth;
 
32
        } 
 
33
        set {
 
34
            this._smooth = value;
 
35
            this.smoothvalue = (uint)uint64.parse(value);
 
36
            this.scalerhistory = null;
 
37
        } 
 
38
    }
 
39
 
 
40
    public Gdk.Color background_color { get; set; }
 
41
    public uint alpha { get; set; }
 
42
    public bool enabled { get; set; }
31
43
    public uint trace_length {
32
44
        get {
33
45
            return this._trace_length;
34
46
        }
35
47
        set {
36
48
            this._trace_length = value;
37
 
            foreach (var trace in this.traces)
38
 
                trace.set_values_length(value);
39
 
        }
40
 
    }
41
 
 
42
 
    // set scalerdelay to 1 and scalerminimum to 1 for percentage values without scaling
43
 
    public IconData(string id, uint traces, uint scalerdelay, double scalerminimum) {
44
 
        this.traces = new IconTraceData[traces];
45
 
        for (uint i = 0; i < traces; ++i)
46
 
            this.traces[i] = new IconTraceData();
 
49
            foreach (var tracedata in this.tracedatas)
 
50
                tracedata.set_values_length(value);
 
51
        }
 
52
    }
 
53
    public string[] traces {
 
54
        get {
 
55
            return this._traces;
 
56
        }
 
57
        set {
 
58
            this._traces = value;
 
59
            while (this._tracedatas.length < this._traces.length) {
 
60
                var tracedata = new TraceData();
 
61
                tracedata.set_values_length(this._trace_length);
 
62
                this._tracedatas += tracedata;
 
63
            }
 
64
            this._tracedatas = this._tracedatas[0:this._traces.length];
 
65
        }
 
66
    }
 
67
 
 
68
    public string id { get; private set; }
 
69
    public double scale { get; private set; default = 1; }
 
70
    public TraceData[] tracedatas { get; private set; }
 
71
 
 
72
    public GraphData(string id) {
47
73
        this.id = id;
48
 
        this.trace_length = 16;
49
 
        this.scalerhistory = new double[scalerdelay];
50
 
        for (uint i = 0; i < scalerdelay; ++i)
51
 
            this.scalerhistory[i] = scalerminimum;
52
 
        this.scalerminimum = scalerminimum;
53
 
    }
54
 
 
55
 
    // needs to
56
 
    // - add data points to the traces
57
 
    // - update the menuitems
58
 
    // - call update_scale
59
 
    public abstract void update();
 
74
    }
 
75
 
 
76
    public void update(Data[] datas) {
 
77
        var parser = new ExpressionParser(datas);
 
78
 
 
79
        foreach (var tracedata in this.tracedatas) {
 
80
            var tokens = parser.tokenize(tracedata.expression);
 
81
            tracedata.add_value(double.parse(parser.evaluate(tokens)));
 
82
        }
 
83
 
 
84
        var minimumtokens = parser.tokenize(this.minimum);
 
85
        var scalerminimum = double.parse(parser.evaluate(minimumtokens));
 
86
        var maximumtokens = parser.tokenize(this.maximum);
 
87
        var scalermaximum = double.parse(parser.evaluate(maximumtokens));
 
88
        this.update_scale(scalerminimum, scalermaximum);
 
89
    }
60
90
 
61
91
    // Fast attack, slow decay
62
92
    // - each cycle, the peak value in the plot is determined
67
97
    //   - it is never smaller than the peak value in the plot
68
98
    //   - after the current peak leaves the plot, the scaling factor gets
69
99
    //     reduced slowly
70
 
    protected virtual void update_scale() {
71
 
        double currentpeak = this.scalerminimum;
 
100
    private void update_scale(double scalerminimum, double scalermaximum) {
 
101
        double currentpeak = scalerminimum;
72
102
        for (uint i = 0, isize = this.trace_length; i < isize; ++i) {
73
103
            double currentvalue = 0;
74
 
            foreach (var trace in this.traces)
75
 
                currentvalue += trace.values[i];
 
104
            foreach (var tracedata in this.tracedatas)
 
105
                if (tracedata.enabled)
 
106
                    currentvalue += tracedata.values[i];
76
107
            currentpeak = double.max(currentpeak, currentvalue);
77
108
        }
 
109
        if (scalermaximum != 0)
 
110
            currentpeak = double.min(currentpeak, scalermaximum);
 
111
        if (this.scalerhistory.length == 0) {
 
112
            this.scalerhistory = new double[this.smoothvalue];
 
113
            for (uint i = 0; i < this.smoothvalue; ++i)
 
114
                this.scalerhistory[i] = scalerminimum;
 
115
        }
78
116
        double historymaximum = Utils.max(this.scalerhistory);
79
117
        if (currentpeak < historymaximum) {
80
118
            for (uint i = 0, isize = this.scalerhistory.length; i + 1 < isize; ++i)
89
127
 
90
128
    public void set_source_color(Cairo.Context ctx)
91
129
    {
92
 
        ctx.set_source_rgba(this.color.red / 65535.0,
93
 
                this.color.green / 65565.0,
94
 
                this.color.blue / 65565.0,
 
130
        ctx.set_source_rgba(this.background_color.red / 65535.0,
 
131
                this.background_color.green / 65565.0,
 
132
                this.background_color.blue / 65565.0,
95
133
                this.alpha / 65565.0);
96
134
    }
97
135
}