16
16
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
17
17
******************************************************************************/
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;
24
private uint smoothvalue;
21
25
private double[] scalerhistory;
22
private double scalerminimum;
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; }
27
public string minimum { get; set; }
28
public string maximum { get; set; }
29
public string smooth {
35
this.smoothvalue = (uint)uint64.parse(value);
36
this.scalerhistory = null;
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 {
33
45
return this._trace_length;
36
48
this._trace_length = value;
37
foreach (var trace in this.traces)
38
trace.set_values_length(value);
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);
53
public string[] traces {
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;
64
this._tracedatas = this._tracedatas[0:this._traces.length];
68
public string id { get; private set; }
69
public double scale { get; private set; default = 1; }
70
public TraceData[] tracedatas { get; private set; }
72
public GraphData(string 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;
56
// - add data points to the traces
57
// - update the menuitems
58
// - call update_scale
59
public abstract void update();
76
public void update(Data[] datas) {
77
var parser = new ExpressionParser(datas);
79
foreach (var tracedata in this.tracedatas) {
80
var tokens = parser.tokenize(tracedata.expression);
81
tracedata.add_value(double.parse(parser.evaluate(tokens)));
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);
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
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);
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;
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)