Skip to content

Commit

Permalink
Merge pull request #5 from moonshine-software/zaman-ua/master
Browse files Browse the repository at this point in the history
Added chart type option
  • Loading branch information
DissNik authored Jan 25, 2025
2 parents 5172e9b + 65392ab commit 136a5fb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,14 @@ The `line()` method allows you to add a value line to the metric. You can add mu
```php
line(
array|Closure $line,
string|array|Closure $color = '#7843E9'
string|array|Closure $color = '#7843E9',
string|array|Closure $type = 'line',
)
```

- `$line` - values for charting,
- `$color` - line color.
- `$color` - line color,
- `$type` - chart type (line, area, column)

```php
use MoonShine\Apexcharts\Components\LineChartMetric;
Expand All @@ -148,14 +150,14 @@ LineChartMetric::make('Orders')
->groupBy('date')
->pluck('sum','date')
->toArray()
])
], type: fn() => 'area')
->line([
'Avg' => Order::query()
->selectRaw('AVG(price) as avg, DATE_FORMAT(created_at, "%d.%m.%Y") as date')
->groupBy('date')
->pluck('avg','date')
->toArray()
], '#EC4176')
], '#EC4176', 'line');
```

<picture>
Expand All @@ -181,7 +183,9 @@ LineChartMetric::make('Orders')
->toArray()
],[
'red', 'blue'
])
], [
type: ['area', 'line']
]);
```

### Sorting keys
Expand Down
2 changes: 2 additions & 0 deletions resources/views/components/metrics/line.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'lines' => [],
'colors' => [],
'labels' => [],
'types',
])
<div
{{ $attributes->merge(['class' => 'chart']) }}
Expand All @@ -13,6 +14,7 @@
{
name: '{{ $label }}',
data: {{ json_encode(array_values($values)) }},
type: '{{ $types[$loop->parent->index % count($types)][$loop->index % count($types[$loop->parent->index])] }}',
},
@endforeach
@endforeach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'colors' => [],
'columnSpanValue' => 12,
'adaptiveColumnSpanValue' => 12,
'types' => [],
])
<x-moonshine::layout.column
:colSpan="$columnSpanValue"
Expand All @@ -17,6 +18,7 @@
:colors="$colors"
:labels="$labels"
:title="$label"
:types="$types"
/>
</x-moonshine::layout.box>
</x-moonshine::layout.column>
Expand Down
27 changes: 20 additions & 7 deletions src/Components/LineChartMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class LineChartMetric extends Metric

protected array $colors = [];

protected array $types = [];

protected bool $withoutSortKeys = false;

protected function assets(): array
Expand All @@ -32,9 +34,11 @@ protected function assets(): array
*/
public function line(
array|Closure $line,
string|array|Closure $color = '#7843E9'
string|array|Closure $color = '#7843E9',
string|array|Closure $type = 'line'
): static {
$this->lines[] = $line instanceof Closure ? $line() : $line;
$lines = $line instanceof Closure ? $line() : $line;
$this->lines[] = $lines;

$color = $color instanceof Closure ? $color() : $color;

Expand All @@ -44,12 +48,15 @@ public function line(
$this->colors = $color;
}

return $this;
}
$type = $type instanceof Closure ? $type() : $type;

public function getColor(int $index): string
{
return $this->colors[$index];
if (is_string($type)) {
$this->types[][] = $type;
} else {
$this->types[] = $type;
}

return $this;
}

public function getColors(): array
Expand All @@ -72,6 +79,11 @@ public function getLines(): array
return $this->lines;
}

public function getTypes(): array
{
return $this->types;
}

public function withoutSortKeys(): static
{
$this->withoutSortKeys = true;
Expand All @@ -93,6 +105,7 @@ protected function viewData(): array
'labels' => $this->getLabels(),
'lines' => $this->getLines(),
'colors' => $this->getColors(),
'types' => $this->getTypes(),
];
}
}

0 comments on commit 136a5fb

Please sign in to comment.