3 * Generates the SVG needed for the plot
8 require_once 'pma_svg_data_point.php';
10 class PMA_Scatter_Plot
13 * @var array Raw data for the plot
18 * @var array Data points of the plot
23 * @var array Set of default settigs values are here.
25 private $_settings = array(
27 // Array of colors to be used for plot.
48 // Plot background color.
49 'bgColor' => '#84AD83',
51 // The width of the plot.
54 // The height of the plot.
57 // Default X Axis label. If empty, label will be taken from the data.
60 // Default Y Axis label. If empty, label will be taken from the data.
63 // Data point label. If empty, label will be taken from the data.
69 * @var array Options that the user has specified.
71 private $_userSpecifiedSettings = null;
74 * Returns the settings array
76 * @return the settings array.
78 public function getSettings()
80 return $this->_settings
;
84 * Returns the data array
86 * @return the data array.
88 public function getData()
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()
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">';
146 <path id="myTextPath1"
148 <path id="myTextPath2"
149 d="M250,10 L370,10"/>
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>
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>
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']);
174 * Get the visualization as a SVG.
176 * @return the visualization as a SVG
178 public function asSVG()
180 $output = $this->_svg();
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) {
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++
) {
225 $coordinates[$i][$j] = 20 +
500 * $coordinates[$i][$j] / $maxC;
227 $coordinates[$i][$j] = 20 +
320 * (1 - $coordinates[$i][$j] / $maxC);
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)
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);