Skip to content

Commit

Permalink
update Chart facade
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamandlou committed Feb 23, 2022
1 parent 3669ebc commit ebf7a37
Showing 1 changed file with 70 additions and 9 deletions.
79 changes: 70 additions & 9 deletions src/Support/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,76 @@

class Chart
{
/**
* @param string $view
* @param array $data
* @return mixed
*/
public function render(string $view, array $chartData, array $data = [])
protected string $id;
protected array $parameters = [];

public function chart(string $id, array $parameters): Chart
{
$this->id = $id;
$this->parameters = $parameters;
return $this;
}

public function id(string $id): Chart
{
$this->id = $id;
return $this;
}

public function type(string $type): Chart
{
$this->parameters['type'] = $type;
return $this;
}

public function labels(array $labels): Chart
{
$this->parameters['data']['labels'] = $labels;
return $this;
}

public function datasets(array $dataasets): Chart
{
$this->parameters['data']['datasets'] = $dataasets;
return $this;
}

public function options(array $options): Chart
{
return view($view, array_merge([
'chartData' => json_encode($chartData),
], $data));
$this->parameters['options'] = $options;
return $this;
}

public function render(string $view, array $data = [])
{
$data = $this->reformatData($data);
return view($view, $data);
}

protected function reformatData(array $data)
{
if (key_exists('chartScript', $data)) {
throw new Exception('chartScript key is exists in array, please rename it');
}
return array_merge($data, [
'chartScript' => $this->script()
]);
}

protected function parameters(): string
{
return json_encode($this->parameters);
}

protected function script()
{
return "
const ctx{$this->id} = document.getElementById('{$this->id}').getContext('2d');
const {$this->id} = new Chart(ctx{$this->id},{$this->parameters()});
";
}
}




0 comments on commit ebf7a37

Please sign in to comment.