-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c30c408
commit 35324ea
Showing
12 changed files
with
273 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { mergedOptionsWithJsonConfig } from "./helpers"; | ||
|
||
const radialChart = () => { | ||
return { | ||
chart: null, | ||
|
||
init() { | ||
setTimeout(() => { | ||
this.drawChart(this.$wire); | ||
}, 0); | ||
}, | ||
|
||
drawChart(component) { | ||
if (this.chart) { | ||
this.chart.destroy(); | ||
} | ||
|
||
const data = component.get("radialChartModel.data"); | ||
const showTotal = component.get("radialChartModel.showTotal"); | ||
const onBarClickEventName = component.get( | ||
"radialChartModel.onBarClickEventName" | ||
); | ||
const jsonConfig = component.get("radialChartModel.jsonConfig"); | ||
|
||
const colors = component.get("radialChartModel.colors"); | ||
|
||
const options = { | ||
series: data.map((item) => item.value), | ||
labels: data.map((item) => item.title), | ||
colors: Object.values(colors).filter(Boolean), | ||
chart: { | ||
height: "100%", | ||
type: "radialBar", | ||
events: { | ||
dataPointSelection: function ( | ||
event, | ||
chartContext, | ||
payload | ||
) { | ||
console.log(payload); | ||
|
||
if (!onBarClickEventName) { | ||
return; | ||
} | ||
|
||
const bar = data[payload.dataPointIndex]; | ||
console.log(bar); | ||
|
||
component.call("onBarClick", bar); | ||
}, | ||
}, | ||
}, | ||
plotOptions: { | ||
radialBar: { | ||
dataLabels: { | ||
total: { | ||
show: showTotal, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
this.chart = new ApexCharts( | ||
this.$refs.container, | ||
mergedOptionsWithJsonConfig(options, jsonConfig) | ||
); | ||
|
||
this.chart.render(); | ||
}, | ||
}; | ||
}; | ||
|
||
export default radialChart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<div | ||
style="width: 100%; height: 100%;" | ||
x-data="{ ...livewireChartsRadialChart() }" | ||
x-init="init()" | ||
> | ||
<div wire:ignore x-ref="container"></div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Asantibanez\LivewireCharts\Charts; | ||
|
||
use Asantibanez\LivewireCharts\Models\RadialChartModel; | ||
use Livewire\Component; | ||
|
||
/** | ||
* Class LivewireRadialChart | ||
* @package Asantibanez\LivewireCharts\Charts | ||
*/ | ||
class LivewireRadialChart extends Component | ||
{ | ||
public $radialChartModel; | ||
|
||
public function mount(RadialChartModel $radialChartModel) | ||
{ | ||
$this->radialChartModel = $radialChartModel->toArray(); | ||
} | ||
|
||
public function onBarClick($bar): void | ||
{ | ||
$onBarClickEventName = data_get($this->radialChartModel, 'onBarClickEventName'); | ||
|
||
if ($onBarClickEventName === null) { | ||
return; | ||
} | ||
|
||
$this->dispatch($onBarClickEventName, $bar); | ||
} | ||
|
||
public function render() | ||
{ | ||
return view('livewire-charts::livewire-radial-chart'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
|
||
namespace Asantibanez\LivewireCharts\Models; | ||
|
||
/** | ||
* Class RadialChartModel | ||
* @package Asantibanez\LivewireCharts\Models | ||
*/ | ||
class RadialChartModel extends BaseChartModel | ||
{ | ||
public $data; | ||
public $showTotal; | ||
public $onBarClickEventName; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
|
||
$this->data = collect(); | ||
|
||
$this->showTotal = true; | ||
} | ||
|
||
public function showTotal(): self | ||
{ | ||
$this->showTotal = true; | ||
return $this; | ||
} | ||
|
||
public function hideTotal(): self | ||
{ | ||
$this->showTotal = false; | ||
return $this; | ||
} | ||
|
||
public function addBar($title, $value, $color = null, $extras = []): self | ||
{ | ||
$this->data->push([ | ||
'title' => $title, | ||
'value' => $value, | ||
'extras' => $extras, | ||
]); | ||
|
||
$this->addColor($color); | ||
|
||
return $this; | ||
} | ||
|
||
public function withOnBarClickEvent($onBarClickEvent): self | ||
{ | ||
$this->onBarClickEventName = $onBarClickEvent; | ||
|
||
return $this; | ||
} | ||
|
||
public function toArray() | ||
{ | ||
return array_merge(parent::toArray(), [ | ||
'data' => $this->data->toArray(), | ||
'showTotal' => $this->showTotal, | ||
'onBarClickEventName' => $this->onBarClickEventName, | ||
]); | ||
} | ||
|
||
public function fromArray($array) | ||
{ | ||
parent::fromArray($array); | ||
|
||
$this->data = collect(data_get($array, 'data', [])); | ||
|
||
$this->showTotal = data_get($array, 'showTotal', true); | ||
|
||
$this->onBarClickEventName = data_get($array, 'onBarClickEventName', null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
|
||
use Asantibanez\LivewireCharts\Charts\LivewireRadialChart; | ||
use Asantibanez\LivewireCharts\Tests\TestCase; | ||
use Livewire\Features\SupportTesting\Testable; | ||
use Livewire\Livewire; | ||
|
||
class LivewireRadialChartTest extends TestCase | ||
{ | ||
private function buildComponent() : Testable | ||
{ | ||
return Livewire::test(LivewireRadialChart::class); | ||
} | ||
|
||
/** @test */ | ||
public function can_build_component() | ||
{ | ||
//Act | ||
$component = $this->buildComponent(); | ||
|
||
//Assert | ||
$this->assertNotNull($component); | ||
} | ||
|
||
/** @test */ | ||
public function should_emit_event_if_present() | ||
{ | ||
//Arrange | ||
$component = $this->buildComponent(); | ||
|
||
$radialChartModel = $component->radialChartModel; | ||
|
||
data_set($radialChartModel, 'onBarClickEventName', 'custom-event'); | ||
|
||
$component->set('radialChartModel', $radialChartModel); | ||
|
||
//Act | ||
$component->runAction('onBarClick', []); | ||
|
||
//Assert | ||
$component->assertDispatched('custom-event'); | ||
} | ||
} |