diff --git a/src/Charts/Highcharts.php b/src/Charts/Highcharts.php index 0c3a5d7..70ccd25 100644 --- a/src/Charts/Highcharts.php +++ b/src/Charts/Highcharts.php @@ -2,15 +2,17 @@ namespace Axis\Charts; +use Axis\Enums\Type; use Axis\Interfaces\Javascriptable; use Axis\Interfaces\Renderable; use Axis\Traits\AsAxisChart; +use Axis\Traits\IsFluent; use Illuminate\Contracts\Support\Htmlable; use Serializable; final class Highcharts implements Htmlable, Javascriptable, Renderable, Serializable { - use AsAxisChart; + use AsAxisChart, IsFluent; public function getContainerElement(): string { @@ -32,4 +34,75 @@ protected function updateScript(): string } JS; } + + public function title(string $title): static + { + data_set($this->config, 'title.text', $title); + + return $this; + } + + /** + * @param iterable $labels + */ + public function labels(iterable $labels): static + { + data_set($this->config, 'xAxis.categories', [...$labels]); + + return $this; + } + + public function type(Type $type): static + { + if ($type === Type::Radar) { + data_set($this->config, 'chart.polar', true); + } + + $type = match ($type) { + Type::Line, + Type::Radar => 'line', + Type::Bar => 'bar', + Type::Column => 'column', + Type::Pie => 'pie', + }; + + data_set($this->config, 'chart.type', $type); + + return $this; + } + + /** + * @param iterable $options + */ + public function options(iterable $options, bool $overwrite = false): static + { + /** @var array $currentOptions */ + $currentOptions = $this->config ?? []; + + $options = $overwrite + ? [...$options] + : array_replace_recursive($currentOptions, [...$options]); + + $this->config = ['series' => $this->config['series'], ...$options]; + + return $this; + } + + /** + * @param iterable> $data + * @param iterable $options + */ + public function series(string $name, iterable $data, iterable $options = []): static + { + $series = ['data' => [...$data], 'name' => $name, ...$options]; + + /** @var array> $currentSeries */ + $currentSeries = $this->config['series'] ?? []; + + $currentSeries[] = $series; + + data_set($this->config, 'series', $currentSeries); + + return $this; + } }