ChartRenderer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 45
c 0
b 0
f 0
wmc 4
lcom 0
cbo 3
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handles() 0 3 1
A render() 0 20 2
A headElements() 0 5 1
1
<?php
2
namespace rtens\domin\delivery\web\renderers\charting;
3
4
use rtens\domin\delivery\web\Element;
5
use rtens\domin\delivery\web\HeadElements;
6
use rtens\domin\delivery\web\WebRenderer;
7
8
class ChartRenderer implements WebRenderer {
9
10
    /**
11
     * @param mixed $value
12
     * @return bool
13
     */
14
    public function handles($value) {
15
        return $value instanceof Chart;
16
    }
17
18
    /**
19
     * @param Chart $value
20
     * @return string
21
     */
22
    public function render($value) {
23
        $data = $value->data();
24
25
        if (!$data) {
26
            return '[no data]';
27
        }
28
29
        $data = json_encode($data);
30
        $options = json_encode($value->options());
31
32
        $type = $value->chartType();
33
        $id = uniqid("$type-chart-");
34
        return (string)new Element('div', [], [
35
            new Element('canvas', ['id' => $id]),
36
            new Element('script', [], ["
37
                var ctx = document.getElementById('$id').getContext('2d');
38
                new Chart(ctx).$type($data, $options);
39
            "])
40
        ]);
41
    }
42
43
    /**
44
     * @param mixed $value
45
     * @return array|Element[]
46
     */
47
    public function headElements($value) {
48
        return [
49
            HeadElements::script('//cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js')
50
        ];
51
    }
52
}