1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once "Services/Chart/classes/class.ilChart.php";
5
6/**
7 * Generator for grid-based charts
8 *
9 * @author Jörg Lützenkirchen <luetzenkirchen@leifos.com>
10 * @version $Id$
11 * @ingroup ServicesChart
12 */
13class ilChartGrid extends ilChart
14{
15    protected $ticks; // [array]
16    protected $integer_axis; // [array]
17
18    const DATA_LINES = 1;
19    const DATA_BARS = 2;
20    const DATA_POINTS = 3;
21
22    protected function __construct($a_id)
23    {
24        parent::__construct($a_id);
25
26        $this->setXAxisToInteger(false);
27        $this->setYAxisToInteger(false);
28    }
29
30    public function getDataInstance($a_type = null)
31    {
32        switch ($a_type) {
33            case self::DATA_BARS:
34                include_once "Services/Chart/classes/class.ilChartDataBars.php";
35                return new ilChartDataBars();
36
37            case self::DATA_POINTS:
38                include_once "Services/Chart/classes/class.ilChartDataPoints.php";
39                return new ilChartDataPoints();
40
41            default:
42            case self::DATA_LINES:
43                include_once "Services/Chart/classes/class.ilChartDataLines.php";
44                return new ilChartDataLines();
45        }
46    }
47
48    protected function isValidDataType(ilChartData $a_series)
49    {
50        if ($a_series instanceof ilChartDataLines
51            || $a_series instanceof ilChartDataBars
52            || $a_series instanceof ilChartDataPoints) {
53            return true;
54        }
55        return false;
56    }
57
58    /**
59     * Set ticks
60     *
61     * @param int|array $a_x
62     * @param int|array $a_y
63     * @param bool $a_labeled
64     */
65    public function setTicks($a_x, $a_y, $a_labeled = false)
66    {
67        $this->ticks = array("x" => $a_x, "y" => $a_y, "labeled" => (bool) $a_labeled);
68    }
69
70    /**
71     * Get ticks
72     *
73     * @return array (x, y)
74     */
75    public function getTicks()
76    {
77        return $this->ticks;
78    }
79
80    /**
81     * Restrict y-axis to integer values
82     *
83     * @param bool $a_status
84     */
85    public function setYAxisToInteger($a_status)
86    {
87        $this->integer_axis["y"] = (bool) $a_status;
88    }
89
90    /**
91     * Restrict x-axis to integer values
92     *
93     * @param bool $a_status
94     */
95    public function setXAxisToInteger($a_status)
96    {
97        $this->integer_axis["x"] = (bool) $a_status;
98    }
99
100    public function parseGlobalOptions(stdClass $a_options)
101    {
102        // axis/ticks
103        $tmp = array();
104        $ticks = $this->getTicks();
105        if ($ticks) {
106            $labeled = (bool) $ticks["labeled"];
107            unset($ticks["labeled"]);
108            foreach ($ticks as $axis => $def) {
109                if (is_numeric($def) || is_array($def)) {
110                    $a_options->{$axis . "axis"} = new stdClass();
111                }
112                if (is_numeric($def)) {
113                    $a_options->{$axis . "axis"}->ticks = $def;
114                } elseif (is_array($def)) {
115                    $a_options->{$axis . "axis"}->ticks = array();
116                    foreach ($def as $idx => $value) {
117                        if ($labeled) {
118                            $a_options->{$axis . "axis"}->ticks[] = array($idx, $value);
119                        } else {
120                            $a_options->{$axis . "axis"}->ticks[] = $value;
121                        }
122                    }
123                }
124            }
125        }
126
127        // optional: remove decimals
128        if ($this->integer_axis["x"] && !isset($a_options->xaxis)) {
129            $a_options->{"xaxis"} = new stdClass();
130            $a_options->{"xaxis"}->tickDecimals = 0;
131        }
132        if ($this->integer_axis["y"] && !isset($a_options->yaxis)) {
133            $a_options->{"yaxis"} = new stdClass();
134            $a_options->{"yaxis"}->tickDecimals = 0;
135        }
136    }
137}
138