source: sourcecode/application/libraries/PHPExcel/Chart/DataSeriesValues.php @ 1

Last change on this file since 1 was 1, checked in by dungnv, 11 years ago
File size: 7.4 KB
Line 
1<?php
2/**
3 * PHPExcel
4 *
5 * Copyright (c) 2006 - 2014 PHPExcel
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 *
21 * @category    PHPExcel
22 * @package             PHPExcel_Chart
23 * @copyright   Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
24 * @license             http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt   LGPL
25 * @version             1.8.0, 2014-03-02
26 */
27
28
29/**
30 * PHPExcel_Chart_DataSeriesValues
31 *
32 * @category    PHPExcel
33 * @package             PHPExcel_Chart
34 * @copyright   Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
35 */
36class PHPExcel_Chart_DataSeriesValues
37{
38
39        const DATASERIES_TYPE_STRING    = 'String';
40        const DATASERIES_TYPE_NUMBER    = 'Number';
41
42        private static $_dataTypeValues = array(
43                self::DATASERIES_TYPE_STRING,
44                self::DATASERIES_TYPE_NUMBER,
45        );
46
47        /**
48         * Series Data Type
49         *
50         * @var string
51         */
52        private $_dataType = null;
53
54        /**
55         * Series Data Source
56         *
57         * @var string
58         */
59        private $_dataSource = null;
60
61        /**
62         * Format Code
63         *
64         * @var string
65         */
66        private $_formatCode = null;
67
68        /**
69         * Series Point Marker
70         *
71         * @var string
72         */
73        private $_marker = null;
74
75        /**
76         * Point Count (The number of datapoints in the dataseries)
77         *
78         * @var integer
79         */
80        private $_pointCount = 0;
81
82        /**
83         * Data Values
84         *
85         * @var array of mixed
86         */
87        private $_dataValues = array();
88
89        /**
90         * Create a new PHPExcel_Chart_DataSeriesValues object
91         */
92        public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = null, $formatCode = null, $pointCount = 0, $dataValues = array(), $marker = null)
93        {
94                $this->setDataType($dataType);
95                $this->_dataSource = $dataSource;
96                $this->_formatCode = $formatCode;
97                $this->_pointCount = $pointCount;
98                $this->_dataValues = $dataValues;
99                $this->_marker = $marker;
100        }
101
102        /**
103         * Get Series Data Type
104         *
105         * @return      string
106         */
107        public function getDataType() {
108                return $this->_dataType;
109        }
110
111        /**
112         * Set Series Data Type
113         *
114         * @param       string  $dataType       Datatype of this data series
115         *                                                              Typical values are:
116         *                                                                      PHPExcel_Chart_DataSeriesValues::DATASERIES_TYPE_STRING
117         *                                                                              Normally used for axis point values
118         *                                                                      PHPExcel_Chart_DataSeriesValues::DATASERIES_TYPE_NUMBER
119         *                                                                              Normally used for chart data values
120         * @return      PHPExcel_Chart_DataSeriesValues
121         */
122        public function setDataType($dataType = self::DATASERIES_TYPE_NUMBER) {
123                if (!in_array($dataType, self::$_dataTypeValues)) {
124                throw new PHPExcel_Chart_Exception('Invalid datatype for chart data series values');
125                }
126                $this->_dataType = $dataType;
127
128                return $this;
129        }
130
131        /**
132         * Get Series Data Source (formula)
133         *
134         * @return      string
135         */
136        public function getDataSource() {
137                return $this->_dataSource;
138        }
139
140        /**
141         * Set Series Data Source (formula)
142         *
143         * @param       string  $dataSource
144         * @return      PHPExcel_Chart_DataSeriesValues
145         */
146        public function setDataSource($dataSource = null, $refreshDataValues = true) {
147                $this->_dataSource = $dataSource;
148
149                if ($refreshDataValues) {
150                        //      TO DO
151                }
152
153                return $this;
154        }
155
156        /**
157         * Get Point Marker
158         *
159         * @return string
160         */
161        public function getPointMarker() {
162                return $this->_marker;
163        }
164
165        /**
166         * Set Point Marker
167         *
168         * @param       string  $marker
169         * @return      PHPExcel_Chart_DataSeriesValues
170         */
171        public function setPointMarker($marker = null) {
172                $this->_marker = $marker;
173
174                return $this;
175        }
176
177        /**
178         * Get Series Format Code
179         *
180         * @return      string
181         */
182        public function getFormatCode() {
183                return $this->_formatCode;
184        }
185
186        /**
187         * Set Series Format Code
188         *
189         * @param       string  $formatCode
190         * @return      PHPExcel_Chart_DataSeriesValues
191         */
192        public function setFormatCode($formatCode = null) {
193                $this->_formatCode = $formatCode;
194
195                return $this;
196        }
197
198        /**
199         * Get Series Point Count
200         *
201         * @return      integer
202         */
203        public function getPointCount() {
204                return $this->_pointCount;
205        }
206
207        /**
208         * Identify if the Data Series is a multi-level or a simple series
209         *
210         * @return      boolean
211         */
212        public function isMultiLevelSeries() {
213                if (count($this->_dataValues) > 0) {
214                        return is_array($this->_dataValues[0]);
215                }
216                return null;
217        }
218
219        /**
220         * Return the level count of a multi-level Data Series
221         *
222         * @return      boolean
223         */
224        public function multiLevelCount() {
225                $levelCount = 0;
226                foreach($this->_dataValues as $dataValueSet) {
227                        $levelCount = max($levelCount,count($dataValueSet));
228                }
229                return $levelCount;
230        }
231
232        /**
233         * Get Series Data Values
234         *
235         * @return      array of mixed
236         */
237        public function getDataValues() {
238                return $this->_dataValues;
239        }
240
241        /**
242         * Get the first Series Data value
243         *
244         * @return      mixed
245         */
246        public function getDataValue() {
247                $count = count($this->_dataValues);
248                if ($count == 0) {
249                        return null;
250                } elseif ($count == 1) {
251                        return $this->_dataValues[0];
252                }
253                return $this->_dataValues;
254        }
255
256        /**
257         * Set Series Data Values
258         *
259         * @param       array   $dataValues
260         * @param       boolean $refreshDataSource
261         *                                      TRUE - refresh the value of _dataSource based on the values of $dataValues
262         *                                      FALSE - don't change the value of _dataSource
263         * @return      PHPExcel_Chart_DataSeriesValues
264         */
265        public function setDataValues($dataValues = array(), $refreshDataSource = TRUE) {
266                $this->_dataValues = PHPExcel_Calculation_Functions::flattenArray($dataValues);
267                $this->_pointCount = count($dataValues);
268
269                if ($refreshDataSource) {
270                        //      TO DO
271                }
272
273                return $this;
274        }
275
276        private function _stripNulls($var) {
277                return $var !== NULL;
278        }
279
280        public function refresh(PHPExcel_Worksheet $worksheet, $flatten = TRUE) {
281        if ($this->_dataSource !== NULL) {
282                $calcEngine = PHPExcel_Calculation::getInstance($worksheet->getParent());
283                        $newDataValues = PHPExcel_Calculation::_unwrapResult(
284                            $calcEngine->_calculateFormulaValue(
285                                '='.$this->_dataSource,
286                                NULL,
287                                $worksheet->getCell('A1')
288                            )
289                        );
290                        if ($flatten) {
291                                $this->_dataValues = PHPExcel_Calculation_Functions::flattenArray($newDataValues);
292                                foreach($this->_dataValues as &$dataValue) {
293                                        if ((!empty($dataValue)) && ($dataValue[0] == '#')) {
294                                                $dataValue = 0.0;
295                                        }
296                                }
297                                unset($dataValue);
298                        } else {
299                                $cellRange = explode('!',$this->_dataSource);
300                                if (count($cellRange) > 1) {
301                                        list(,$cellRange) = $cellRange;
302                                }
303
304                                $dimensions = PHPExcel_Cell::rangeDimension(str_replace('$','',$cellRange));
305                                if (($dimensions[0] == 1) || ($dimensions[1] == 1)) {
306                                        $this->_dataValues = PHPExcel_Calculation_Functions::flattenArray($newDataValues);
307                                } else {
308                                        $newArray = array_values(array_shift($newDataValues));
309                                        foreach($newArray as $i => $newDataSet) {
310                                                $newArray[$i] = array($newDataSet);
311                                        }
312
313                                        foreach($newDataValues as $newDataSet) {
314                                                $i = 0;
315                                                foreach($newDataSet as $newDataVal) {
316                                                        array_unshift($newArray[$i++],$newDataVal);
317                                                }
318                                        }
319                                        $this->_dataValues = $newArray;
320                                }
321                        }
322                        $this->_pointCount = count($this->_dataValues);
323                }
324
325        }
326
327}
Note: See TracBrowser for help on using the repository browser.