Skip to content

Commit

Permalink
Добавлен Apex
Browse files Browse the repository at this point in the history
  • Loading branch information
darkeum committed Sep 15, 2022
1 parent 0bcdef1 commit 9728b3a
Show file tree
Hide file tree
Showing 8 changed files with 491 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/Classes/Apex/Chart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Darkeum\Charts\Classes\Apex;

use Illuminate\Support\Collection;
use Darkeum\Charts\Classes\BaseChart;
use Darkeum\Charts\Features\Apex\Chart as ChartFeatures;

class Chart extends BaseChart
{
use ChartFeatures;

/**
* Chartjs dataset class.
*
* @var object
*/
public $dataset = Dataset::class;

/**
* Initiates the Chartjs Line Chart.
*
* @return self
*/
public function __construct()
{
parent::__construct();

$this->container = 'charts::apex.container';
$this->script = 'charts::apex.script';

return $this->options([]);
}

public function formatDatasets()
{
$result = [];
foreach ($this->datasets as $dataset) {
$new = [];
foreach ($dataset->values as $value) {
$new[] = $value;
}
$result[] = [
'name' => $dataset->name,
'data' => $new,
];
}
return json_encode($result);
}
}
44 changes: 44 additions & 0 deletions src/Classes/Apex/Dataset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Darkeum\Charts\Classes\Apex;

use Darkeum\Charts\Classes\DatasetClass;
use Darkeum\Charts\Features\Chartjs\Dataset as DatasetFeatures;

class Dataset extends DatasetClass
{
use DatasetFeatures;

/**
* Creates a new dataset with the given values.
*
* @param string $name
* @param string $type
* @param array $values
*/
public function __construct(string $name, string $type, array $values)
{
parent::__construct($name, $type, $values);

$this->options([
'borderWidth' => 2,
]);
}

/**
* Formats the dataset for chartjs.
*
* @return array
*/
public function format()
{
return [
'data' => $this->values,
'name' => $this->name,
];
// return array_merge($this->options, [
// 'data' => $this->values,
// 'name' => $this->name,
// ]);
}
}
117 changes: 117 additions & 0 deletions src/Features/Apex/Chart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Darkeum\Charts\Features\Apex;

trait Chart
{
/**
* Minalist chart display (Hide labels and axes).
*
* @return self
*/
public function minimalist(bool $display)
{
$this->displayLegend(!$display);

return $this->displayAxes(!$display);
}

/**
* Display the chart legend.
*
* @param bool $legend
*
* @return self
*/
public function displayLegend(bool $legend)
{
return $this->options([
'legend' => [
'display' => $legend,
],
]);
}

/**
* Display the chart axis.
*
* @param bool $axes
*
* @return self
*/
public function displayAxes(bool $axes, bool $strict = false)
{
if ($strict) {
return $this->options([
'scale' => [
'display' => $axes,
],
]);
}

return $this->options([
'scales' => [
'xAxes' => [
[
'display' => $axes,
],
],
'yAxes' => [
[
'display' => $axes,
],
],
],
]);
}

/**
* Set the bar width of the X Axis.
*
* @param float $width
*
* @return self
*/
public function barWidth(float $width)
{
return $this->options([
'scales' => [
'xAxes' => [
[
'barPercentage' => $width,
],
],
],
]);
}

/**
* Set the chart title.
*
* @param string $title
* @param int $font_size
* @param string $color
* @param string $font_weight
* @param string $font_family
*
* @return self
*/
public function title(
string $title,
int $font_size = 14,
string $color = '#666',
string $font_weight = 'bold',
string $font_family = "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
) {
return $this->options([
'title' => [
'display' => true,
'fontFamily' => $font_family,
'fontSize' => $font_size,
'fontColor' => $color,
'fontStyle' => $font_weight,
'text' => $title,
],
]);
}
}
100 changes: 100 additions & 0 deletions src/Features/Apex/Dataset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Darkeum\Charts\Features\Apex;

use Illuminate\Support\Collection;

trait Dataset
{
/**
* Set the dataset border color.
*
* @param string|array|Collection $color
*
* @return self
*/
public function color($color)
{
if ($color instanceof Collection) {
$color = $color->toArray();
}

return $this->options([
'borderColor' => $color,
]);
}

/**
* Set the dataset background color.
*
* @param string|array|Collection $color
*
* @return self
*/
public function backgroundColor($color)
{
if ($color instanceof Collection) {
$color = $color->toArray();
}

return $this->options([
'backgroundColor' => $color,
]);
}

/**
* Determines if the dataset is filled.
*
* @param bool $filled
*
* @return self
*/
public function fill(bool $filled)
{
return $this->options([
'fill' => $filled,
]);
}

/**
* Set the chart line tension.
*
* @param int $tension
*
* @return self
*/
public function lineTension(float $tension)
{
return $this->options([
'lineTension' => $tension,
]);
}

/**
* Set the line to a dashed line in the chart options.
*
* @param array $dashed
*
* @return self
*/
public function dashed(array $dashed = [5])
{
return $this->options([
'borderDash' => $dashed,
]);
}

/**
* Set the label of the dataset.
*
* @param $label string
*
* @return self
*/
public function label($label)
{
return $this->options([
'label' => $label,
]);
}
}
65 changes: 65 additions & 0 deletions src/Support/Livewire/ChartComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Darkeum\Charts\Support\Livewire;

use Illuminate\View\View;
use Boot\System\Livewire\Component;

/**
* Class ChartComponent
*
* @package App\Support\Livewire
*/
abstract class ChartComponent extends Component
{

/**
* @var string|null
*/
public ?string $chart_id = null;

/**
* @var string|null
*/
public ?string $chart_data_checksum = null;

/**
* @return string
*/
protected abstract function chartClass(): string;

/**
* @return \App\Support\Livewire\ChartComponentData
*/
protected abstract function chartData(): ChartComponentData;

/**
* @return string
*/
protected abstract function view(): string;

/**
* @return \Illuminate\View\View
*/
public function render(): View
{
$chart_data = $this->chartData();

if (!$this->chart_id) {
$chart_class = $this->chartClass();

$chart = new $chart_class($chart_data);

$this->chart_id = $chart->id;
} elseif ($chart_data->checksum() !== $this->chart_data_checksum) {
$this->emit('chartUpdate', $this->chart_id, $chart_data->labels(), $chart_data->datasets());
}
$this->emit('chartUpdate', $this->chart_id, $chart_data->labels(), $chart_data->datasets());
$this->chart_data_checksum = $chart_data->checksum();

return view($this->view(), [
'chart' => ($chart ?? null),
'chart_id' => ($this->chart_id)
]);
}
}
Loading

0 comments on commit 9728b3a

Please sign in to comment.