1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Chart legend
6 *
7 * @author Jörg Lützenkirchen <luetzenkirchen@leifos.com>
8 * @version $Id$
9 * @ingroup ServicesChart
10 */
11class ilChartLegend
12{
13    protected $position; // [string]
14    protected $columns; // [int]
15    protected $margin_x; // [int]
16    protected $margin_y; // [int]
17    protected $background; // [color]
18    protected $opacity; // [float] 0-1
19    protected $border; // [color]
20    protected $container; // [string]
21
22    /**
23     * Constructor
24     */
25    public function __construct()
26    {
27        $this->setPosition("ne");
28        $this->setColumns(1);
29        $this->setMargin(5, 5);
30        $this->setBackground("#888");
31        $this->setOpacity(0.1);
32        $this->setLabelBorder("#bbb");
33    }
34
35    /**
36     * Set Position
37     *
38     * @param string $position
39     */
40    public function setPosition($a_position)
41    {
42        $all = array("ne", "nw", "se", "sw");
43        if (in_array((string) $a_position, $all)) {
44            $this->position = (string) $a_position;
45        }
46    }
47
48    /**
49     * Get Position
50     *
51     * @return string
52     */
53    public function getPosition()
54    {
55        return $this->position;
56    }
57
58    /**
59     * Set number of columns
60     *
61     * @param int $a_value
62     */
63    public function setColumns($a_value)
64    {
65        $this->columns = (int) $a_value;
66    }
67
68    /**
69     * Get number of columns
70     *
71     * @return int
72     */
73    public function getColumns()
74    {
75        return $this->columns;
76    }
77
78    /**
79     * Set margins
80     *
81     * @param int $a_x
82     * @param int $a_y
83     */
84    public function setMargin($a_x, $a_y)
85    {
86        $this->margin_x = (int) $a_x;
87        $this->margin_y = (int) $a_y;
88    }
89
90    /**
91     * Get margins
92     *
93     * @return array (x, y)
94     */
95    public function getMargin()
96    {
97        return array("x" => $this->margin_x, "y" => $this->margin_y);
98    }
99
100    /**
101     * Set background color
102     *
103     * @param string $a_color
104     */
105    public function setBackground($a_color)
106    {
107        if (ilChart::isValidColor($a_color)) {
108            $this->background = $a_color;
109        }
110    }
111
112    /**
113     * Get background color
114     *
115     * @return string
116     */
117    public function getBackground()
118    {
119        return $this->background;
120    }
121
122    /**
123     * Set Opacity
124     *
125     * @param float $a_value
126     */
127    public function setOpacity($a_value)
128    {
129        $a_value = (float) $a_value;
130        if ($a_value >= 0 && $a_value <= 1) {
131            $this->opacity = $a_value;
132        }
133    }
134
135    /**
136     * Get opacity
137     *
138     * @return float
139     */
140    public function getOpacity()
141    {
142        return $this->opacity;
143    }
144
145    /**
146     * Set label border
147     *
148     * @param string $a_color
149     */
150    public function setLabelBorder($a_color)
151    {
152        if (ilChart::isValidColor($a_color)) {
153            $this->border = $a_color;
154        }
155    }
156
157    /**
158     * Get label border
159     *
160     * @return string
161     */
162    public function getLabelBorder()
163    {
164        return $this->border;
165    }
166
167    /**
168     * Set container id
169     *
170     * @param string
171     */
172    public function setContainer($a_value)
173    {
174        $this->container = trim($a_value);
175    }
176
177    /**
178     * Get container id
179     *
180     * @return string
181     */
182    public function getContainer()
183    {
184        return $this->container;
185    }
186
187    /**
188     * Convert (global) properties to flot config
189     *
190     * @param object $a_options
191     */
192    public function parseOptions(stdClass $a_options)
193    {
194        $a_options->show = true;
195
196        $a_options->noColumns = $this->getColumns();
197        $a_options->position = $this->getPosition();
198
199        $margin = $this->getMargin();
200        $a_options->margin = array($margin["x"], $margin["y"]);
201
202        $a_options->backgroundColor = ilChart::renderColor($this->getBackground());
203        $a_options->backgroundOpacity = str_replace(",", ".", $this->getOpacity());
204        $a_options->labelBoxBorderColor = ilChart::renderColor($this->getLabelBorder());
205
206        $container = $this->getContainer();
207        if ($container) {
208            $a_options->container = '#' . $container;
209        }
210    }
211}
212