1
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
<?php
/**
* File containing the ezcGraphBarChart class
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package Graph
* @version //autogentag//
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*/
/**
* Class for bar charts. Can make use of an unlimited amount of datasets and
* will display them as bars by default.
* X axis:
* - Labeled axis
* - Boxed axis label renderer
* Y axis:
* - Numeric axis
* - Exact axis label renderer
*
* <code>
* // Create a new horizontal bar chart
* $chart = new ezcGraphHorizontalBarChart();
*
* // Add data to line chart
* $chart->data['sample dataset'] = new ezcGraphArrayDataSet(
* array(
* '100' => 1.2,
* '200' => 43.2,
* '300' => -34.14,
* '350' => 65,
* '400' => 123,
* )
* );
*
* // Render chart with the special designated renderer and default SVG driver
* $chart->renderer = new ezcGraphHorizontalRenderer();
* $chart->render( 500, 200, 'bar_chart.svg' );
* </code>
*
* Each chart consists of several chart elements which represents logical
* parts of the chart and can be formatted independently. The bar chart
* consists of:
* - title ( {@link ezcGraphChartElementText} )
* - legend ( {@link ezcGraphChartElementLegend} )
* - background ( {@link ezcGraphChartElementBackground} )
* - xAxis ( {@link ezcGraphChartElementLabeledAxis} )
* - yAxis ( {@link ezcGraphChartElementNumericAxis} )
*
* The type of the axis may be changed and all elements can be configured by
* accessing them as properties of the chart:
*
* <code>
* $chart->legend->position = ezcGraph::RIGHT;
* </code>
*
* The chart itself also offers several options to configure the appearance. As
* bar charts extend line charts the the extended configure options are
* available in {@link ezcGraphLineChartOptions} extending the
* {@link ezcGraphChartOptions}.
*
* @property ezcGraphLineChartOptions $options
* Chart options class
*
* @version //autogentag//
* @package Graph
* @mainclass
*/
class ezcGraphHorizontalBarChart extends ezcGraphBarChart
{
/**
* Constructor
*
* @param array $options Default option array
* @return void
* @ignore
*/
public function __construct( array $options = array() )
{
parent::__construct();
$this->addElement( 'xAxis', new ezcGraphChartElementNumericAxis() );
$this->elements['xAxis']->axisLabelRenderer = new ezcGraphAxisCenteredLabelRenderer();
$this->elements['xAxis']->position = ezcGraph::LEFT;
$this->addElement( 'yAxis', new ezcGraphChartElementLabeledAxis() );
$this->elements['yAxis']->axisLabelRenderer = new ezcGraphAxisBoxedLabelRenderer();
$this->elements['yAxis']->position = ezcGraph::BOTTOM;
$this->renderer = new ezcGraphHorizontalRenderer();
}
/**
* Render the assigned data
*
* Will renderer all charts data in the remaining boundings after drawing
* all other chart elements. The data will be rendered depending on the
* settings in the dataset.
*
* @param ezcGraphRenderer $renderer Renderer
* @param ezcGraphBoundings $boundings Remaining boundings
* @return void
*/
protected function renderData( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings, ezcGraphBoundings $innerBoundings )
{
// Use inner boundings for drawning chart data
$boundings = $innerBoundings;
$xAxisNullPosition = $this->elements['xAxis']->getCoordinate( false );
// Initialize counters
$nr = array();
$count = array();
foreach ( $this->data as $data )
{
if ( !isset( $nr[$data->displayType->default] ) )
{
$nr[$data->displayType->default] = 0;
$count[$data->displayType->default] = 0;
}
$nr[$data->displayType->default]++;
$count[$data->displayType->default]++;
}
$checkedRegularSteps = false;
// Display data
foreach ( $this->data as $datasetName => $data )
{
--$nr[$data->displayType->default];
// Check which axis should be used
$xAxis = ( $data->xAxis->default ? $data->xAxis->default: $this->elements['xAxis'] );
$yAxis = ( $data->yAxis->default ? $data->yAxis->default: $this->elements['yAxis'] );
// Determine fill color for dataset
if ( $this->options->fillLines !== false )
{
$fillColor = clone $data->color->default;
$fillColor->alpha = (int) round( ( 255 - $fillColor->alpha ) * ( $this->options->fillLines / 255 ) );
}
else
{
$fillColor = null;
}
// Ensure regular steps on axis when used with bar charts and
// precalculate some values use to render bar charts
//
// Called only once and only when bars should be rendered
if ( ( $checkedRegularSteps === false ) &&
( $data->displayType->default === ezcGraph::BAR ) )
{
$height = $this->calculateStepWidth( $yAxis, $xAxis, $boundings->height )->y;
}
// Draw lines for dataset
$lastPoint = false;
foreach ( $data as $key => $value )
{
// Calculate point in chart
$point = $xAxis->axisLabelRenderer->modifyChartDataPosition(
$yAxis->axisLabelRenderer->modifyChartDataPosition(
new ezcGraphCoordinate(
$xAxis->getCoordinate( $value ),
$yAxis->getCoordinate( $key )
)
)
);
// Render depending on display type of dataset
switch ( true )
{
case ( $data->displayType->default === ezcGraph::BAR ) &&
$this->options->stackBars :
// Check if a bar has already been stacked
if ( !isset( $stackedValue[(int) ( $point->y * 10000 )][(int) $value > 0] ) )
{
$start = new ezcGraphCoordinate(
$xAxisNullPosition,
$point->y
);
$stackedValue[(int) ( $point->y * 10000 )][(int) $value > 0] = $value;
}
else
{
$start = $yAxis->axisLabelRenderer->modifyChartDataPosition(
$xAxis->axisLabelRenderer->modifyChartDataPosition(
new ezcGraphCoordinate(
$xAxis->getCoordinate( $stackedValue[(int) ( $point->y * 10000 )][(int) $value > 0] ),
$yAxis->getCoordinate( $key )
)
)
);
$point = $yAxis->axisLabelRenderer->modifyChartDataPosition(
$xAxis->axisLabelRenderer->modifyChartDataPosition(
new ezcGraphCoordinate(
$xAxis->getCoordinate( $stackedValue[(int) ( $point->y * 10000 )][(int) $value > 0] += $value ),
$yAxis->getCoordinate( $key )
)
)
);
}
// Force one symbol for each stacked bar
if ( !isset( $stackedSymbol[(int) ( $point->y * 10000 )] ) )
{
$stackedSymbol[(int) ( $point->y * 10000 )] = $data->symbol[$key];
}
// Store stacked value for next iteration
$side = ( $point->x == 0 ? 1 : $point->x / abs( $point->x ) );
$stacked[(int) ( $point->y * 10000 )][$side] = $point;
$renderer->drawHorizontalStackedBar(
$boundings,
new ezcGraphContext( $datasetName, $key, $data->url[$key] ),
$data->color->default,
$start,
$point,
$height,
$stackedSymbol[(int) ( $point->y * 10000 )],
$xAxisNullPosition
);
// Render highlight string if requested
if ( $data->highlight[$key] )
{
$renderer->drawDataHighlightText(
$boundings,
new ezcGraphContext( $datasetName, $key, $data->url[$key] ),
$point,
$xAxisNullPosition,
$nr[$data->displayType->default],
$count[$data->displayType->default],
$this->options->highlightFont,
( $data->highlightValue[$key] ? $data->highlightValue[$key] : $value ),
$this->options->highlightSize + $this->options->highlightFont->padding * 2,
( $this->options->highlightLines ? $data->color[$key] : null ),
( $this->options->highlightXOffset ? $this->options->highlightXOffset : 0 ),
( $this->options->highlightYOffset ? $this->options->highlightYOffset : 0 ),
$height,
$data->displayType->default
);
}
break;
case $data->displayType->default === ezcGraph::BAR:
$renderer->drawHorizontalBar(
$boundings,
new ezcGraphContext( $datasetName, $key, $data->url[$key] ),
$data->color[$key],
$point,
$height,
$nr[$data->displayType->default],
$count[$data->displayType->default],
$data->symbol[$key],
$xAxisNullPosition
);
// Render highlight string if requested
if ( $data->highlight[$key] )
{
$renderer->drawDataHighlightText(
$boundings,
new ezcGraphContext( $datasetName, $key, $data->url[$key] ),
$point,
$xAxisNullPosition,
$nr[$data->displayType->default],
$count[$data->displayType->default],
$this->options->highlightFont,
( $data->highlightValue[$key] ? $data->highlightValue[$key] : $value ),
$this->options->highlightSize + $this->options->highlightFont->padding * 2,
( $this->options->highlightLines ? $data->color[$key] : null ),
( $this->options->highlightXOffset ? $this->options->highlightXOffset : 0 ),
( $this->options->highlightYOffset ? $this->options->highlightYOffset : 0 ),
$height,
$data->displayType->default
);
}
break;
default:
throw new ezcGraphInvalidDisplayTypeException( $data->displayType->default );
break;
}
// Store last point, used to connect lines in line chart.
$lastPoint = $point;
}
}
}
/**
* Aggregate and calculate value boundings on axis.
*
* This function is nearly the same as in ezcGraphLineChart, but reverses
* the usage of keys and values for the axis.
*
* @return void
*/
protected function setAxisValues()
{
// Virtual data set build for agrregated values sums for bar charts
$virtualBarSumDataSet = array( array(), array() );
// Calculate axis scaling and labeling
foreach ( $this->data as $dataset )
{
$nr = 0;
$labels = array();
$values = array();
foreach ( $dataset as $label => $value )
{
$labels[] = $label;
$values[] = $value;
// Build sum of all bars
if ( $this->options->stackBars &&
( $dataset->displayType->default === ezcGraph::BAR ) )
{
if ( !isset( $virtualBarSumDataSet[(int) $value >= 0][$nr] ) )
{
$virtualBarSumDataSet[(int) $value >= 0][$nr++] = $value;
}
else
{
$virtualBarSumDataSet[(int) $value >= 0][$nr++] += $value;
}
}
}
// Check if data has been associated with another custom axis, use
// default axis otherwise.
if ( $dataset->xAxis->default )
{
$dataset->xAxis->default->addData( $values );
}
else
{
$this->elements['xAxis']->addData( $values );
}
if ( $dataset->yAxis->default )
{
$dataset->yAxis->default->addData( array_reverse( $labels ) );
}
else
{
$this->elements['yAxis']->addData( array_reverse( $labels ) );
}
}
// Also use stacked bar values as base for x axis value span
// calculation
if ( $this->options->stackBars )
{
$this->elements['xAxis']->addData( $virtualBarSumDataSet[0] );
$this->elements['xAxis']->addData( $virtualBarSumDataSet[1] );
}
// There should always be something assigned to the main x and y axis.
if ( !$this->elements['xAxis']->initialized ||
!$this->elements['yAxis']->initialized )
{
throw new ezcGraphNoDataException();
}
// Calculate boundings from assigned data
$this->elements['xAxis']->calculateAxisBoundings();
$this->elements['yAxis']->calculateAxisBoundings();
}
}
?>
|