Fixed: 'Browse foreign values' not working properly
[phpmyadmin/ammaryasirr.git] / libraries / svg_plot / pma_scatter_plot.php
blob10c0aea343eae8c9f0e902b5c8dcd9171ade1166
1 <?php
2 /**
3 * Generates the SVG needed for the plot
5 * @package phpMyAdmin
6 */
8 require_once 'pma_svg_data_point.php';
10 class PMA_Scatter_Plot
12 /**
13 * @var array Raw data for the plot
15 private $_data;
17 /**
18 * @var array Data points of the plot
20 private $_dataPoints;
22 /**
23 * @var array Set of default settigs values are here.
25 private $_settings = array(
27 // Array of colors to be used for plot.
28 'colors' => array(
29 '#BCE02E',
30 '#E0642E',
31 '#E0D62E',
32 '#2E97E0',
33 '#B02EE0',
34 '#E02E75',
35 '#5CE02E',
36 '#E0B02E',
37 '#000000',
38 '#0022E0',
39 '#726CB1',
40 '#481A36',
41 '#BAC658',
42 '#127224',
43 '#825119',
44 '#238C74',
45 '#4C489B',
46 '#87C9BF',
48 // Plot background color.
49 'bgColor' => '#84AD83',
51 // The width of the plot.
52 'width' => 520,
54 // The height of the plot.
55 'height' => 325,
57 // Default X Axis label. If empty, label will be taken from the data.
58 'xLabel' => '',
60 // Default Y Axis label. If empty, label will be taken from the data.
61 'yLabel' => '',
63 // Data point label. If empty, label will be taken from the data.
64 'dataLabel' => '',
68 /**
69 * @var array Options that the user has specified.
71 private $_userSpecifiedSettings = null;
73 /**
74 * Returns the settings array
76 * @return the settings array.
78 public function getSettings()
80 return $this->_settings;
83 /**
84 * Returns the data array
86 * @return the data array.
88 public function getData()
90 return $this->_data;
93 /**
94 * Constructor. Stores user specified options.
96 * @param array $data Data for the visualization
97 * @param array $options Users specified options
99 public function __construct($data, $options)
101 $this->_userSpecifiedSettings = $options;
102 $this->_data = $data;
106 * All the variable initialization, options handling has to be done here.
108 protected function init()
110 $this->_handleOptions();
114 * A function which handles passed parameters. Useful if desired
115 * chart needs to be a little bit different from the default one.
117 private function _handleOptions()
119 $this->_dataPoints = array();
120 if (! is_null($this->_userSpecifiedSettings)) {
121 foreach (array_keys($this->_userSpecifiedSettings) as $key){
122 $this->_settings[$key] = $this->_userSpecifiedSettings[$key];
125 if ($this->_settings['dataLabel'] == '') {
126 $labels = array_keys($this->_data[0]);
127 $this->_settings['dataLabel'] = $labels[0];
132 * Generate the visualization in SVG format.
134 * @return the generated image resource
136 private function _svg()
138 $this->init();
140 $output = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . "\n";
141 $output .= '<svg version="1.1" xmlns:svg="http://www.w3.org/2000/svg"'
142 . ' xmlns="http://www.w3.org/2000/svg" width="' . $this->_settings['width'] . '"'
143 . ' height="' . $this->_settings['height'] . '">';
144 $output .= '<g id="groupPanel">';
145 $output .= '<defs>
146 <path id="myTextPath1"
147 d="M10,190 L10,50"/>
148 <path id="myTextPath2"
149 d="M250,10 L370,10"/>
150 </defs>';
151 $output .= '<text x="6" y="190" style="font-family: Arial; font-size : 54; stroke:none; fill:#000000;" >
152 <textPath xlink:href="#myTextPath1" >';
153 $output .= $this->_settings['yLabel'];
154 $output .= '</textPath>
155 </text>';
157 $output .= '<text x="250" y="10" style="font-family: Arial; font-size : 54; stroke:none; fill:#000000;" >
158 <textPath xlink:href="#myTextPath2" >';
159 $output .= $this->_settings['xLabel'];
160 $output .= '</textPath>
161 </text>';
164 $scale_data = $this->_scaleDataSet($this->_data, $this->_settings['xLabel'], $this->_settings['yLabel']);
165 $output .= $this->_prepareDataSet($this->_data, 0, $scale_data, $this->_settings['dataLabel']);
167 $output .= '</g>';
168 $output .= '</svg>';
170 return $output;
174 * Get the visualization as a SVG.
176 * @return the visualization as a SVG
178 public function asSVG()
180 $output = $this->_svg();
181 return $output;
185 * Calculates the scale, horizontal and vertical offset that should be used.
187 * @param array $data Row data
189 * @return an array containing the scale, x and y offsets
191 private function _scaleDataSet($data,$xField,$yField)
194 // Currently assuming only numeric fields are selected
195 $coordinates = array();
196 foreach ($data as $row) {
197 $coordinates[0][] = $row[$xField];
198 $coordinates[1][] = $row[$yField];
200 for ($i = 0 ; $i < 2 ; $i++) {
202 $maxC = ($i == 0) ? 500 : 320;
204 if( !is_numeric($coordinates[$i][0])) {
205 $uniqueC = array_unique($coordinates[$i]);
206 $countC = count(array_unique($coordinates[$i]));
207 $map = $tmp = array();
208 foreach ($uniqueC as $uc) {
209 $tmp[] = $uc;
211 for ($j = 0 ; $j < $countC ; $j++) {
212 $map[$tmp[$j]] = 20 + $j * $maxC / $countC;
214 for($j = 0 ; $j < count($coordinates[$i]) ; $j++) {
215 $coordinates[$i][$j] = $map[$coordinates[$i][$j]];
219 elseif (is_numeric($coordinates[$i][0])) {
221 $maxC = max($coordinates[$i]);
222 for($j = 0 ; $j < count($coordinates[$i]) ; $j++) {
224 if ($i == 0)
225 $coordinates[$i][$j] = 20 + 500 * $coordinates[$i][$j] / $maxC;
226 else
227 $coordinates[$i][$j] = 20 + 320 * (1 - $coordinates[$i][$j] / $maxC);
231 return $coordinates;
235 * Prepares and return the dataset as needed by the visualization.
237 * @param array $data Raw data
238 * @param int $color_number Start index to the color array
239 * @param array $scale_data Data related to scaling
240 * @param string $label Label for the data points
241 * @param image $results Image object in the case of png
243 * @return the formatted array of data.
245 private function _prepareDataSet($data, $color_number, $scale_data, $label)
247 $result = '';
248 // loop through the rows
249 for($i = 0 ; $i < count($data) ; $i++) {
251 $index = $color_number % sizeof($this->_settings['colors']);
253 $data_element = new PMA_SVG_Data_Point($scale_data[0][$i],$scale_data[1][$i],$data[$i][$label],$data[$i]);
255 $options = array('color' => $this->_settings['colors'][$index], 'id' => $i);
256 $this->_dataPoints[] = $data_element;
258 $result .= $data_element->prepareRowAsSVG($options);
259 $color_number++;
263 return $result;