Completed
Push — master ( e00767...142210 )
by
unknown
09:09
created

formats/d3/SRF_D3Chart.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * A query printer for D3 charts using the D3 JavaScript library
5
 * and SMWAggregatablePrinter.
6
 *
7
 * @file SRF_D3Chart.php
8
 * @ingroup SemanticResultFormats
9
 * @licence GNU GPL v2 or later
10
 *
11
 * @since 1.8
12
 *
13
 * @author mwjames
14
 */
15
class SRFD3Chart extends SMWAggregatablePrinter {
16
17
	/*
18
	 * @see SMWResultPrinter::getName
19
	 *
20
	 */
21
	public function getName() {
22
		return wfMessage( 'srf-printername-d3chart' )->text();
23
	}
24
25
	/**
26
	 * @see SMWResultPrinter::getFormatOutput
27
	 *
28
	 * @since 1.8
29
	 *
30
	 * @param array $data label => value
31
	 * @return string
32
	 */
33
	protected function getFormatOutput( array $data ) {
34
35
		// Object count
36
		static $statNr = 0;
37
		$d3chartID = 'd3-chart-' . ++$statNr;
38
39
		$this->isHTML = true;
40
41
		// Reorganize the raw data
42
		foreach ( $data as $name => $value ) {
43
			if ( $value >= $this->params['min'] ) {
44
				$dataObject[] = [ 'label' => $name , 'value' => $value ];
45
			}
46
		}
47
48
		// Ensure right conversion
49
		$width = strstr( $this->params['width'] ,"%") ? $this->params['width'] : $this->params['width'] . 'px';
50
51
		// Prepare transfer objects
52
		$d3data =  [
53
			'data' => $dataObject,
54
			'parameters' =>  [
55
				'colorscheme' => $this->params['colorscheme'] ? $this->params['colorscheme'] : null,
56
				'charttitle'  => $this->params['charttitle'],
57
				'charttext'   => $this->params['charttext'],
58
				'datalabels'  => $this->params['datalabels']
59
			]
60
		];
61
62
		// Encoding
63
		$requireHeadItem =  [ $d3chartID => FormatJson::encode( $d3data ) ];
64
		SMWOutputs::requireHeadItem( $d3chartID, Skin::makeVariablesScript( $requireHeadItem ) );
65
66
		// RL module
67
		$resource = 'ext.srf.d3.chart.' . $this->params['charttype'];
68
		SMWOutputs::requireResource( $resource );
69
70
		// Chart/graph placeholder
71
		$chart = Html::rawElement( 'div', [
72
			'id'    => $d3chartID,
73
			'class' => 'container',
74
			'style' => 'display:none;'
75
			], null
76
		);
77
78
		// Processing placeholder
79
		$processing = SRFUtils::htmlProcessingElement( $this->isHTML );
80
81
		// Beautify class selector
82
		$class = $this->params['charttype'] ?  '-' . $this->params['charttype'] : '';
83
		$class = $this->params['class'] ? $class . ' ' . $this->params['class'] : $class . ' d3-chart-common';
84
85
		// D3 wrappper
86
		return Html::rawElement( 'div', [
87
			'class' => 'srf-d3-chart' . $class ,
88
			'style' => "width:{$width}; height:{$this->params['height']}px;"
89
			], $processing . $chart
90
		);
91
	}
92
93
	/**
94
	 * @see SMWResultPrinter::getParamDefinitions
95
	 *
96
	 * @since 1.8
97
	 *
98
	 * @param $definitions array of IParamDefinition
99
	 *
100
	 * @return array of IParamDefinition|array
101
	 */
102
	public function getParamDefinitions( array $definitions ) {
0 ignored issues
show
getParamDefinitions uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
103
		$params = parent::getParamDefinitions( $definitions );
104
105
		$params['min'] = [
106
			'type' => 'integer',
107
			'message' => 'srf-paramdesc-minvalue',
108
			'default' => false,
109
			'manipulatedefault' => false,
110
		];
111
112
		$params['charttype'] = [
113
			'message' => 'srf-paramdesc-charttype',
114
			'default' => 'treemap',
115
			'values' => [ 'treemap', 'bubble' ],
116
		];
117
118
		$params['height'] = [
119
			'type' => 'integer',
120
			'message' => 'srf_paramdesc_chartheight',
121
			'default' => 400,
122
			'lowerbound' => 1,
123
		];
124
125
		$params['width'] = [
126
			'message' => 'srf_paramdesc_chartwidth',
127
			'default' => '100%',
128
		];
129
130
		$params['charttitle'] = [
131
			'message' => 'srf_paramdesc_charttitle',
132
			'default' => '',
133
		];
134
135
		$params['charttext'] = [
136
			'message' => 'srf-paramdesc-charttext',
137
			'default' => '',
138
		];
139
140
		$params['class'] = [
141
			'message' => 'srf-paramdesc-class',
142
			'default' => '',
143
		];
144
145
		$params['datalabels'] = [
146
			'message' => 'srf-paramdesc-datalabels',
147
			'default' => 'none',
148
			'values' => [ 'value', 'label' ],
149
		];
150
151
		$params['colorscheme'] = [
152
			'message' => 'srf-paramdesc-colorscheme',
153
			'default' => '',
154
			'values' => $GLOBALS['srfgColorScheme'],
155
		];
156
157
		$params['chartcolor'] = [
158
			'message' => 'srf-paramdesc-chartcolor',
159
			'default' => '',
160
		];
161
162
		return $params;
163
	}
164
}
165