Skip to content

Commit 606804c

Browse files
committed
Chart start at 0 - #1
Now it is possible to use the helper function `beginAtZero` to set the start value at 0
1 parent fe17e91 commit 606804c

4 files changed

Lines changed: 87 additions & 21 deletions

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
# ChartJS-PHP
2+
3+
<p align="center">
4+
<a href="https://packagist.org/packages/bbsnly/chartjs-php"><img src="https://poser.pugx.org/bbsnly/chartjs-php/d/total.svg" alt="Total Downloads"></a>
5+
<a href="https://packagist.org/packages/bbsnly/chartjs-php"><img src="https://poser.pugx.org/bbsnly/chartjs-php/v/stable.svg" alt="Latest Stable Version"></a>
6+
<a href="https://packagist.org/packages/bbsnly/chartjs-php"><img src="https://poser.pugx.org/bbsnly/chartjs-php/license.svg" alt="License"></a>
7+
</p>
8+
29
The package helps you to generate [ChartJS](http://www.chartjs.org/ "ChartJS")
310
element directly in PHP and translate it to JSON.
411

@@ -9,7 +16,7 @@ element directly in PHP and translate it to JSON.
916
`composer test`
1017

1118
## Requirements
12-
* `php >= 5.6`
19+
* `php >= 7.0`
1320
* `ChartJS >= 2.0`
1421

1522
## Charts

src/Chart.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public function get()
5050
public function toArray()
5151
{
5252
return [
53-
'type' => $this->type,
54-
'data' => $this->data->toArray(),
53+
'type' => $this->type,
54+
'data' => $this->data->toArray(),
5555
'options' => $this->options->toArray()
5656
];
5757
}
@@ -101,4 +101,22 @@ public function data($data)
101101

102102
return $this;
103103
}
104+
105+
/**
106+
*
107+
*/
108+
public function beginAtZero()
109+
{
110+
$this->options->scales([
111+
'yAxes' => [
112+
[
113+
'ticks' => [
114+
'beginAtZero' => true
115+
]
116+
]
117+
]
118+
]);
119+
120+
return $this;
121+
}
104122
}

tests/BaseChartTest.php

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public function can_create_it()
2929
public function it_is_responsive()
3030
{
3131
$expected = [
32-
'type' => 'line',
33-
'data' => [],
32+
'type' => 'line',
33+
'data' => [],
3434
'options' => [
3535
'responsive' => true
3636
]
@@ -48,10 +48,10 @@ public function it_is_responsive()
4848
public function it_is_animated()
4949
{
5050
$expected = [
51-
'type' => 'line',
52-
'data' => [],
51+
'type' => 'line',
52+
'data' => [],
5353
'options' => [
54-
'responsive' => true,
54+
'responsive' => true,
5555
'responsiveAnimationDuration' => 10
5656
]
5757
];
@@ -84,8 +84,8 @@ public function it_has_basic_datasets_json()
8484
$this->chart->data($data);
8585

8686
$expected = json_encode([
87-
'type' => null,
88-
'data' => [
87+
'type' => null,
88+
'data' => [
8989
'datasets' => [
9090
(object)[
9191
'data' => [10, 20, 30]
@@ -116,8 +116,8 @@ public function it_has_basic_data_with_labels()
116116
$this->chart->data($data);
117117

118118
$expected = [
119-
'type' => null,
120-
'data' => [
119+
'type' => null,
120+
'data' => [
121121
'datasets' => [
122122
[
123123
'data' => [10, 20, 30],
@@ -126,7 +126,7 @@ public function it_has_basic_data_with_labels()
126126
'data' => [30, 20, 10]
127127
]
128128
],
129-
'labels' => ['Red', 'Green', 'Blue']
129+
'labels' => ['Red', 'Green', 'Blue']
130130
],
131131
'options' => []
132132
];
@@ -149,23 +149,65 @@ public function it_has_basic_data_with_labels_and_colors()
149149
$this->chart->data($data);
150150

151151
$expected = [
152-
'type' => null,
153-
'data' => [
152+
'type' => null,
153+
'data' => [
154154
'datasets' => [
155155
[
156-
'data' => [10, 20, 30],
156+
'data' => [10, 20, 30],
157157
'backgroundColor' => ['red', 'green', 'blue']
158158
],
159159
[
160-
'data' => [30, 20, 10],
160+
'data' => [30, 20, 10],
161161
'backgroundColor' => ['red', 'green', 'blue']
162162
]
163163
],
164-
'labels' => ['Red', 'Green', 'Blue']
164+
'labels' => ['Red', 'Green', 'Blue']
165165
],
166166
'options' => []
167167
];
168168

169169
$this->assertEquals($expected, $this->chart->get());
170170
}
171+
172+
/** @test */
173+
public function can_set_start_from_zero_using_helper()
174+
{
175+
$data = new Data;
176+
177+
$datasets = [
178+
(new Dataset)->data([10, 20, 30])->backgroundColor(['red', 'green', 'blue'])
179+
];
180+
181+
$data->datasets($datasets)->labels(['Red', 'Green', 'Blue']);
182+
183+
$this->chart->data($data);
184+
185+
$this->chart->beginAtZero();
186+
187+
$expected = [
188+
'type' => null,
189+
'data' => [
190+
'datasets' => [
191+
[
192+
'data' => [10, 20, 30],
193+
'backgroundColor' => ['red', 'green', 'blue']
194+
]
195+
],
196+
'labels' => ['Red', 'Green', 'Blue']
197+
],
198+
'options' => [
199+
'scales' => [
200+
'yAxes' => [
201+
[
202+
'ticks' => [
203+
'beginAtZero' => true
204+
]
205+
]
206+
]
207+
]
208+
]
209+
];
210+
211+
$this->assertEquals($expected, $this->chart->get());
212+
}
171213
}

tests/examples/bar.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@
1919

2020
$chart->data($data);
2121

22-
$options = $options->responsive(false)
23-
// Start at zero
24-
->scales((new \Chart\Config\Config())->yAxes([(new \Chart\Config\Config())->ticks(['beginAtZero' => true])]));
22+
$options = $options->responsive(false);
2523
$chart->options($options);
2624

25+
$chart->beginAtZero();
2726
$myChart = $chart->toJson();
2827

2928
include_once __DIR__ . '/base.php';

0 commit comments

Comments
 (0)