Skip to content

Commit

Permalink
feat: add fluent api to highcharts
Browse files Browse the repository at this point in the history
  • Loading branch information
epessine committed Feb 23, 2024
1 parent 9d66ebd commit 8d44435
Showing 1 changed file with 74 additions and 1 deletion.
75 changes: 74 additions & 1 deletion src/Charts/Highcharts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<string> $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<mixed, mixed> $options
*/
public function options(iterable $options, bool $overwrite = false): static
{
/** @var array<string, mixed> $currentOptions */
$currentOptions = $this->config ?? [];

$options = $overwrite
? [...$options]
: array_replace_recursive($currentOptions, [...$options]);

$this->config = ['series' => $this->config['series'], ...$options];

return $this;
}

/**
* @param iterable<int|iterable<int>> $data
* @param iterable<mixed, mixed> $options
*/
public function series(string $name, iterable $data, iterable $options = []): static
{
$series = ['data' => [...$data], 'name' => $name, ...$options];

/** @var array<array<string, mixed>> $currentSeries */
$currentSeries = $this->config['series'] ?? [];

$currentSeries[] = $series;

data_set($this->config, 'series', $currentSeries);

return $this;
}
}

0 comments on commit 8d44435

Please sign in to comment.