Skip to content

Commit

Permalink
Исправления
Browse files Browse the repository at this point in the history
  • Loading branch information
darkeum committed Sep 20, 2022
1 parent a02850f commit 325517d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 26 deletions.
31 changes: 20 additions & 11 deletions src/Classes/Apex/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Chart extends BaseChart
*
* @var object
*/
public $dataset = Dataset::class;
public $dataset = Dataset::class;

/**
* Initiates the Chartjs Line Chart.
Expand All @@ -32,19 +32,28 @@ public function __construct()
return $this->options([]);
}

public function formatDatasets()
public function formatDatasets($encode = true)
{
$result = [];
foreach ($this->datasets as $dataset) {
$new = [];
foreach ($dataset->values as $value) {
$new[] = $value;
if ($this->type == 'donut') {
foreach ($this->datasets as $dataset) {
foreach ($dataset->values as $value) {
$result[] = $value;
}
}
} else {
foreach ($this->datasets as $dataset) {
$new = [];
foreach ($dataset->values as $value) {
$new[] = $value;
}
$result[] = [
'name' => $dataset->name,
'data' => $new,
];
}
$result[] = [
'name' => $dataset->name,
'data' => $new,
];
}
return json_encode($result);

return $encode ? json_encode($result) : $result;
}
}
18 changes: 15 additions & 3 deletions src/Classes/BaseChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class BaseChart
*/
public $datasets = [];

public $encode_options = true;

/**
* Stores the dataset class to be used.
*
Expand Down Expand Up @@ -277,6 +279,13 @@ public function type(string $type)
return $this;
}

public function encode_options(bool $encode_options)
{
$this->encode_options = $encode_options;

return $this;
}

/**
* Set the chart height.
*
Expand Down Expand Up @@ -337,8 +346,11 @@ public function formatOptions(bool $strict = false, bool $noBraces = false)
if (!$strict && count($this->options) === 0) {
return '';
}

$options = Encoder::encode($this->options);
if ($this->encode_options) {
$options = Encoder::encode($this->options);
} else {
$options = $this->options;
}

return $noBraces ? substr($options, 1, -1) : $options;
}
Expand Down Expand Up @@ -389,7 +401,7 @@ public function reset()
*
* @return string
*/
public function formatDatasets()
public function formatDatasets($encode = true)
{
// This little boy was commented because it's not compatible
// in laravel < 5.4
Expand Down
2 changes: 1 addition & 1 deletion src/Classes/C3/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct()
*
* @return void
*/
public function formatDatasets()
public function formatDatasets($encode = false)
{
$datasets = Collection::make($this->datasets);

Expand Down
33 changes: 25 additions & 8 deletions src/Support/Livewire/ChartComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ abstract class ChartComponent extends Component
/**
* @var string|null
*/
public ?bool $has_second = false;

public ?string $chart_id = null;

public ?string $chart_second_id = null;

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

public ?string $chart_second_data_checksum = null;

/**
* @return string
*/
Expand All @@ -44,22 +50,33 @@ protected abstract function view(): string;
public function render(): View
{
$chart_data = $this->chartData();

$chart_class = $this->chartClass();
$chart = new $chart_class($chart_data);
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->formatDatasets(false));
}
$this->emit('chartUpdate', $this->chart_id, $chart_data->labels(), $chart_data->datasets());
$this->chart_data_checksum = $chart_data->checksum();


if($this->has_second) {
$chart_second_data = $this->chartSecondData();
$chart_second_class = $this->chartSecondClass();
$chart_second = new $chart_second_class($chart_second_data);
if (!$this->chart_second_id) {
$this->chart_second_id = $chart_second->id;
} elseif ($chart_second_data->checksum() !== $this->chart_second_data_checksum) {
$this->emit('chartUpdate', $this->chart_second_id, $chart_second_data->labels(), $chart_second->formatDatasets(false));
}
$this->chart_second_data_checksum = $chart_second_data->checksum();
}

return view($this->view(), [
'chart' => ($chart ?? null),
'chart_id' => ($this->chart_id)
'chart_second' => ($chart_second ?? null),
'chart_id' => ($this->chart_id),
'chart_second_id' => ($this->chart_second_id ?? null),
]);
}
}
8 changes: 5 additions & 3 deletions src/Views/apex/script.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ function {{ $chart->id }}_create(data) {
series: data,
@if (!isset($chart->options['xaxis']))
xaxis: {
categories: {!! $chart->formatLabels() !!}
},
@if ($chart->type != 'donut')
xaxis: {
categories: {!! $chart->formatLabels() !!}
},
@endif
@endif
});
Expand Down

0 comments on commit 325517d

Please sign in to comment.