1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
36:
37: Yii::import ('application.components.sortableWidget.SortableWidget');
38:
39: 40: 41:
42: abstract class ChartWidget extends SortableWidget {
43:
44: const SECPERDAY = 86400;
45: const SECPERWEEK = 604800;
46:
47: public $template = '<div class="submenu-title-bar widget-title-bar">{widgetLabel}{chartSubtypeSelector}{closeButton}{minimizeButton}{settingsMenu}</div>{widgetContents}';
48:
49: 50: 51:
52: public $chartType;
53:
54: public $viewFile = '_chartWidget';
55:
56: protected $containerClass = 'sortable-widget-container x2-layout-island sortable-chart-widget';
57:
58: protected $_translations;
59:
60: private static $_JSONPropertiesStructure;
61:
62: private $_chartSettings;
63:
64: public function getChartSettings () {
65: if (!isset ($this->_chartSettings)) {
66: $this->_chartSettings = array_merge (
67: array_filter (self::getJSONProperty (
68: $this->profile, 'chartSettings', $this->widgetType, $this->widgetUID),
69: function ($setting) {
70: return $setting !== null;
71: }),
72: array (
73: 'chartIsShown' => self::getJSONProperty (
74: $this->profile, 'minimized', $this->widgetType,
75: $this->widgetUID),
76: )
77: );
78: }
79: return $this->_chartSettings;
80: }
81:
82: public function getViewFileParams () {
83: if (!isset ($this->_viewFileParams)) {
84: $this->_viewFileParams = array_merge (
85: parent::getViewFileParams (),
86: array (
87: 'suppressDateRangeSelector' => false,
88: 'chartSubtype' => self::getJSONProperty (
89: $this->profile, 'chartSubtype', $this->widgetType, $this->widgetUID),
90: 'widgetUID' => $this->widgetUID
91: )
92: );
93: }
94: return $this->_viewFileParams;
95: }
96:
97: 98: 99:
100: public static function getJSONPropertiesStructure () {
101: if (!isset (self::$_JSONPropertiesStructure)) {
102: self::$_JSONPropertiesStructure = array_merge (
103: parent::getJSONPropertiesStructure (),
104: array (
105: 'chartSubtype' => 'line',
106: 'chartSettings' => array (),
107: )
108: );
109: }
110: return self::$_JSONPropertiesStructure;
111: }
112:
113: 114: 115:
116: public function getChartSetting ($settingName) {
117: $chartSettings = self::getJSONProperty (
118: $this->profile, 'chartSettings', $this->widgetType, $this->widgetUID);
119:
120: if (in_array ($settingName, array_keys ($chartSettings))) {
121: return $chartSettings[$settingName];
122: } else {
123: throw new CException (Yii::t('app', 'Invalid chart setting name.'));
124: }
125: }
126:
127: 128: 129:
130: public function getSetupScript () {
131: if (!isset ($this->_setupScript)) {
132: $widgetClass = get_called_class ();
133: $this->_setupScript = "
134: $(function () {
135: x2.".$widgetClass.$this->widgetUID." = new ChartWidget (".
136: CJSON::encode ($this->getJSSortableWidgetParams ()).
137: ");
138: });
139: ";
140: }
141: return $this->_setupScript;
142: }
143:
144: 145: 146:
147: public function getPackages () {
148: if (!isset ($this->_packages)) {
149: $this->_packages = array_merge (
150: parent::getPackages (),
151: array (
152: 'ChartWidgetJS' => array(
153: 'baseUrl' => Yii::app()->request->baseUrl,
154: 'js' => array(
155: 'js/jqplot/jquery.jqplot.js',
156: 'js/jqplot/plugins/jqplot.pieRenderer.js',
157: 'js/jqplot/plugins/jqplot.categoryAxisRenderer.js',
158: 'js/jqplot/plugins/jqplot.pointLabels.js',
159: 'js/jqplot/plugins/jqplot.dateAxisRenderer.js',
160: 'js/jqplot/plugins/jqplot.highlighter.js',
161: 'js/jqplot/plugins/jqplot.enhancedLegendRenderer.js',
162: 'js/lib/moment-with-locales.min.js',
163: 'js/sortableWidgets/ChartWidget.js',
164: 'js/X2Chart/X2Chart.js',
165: ),
166: 'depends' => array ('SortableWidgetJS')
167: ),
168: 'ChartWidgetCss' => array(
169: 'baseUrl' => Yii::app()->getTheme ()->getBaseUrl (),
170: 'css' => array(
171: 'css/x2chart.css',
172: )
173: ),
174: 'ChartWidgetCssExt' => array(
175: 'baseUrl' => Yii::app()->request->baseUrl,
176: 'css' => array(
177: 'js/jqplot/jquery.jqplot.css',
178: ),
179: ),
180: )
181: );
182: if (AuxLib::isIE8 ()) {
183: $this->_packages['ChartWidgetJS']['js'][] = 'js/jqplot/excanvas.js';
184: }
185: }
186: return $this->_packages;
187: }
188:
189: 190: 191:
192: public function renderChartSubtypeSelector () {
193: $subtype = self::getJSONProperty (
194: $this->profile, 'chartSubtype', $this->widgetType, $this->widgetUID);
195:
196: echo
197: "<select class='x2-minimal-select chart-subtype-selector'>
198: <option ".($subtype === 'line' ? 'selected="selected" ' : '')."value='line'>".
199: Yii::t('app', 'Line Chart').
200: "</option>
201: <option ".($subtype === 'pie' ? 'selected="selected" ' : '')."value='pie'>".
202: Yii::t('app', 'Pie Chart').
203: "</option>
204: </select>";
205: }
206:
207: 208: 209:
210: protected function getJSSortableWidgetParams () {
211: if (!isset ($this->_JSSortableWidgetParams)) {
212: $this->_JSSortableWidgetParams = array_merge (
213: parent::getJSSortableWidgetParams (), array (
214: 'chartType' => $this->chartType,
215: ));
216: }
217: return $this->_JSSortableWidgetParams;
218: }
219:
220: 221: 222:
223: protected function getTranslations () {
224: if (!isset ($this->_translations )) {
225: $longMonthNames = Yii::app()->getLocale ()->getMonthNames ();
226: $shortMonthNames = Yii::app()->getLocale ()->getMonthNames ('abbreviated');
227:
228: $translations = array (
229: 'Create' => Yii::t('app','Create'),
230: 'Cancel' => Yii::t('app','Cancel'),
231: 'Create Chart Setting' => Yii::t('app','Create Chart Setting'),
232: 'Check all' => Yii::t('app','Check all'),
233: 'Uncheck all' => Yii::t('app','Uncheck all'),
234: 'metric(s) selected' => Yii::t('app','metric(s) selected')
235: );
236:
237: $englishMonthNames =
238: array ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
239: 'September', 'October', 'November', 'December');
240: $englishMonthAbbrs =
241: array ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov',
242: 'Dec');
243:
244: foreach ($longMonthNames as $key=>$val) {
245: $translations[$englishMonthNames[$key - 1]] = $val;
246: }
247: foreach ($shortMonthNames as $key=>$val) {
248: $translations[$englishMonthAbbrs[$key - 1]] = $val;
249: }
250:
251: $this->_translations = array_merge (
252: parent::getTranslations (),
253: $translations
254: );
255: }
256: return $this->_translations;
257: }
258:
259: 260: 261:
262: protected function getCss () {
263: if (!isset ($this->_css)) {
264: $this->_css = array_merge (
265: parent::getCss (),
266: array (
267: 'sortableWidgetChartCss' => "
268: .sortable-widget-container .chart-subtype-selector {
269: margin: 1px 0px 4px 5px;
270: border: 1px solid #ddd;
271: }
272:
273: .sortable-widget-container div.chart-container {
274: -moz-border-radius: 0px !important;
275: -o-border-radius: 0px !important;
276: -webkit-border-radius: 0px !important;
277: border-radius: 0px !important;
278: }
279:
280: .sortable-chart-widget .chart-controls-container {
281: width: 604px;
282: padding: 3px;
283: }
284:
285: .sortable-chart-widget .chart-widget-button-container .relabel-widget-button {
286: margin-right: 5px;
287: }
288:
289: @media (max-width: 684px) {
290: .sortable-chart-widget .chart-controls-container {
291: width: 95%;
292: padding: 3px;
293: }
294: .sortable-chart-widget .popup-dropdown-menu.flipped:before {
295: right: 52px;
296: }
297: .sortable-chart-widget .popup-dropdown-menu {
298: left: 0 !important;
299: right: 0!important;
300: margin: auto;
301: }
302: }
303:
304: @media (max-width: 529px) {
305: .sortable-chart-widget .chart-container .bin-size-button-set {
306: margin-top: 6px;
307: }
308: }
309:
310: /* menu contents */
311: @media (max-width: 500px) {
312: .sortable-chart-widget .chart-container .chart-filters-container {
313: height: auto;
314: }
315: .sortable-chart-widget .ui-multiselect {
316: margin-top: 0 !important;
317: }
318: }
319: ")
320: );
321: }
322: return $this->_css;
323: }
324:
325:
326: 327: 328: 329: 330: 331:
332: protected function getStartEndTimestamp ($defaultStartTs, $defaultEndTs) {
333: $startDate;
334: $endDate;
335: if ($this->getChartSetting ('dateRange') !== null &&
336: $this->getChartSetting ('dateRange') !== 'Custom') {
337:
338: $dateRange = $this->getChartSetting ('dateRange');
339: switch ($dateRange) {
340: case 'Today':
341: $startDate = time ();
342: $endDate = time ();
343: break;
344: case 'Yesterday':
345: $startDate = strtotime ('Yesterday');
346: $endDate = strtotime ('Yesterday');
347: break;
348: case 'This Week':
349: $startDate = strtotime ('Sunday this week');
350: $endDate = time ();
351: break;
352: case 'Last Week':
353: $startDate = strtotime ('-2 Sunday');
354: $endDate = strtotime ('-1 Saturday');
355: break;
356: case 'This Month':
357: $startDate = mktime (0, 0, 0, date ('m'), 1, date('o'));
358: $endDate = time ();
359: break;
360: case 'Last Six Months':
361: $startDate = mktime (0, 0, 0, date ('m') - 6, 1, date('o'));
362: $endDate = time ();
363: break;
364: case 'This Year':
365: $startDate = mktime (0, 0, 0, 1, 1, date('o'));
366: $endDate = time ();
367: break;
368: case 'Last Year':
369: $startDate = mktime (0, 0, 0, 1, 1, date('o') - 1);
370: $endDate = mktime (0, 0, 0, 11, 31, date('o') - 1);
371: break;
372: case 'Last Month':
373: default:
374: $startDate = mktime (0, 0, 0, date ('m') - 1, 1, date('o'));
375: $endDate = mktime (0, 0, 0, date ('m'), 1, date('o')) - self::SECPERDAY;
376: break;
377: 378:
379: }
380: } else {
381: if ($this->getChartSetting ('startDate') !== null) {
382: $startDate = $this->getChartSetting ('startDate') / 1000;
383: } else {
384: $startDate = $defaultStartTs;
385: }
386: if ($this->getChartSetting ('endDate')) {
387: $endDate = $this->getChartSetting ('endDate') / 1000;
388: } else {
389: $endDate = $defaultEndTs;
390: }
391: }
392: $endDate += self::SECPERDAY - 1;
393: return array ($startDate, $endDate);
394: }
395:
396:
397: }
398: ?>
399: