1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Abstract chart data series base class
6 *
7 * @author Jörg Lützenkirchen <luetzenkirchen@leifos.com>
8 * @version $Id$
9 * @ingroup ServicesChart
10 */
11abstract class ilChartData
12{
13    protected $type; // [string]
14    protected $label; // [string]
15    protected $data; // [array]
16    protected $fill; // [float]
17    protected $fill_color; // [color/gradient]
18    protected $hidden; // [bool]
19
20    /**
21     * Get series type
22     *
23     * @return string
24     */
25    abstract protected function getTypeString();
26
27    /**
28     * Set hidden
29     *
30     * @param bool $a_value
31     */
32    public function setHidden($a_value)
33    {
34        $this->hidden = (bool) $a_value;
35    }
36
37    /**
38     * Is hidden?
39     *
40     * @return bool
41     */
42    public function isHidden()
43    {
44        return $this->hidden;
45    }
46
47    /**
48     * Set label
49     *
50     * @param string $a_value
51     */
52    public function setLabel($a_value)
53    {
54        $this->label = (string) $a_value;
55    }
56
57    /**
58     * Get label
59     *
60     * @return string
61     */
62    public function getLabel()
63    {
64        return $this->label;
65    }
66
67    /**
68     * Set data
69     *
70     * @param float $a_x
71     * @param float $a_y
72     */
73    public function addPoint($a_x, $a_y = null)
74    {
75        if ($a_y !== null) {
76            $this->data[] = array($a_x, $a_y);
77        } else {
78            $this->data[] = $a_x;
79        }
80    }
81
82    /**
83     * Get data
84     *
85     * @return array
86     */
87    public function getData()
88    {
89        return $this->data;
90    }
91
92    /**
93     * Set fill
94     *
95     * @param float $a_value
96     * @param string $a_color
97     */
98    public function setFill($a_value, $a_color = null)
99    {
100        $this->fill = $a_value;
101        if (ilChart::isValidColor($a_color)) {
102            $this->fill_color = $a_color;
103        }
104    }
105
106    /**
107     * Get fill
108     *
109     * @return array (fill, color)
110     */
111    public function getFill()
112    {
113        return array("fill" => $this->fill, "color" => $this->fill_color);
114    }
115
116    /**
117     * Convert data options to flot config
118     *
119     * @param array $a_options
120     * @param ilChart $a_chart
121     */
122    protected function parseDataOptions(array &$a_options)
123    {
124    }
125
126    /**
127     * Convert data to flot config
128     *
129     * @param array $a_data
130     * @return object
131     */
132    public function parseData(array &$a_data)
133    {
134        $series = new stdClass();
135        $series->label = str_replace("\"", "\\\"", $this->getLabel());
136
137        $series->data = array();
138        foreach ($this->getData() as $point) {
139            $series->data[] = array($point[0], $point[1]);
140        }
141
142        $options = array("show" => ($this->isHidden() ? false : true));
143
144        $fill = $this->getFill();
145        if ($fill["fill"]) {
146            $options["fill"] = $fill["fill"];
147            if ($fill["color"]) {
148                $options["fillColor"] = ilChart::renderColor($fill["color"], $fill["fill"]);
149            }
150        }
151
152        $this->parseDataOptions($options);
153
154        $series->{$this->getTypeString()} = $options;
155
156        $a_data[] = $series;
157    }
158
159    /**
160     * Convert (global) properties to flot config
161     *
162     * @param object $a_options
163     * @param ilChart $a_chart
164     */
165    public function parseGlobalOptions(stdClass $a_options, ilChart $a_chart)
166    {
167    }
168}
169