Passed
Push — master ( 8f5e6a...061d3e )
by Felipe
03:26
created

ColorMapDriver::Draw()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 94
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 55
nc 18
nop 6
dl 0
loc 94
rs 6.4883
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php // content="text/plain; charset=utf-8"
2
require_once('../jpgraph.php');
3
require_once('../jpgraph_canvas.php');
4
require_once('../jpgraph_colormap.inc.php');
5
6
class ColorMapDriver
7
{
8
    const WIDTH     = 600; // Image width
9
    const LMARG     = 90;  // Left margin
10
    const RMARG     = 25;  // Right margin
11
    const MAPMARG   = 35;  // Map margin between each map
12
    const MODHEIGHT = 30; // Module height (=Map height)
13
    const YSTART    = 60; // Start coordinate for map list
14
15
    public function Draw($aTitle, $aStart, $aEnd, $n=64, $aReverse=false, $addColorNames = false)
16
    {
17
18
        // Setup to draw colormap with names platoe colors
19
        $lmarg = ColorMapDriver::LMARG; // left margin
20
        $rmarg = ColorMapDriver::RMARG; // right margin
21
        $width = ColorMapDriver::WIDTH; // Overall image width
22
23
        // Module height
24
        $mh = ColorMapDriver::MODHEIGHT;
25
26
        // Step between each map
27
        $ymarg = $mh + ColorMapDriver::MAPMARG;
28
29
        if ($addColorNames) {
30
            $ymarg += 50;
31
        }
32
33
        // Start position
34
        $xs=$lmarg;
35
        $ys=ColorMapDriver::YSTART;
36
37
        // Setup a basic canvas graph
38
        $height = ($aEnd - $aStart + 1) * $ymarg + 50;
39
        $graph  = new CanvasGraph($width, $height);
40
        $graph->img->SetColor('darkgray');
41
        $graph->img->Rectangle(0, 0, $width - 1, $height - 1);
42
43
        $t = new Text($aTitle, $width / 2, 5);
44
        $t->SetAlign('center', 'top');
45
        $t->SetFont(FF_ARIAL, FS_BOLD, 14);
46
        $t->Stroke($graph->img);
47
48
        // Instantiate a colormap
49
        $cm = new ColorMap();
50
        $cm->InitRGB($graph->img->rgb);
51
52
        for ($mapidx=$aStart; $mapidx <= $aEnd; ++$mapidx, $ys += $ymarg) {
53
            $cm->SetMap($mapidx, $aReverse);
54
            $n                       = $cm->SetNumColors($n);
55
            list($mapidx, $maparray) = $cm->GetCurrMap();
56
            $ncols                   = count($maparray);
57
            $colbuckets              = $cm->GetBuckets();
58
59
            // The module width will depend on the actual number of colors
60
            $mw = round(($width - $lmarg - $rmarg) / $n);
61
62
            // Draw color map title (name)
63
            $t->Set('Basic colors: '.$ncols.',   Total colors: '.$n);
64
            $t->SetAlign('center', 'bottom');
65
            $t->SetAngle(0);
66
            $t->SetFont(FF_TIMES, FS_NORMAL, 14);
67
            $t->Stroke($graph->img, $width / 2, $ys - 3);
68
69
            // Add the name/number of the map to the left
70
            $t->SetAlign('right', 'center');
71
            $t->Set('Map: '.$mapidx);
72
            $t->SetFont(FF_ARIAL, FS_NORMAL, 14);
73
            $t->Stroke($graph->img, $xs - 20, round($ys + $mh / 2));
74
75
            // Setup text properties for the color names
76
            if ($addColorNames) {
77
                $t->SetAngle(30);
78
                $t->SetFont(FF_ARIAL, FS_NORMAL, 12);
79
                $t->SetAlign('right', 'top');
80
            }
81
82
            // Loop through all colors in the map
83
            $x = $xs;
84
            $y = $ys;
85
            $k=0;
86
            for ($i=0; $i < $n; ++$i) {
87
                $graph->img->SetColor($colbuckets[$i]);
88
                $graph->img->FilledRectangle($x, $y, $x + $mw, $y + $mh);
89
90
                // Mark all basic colors in the map with a bar and name
91
                if ($i % (($n - $ncols) / ($ncols - 1) + 1) == 0) {
92
                    $graph->img->SetColor('black');
93
                    $graph->img->FilledRectangle($x, $y + $mh + 4, $x + $mw - 1, $y + $mh + 6);
94
                    if ($addColorNames) {
95
                        $t->Set($maparray[$k++]);
96
                        $t->Stroke($graph->img, $x + $mw / 2, $y + $mh + 10);
97
                    }
98
                }
99
                $x += $mw;
100
            }
101
102
            // Draw a border around the map
103
            $graph->img->SetColor('black');
104
            $graph->img->Rectangle($xs, $ys, $xs + $mw * $n, $ys + $mh);
105
        }
106
107
        // Send back to client
108
        $graph->Stroke();
109
    }
110
}
111
112
$driver = new ColorMapDriver();
113
114
$title     = "Standard maps";
115
$reverse   = false;
116
$n         = 64; $s        =0; $e        =9;
117
$showNames = false;
118
119
120
/*
121
$title = "Center maps";
122
$reverse = false;
123
$n = 64; $s=10; $e=14;
124
$showNames = false;
125
*/
126
127
/*
128
$title = "Continues maps";
129
$reverse = false;
130
$n = 64; $s=15; $e=21;
131
$showNames = false;
132
*/
133
$driver->Draw($title, $s, $e, $n, $reverse, $showNames);
134