1: <?php
2: /**
3: * LICENSE (BSD)
4: *
5: * Copyright (c) 2013, Gerd Christian Kunze
6: * All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions are
10: * met:
11: *
12: * * Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: *
15: * * Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * * Neither the name of Gerd Christian Kunze nor the names of the
20: * contributors may be used to endorse or promote products derived from
21: * this software without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34: *
35: * X
36: * 27.03.2013 09:27
37: */
38: namespace MOC\Module\Office\Chart\Axis;
39: use MOC\Api;
40: use MOC\Generic\Device\Module;
41:
42: /**
43: *
44: */
45: class X implements Module {
46:
47: /**
48: * Get Dependencies
49: *
50: * @static
51: * @return \MOC\Core\Depending
52: */
53: public static function InterfaceDepending() {
54: return Api::Core()->Depending();
55: }
56:
57: /**
58: * Get Singleton/Instance
59: *
60: * @static
61: * @return X
62: */
63: public static function InterfaceInstance() {
64: return new X();
65: }
66:
67: /**
68: * Get Changelog
69: *
70: * @static
71: * @return \MOC\Core\Changelog
72: */
73: public static function InterfaceChangelog() {
74: return Api::Core()->Changelog()->Create( __CLASS__ );
75: }
76:
77: /*
78: xaxis, yaxis: {
79: mode: null or "time" ("time" requires jquery.flot.time.js plugin)
80: timezone: null, "browser" or timezone (only makes sense for mode: "time")
81:
82: autoscaleMargin: null or number
83:
84: ticks: null or number or ticks array or (fn: axis -> ticks array)
85: tickSize: number or array
86: minTickSize: number or array
87: tickFormatter: (fn: number, object -> string) or string
88: tickDecimals: null or number
89:
90: labelWidth: null or number
91: labelHeight: null or number
92: reserveSpace: null or true
93:
94: tickLength: null or number
95:
96: alignTicksWithAxis: null or number
97: }
98: */
99: private $Configuration = array(
100: 'show' => true,
101: 'alignTicksWithAxis' => false,
102: 'axisLabelFontFamily' => 'sans-serif',
103: 'axisLabelUseCanvas' => true,
104: // 'axisLabelUseHtml' => true,
105: 'axisLabelFontSizePixels' => 11
106: );
107:
108: /**
109: * @param string $Position 'top','bottom'
110: *
111: * @return X
112: */
113: public function Position( $Position = 'bottom' ) {
114: $this->Configuration['position'] = $Position;
115: return $this;
116: }
117:
118: /**
119: * @param string $Label
120: *
121: * @return Y
122: */
123: public function Label( $Label = 'X-Axis' ) {
124: $this->Configuration['axisLabel'] = $Label;
125: return $this;
126: }
127:
128: /**
129: * @param string $HexColor
130: *
131: * @return X
132: */
133: public function Color( $HexColor = '#333333' ) {
134: $this->Configuration['color'] = $HexColor;
135: return $this;
136: }
137:
138: /**
139: * @param int $Size
140: *
141: * @return X
142: */
143: public function FontSize( $Size = 11 ) {
144: $this->Configuration['font']['size'] = $Size;
145: return $this;
146: }
147:
148: /**
149: * @param string $Style
150: *
151: * @return X
152: */
153: public function FontStyle( $Style = 'italic' ) {
154: $this->Configuration['font']['style'] = $Style;
155: return $this;
156: }
157:
158: /**
159: * @param string $Weight
160: *
161: * @return X
162: */
163: public function FontWeight( $Weight = 'bold' ) {
164: $this->Configuration['font']['weight'] = $Weight;
165: return $this;
166: }
167:
168: /**
169: * @param string $Family
170: *
171: * @return X
172: */
173: public function FontFamily( $Family = 'sans-serif' ) {
174: $this->Configuration['font']['family'] = $Family;
175: return $this;
176: }
177:
178: /**
179: * @param string $Variant
180: *
181: * @return X
182: */
183: public function FontVariant( $Variant = 'small-caps' ) {
184: $this->Configuration['font']['variant'] = $Variant;
185: return $this;
186: }
187:
188: /**
189: * @param string $HexColor
190: *
191: * @return X
192: */
193: public function FontColor( $HexColor = '#333333' ) {
194: $this->Configuration['font']['color'] = $HexColor;
195: return $this;
196: }
197:
198: /**
199: * @return X
200: */
201: public function ScaleToLog() {
202: $this->Configuration['transform'] = 'function (v) { return Math.log(v); }';
203: $this->Configuration['inverseTransform'] = 'function (v) { return Math.exp(v); }';
204: return $this;
205: }
206:
207: /**
208: * @return X
209: */
210: public function ScaleToCategory() {
211: $this->Configuration['mode'] = "categories";
212: $this->Configuration['tickLength'] = 0;
213: return $this;
214: }
215:
216:
217: /**
218: * @param null|int|float $Minimum
219: *
220: * @return X
221: */
222: public function ScaleMinimum( $Minimum = null ) {
223: $this->Configuration['min'] = $Minimum;
224: return $this;
225: }
226:
227: /**
228: * @param null|int|float $Maximum
229: *
230: * @return X
231: */
232: public function ScaleMaximum( $Maximum = null ) {
233: $this->Configuration['max'] = $Maximum;
234: return $this;
235: }
236:
237: /**
238: * @param string $HexColor
239: *
240: * @return X
241: */
242: public function TickColor( $HexColor = '#333333' ) {
243: $this->Configuration['tickColor'] = $HexColor;
244: return $this;
245: }
246:
247: /**
248: * @param int $Precision
249: *
250: * @return X
251: */
252: public function TickFormatDecimal( $Precision = 2 ) {
253: $this->Configuration['tickDecimals'] = $Precision;
254: $this->Configuration['tickFormatter'] = 'function formatter( val, axis ) { return val.toFixed( axis.tickDecimals ); }';
255: return $this;
256: }
257:
258: /**
259: * @param int|float $Tick
260: * @param string $Label
261: *
262: * @return $this
263: */
264: public function TickFormatLabel( $Tick, $Label ) {
265: $this->Configuration['ticks'][$Tick] = $Label;
266: return $this;
267: }
268:
269:
270: /**
271: * @return string
272: */
273: public function _getConfiguration() {
274:
275: // Fix: ticks object => array
276: if( isset( $this->Configuration['ticks'] ) && is_array( $this->Configuration['ticks'] ) ) {
277: $this->Configuration['ticks'] = json_encode(
278: array_map(
279: function($key, $value) { return array($key, $value); },
280: array_keys( $this->Configuration['ticks'] ),
281: array_values( $this->Configuration['ticks'] )
282: )
283: );
284: }
285:
286: $Data = json_encode( $this->Configuration, JSON_FORCE_OBJECT );
287:
288: // Fix: ticks array(string) => array(array)
289: $Data = str_replace( '"[', "[", $Data );
290: $Data = str_replace( ']"', "]", $Data );
291: $Data = str_replace( '\"', '"', $Data );
292:
293: // Fix: string is function
294: $Data = preg_replace( '!"(function[^{]+{.*?})"(,|})!is', '${1}${2}', $Data );
295:
296: return $Data;
297: }
298: }
299: