Skip to content

Commit 3feeb9d

Browse files
authored
Merge pull request #667 from code16/improve-charts
Improve charts
2 parents c09d071 + 22b75f3 commit 3feeb9d

40 files changed

Lines changed: 4237 additions & 519 deletions

components.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
{
22
"$schema": "https://shadcn-vue.com/schema.json",
3-
"style": "default",
3+
"style": "new-york",
44
"typescript": true,
55
"tailwind": {
6-
"config": "tailwind.config.js",
6+
"config": "",
77
"css": "resources/css/shadcn.css",
88
"baseColor": "slate",
99
"cssVariables": true
1010
},
11-
"framework": "laravel",
1211
"aliases": {
1312
"components": "@/components",
1413
"utils": "@/utils/cn"

demo/app/Sharp/Dashboard/DemoDashboard.php

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
use Code16\Sharp\Dashboard\Layout\DashboardLayoutRow;
1717
use Code16\Sharp\Dashboard\Layout\DashboardLayoutSection;
1818
use Code16\Sharp\Dashboard\SharpDashboard;
19+
use Code16\Sharp\Dashboard\Widgets\SharpAreaGraphWidget;
1920
use Code16\Sharp\Dashboard\Widgets\SharpBarGraphWidget;
2021
use Code16\Sharp\Dashboard\Widgets\SharpFigureWidget;
2122
use Code16\Sharp\Dashboard\Widgets\SharpGraphWidgetDataSet;
22-
use Code16\Sharp\Dashboard\Widgets\SharpLineGraphWidget;
2323
use Code16\Sharp\Dashboard\Widgets\SharpOrderedListWidget;
2424
use Code16\Sharp\Dashboard\Widgets\SharpPanelWidget;
2525
use Code16\Sharp\Dashboard\Widgets\SharpPieGraphWidget;
@@ -31,19 +31,13 @@
3131

3232
class DemoDashboard extends SharpDashboard
3333
{
34-
private static array $colors = [
35-
'#2a9d90',
36-
'#e76e50',
37-
'#274754',
38-
'#e8c468',
39-
'#f4a462',
40-
// '#3B82F6',
41-
// '#064E3B',
42-
// '#EC4899',
43-
// '#78350F',
44-
// '#9CA3AF',
34+
const array COLORS = [
35+
'oklch(0.809 0.105 251.813)',
36+
'oklch(0.623 0.214 259.815)',
37+
'oklch(0.546 0.245 262.881)',
38+
'oklch(0.488 0.243 264.376)',
39+
'oklch(0.424 0.199 265.638)',
4540
];
46-
private static int $colorsIndex = 0;
4741

4842
protected function buildWidgets(WidgetsContainer $widgetsContainer): void
4943
{
@@ -52,19 +46,25 @@ protected function buildWidgets(WidgetsContainer $widgetsContainer): void
5246
SharpBarGraphWidget::make('authors_bar')
5347
->setTitle('Posts by author')
5448
->setShowLegend(false)
55-
->setHorizontal(),
49+
// ->setHorizontal(),
5650
)
5751
->addWidget(
5852
SharpPieGraphWidget::make('categories_pie')
5953
->setTitle('Posts by category'),
6054
)
6155
->addWidget(
62-
SharpLineGraphWidget::make('visits_line')
56+
SharpAreaGraphWidget::make('visits_line')
6357
->setTitle('Visits')
6458
->setHeight(200)
65-
->setShowLegend()
66-
->setMinimal()
67-
->setCurvedLines(),
59+
// ->setStacked()
60+
// ->setShowStackTotal(label: 'Total visits')
61+
// ->setShowLegend()
62+
->setDisplayHorizontalAxisAsTimeline()
63+
// ->setEnableHorizontalAxisLabelSampling()
64+
// ->setShowGradient()
65+
// ->setShowDots()
66+
// ->setMinimal()
67+
6868
)
6969
->addWidget(
7070
SharpFigureWidget::make('draft_panel')
@@ -176,21 +176,21 @@ protected function setLineGraphDataSet(): void
176176
{
177177
$visits = collect(CarbonPeriod::create($this->getStartDate(), $this->getEndDate()))
178178
->mapWithKeys(function (Carbon $day, $k) {
179-
return [$day->isoFormat('L') => (int) (rand(10000, 20000) * 1.02)];
179+
return [$day->format('Y-m-d') => (int) (rand(10000, 20000) * 1.02)];
180180
});
181181

182182
$this
183183
->addGraphDataSet(
184184
'visits_line',
185185
SharpGraphWidgetDataSet::make($visits)
186186
->setLabel('Visits')
187-
->setColor(static::nextColor()),
187+
->setColor(static::COLORS[0]),
188188
)
189189
->addGraphDataSet(
190190
'visits_line',
191191
SharpGraphWidgetDataSet::make($visits->map(fn ($value) => (int) ($value / (rand(20, 40) / 10))))
192192
->setLabel('New')
193-
->setColor(static::nextColor()),
193+
->setColor(static::COLORS[1]),
194194
);
195195
}
196196

@@ -205,14 +205,14 @@ protected function setBarsGraphDataSet(): void
205205
)]
206206
)
207207
->orderBy('posts_count', 'desc')
208-
->limit(8)
208+
->limit(15)
209209
->get()
210210
->pluck('posts_count', 'name');
211211

212212
$this->addGraphDataSet(
213213
'authors_bar',
214214
SharpGraphWidgetDataSet::make($data)
215-
->setColor(static::nextColor()),
215+
->setColor(static::COLORS[1]),
216216
);
217217
}
218218

@@ -229,12 +229,12 @@ protected function setPieGraphDataSet(): void
229229
->limit(5)
230230
->orderBy('posts_count', 'desc')
231231
->get()
232-
->each(function (Category $category) {
232+
->each(function (Category $category, $i) {
233233
$this->addGraphDataSet(
234234
'categories_pie',
235235
SharpGraphWidgetDataSet::make([$category->posts_count])
236236
->setLabel($category->name)
237-
->setColor(static::nextColor()),
237+
->setColor(static::COLORS[$i % count(static::COLORS)]),
238238
);
239239
});
240240
}
@@ -284,15 +284,6 @@ protected function setCustomPanelDataSet(): void
284284
}
285285
}
286286

287-
private static function nextColor(): string
288-
{
289-
if (static::$colorsIndex >= count(static::$colors)) {
290-
static::$colorsIndex = 0;
291-
}
292-
293-
return static::$colors[static::$colorsIndex++];
294-
}
295-
296287
protected function getStartDate(): Carbon
297288
{
298289
return $this->queryParams->filterFor(PeriodRequiredFilter::class)->getStart();

demo/database/seeders/DatabaseSeeder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public function run()
2626
$editor2 = User::factory(['email' => 'editor2@example.org'])
2727
->create();
2828

29+
User::factory(50)->create();
30+
2931
collect([$admin, $editor1, $editor2])
3032
->shuffle()
3133
->each(function (User $user, int $k) {

docs/guide/dashboard-widgets/graph.md

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,77 @@
11
# Graph widget
22

3-
This widget intends to display a graph visualization of any data.
3+
This widget intends to display a graph visualization of any data. There are four graph types, and they mostly share the same API. To choose one or the other, use its dedicated class:
44

5-
## Types
5+
## Line
66

7-
There are three graph types, and they mostly share the same API. To choose one or the other, use its dedicated class:
7+
```php
8+
$widgetsContainer->addWidget(
9+
SharpLineGraphWidget::make('sales')
10+
);
11+
```
12+
13+
Along with the [common configuration](#common-configuration), the following methods are available:
14+
15+
### `setShowDots(bool $showDots = true)`
16+
17+
Display dots on the graph lines.
818

9-
### Line graph
19+
### `setCurvedLines(bool $curvedLines = true)`
20+
21+
Display lines with curved angles. Default is `true`.
1022

23+
## Area
1124
```php
1225
$widgetsContainer->addWidget(
13-
SharpLineGraphWidget::make('sales')
26+
SharpAreaGraphWidget::make('sales')
1427
);
1528
```
1629

17-
### Bar graph
30+
Along with the [common configuration](#common-configuration), the following methods are available:
31+
32+
### `setCurvedLines(bool $curvedLines = true)`
33+
34+
Display lines with curved angles. Default is `true`.
35+
36+
### `setOpacity(float $opacity)`
37+
38+
Change the opacity of the filled areas. Default is `0.4`.
1839

40+
### `setShowGradient(bool $showGradient = true)`
41+
42+
Display a gradient on top of the filled areas.
43+
44+
### `setStacked(bool $stacked = true)`
45+
46+
Stack areas on top of each other. Useful for comparing two or more series. The order of `->addGraphDataSet()` calls defines the stacking order.
47+
48+
### `setShowStackTotal(bool $showStackTotal = true, ?string $label = null)`
49+
50+
Show the total of all stacked areas in the tooltip. The label can be customized.
51+
52+
## Bar
1953
```php
2054
$widgetsContainer->addWidget(
2155
SharpBarGraphWidget::make('sales')
2256
);
2357
```
2458

25-
### Pie graph
59+
Along with the [common configuration](#common-configuration), the following methods are available:
60+
61+
### `setHorizontal(bool $horizontal = true)`
62+
63+
Display horizontal bars instead of vertical ones. Default is `false`.
64+
65+
66+
## Pie
2667

2768
```php
2869
$widgetsContainer->addWidget(
2970
SharpPieGraphWidget::make('sales')
3071
);
3172
```
3273

33-
## Attributes (setters)
74+
## Common configuration
3475

3576
### `setRatio(string $ratio)`
3677

@@ -50,15 +91,11 @@ If true, legend and axis are hidden. Default is `false`.
5091

5192
### `setDisplayHorizontalAxisAsTimeline(bool $displayAsTimeline = true)`
5293

53-
**(Line and Bar)** If true, and if X axis values are valid dates, the graph will create a timeline repartition of dates, creating visual gaps between dates. Default is `false`.
94+
**(Line, Area, Bar)** If true, and if X axis values are valid dates, the graph will create a timeline repartition of dates, creating visual gaps between dates. Default is `false`.
5495

55-
### `setCurvedLines(bool $curvedLines = true)`
56-
57-
**(Line only)** Display lines with curved angles. Default is `true`.
58-
59-
### `setHorizontal(bool $horizontal = true)`
96+
### `setEnableHorizontalAxisLabelSampling(bool $enableLabelSampling = true)`
6097

61-
**(Bar only)** Display horizontal bars instead of vertical ones. Default is `false`.
98+
**(Line, Area, Bar)** If true, only some labels will be displayed depending on available space. It prevents label rotation. This method has no impact when `setDisplayHorizontalAxisAsTimeline()` is called. Default is `false`.
6299

63100
## Data valuation
64101

0 commit comments

Comments
 (0)