summaryrefslogtreecommitdiffstats
path: root/tests/radar_chart.php
blob: 2265d4a1f9cff52b8e60100b07a4bdcddf616342 (plain)
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
<?php
/**
 * ezcGraphRadarChartTest 
 * 
 * @package Graph
 * @version //autogen//
 * @subpackage Tests
 * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
 * @license http://ez.no/licenses/new_bsd New BSD License
 */

require_once dirname( __FILE__ ) . '/test_case.php';

/**
 * Tests for ezcGraph class.
 * 
 * @package ImageAnalysis
 * @subpackage Tests
 */
class ezcGraphRadarChartTest extends ezcGraphTestCase
{
    protected $basePath;

    protected $tempDir;

	public static function suite()
	{
		return new PHPUnit_Framework_TestSuite( "ezcGraphRadarChartTest" );
	}

    protected function setUp()
    {
        static $i = 0;
        if ( version_compare( phpversion(), '5.1.3', '<' ) )
        {
            $this->markTestSkipped( "These tests required atleast PHP 5.1.3" );
        }
        $this->tempDir = $this->createTempDir( __CLASS__ . sprintf( '_%03d_', ++$i ) ) . '/';
        $this->basePath = dirname( __FILE__ ) . '/data/';
    }

    protected function tearDown()
    {
        if ( !$this->hasFailed() )
        {
            $this->removeTempDir();
        }
    }

    protected function getRandomData( $count, $min = 0, $max = 1000, $randomize = 23 )
    {
        // Make data reproducible
        mt_srand( $randomize );

        for ( $i = 0; $i < $count; ++$i )
        {
            $data[] = mt_rand( $min, $max );
        }

        return $data;
    }

    public function testDrawMultipleAxis()
    {
        $chart = new ezcGraphRadarChart();
        $chart->data['sampleData'] = new ezcGraphArrayDataSet( array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1 ) );

        $mockedRenderer = $this->getMock( 'ezcGraphRenderer2d', array(
            'drawAxis',
        ) );

        $mockedRenderer
           ->expects( $this->at( 0 ) )
            ->method( 'drawAxis' )
            ->with(
                $this->equalTo( new ezcGraphBoundings( 100., 0., 500., 200. ), 1. ),
                $this->equalTo( new ezcGraphCoordinate( 200., 100. ), 1. ),
                $this->equalTo( new ezcGraphCoordinate( 200., 0. ), 1. )
            );
        $mockedRenderer
           ->expects( $this->at( 1 ) )
            ->method( 'drawAxis' )
            ->with(
                $this->equalTo( new ezcGraphBoundings( 100., 0., 500., 200. ), 1. ),
                $this->equalTo( new ezcGraphCoordinate( 200., 100. ), 1. ),
                $this->equalTo( new ezcGraphCoordinate( 400., 100. ), 1. )
            );
        $mockedRenderer
           ->expects( $this->at( 3 ) )
            ->method( 'drawAxis' )
            ->with(
                $this->equalTo( new ezcGraphBoundings( 100., 0., 500., 200. ), 1. ),
                $this->equalTo( new ezcGraphCoordinate( 200., 100. ), 1. ),
                $this->equalTo( new ezcGraphCoordinate( 0., 100. ), 1. )
            );

        $chart->renderer = $mockedRenderer;

        $chart->render( 500, 200 );
    }

    public function testDrawDataLines()
    {
        $chart = new ezcGraphRadarChart();
        $chart->data['sampleData'] = new ezcGraphArrayDataSet( array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1 ) );
        $chart->data['sampleData']->color = '#CC0000';

        $mockedRenderer = $this->getMock( 'ezcGraphRenderer2d', array(
            'drawRadarDataLine',
        ) );

        $mockedRenderer
           ->expects( $this->at( 0 ) )
            ->method( 'drawRadarDataLine' )
            ->with(
                $this->equalTo( new ezcGraphBoundings( 100., 0., 500., 200. ), 1. ),
                $this->equalTo( new ezcGraphContext( 'sampleData', 'sample 1' ) ),
                $this->equalTo( ezcGraphColor::fromHex( '#CC0000' ) ),
                $this->equalTo( new ezcGraphCoordinate( 200., 100. ), 1. ),
                $this->equalTo( new ezcGraphCoordinate( .0, .585 ), .05 ),
                $this->equalTo( new ezcGraphCoordinate( .0, .585 ), .05 )
            );
        $mockedRenderer
           ->expects( $this->at( 1 ) )
            ->method( 'drawRadarDataLine' )
            ->with(
                $this->equalTo( new ezcGraphBoundings( 100., 0., 500., 200. ), 1. ),
                $this->equalTo( new ezcGraphContext( 'sampleData', 'sample 2' ) ),
                $this->equalTo( ezcGraphColor::fromHex( '#CC0000' ) ),
                $this->equalTo( new ezcGraphCoordinate( 200., 100. ), 1. ),
                $this->equalTo( new ezcGraphCoordinate( .0, .585 ), .05 ),
                $this->equalTo( new ezcGraphCoordinate( .25, .0525 ), .05 )
            );
        $mockedRenderer
           ->expects( $this->at( 4 ) )
            ->method( 'drawRadarDataLine' )
            ->with(
                $this->equalTo( new ezcGraphBoundings( 100., 0., 500., 200. ), 1. ),
                $this->equalTo( new ezcGraphContext( 'sampleData', 'sample 5' ) ),
                $this->equalTo( ezcGraphColor::fromHex( '#CC0000' ) ),
                $this->equalTo( new ezcGraphCoordinate( 200., 100. ), 1. ),
                $this->equalTo( new ezcGraphCoordinate( .75, .3 ), .05 ),
                $this->equalTo( new ezcGraphCoordinate( 1., .0025 ), .05 )
            );

        $chart->renderer = $mockedRenderer;

        $chart->render( 500, 200 );
    }

    public function testDrawDataLinesWithSymbols()
    {
        $chart = new ezcGraphRadarChart();
        $chart->data['sampleData'] = new ezcGraphArrayDataSet( array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1 ) );
        $chart->data['sampleData']->color = '#CC0000';
        $chart->data['sampleData']->symbol = ezcGraph::DIAMOND;

        $mockedRenderer = $this->getMock( 'ezcGraphRenderer2d', array(
            'drawRadarDataLine',
        ) );

        $mockedRenderer
           ->expects( $this->at( 0 ) )
            ->method( 'drawRadarDataLine' )
            ->with(
                $this->equalTo( new ezcGraphBoundings( 100., 0., 500., 200. ), 1. ),
                $this->equalTo( new ezcGraphContext( 'sampleData', 'sample 1' ) ),
                $this->equalTo( ezcGraphColor::fromHex( '#CC0000' ) ),
                $this->equalTo( new ezcGraphCoordinate( 200., 100. ), 1. ),
                $this->equalTo( new ezcGraphCoordinate( .0, .585 ), .05 ),
                $this->equalTo( new ezcGraphCoordinate( .0, .585 ), .05 ),
                $this->equalTo( 0 ),
                $this->equalTo( 1 ),
                $this->equalTo( ezcGraph::DIAMOND ),
                $this->equalTo( ezcGraphColor::fromHex( '#CC0000' ) )
            );
        $mockedRenderer
           ->expects( $this->at( 1 ) )
            ->method( 'drawRadarDataLine' )
            ->with(
                $this->equalTo( new ezcGraphBoundings( 100., 0., 500., 200. ), 1. ),
                $this->equalTo( new ezcGraphContext( 'sampleData', 'sample 2' ) ),
                $this->equalTo( ezcGraphColor::fromHex( '#CC0000' ) ),
                $this->equalTo( new ezcGraphCoordinate( 200., 100. ), 1. ),
                $this->equalTo( new ezcGraphCoordinate( .0, .585 ), .05 ),
                $this->equalTo( new ezcGraphCoordinate( .25, .0525 ), .05 ),
                $this->equalTo( 0 ),
                $this->equalTo( 1 ),
                $this->equalTo( ezcGraph::DIAMOND ),
                $this->equalTo( ezcGraphColor::fromHex( '#CC0000' ) )
            );
        $mockedRenderer
           ->expects( $this->at( 4 ) )
            ->method( 'drawRadarDataLine' )
            ->with(
                $this->equalTo( new ezcGraphBoundings( 100., 0., 500., 200. ), 1. ),
                $this->equalTo( new ezcGraphContext( 'sampleData', 'sample 5' ) ),
                $this->equalTo( ezcGraphColor::fromHex( '#CC0000' ) ),
                $this->equalTo( new ezcGraphCoordinate( 200., 100. ), 1. ),
                $this->equalTo( new ezcGraphCoordinate( .75, .3 ), .05 ),
                $this->equalTo( new ezcGraphCoordinate( 1., .0025 ), .05 ),
                $this->equalTo( 0 ),
                $this->equalTo( 1 ),
                $this->equalTo( ezcGraph::DIAMOND ),
                $this->equalTo( ezcGraphColor::fromHex( '#CC0000' ) )
            );

        $chart->renderer = $mockedRenderer;

        $chart->render( 500, 200 );
    }

    public function testDrawGridLines()
    {
        $filename = $this->tempDir . __FUNCTION__ . '.svg';

        $chart = new ezcGraphRadarChart();
        $chart->palette = new ezcGraphPaletteBlack();
        $chart->data['sample'] = new ezcGraphArrayDataSet( $this->getRandomData( 6 ) );

        $mockedRenderer = $this->getMock( 'ezcGraphRenderer2d', array(
            'drawGridLine',
        ) );

        $mockedRenderer
           ->expects( $this->at( 0 ) )
            ->method( 'drawGridLine' )
            ->with(
                $this->equalTo( new ezcGraphCoordinate( 338., 93.8 ), .1 ),
                $this->equalTo( new ezcGraphCoordinate( 300., 80. ), .1 ),
                $this->equalTo( ezcGraphColor::fromHex( '#888A85' ) )
            );
        $mockedRenderer
           ->expects( $this->at( 1 ) )
            ->method( 'drawGridLine' )
            ->with(
                $this->equalTo( new ezcGraphCoordinate( 343.75, 92.9 ), .1 ),
                $this->equalTo( new ezcGraphCoordinate( 300., 77. ), .1 ),
                $this->equalTo( ezcGraphColor::fromHex( '#888A8588' ) )
            );

        // Next axis
        $mockedRenderer
           ->expects( $this->at( 21 ) )
            ->method( 'drawGridLine' )
            ->with(
                $this->equalTo( new ezcGraphCoordinate( 323.5, 116.2 ), .1 ),
                $this->equalTo( new ezcGraphCoordinate( 338., 93.8 ), .1 ),
                $this->equalTo( ezcGraphColor::fromHex( '#888A85' ) )
            );

        $chart->renderer = $mockedRenderer;

        $chart->render( 500, 200 );
    }

    public function testRadarChartOptionsPropertyFillRadars()
    {
        $options = new ezcGraphRadarChartOptions();

        $this->assertSame(
            false,
            $options->fillLines,
            'Wrong default value for property fillLines in class ezcGraphRadarChartOptions'
        );

        $options->fillLines = 230;
        $this->assertSame(
            230,
            $options->fillLines,
            'Setting property value did not work for property fillLines in class ezcGraphRadarChartOptions'
        );

        $options->fillLines = false;
        $this->assertSame(
            false,
            $options->fillLines,
            'Setting property value did not work for property fillLines in class ezcGraphRadarChartOptions'
        );

        try
        {
            $options->fillLines = true;
        }
        catch ( ezcBaseValueException $e )
        {
            return true;
        }

        $this->fail( 'Expected ezcBaseValueException.' );
    }

    public function testRadarChartOptionsPropertySymbolSize()
    {
        $options = new ezcGraphRadarChartOptions();

        $this->assertSame(
            8,
            $options->symbolSize,
            'Wrong default value for property symbolSize in class ezcGraphRadarChartOptions'
        );

        $options->symbolSize = 10;
        $this->assertSame(
            10,
            $options->symbolSize,
            'Setting property value did not work for property symbolSize in class ezcGraphRadarChartOptions'
        );

        try
        {
            $options->symbolSize = false;
        }
        catch ( ezcBaseValueException $e )
        {
            return true;
        }

        $this->fail( 'Expected ezcBaseValueException.' );
    }

    public function testRadarChartOptionsPropertyHighlightFont()
    {
        $options = new ezcGraphRadarChartOptions();

        $options->highlightFont = $file = $this->basePath . 'font.ttf';
        $this->assertSame(
            $file,
            $options->highlightFont->path,
            'Setting property value did not work for property highlightFont in class ezcGraphRadarChartOptions'
        );

        $this->assertSame(
            true,
            $options->highlightFontCloned,
            'Font should be cloned now.'
        );

        $fontOptions = new ezcGraphFontOptions();
        $fontOptions->path = $this->basePath . 'font2.ttf';

        $options->highlightFont = $fontOptions;
        $this->assertSame(
            $fontOptions,
            $options->highlightFont,
            'Setting property value did not work for property highlightFont in class ezcGraphRadarChartOptions'
        );

        try
        {
            $options->highlightFont = false;
        }
        catch ( ezcBaseValueException $e )
        {
            return true;
        }

        $this->fail( 'Expected ezcBaseValueException.' );
    }

    public function testRadarChartOptionsPropertyHighlightSize()
    {
        $options = new ezcGraphRadarChartOptions();

        $this->assertSame(
            14,
            $options->highlightSize,
            'Wrong default value for property highlightSize in class ezcGraphRadarChartOptions'
        );

        $options->highlightSize = 20;
        $this->assertSame(
            20,
            $options->highlightSize,
            'Setting property value did not work for property highlightSize in class ezcGraphRadarChartOptions'
        );

        try
        {
            $options->highlightSize = false;
        }
        catch ( ezcBaseValueException $e )
        {
            return true;
        }

        $this->fail( 'Expected ezcBaseValueException.' );
    }

    public function testRadarChartOptionsPropertyHighlightRadars()
    {
        $options = new ezcGraphRadarChartOptions();

        $this->assertSame(
            false,
            $options->highlightRadars,
            'Wrong default value for property highlightRadars in class ezcGraphRadarChartOptions'
        );

        $options->highlightRadars = true;
        $this->assertSame(
            true,
            $options->highlightRadars,
            'Setting property value did not work for property highlightRadars in class ezcGraphRadarChartOptions'
        );

        try
        {
            $options->highlightRadars = 42;
        }
        catch ( ezcBaseValueException $e )
        {
            return true;
        }

        $this->fail( 'Expected ezcBaseValueException.' );
    }

    public function testRadarChartElementAxis()
    {
        $chart = new ezcGraphRadarChart();

        $this->assertSame(
            true,
            $chart->axis instanceof ezcGraphChartElementNumericAxis,
            'Wrong default value for chart element axis in class ezcGraphRadarChart'
        );

        $chart->axis = new ezcGraphChartElementLogarithmicalAxis();
        $this->assertSame(
            true,
            $chart->axis instanceof ezcGraphChartElementLogarithmicalAxis,
            'Setting element value for chart element axis in class ezcGraphRadarChart'
        );

        try
        {
            $chart->axis = 42;
        }
        catch ( ezcBaseValueException $e )
        {
            return true;
        }

        $this->fail( 'Expected ezcBaseValueException.' );
    }

    public function testRadarChartElementRotationAxis()
    {
        $chart = new ezcGraphRadarChart();

        $this->assertSame(
            true,
            $chart->rotationAxis instanceof ezcGraphChartElementLabeledAxis,
            'Wrong default value for chart element axis in class ezcGraphRadarChart'
        );

        $chart->rotationAxis = new ezcGraphChartElementLogarithmicalAxis();
        $this->assertSame(
            true,
            $chart->rotationAxis instanceof ezcGraphChartElementLogarithmicalAxis,
            'Setting element value for chart element axis in class ezcGraphRadarChart'
        );

        try
        {
            $chart->rotationAxis = 42;
        }
        catch ( ezcBaseValueException $e )
        {
            return true;
        }

        $this->fail( 'Expected ezcBaseValueException.' );
    }

    public function testRadarSimple()
    {
        $filename = $this->tempDir . __FUNCTION__ . '.svg';

        $chart = new ezcGraphRadarChart();
        $chart->palette = new ezcGraphPaletteTango();

        $chart->data['sample'] = new ezcGraphArrayDataSet( $this->getRandomData( 6 ) );

        $chart->render( 500, 200, $filename );

        $this->compare(
            $filename,
            $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.svg'
        );
    }

    public function testRadarSimpleNoDataFailure()
    {
        $filename = $this->tempDir . __FUNCTION__ . '.svg';

        $chart = new ezcGraphRadarChart();
        $chart->palette = new ezcGraphPaletteTango();

        try
        {
            $chart->render( 500, 200, $filename );
        }
        catch ( ezcGraphNoDataException $e )
        {
            return true;
        }

        $this->fail( 'Expected ezcGraphNoDataException.' );
    }

    public function testRadarMinorAxis()
    {
        $filename = $this->tempDir . __FUNCTION__ . '.svg';

        $chart = new ezcGraphRadarChart();
        $chart->palette = new ezcGraphPaletteBlack();

        $chart->options->fillLines = 210;

        $chart->data['sample'] = new ezcGraphArrayDataSet( $this->getRandomData( 31 ) );

        $chart->render( 500, 200, $filename );

        $this->compare(
            $filename,
            $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.svg'
        );
    }

    public function testRenderLineChartToOutput()
    {
        $filename = $this->tempDir . __FUNCTION__ . '.svg';

        $chart = new ezcGraphRadarChart();
        $chart->palette = new ezcGraphPaletteTango();

        $chart->data['sample'] = new ezcGraphArrayDataSet( $this->getRandomData( 6 ) );

        ob_start();
        // Suppress header already sent warning
        @$chart->renderToOutput( 500, 200 );
        file_put_contents( $filename, ob_get_clean() );

        $this->compare(
            $filename,
            $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.svg'
        );
    }

    public function testRadarNumericRotationAxis()
    {
        $filename = $this->tempDir . __FUNCTION__ . '.svg';

        $chart = new ezcGraphRadarChart();
        $chart->palette = new ezcGraphPaletteBlack();

        $chart->options->fillLines = 210;

        $chart->data['sample'] = new ezcGraphArrayDataSet( $this->getRandomData( 31 ) );
        $chart->rotationAxis = new ezcGraphChartElementNumericAxis();

        $chart->render( 500, 200, $filename );

        $this->compare(
            $filename,
            $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.svg'
        );
    }

    public function testRadarRendererFailure()
    {
        $filename = $this->tempDir . __FUNCTION__ . '.svg';

        $chart = new ezcGraphRadarChart();
        $chart->palette = new ezcGraphPaletteTango();

        $chart->data['sample'] = new ezcGraphArrayDataSet( $this->getRandomData( 6 ) );

        try
        {
            $chart->renderer = new ezcGraphRenderer3d();
        }
        catch ( ezcBaseValueException $e )
        {
            return true;
        }

        $this->fail( 'Expected ezcBasePropertyValueException.' );
    }

    public function testRadarMultiple()
    {
        $filename = $this->tempDir . __FUNCTION__ . '.svg';

        $chart = new ezcGraphRadarChart();
        $chart->palette = new ezcGraphPaletteBlack();

        $chart->options->fillLines = 210;

        $chart->data['sample 1'] = new ezcGraphArrayDataSet( $this->getRandomData( 8 ) );
        $chart->data['sample 2'] = new ezcGraphArrayDataSet( $this->getRandomData( 8, 250, 1000, 12 ) );
        $chart->data['sample 3'] = new ezcGraphArrayDataSet( $this->getRandomData( 8, 0, 500, 42 ) );

        $chart->render( 500, 200, $filename );

        $this->compare(
            $filename,
            $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.svg'
        );
    }

    public function testRadarLogarithmicalAxis()
    {
        $filename = $this->tempDir . __FUNCTION__ . '.svg';

        $chart = new ezcGraphRadarChart();
        $chart->palette = new ezcGraphPaletteBlack();

        $chart->axis = new ezcGraphChartElementLogarithmicalAxis();

        $chart->options->fillLines = 210;

        $chart->data['sample 1'] = new ezcGraphArrayDataSet( $this->getRandomData( 8, 1, 1000000 ) );
        $chart->data['sample 2'] = new ezcGraphArrayDataSet( $this->getRandomData( 8, 1, 1000000, 42 ) );

        $chart->render( 500, 200, $filename );

        $this->compare(
            $filename,
            $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.svg'
        );
    }
}
?>
OpenPOWER on IntegriCloud