Skip to content

Commit 9c71c38

Browse files
committed
v2
1 parent d3f948e commit 9c71c38

File tree

4 files changed

+20
-53
lines changed

4 files changed

+20
-53
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@
22

33
All notable changes to `php-sparkline` will be documented in this file.
44

5+
## 2.0.0
6+
7+
- Removed `SparkLine::new()`, use `new SparkLine()` instead
8+
- Removed `SparkLine::getPeriod()`
9+
- Removed dependencies on `spatie/period` and `laravel/collection`
10+
- Rename `SparkLineDay` to `SparkLineEntry`
11+
- Allow integers to be passed directly into a new `SparkLine` instead of requiring `SparkLineEntry` objects

README.md

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,80 +17,41 @@ composer require brendt/php-sparkline
1717
## Usage
1818

1919
```php
20-
$sparkLine = SparkLine::new(
21-
new SparkLineDay(
22-
count: 1,
23-
),
24-
new SparkLineDay(
25-
count: 2,
26-
),
27-
// …
28-
));
20+
$sparkLine = new SparkLine(1, 2, 5, 10, 2));
2921

3022
$total = $sparkLine->getTotal();
31-
$period = $sparkLine->getPeriod(); // Spatie\Period
23+
3224
$svg = $sparkLine->make();
3325
```
3426

3527
![](./.github/img/0.png)
3628

37-
To construct a sparkline, you'll have to pass in a collection of `Brendt\SparkLineDay` objects. This object takes two parameters: a `count`, and a `DateTimeInterface`. You could for example convert database entries like so:
29+
To construct a sparkline, you'll have to pass in an array of values.
3830

39-
```php
40-
$days = PostVistisPerDay::query()
41-
->orderByDesc('day')
42-
->limit(20)
43-
->get()
44-
->map(fn (SparkLineDay $row) => new SparkLineDay(
45-
count: $row->visits,
46-
day: Carbon::make($row->day),
47-
));
48-
```
31+
### Customization
4932

50-
In many cases though, you'll want to aggregate data with an SQL query, and convert those aggregations on the fly to `SparkLineDay` objects:
33+
You can pick any amount of colors and the sparkline will automatically generate a gradient from them:
5134

5235
```php
53-
$days = DB::query()
54-
->from((new Post())->getTable())
55-
->selectRaw('`published_at_day`, COUNT(*) as `visits`')
56-
->groupBy('published_at_day')
57-
->orderByDesc('published_at_day')
58-
->whereNotNull('published_at_day')
59-
->limit(20)
60-
->get()
61-
->map(fn (object $row) => new SparkLineDay(
62-
count: $row->visits,
63-
day: Carbon::make($row->published_at_day),
64-
));
36+
$sparkLine = (new SparkLine($days))->withColors('#4285F4', '#31ACF2', '#2BC9F4');
6537
```
6638

67-
### Customization
39+
![](./.github/img/1.png)
6840

69-
This package offers some methods to customize the sparkline. First off, you can pick any amount of colors and the sparkline will automatically generate a gradient from them:
41+
You can configure the stroke width:
7042

7143
```php
72-
$sparkLine = SparkLine::new($days)->withColors('#4285F4', '#31ACF2', '#2BC9F4');
44+
$sparkLine = (new SparkLine($days))->withStrokeWidth(4);
7345
```
7446

75-
![](./.github/img/1.png)
76-
77-
Next, you can configure a bunch of numbers:
47+
As well as the dimensions (in pixels):
7848

7949
```php
80-
$sparkLine = SparkLine::new($days)
81-
->withStrokeWidth(4)
82-
->withDimensions(500, 100)
83-
->withMaxItemAmount(100)
84-
->withMaxValue(20);
50+
$sparkLine = SparkLine::new($days)->withDimensions(width: 500, height: 100);
8551
```
8652

8753
![](./.github/img/2.png)
8854

89-
- **`withStrokeWidth`** will determine the stroke's width
90-
- **`withDimensions`** will determine the width and height of the rendered SVG
91-
- **`withMaxItemAmount`** will determine how many days will be shown. If you originally passed on more days than this max, then the oldest ones will be omitted. If the max amount is set to a number that's _higher_ than the current amount of days, then the sparkline will contain empty days. By default, the amount of given days will be used.
92-
- **`withMaxValue`** will set the maximum value of the sparkline. This is useful if you have multiple sparklines that should all have the same scale. By default, the maximum value is determined based on the given days.
93-
9455
## Testing
9556

9657
```bash

src/SparkLine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function __toString(): string
112112
return $this->make();
113113
}
114114

115-
public function getCoordinates(): string
115+
private function getCoordinates(): string
116116
{
117117
$divider = min($this->width, $this->maxItemAmount);
118118

tests/index.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
)
1212
))
1313
->withStrokeWidth(1)
14-
->withDimensions(width: 1000, height: 200)
15-
;
14+
->withDimensions(width: 1000, height: 200);
1615

1716
?>
1817

0 commit comments

Comments
 (0)