Completed
Push — master ( 15cb6d...86e5be )
by Nikolas
03:31
created

ChartRenderer   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 17
c 4
b 0
f 1
lcom 1
cbo 6
dl 0
loc 65
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A handles() 0 3 2
A prepareData() 0 10 4
A prepareDataPoints() 0 7 3
B prepareDataSets() 0 15 5
A prepareScatterDate() 0 16 3
1
<?php
2
namespace rtens\domin\delivery\cli\renderers;
3
4
use rtens\domin\delivery\cli\renderers\tables\TableRenderer;
5
use rtens\domin\delivery\web\renderers\charting\Chart;
6
use rtens\domin\delivery\web\renderers\charting\charts\DataPointChart;
7
use rtens\domin\delivery\web\renderers\charting\charts\DataSetChart;
8
use rtens\domin\delivery\web\renderers\charting\charts\ScatterChart;
9
10
class ChartRenderer extends TableRenderer {
11
    /**
12
     * @param mixed $value
13
     * @return bool
14
     */
15
    public function handles($value) {
16
        return $value instanceof Chart && $this->canDrawTables();
17
    }
18
19
    /**
20
     * @param Chart $object
21
     * @return array
22
     */
23
    protected function prepareData($object) {
24
        if ($object instanceof DataPointChart) {
25
            return $this->prepareDataPoints($object);
26
        } else if ($object instanceof DataSetChart) {
27
            return $this->prepareDataSets($object);
28
        } else if ($object instanceof ScatterChart) {
29
            return $this->prepareScatterDate($object);
30
        }
31
        throw new \InvalidArgumentException("Cannot render [" . get_class($object) . "]");
32
    }
33
34
    private function prepareDataPoints(DataPointChart $chart) {
35
        $data = [];
36
        foreach ($chart->getDataPoints() as $i => $point) {
37
            $data[$point->getLabel() ?: 'val' . $i] = $point->getValue();
38
        }
39
        return [$data];
40
    }
41
42
    protected function prepareDataSets(DataSetChart $object) {
43
        $headers = $object->getLabels();
44
45
        $data = [];
46
        foreach ($object->getDataSets() as $i => $set) {
47
            $dataSet = [
48
                '' => $set->getLabel() ?: 'set' . $i
49
            ];
50
            foreach ($set->getValues() as $j => $value) {
51
                $dataSet[$headers[$j] ?: 'val' . $j] = $value;
52
            }
53
            $data[] = $dataSet;
54
        }
55
        return $data;
56
    }
57
58
    private function prepareScatterDate(ScatterChart $chart) {
59
        $data = [];
60
61
        foreach ($chart->getScatterData() as $set) {
62
            foreach ($set->getDataPoints() as $point) {
63
                $data[] = [
64
                    '' => $set->getLabel(),
65
                    'x' => $point->getX(),
66
                    'y' => $point->getY(),
67
                    'r' => $point->getR()
68
                ];
69
            }
70
        }
71
72
        return $data;
73
    }
74
}