source: trunk/xgraph/jpgraph/Examples/colormaps.php

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