/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk

« back to all changes in this revision

Viewing changes to classes/views/basetimeseriesview.php

  • Committer: Suren A. Chilingaryan
  • Date: 2020-02-06 06:07:51 UTC
  • Revision ID: csa@suren.me-20200206060751-n4lbi25y4wh10f3u
Generalize data filtering/resampling in the VIEWs and provide common tools for secondary time series plots (based on work of Jalal)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
require_once($ADEI_ROOTDIR . "/classes/draw.php");
 
4
 
 
5
abstract class BASETimeSeriesView extends VIEW {
 
6
 var $axis_label_font_size = 8;
 
7
 var $axis_title_font_size = 8;
 
8
 var $axis_title_symbol_width = 8;
 
9
 var $axis_title_height = 20;
 
10
 var $x_axis_margin_correction = 10;
 
11
 
 
12
 
 
13
 function __construct(REQUEST $req  = NULL, $options) {
 
14
    parent::__construct($req, $options);
 
15
 }
 
16
 
 
17
 function GetTimeAxisLabelsWidth($format) {
 
18
    global $GRAPH_MARGINS;
 
19
 
 
20
    //    $bottom_margin_threshold = $length > 86400 && $length < 1036800 ? 70 : 40;
 
21
    if ($format['time_format']) {
 
22
        $formatted = date($format['time_format']);
 
23
        $symbols = strlen($formatted);
 
24
        $ret = $this->axis_title_symbol_width * $symbols;
 
25
    } else {
 
26
        $ret = $GRAPH_MARGINS['axis'] - $this->axis_title_height;
 
27
    }
 
28
    return $ret;
 
29
 }
 
30
 
 
31
 function GetYAxisLabelsWidth($format) {
 
32
    global $GRAPH_MARGINS;
 
33
 
 
34
    return $GRAPH_MARGINS['axis'] - $this->axis_title_height;
 
35
 }
 
36
 
 
37
 
 
38
 private function GetTimeAxisMajorTickStep($length, $width, $min_tick) {
 
39
    $n_ticks = $width / 40;
 
40
    if (!$n_ticks) $n_ticks = 1;
 
41
    $tick_step = $length / $n_ticks;
 
42
    return 2 * $min_tick * ceil($tick_step / $min_tick / 2);
 
43
 }
 
44
 
 
45
 function GetTimeAxisFormat(INTERVAL $ivl, $width = 0) {
 
46
     global $GRAPH_SUBSECOND_THRESHOLD;
 
47
 
 
48
    $time_format = false;
 
49
    $min_ticks = 0; $maj_ticks = 0;
 
50
 
 
51
    $length = $ivl->GetWindowSize();
 
52
 
 
53
    if ($length > 315360000) { // 10 years
 
54
        $time_format = 'Y';
 
55
        $min_ticks = 315360000;
 
56
    } elseif ($length > 31104000) { // 1 year
 
57
        $time_format = 'M, Y';
 
58
        $min_ticks = 3104000;
 
59
    } elseif ($length > 1036800) { // 12 days
 
60
        $time_format = 'M d';
 
61
        $min_ticks = 3600 * 24;
 
62
    } elseif ($length > 86400) { // 1 day
 
63
        $time_format = 'M d, H:i';
 
64
        $min_ticks = 3600 * 4;
 
65
    } elseif ($length > 14400) { // 4 hours
 
66
        $time_format = 'H:i';
 
67
        $min_ticks = 1800;
 
68
    } elseif ($length > 1800) { // 30 min
 
69
        $time_format = 'H:i';
 
70
        $min_ticks = 150;
 
71
    } elseif ($length > 600) { // 5 min
 
72
        $time_format = 'H:i';
 
73
        $min_ticks = 30;
 
74
    } elseif ($length > $GRAPH_SUBSECOND_THRESHOLD) {
 
75
        $time_format = 'H:i:s';
 
76
        $min_ticks = $GRAPH_SUBSECOND_THRESHOLD;
 
77
    } else { 
 
78
        /* We need to adjust data in the VIEW code, like this:
 
79
            $from = $xdata[0];
 
80
            foreach ($xdata as &$x) $x = dsMathPreciseSubstract($x, $from);*/
 
81
    }
 
82
 
 
83
    if (($width)&&($min_ticks)) {
 
84
        $maj_ticks = $this->GetTimeAxisMajorTickStep($length, $width, $min_ticks);
 
85
    }
 
86
 
 
87
    return array(
 
88
        'time_format' => $time_format,
 
89
        'minor_tick_step' => $min_ticks,
 
90
        'major_tick_step' => $maj_ticks
 
91
    );
 
92
 }
 
93
 
 
94
 function ConfigureTimeAxis(Graph $graph, INTERVAL $ivl, $width) {
 
95
     global $GRAPH_SUBSECOND_THRESHOLD;
 
96
 
 
97
    $format = $this->GetTimeAxisFormat($ivl, $width);
 
98
    if ($format['time_format']) {
 
99
        $graph->SetScale('datlin', 0, 0, $this->ivl->GetWindowStart(), $this->ivl->GetWindowEnd());
 
100
        $graph->xaxis->scale->SetDateFormat($format['time_format']);
 
101
    } else {
 
102
        $graph->SetScale('linlin', 0, 0, 0, $this->ivl->GetWindowSize());
 
103
        $graph->xaxis->SetLabelFormatCallback(array(new DRAW_JPGraphAxisHolder(NULL, NULL, $graph->xaxis), 'YLabelFormat'));
 
104
    }
 
105
 
 
106
    if ($format['major_tick_step'])
 
107
        $graph->xaxis->scale->ticks->Set($format['major_tick_step'], $format['minor_tick_step']);
 
108
 
 
109
    $graph->xaxis->SetPos('min');
 
110
    $graph->xaxis->SetLabelAngle(90);
 
111
    $graph->xaxis->SetTextLabelInterval(1);
 
112
    $graph->xaxis->SetFont(FF_ARIAL, FS_NORMAL, $this->axis_label_font_size);
 
113
    $graph->xaxis->title->SetFont(FF_ARIAL, FS_BOLD, $this->axis_title_font_size);
 
114
 
 
115
    $format['labels_width'] = $this->GetTimeAxisLabelsWidth($format);
 
116
    return $format;
 
117
 }
 
118
 
 
119
 function GetYAxisFormat(array $minmax = NULL, $height = 0) {
 
120
    return array();
 
121
 }
 
122
 
 
123
 function ConfigureYAxis(Graph $graph, Axis $axis, array $minmax = NULL, $height = 0) {
 
124
    $axis->SetLabelFormatCallback(array(new DRAW_JPGraphAxisHolder(NULL, NULL, $axis), 'YLabelFormat'));
 
125
 
 
126
    $axis->SetFont(FF_ARIAL, FS_NORMAL, $this->axis_label_font_size);
 
127
    $axis->title->SetFont(FF_ARIAL, FS_BOLD, $this->axis_title_font_size);
 
128
 
 
129
    $format = $this->GetYAxisFormat($minmax, $height);
 
130
    $format['labels_width'] = $this->GetYAxisLabelsWidth($format);
 
131
    return $format;
 
132
 }
 
133
 
 
134
 function ConfigureXAxisTitle(Graph $graph, $label, $position = 0) {
 
135
    $graph->xaxis->SetTitle("$label");
 
136
 
 
137
    if ($position) {
 
138
        $graph->xaxis->SetTitleMargin($position - $this->x_axis_margin_correction);
 
139
        return $position + $this->axis_title_height;// + $this->x_axis_margin_correction;
 
140
    }
 
141
 
 
142
    return false;
 
143
 }
 
144
 
 
145
 function ConfigureYAxisTitle(Graph $graph, Axis $axis, $label, $position = 0) {
 
146
    $axis->SetTitle("$label");
 
147
 
 
148
    if ($position) {
 
149
        $axis->SetTitleMargin($position);
 
150
        return $position + $this->axis_title_height;
 
151
    }
 
152
 
 
153
    return false;
 
154
 }
 
155
 
 
156
}
 
157
 
 
158
?>
 
 
b'\\ No newline at end of file'