Skip to content

Commit aa94016

Browse files
committed
Added TreeMap chart
- Added tests
1 parent 4a74606 commit aa94016

File tree

10 files changed

+232
-1
lines changed

10 files changed

+232
-1
lines changed

public/app.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import lineChart from "./lineChart"
66
import multiLineChart from "./multiLineChart"
77
import pieChart from "./pieChart"
88
import radarChart from "./radarChart"
9+
import treeMapChart from "./treeMapChart"
910

1011
window.ApexCharts = ApexCharts
1112
window.livewireChartsAreaChart = areaChart
@@ -15,3 +16,4 @@ window.livewireChartsMultiLineChart = multiLineChart
1516
window.livewireChartsPieChart = pieChart
1617
window.livewireChartsMultiColumnChart = multiColumnChart
1718
window.livewireChartsRadarChart = radarChart
19+
window.livewireChartsTreeMapChart = treeMapChart

resources/js/treeMapChart.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
const treeMapChart = () => {
3+
return {
4+
chart: null,
5+
6+
init() {
7+
setTimeout(() => {
8+
this.drawChart(this.$wire)
9+
}, 0)
10+
},
11+
12+
drawChart(component) {
13+
if (this.chart) {
14+
this.chart.destroy()
15+
}
16+
17+
const title = component.get('treeMapChartModel.title');
18+
const animated = component.get('treeMapChartModel.animated');
19+
const distributed = component.get('treeMapChartModel.distributed');
20+
const onBlockClickEventName = component.get('treeMapChartModel.onBlockClickEventName');
21+
const data = component.get('treeMapChartModel.data');
22+
const colors = component.get('treeMapChartModel.colors');
23+
const enableShades = component.get('treeMapChartModel.enableShades');
24+
25+
const series = Object.keys(data)
26+
.map(seriesName => ({
27+
name: seriesName,
28+
data: data[seriesName].map(item => ({
29+
x: item.title,
30+
y: item.value,
31+
}))
32+
}))
33+
34+
const options = {
35+
series: series,
36+
37+
legend: { show: false },
38+
39+
title: { text: title },
40+
41+
chart: {
42+
height: '100%',
43+
type: 'treemap',
44+
45+
toolbar: {show: false},
46+
47+
animations: {enabled: animated},
48+
49+
events: {
50+
click: function(event, chartContext, {seriesIndex, dataPointIndex}) {
51+
if (!onBlockClickEventName) {
52+
return
53+
}
54+
55+
const block = data[series[seriesIndex].name][dataPointIndex]
56+
57+
component.emit(onBlockClickEventName, block)
58+
},
59+
}
60+
},
61+
62+
plotOptions: {
63+
treemap: {
64+
distributed: distributed,
65+
enableShades: enableShades,
66+
}
67+
},
68+
69+
colors: colors,
70+
};
71+
72+
this.chart = new ApexCharts(this.$refs.container, options);
73+
this.chart.render();
74+
}
75+
}
76+
}
77+
78+
export default treeMapChart
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div
2+
style="width: 100%; height: 100%;"
3+
x-data="{ ...livewireChartsTreeMapChart() }"
4+
x-init="init()"
5+
>
6+
<div wire:ignore x-ref="container"></div>
7+
</div>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Asantibanez\LivewireCharts\Charts;
4+
5+
use Asantibanez\LivewireCharts\Models\TreeMapChartModel;
6+
use Livewire\Component;
7+
8+
/**
9+
* Class LivewireTreeMapChart
10+
* @package Asantibanez\LivewireCharts\Charts
11+
*/
12+
class LivewireTreeMapChart extends Component
13+
{
14+
public $treeMapChartModel;
15+
16+
public function mount(TreeMapChartModel $treeMapChartModel)
17+
{
18+
$this->treeMapChartModel = $treeMapChartModel->toArray();
19+
}
20+
21+
public function render()
22+
{
23+
return view('livewire-charts::livewire-tree-map-chart');
24+
}
25+
}

src/Facades/LivewireCharts.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Asantibanez\LivewireCharts\Models\LineChartModel;
88
use Asantibanez\LivewireCharts\Models\PieChartModel;
99
use Asantibanez\LivewireCharts\Models\RadarChartModel;
10+
use Asantibanez\LivewireCharts\Models\TreeMapChartModel;
1011
use Illuminate\Support\Facades\Facade;
1112

1213
/**
@@ -19,6 +20,7 @@
1920
* @method static AreaChartModel areaChartModel()
2021
* @method static PieChartModel pieChartModel()
2122
* @method static RadarChartModel radarChartModel()
23+
* @method static TreeMapChartModel treeMapChartModel()
2224
*/
2325
class LivewireCharts extends Facade
2426
{

src/LivewireCharts.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Asantibanez\LivewireCharts\Models\LineChartModel;
1010
use Asantibanez\LivewireCharts\Models\PieChartModel;
1111
use Asantibanez\LivewireCharts\Models\RadarChartModel;
12+
use Asantibanez\LivewireCharts\Models\TreeMapChartModel;
1213

1314
class LivewireCharts
1415
{
@@ -50,4 +51,9 @@ public function radarChartModel()
5051
{
5152
return new RadarChartModel();
5253
}
54+
55+
public function treeMapChartModel()
56+
{
57+
return new TreeMapChartModel();
58+
}
5359
}

src/LivewireChartsServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Asantibanez\LivewireCharts\Charts\LivewireLineChart;
88
use Asantibanez\LivewireCharts\Charts\LivewirePieChart;
99
use Asantibanez\LivewireCharts\Charts\LivewireRadarChart;
10+
use Asantibanez\LivewireCharts\Charts\LivewireTreeMapChart;
1011
use Asantibanez\LivewireCharts\Console\InstallCommand;
1112
use Illuminate\Support\Facades\Blade;
1213
use Illuminate\Support\ServiceProvider;
@@ -71,6 +72,7 @@ private function registerComponents()
7172
Livewire::component('livewire-pie-chart', LivewirePieChart::class);
7273
Livewire::component('livewire-area-chart', LivewireAreaChart::class);
7374
Livewire::component('livewire-radar-chart', LivewireRadarChart::class);
75+
Livewire::component('livewire-tree-map-chart', LivewireTreeMapChart::class);
7476
}
7577

7678
private function registerDirectives()

src/Models/TreeMapChartModel.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
4+
namespace Asantibanez\LivewireCharts\Models;
5+
6+
/**
7+
* Class TreeMapChartModel
8+
* @package Asantibanez\LivewireCharts\Models
9+
*/
10+
class TreeMapChartModel extends BaseChartModel
11+
{
12+
use HasTitle;
13+
use HasColors;
14+
15+
public $data;
16+
public $distributed;
17+
public $onBlockClickEventName;
18+
19+
public function __construct()
20+
{
21+
parent::__construct();
22+
23+
$this->data = collect();
24+
25+
$this->distributed = false;
26+
27+
$this->setColors(['#2E93fA']);
28+
}
29+
30+
public function setDistributed($distributed)
31+
{
32+
$this->distributed = $distributed;
33+
34+
return $this;
35+
}
36+
37+
public function addBlock($title, $value, $extras = [])
38+
{
39+
return $this->addSeriesBlock($this->title, $title, $value, $extras);
40+
}
41+
42+
public function addSeriesBlock($seriesName, $title, $value, $extras = [])
43+
{
44+
$series = $this->data->get($seriesName, collect());
45+
46+
$series->push([
47+
'seriesName' => $seriesName,
48+
'title' => $title,
49+
'value' => $value,
50+
'extras' => $extras,
51+
]);
52+
53+
$this->data->put($seriesName, $series);
54+
55+
return $this;
56+
}
57+
58+
public function withOnBlockClickEvent($onBlockClickEventName)
59+
{
60+
$this->onBlockClickEventName = $onBlockClickEventName;
61+
62+
return $this;
63+
}
64+
65+
public function toArray()
66+
{
67+
return array_merge(parent::toArray(), [
68+
'data' => $this->data->toArray(),
69+
'distributed' => $this->distributed,
70+
'onBlockClickEventName' => $this->onBlockClickEventName,
71+
]);
72+
}
73+
74+
public function fromArray($array)
75+
{
76+
parent::fromArray($array);
77+
78+
$this->data = collect(data_get($array, 'data', []));
79+
80+
$this->distributed = data_get($array, 'distributed', false);
81+
82+
$this->onBlockClickEventName = data_get($array, 'onBlockClickEventName', null);
83+
}
84+
}

tests/LivewireTreeMapChartTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Asantibanez\LivewireCharts\Tests;
4+
5+
use Asantibanez\LivewireCharts\Charts\LivewireTreeMapChart;
6+
use Livewire\Livewire;
7+
use Livewire\Testing\TestableLivewire;
8+
9+
class LivewireTreeMapChartTest extends TestCase
10+
{
11+
private function buildComponent() : TestableLivewire
12+
{
13+
return Livewire::test(LivewireTreeMapChart::class);
14+
}
15+
16+
/** @test */
17+
public function can_build_component()
18+
{
19+
//Act
20+
$component = $this->buildComponent();
21+
22+
//Assert
23+
$this->assertNotNull($component);
24+
}
25+
}

0 commit comments

Comments
 (0)