-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractChart.php
149 lines (129 loc) · 4.05 KB
/
AbstractChart.php
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
<?php
namespace RadiateCode\DaChartjs\Abstracts;
use RadiateCode\DaChartjs\Chart;
abstract class AbstractChart
{
/**
* Chart title
*
* ---------------------------------------------------------------------
* Note: it can be use as chart id or chart name in js & html
* ---------------------------------------------------------------------
*
* @return string
*/
abstract protected function chartTitle(): string;
/**
* Chart type
*
* ---------------------------------------------------------------------
* Note: Chart type must be concrete class of TypeInterface
* ---------------------------------------------------------------------
*
* @return string
*/
abstract protected function chartType(): string;
/**
* Chart labels
*
* ---------------------------------------------------------------------------------
* Note: This labels are used to label the data index (default x axis) in the chart view
* ---------------------------------------------------------------------------------
*
* @return array
*/
abstract protected function labels(): array;
/**
* Dataset
*
* -------------------------------------------------------------------------------------------------
* Note: datasets can be generate by Dataset Facade Or we can pass custom array with dataset properties,
* -------------------------------------------------------------------------------------------------
*
* @return array
*/
abstract protected function datasets(): array;
/**
* Chart size
*
* [Note: Set height and width. Width can be optional when we want responsive chart]
* @return array
*/
protected function chartSize(): array
{
return [];
}
/**
* change default options
*
* ---------------------------------------------------------------------------------------------
* Note: Each type of chart has default options, we can use this method
* when we need to modify those default options
* ---------------------------------------------------------------------------------------------
*
* @return array
*/
protected function changeDefaultOptions(): array
{
return [];
}
/**
* Custom options
*
* ---------------------------------------------------------------------------------------------
* Note: If we don't want to use the default, then this methods can be useful for custom options
* ---------------------------------------------------------------------------------------------
*
* @return array|string
*/
protected function options()
{
return [];
}
/**
* Render the chart
*
* @return array
*/
public function render(): array
{
return $this->chart()->render();
}
/**
* Build the chart html & js scripts
*/
public function template()
{
return $this->chart()->template();
}
/**
* chart Config
*
* @return Chart
*/
private function chart(): Chart
{
$chart = Chart::build($this->chartTitle(), $this->chartType());
$optionModifications = $this->changeDefaultOptions();
$chartSize = $this->chartSize();
if ( ! empty($this->options())) {
$chart->options($this->options());
}
if ( ! empty($optionModifications)) {
foreach ($optionModifications as $key => $value) {
$chart->changeDefaultOption($key, $value);
}
}
if ( ! empty($chartSize)) {
if (array_key_exists('height', $chartSize)) {
$chart->size($chartSize['height']);
}
if (array_key_exists('height', $chartSize) && array_key_exists('width', $chartSize)) {
$chart->size($chartSize['height'], $chartSize['width']);
}
}
return $chart
->labels($this->labels())
->datasets($this->datasets());
}
}