42 Commits

Author SHA1 Message Date
Andrés Santibáñez
d58465084e Merge pull request #105 from redsquirrelstudio/livewire-3-event-fix
Changed 'emit' functions in Area,Line, and Radar chart to 'dispatch' …
2023-11-07 14:50:23 -05:00
Andrés Santibáñez
5a222abdbf Merge pull request #108 from stijnvanouplines/master
Added missing jsonConfig
2023-11-07 14:40:56 -05:00
Stijn Vanouplines
af44417115 Added missing jsonConfig 2023-10-27 17:37:09 +02:00
Lewis Raggett
4c70acd41c Changed 'emit' functions in Area,Line, and Radar chart to 'dispatch' to work with Livewire 3 2023-10-05 11:41:49 +01:00
asantibanez
ca044277ee Updated README 2023-08-24 10:04:02 -05:00
asantibanez
badbd7d872 Fixed README 2023-07-25 17:22:42 -05:00
asantibanez
802416ee7a v3.0.0 released 2023-07-25 17:14:18 -05:00
asantibanez
d957e698bb Updated build 2023-07-25 17:10:45 -05:00
asantibanez
46d82cf6bc Refactored how event was triggered 2023-07-25 17:07:51 -05:00
asantibanez
1f26a9ed39 Updated README 2023-07-25 16:58:00 -05:00
asantibanez
55d76bcecd Fixed error dispatching 2023-07-25 16:51:53 -05:00
asantibanez
5034771e57 Added helper methods 2023-07-25 16:51:45 -05:00
asantibanez
6b2667f590 Changed properties visibility 2023-07-25 16:51:35 -05:00
asantibanez
c5784b0ced Added helper methods for PieChartModel 2023-07-25 16:28:59 -05:00
asantibanez
2d9bcfe480 Fixed event dispatching. Added tests. 2023-07-25 16:19:04 -05:00
asantibanez
aa822000af Made properties private so everything is accessed via methods 2023-07-25 16:18:48 -05:00
asantibanez
682b282fe8 Updated dependencies 2023-07-25 16:18:19 -05:00
asantibanez
de936c77d6 Fixed arguments 2023-07-25 16:18:03 -05:00
asantibanez
17efda3ed0 Merge branch 'master' of github.com:asantibanez/livewire-charts 2023-07-25 13:50:56 -05:00
Andrés Santibáñez
25b13b3573 Merge pull request #50 from syntaxlexx/theme-mods
Feat: added theme setting options - dark mode, palette
2023-07-25 14:48:22 -04:00
asantibanez
30eae50c04 Bumped up CI 2023-07-25 13:43:41 -05:00
asantibanez
c79dfeb0ab Refactored tests with new Testable class 2023-07-25 13:42:43 -05:00
asantibanez
3dcc97bef3 Started Livewire v3 support 2023-07-25 13:42:29 -05:00
/SyntaxLexx
c408b639e9 Merge branch 'master' into theme-mods 2023-06-10 03:44:07 +03:00
asantibanez
ac1e6130d4 Updated export 2023-02-01 18:32:57 -05:00
asantibanez
756781c3a9 Added ability to add custom configs to pie charts 2023-02-01 18:32:06 -05:00
asantibanez
93dbaffc61 Added custom configs to Column Chart 2023-02-01 18:28:06 -05:00
asantibanez
6926a504b1 Added deepmerge dependency 2023-02-01 18:24:15 -05:00
asantibanez
40828df23e Added ability to specify custom json config to charts 2023-02-01 18:23:53 -05:00
asantibanez
9f9554314e v2.5.0 released 2023-02-01 13:16:56 -05:00
asantibanez
c7997758bd Added ability to format value via "extras.formatted" 2023-02-01 13:13:28 -05:00
asantibanez
5bc85376a3 Extracted title 2023-02-01 13:13:28 -05:00
Andrés Santibáñez
e6c34e2d49 Merge pull request #83 from laravel-shift/l10-compatibility
Laravel 10.x Compatibility
2023-02-01 12:40:17 -05:00
asantibanez
66ce742311 v2.4.2 released 2023-02-01 12:38:11 -05:00
asantibanez
cd56963f6e Extracted value into variable 2023-02-01 12:35:21 -05:00
asantibanez
ce43d0b97c Fixed column title 2023-02-01 12:35:10 -05:00
Shift
d1ce67f9e5 Bump dependencies for Laravel 10 2023-01-31 19:47:59 +00:00
Lexx YungCarter
1e04ebf6d9 feat: merged with master 2022-10-17 16:02:42 +03:00
asantibanez
072a98da5c Updated CHANGELOG 2022-02-18 16:47:04 -05:00
asantibanez
27198d12b8 Fixed xAxis auto-labels for MultiColumnChart 2022-02-18 16:43:46 -05:00
Lexx YungCarter
9ad481000c fix: typo in palette 2021-08-01 05:16:16 +03:00
Lexx YungCarter
fe0c62be75 feat: added theme setting options - dark mode, pallates 2021-08-01 05:13:49 +03:00
34 changed files with 16700 additions and 2070 deletions

View File

@@ -15,7 +15,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
php-version: '8.1'
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd, exif, imagick, bcmath
coverage: none

View File

@@ -1,5 +1,24 @@
# Changelog
## 3.0.0 - 2023-07-25
- Added Livewire v3 support
- Added ability to set chart theme (thanks to @syntaxlexx)
- Added ability to configure chart via JSON properties
## 2.5.0 - 2023-02-01
- Added tooltip override via `extras.tooltip`
## 2.4.2 - 2023-02-01
- Fixed hover title on column chart
## 2.4.1 - 2022-02-18
- Fixed xAxis auto-categories for MultiColumnChart
## 2.4.0 - 2022-02-14
- Added Radar Chart (thanks to @AlexHupe)
- Added Tree Map Chart
- Added "donut" type to Pie Chart (thanks to @nicko170)
## 2.3.0 - 2021-02-12
- Added `init()` method for better boot and $wire hoisting
- Added color customization for columns, multiline and pie chart

113
README.md
View File

@@ -19,21 +19,19 @@ You can install the package via composer:
composer require asantibanez/livewire-charts
```
Next, you must export the package public scripts. To do this run `php artisan vendor:publish`
and export `livewire-charts:public`. This command will export a `vendor/livewire-charts` folder under the
`public` directory of your app.
Next, you must export the package public scripts. To do this run
```bash
php artisan vendor:publish --tag=livewire-charts:public
```
php artisan livewire-charts:install
```
> Note: This last step can also be done using `livewire-charts:install` command in the artisan console.
This command will export a `vendor/livewire-charts` folder under the `public` directory of your app which is used
by the `@livewireChartsScripts` directive. More on this topic later.
## Requirements
This package requires the following packages/libraries to work:
- `Laravel Livewire v2` (https://laravel-livewire.com/)
- `Alpine Js` (https://github.com/alpinejs/alpine)
- `Laravel Livewire v3` (https://livewire.laravel.com/)
- `Apex Charts` (https://apexcharts.com/)
Please follow each package/library instructions on how to set them properly in your project.
@@ -75,9 +73,11 @@ With `$columnChartModel` at hand, we pass it to our `LivewireColumnChart` compon
```blade
<livewire:livewire-column-chart
{{-- key="{{ $columnChartModel->reactiveKey() }}" --}}
:column-chart-model="$columnChartModel"
/>
```
>Note: Optionally add "key" to enable props reactivity if needed 💪
Next, include the `@livewireChartsScripts` directive next to your other app scripts
@@ -126,10 +126,12 @@ $columnChartModel =
We can listen to the `onClickEvent` registering a listener in any other Livewire component.
```php
protected $listeners = [
'onColumnClick' => 'handleOnColumnClick',
];
```
#[On('onColumnClick')]
public function handleOnColumnClick($event)
{
// Handle event
}
```
## "Reactive" Charts
@@ -168,20 +170,21 @@ for each type of chart.
### All
| Method | Description |
|--------|-------------|
| setTitle(string $title) | Sets chart title |
| setAnimated(boolean $animated) | Enables/disables animation |
| setDataLabelsEnabled(boolean $enabled) | Enables/disables data labels |
| withDataLabels() | Enables data labels |
| withoutDataLabels() | Disables data labels |
| withLegend() | Shows legends |
| withoutLegend() | Hides legends |
| setColors(array $colors) | Set colors for chart |
| addColors(string $color) | Append $color to $colors set |
| setXAxisCategories(array $categories) | Enables custom categories for chart X Axis |
| sparklined() | Enables Apex Charts sparkline feature |
| setSparklineEnabled(boolean $isEnabled) | Enables/Disables Apex Charts sparkline feature |
| Method | Description |
|-----------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------|
| setTitle(string $title) | Sets chart title |
| setAnimated(boolean $animated) | Enables/disables animation |
| setDataLabelsEnabled(boolean $enabled) | Enables/disables data labels |
| withDataLabels() | Enables data labels |
| withoutDataLabels() | Disables data labels |
| withLegend() | Shows legends |
| withoutLegend() | Hides legends |
| setColors(array $colors) | Set colors for chart |
| addColors(string $color) | Append $color to $colors set |
| setXAxisCategories(array $categories) | Enables custom categories for chart X Axis |
| sparklined() | Enables Apex Charts sparkline feature |
| setSparklineEnabled(boolean $isEnabled) | Enables/Disables Apex Charts sparkline feature |
| setJsonConfig(array $config) | Adds extra customization to charts by passing Apex charts properties keys and values. Nested keys must be added using dot (.) notation |
### LivewireLineChart
@@ -196,25 +199,28 @@ for each type of chart.
### LivewireColumnChart
| Method | Description |
|--------|-------------|
| setOpacity(int $opacity) | Sets columns' opacity |
| setColumnWidth(int $columnWidth) | Sets columns' width |
| setHorizontal(boolean $value) | Sets columns or bars |
| multiColumn() | Sets chart to display multiple column series |
| singleColumn() | Sets chart to display a single column series |
| stacked() | Sets chart to display column series stacked |
| addColumn(string $title, double $value, string $color, array $extras = []) | Adds a column to the chart with the specified color. `$extras` is forwarded on click event |
| addSeriesColumn(string $seriesName, string $title, double $value, array $extras = []) | Adds a column to a multicolumn chart. `$extras` is forwarded on click event |
| onColumnClickEventName(string $eventName) | Event Name that will be fired when a column of the chart is clicked |
| Method | Description |
|---------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------|
| setOpacity(int $opacity) | Sets columns' opacity |
| setColumnWidth(int $columnWidth) | Sets columns' width |
| setHorizontal() | Displays columns horizontally |
| setVertical() | Displays columns vertically |
| multiColumn() | Sets chart to display multiple column series |
| singleColumn() | Sets chart to display a single column series |
| stacked() | Sets chart to display column series stacked |
| addColumn(string $title, double $value, string $color, array $extras = []) | Adds a column to the chart with the specified color. `$extras` is forwarded on click event |
| addSeriesColumn(string $seriesName, string $title, double $value, array $extras = []) | Adds a column to a multicolumn chart. `$extras` is forwarded on click event |
| withOnColumnClickEventName(string $eventName) | Event Name that will be fired when a column of the chart is clicked |
### LivewirePieChart
| Method | Description |
|--------|-------------|
| setOpacity(int $opacity) | Sets slices' opacity |
| Method | Description |
|---------------------------------------------------------------------------|-------------------------------------------------------------------------------------------|
| setOpacity(int $opacity) | Sets slices' opacity |
| asPie() | Displays chart as pie |
| asDonut() | Displays chart as donut |
| addSlice(string $title, double $value, string $color, array $extras = []) | Adds a slice to the chart with the specified color. `$extras` is forwarded on click event |
| withOnSliceClickEvent(string $eventName) | Event Name that will be fired when a column of the chart is clicked |
| withOnSliceClickEvent(string $eventName) | Event Name that will be fired when a column of the chart is clicked |
### LivewireAreaChart
@@ -251,6 +257,31 @@ inside your resources folder. You can then adjust imports as you see fit in your
>Note: You must remove @livewireChartsScripts directive so it doesn't clash with the scripts in your build system.
## Advanced Usage - Custom Json Configs
The `setJsonConfig()` method on every chart allows adding custom Apex properties not provided first hand by the package.
This means that any chart property supported by Apex chart can be passed down using this method.
```php
$chart->setJsonConfig([
'plotOptions.pie.startAngle' => -90,
'plotOptions.pie.endAngle' => 90,
'plotOptions.pie.offsetY' => 10,
'grid.padding.bottom' => -180,
]);
```
>Note: If you want to add nested properties, you can use dot (.) notation for this matter.
You can even pass down simple JS functions. For example, for formatting you can pass down your own closure
```php
$chart->setJsonConfig([
'tooltip.y.formatter' => '(val) => `$${val} million dollars baby!`'
])
```
## Troubleshooting
Chart components must be placed inside a container with fixed height. This is because the chart will use all the given

View File

@@ -17,12 +17,12 @@
],
"require": {
"php": "^7.2|^7.3|^7.4|^8.0",
"illuminate/support": "^7.0|^8.0|^9.0",
"livewire/livewire": "^2.3"
"illuminate/support": "^7.0|^8.0|^9.0|^10.0",
"livewire/livewire": "^3.0"
},
"require-dev": {
"orchestra/testbench": "^5.0|^6.0|^7.0",
"phpunit/phpunit": "^8.0"
"orchestra/testbench": "^5.0|^6.0|^7.0|^8.0",
"phpunit/phpunit": "^8.0|^9.5.10"
},
"autoload": {
"psr-4": {
@@ -37,7 +37,6 @@
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
},
"config": {
"sort-packages": true
@@ -51,5 +50,7 @@
"LivewireCharts": "Asantibanez\\LivewireCharts\\LivewireChartsFacade"
}
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}

18182
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -12,5 +12,8 @@
"cross-env": "^7.0.2",
"laravel-mix": "^5.0.9",
"vue-template-compiler": "^2.6.12"
},
"dependencies": {
"deepmerge": "^2.2.1"
}
}

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +1,6 @@
/*!
* ApexCharts v3.22.2
* (c) 2018-2020 Juned Chhipa
* ApexCharts v3.41.0
* (c) 2018-2023 ApexCharts
* Released under the MIT License.
*/

View File

@@ -1,3 +1,4 @@
import { mergedOptionsWithJsonConfig } from './helpers'
const areaChart = () => {
return {
@@ -20,6 +21,7 @@ const areaChart = () => {
const onPointClickEventName = component.get('areaChartModel.onPointClickEventName')
const data = component.get('areaChartModel.data');
const sparkline = component.get('areaChartModel.sparkline');
const jsonConfig = component.get('areaChartModel.jsonConfig');
const categories = component.get('areaChartModel.xAxis.categories').length > 0
? component.get('areaChartModel.xAxis.categories')
@@ -83,9 +85,12 @@ const areaChart = () => {
bottom: 0,
}
},
theme: component.get('areaChartModel.theme') || {},
};
this.chart = new ApexCharts(this.$refs.container, options);
this.chart = new ApexCharts(this.$refs.container, mergedOptionsWithJsonConfig(options, jsonConfig));
this.chart.render();
}
}

View File

@@ -1,3 +1,4 @@
import { mergedOptionsWithJsonConfig } from './helpers'
const columnChart = () => {
return {
@@ -14,6 +15,7 @@ const columnChart = () => {
this.chart.destroy()
}
const title = component.get('columnChartModel.title')
const animated = component.get('columnChartModel.animated') || false;
const onColumnClickEventName = component.get('columnChartModel.onColumnClickEventName')
const dataLabels = component.get('columnChartModel.dataLabels') || {};
@@ -22,9 +24,13 @@ const columnChart = () => {
const grid = component.get('columnChartModel.grid');
const columnWidth = component.get('columnChartModel.columnWidth');
const horizontal = component.get('columnChartModel.horizontal');
const jsonConfig = component.get('columnChartModel.jsonConfig');
const data = component.get('columnChartModel.data');
const series = [{ data: data.map(item => item.value)}]
const series = [{
name: title,
data: data.map(item => item.value)
}]
const options = {
series: series,
@@ -80,13 +86,24 @@ const columnChart = () => {
yaxis: {
title: {
text: component.get('columnChartModel.title'),
text: title,
}
},
fill: {
opacity: component.get('columnChartModel.opacity') || 0.5
},
theme: component.get('columnChartModel.theme') || {},
tooltip: {
y: {
formatter: function(value, series) {
return data[series.dataPointIndex].extras.tooltip || value;
}
}
},
};
const colors = component.get('columnChartModel.colors');
@@ -95,7 +112,7 @@ const columnChart = () => {
options['colors'] = colors
}
this.chart = new ApexCharts(this.$refs.container, options);
this.chart = new ApexCharts(this.$refs.container, mergedOptionsWithJsonConfig(options, jsonConfig));
this.chart.render();
}
}

27
resources/js/helpers.js Normal file
View File

@@ -0,0 +1,27 @@
import deepmerge from 'deepmerge'
export const addPathToObjectWithValue = (obj, path, value) => {
const pList = path.split('.');
const key = pList.pop();
const pointer = pList.reduce((accumulator, currentValue) => {
if (accumulator[currentValue] === undefined) accumulator[currentValue] = {};
return accumulator[currentValue];
}, obj);
pointer[key] = value;
return obj;
}
export const mergedOptionsWithJsonConfig = (options, jsonConfig) => {
const customOptions = Object.keys(jsonConfig)
.reduce(function (obj, key) {
const value = jsonConfig[key]
if (typeof value === 'string' && value.includes('function')) {
return addPathToObjectWithValue(obj, key, eval('(' + value + ')'))
}
return addPathToObjectWithValue(obj, key, eval(value))
}, {})
return deepmerge(options, customOptions)
}

View File

@@ -1,3 +1,4 @@
import { mergedOptionsWithJsonConfig } from './helpers'
const lineChart = () => {
return {
@@ -20,6 +21,7 @@ const lineChart = () => {
const data = component.get('lineChartModel.data');
const onPointClickEventName = component.get('lineChartModel.onPointClickEventName');
const sparkline = component.get('lineChartModel.sparkline');
const jsonConfig = component.get('lineChartModel.jsonConfig');
const series = [{
name: title,
@@ -61,6 +63,8 @@ const lineChart = () => {
stroke: component.get('lineChartModel.stroke') || {},
theme: component.get('lineChartModel.theme') || {},
title: {
text: title,
align: 'center'
@@ -96,9 +100,17 @@ const lineChart = () => {
}
)
},
tooltip: {
y: {
formatter: function(value, series) {
return data[series.dataPointIndex].extras.tooltip || value;
}
}
},
};
this.chart = new ApexCharts(this.$refs.container, options);
this.chart = new ApexCharts(this.$refs.container, mergedOptionsWithJsonConfig(options, jsonConfig));
this.chart.render();
}
}

View File

@@ -1,3 +1,4 @@
import { mergedOptionsWithJsonConfig } from './helpers'
const multiColumnChart = () => {
return {
@@ -14,6 +15,7 @@ const multiColumnChart = () => {
this.chart.destroy()
}
const title = component.get('columnChartModel.title')
const stacked = component.get('columnChartModel.isStacked');
const animated = component.get('columnChartModel.animated');
const onColumnClickEventName = component.get('columnChartModel.onColumnClickEventName')
@@ -23,6 +25,7 @@ const multiColumnChart = () => {
const grid = component.get('columnChartModel.grid');
const columnWidth = component.get('columnChartModel.columnWidth');
const horizontal = component.get('columnChartModel.horizontal');
const jsonConfig = component.get('columnChartModel.jsonConfig');
const data = component.get('columnChartModel.data');
const series = Object.keys(data)
@@ -33,7 +36,7 @@ const multiColumnChart = () => {
const categories = component.get('columnChartModel.xAxis.categories').length > 0
? component.get('columnChartModel.xAxis.categories')
: []
: data[series[0].name].map(item => item.title)
;
const options = {
@@ -81,13 +84,16 @@ const multiColumnChart = () => {
yaxis: {
title: {
text: component.get('columnChartModel.title'),
text: title,
}
},
fill: {
opacity: component.get('columnChartModel.opacity'),
},
theme: component.get('columnChartModel.theme') || {},
};
const colors = component.get('columnChartModel.colors');
@@ -96,7 +102,7 @@ const multiColumnChart = () => {
options['colors'] = colors
}
this.chart = new ApexCharts(this.$refs.container, options);
this.chart = new ApexCharts(this.$refs.container, mergedOptionsWithJsonConfig(options, jsonConfig));
this.chart.render();
}
}

View File

@@ -1,3 +1,4 @@
import { mergedOptionsWithJsonConfig } from './helpers'
const multiLineChart = () => {
return {
@@ -20,6 +21,7 @@ const multiLineChart = () => {
const data = component.get('lineChartModel.data');
const onPointClickEventName = component.get('lineChartModel.onPointClickEventName');
const sparkline = component.get('lineChartModel.sparkline');
const jsonConfig = component.get('lineChartModel.jsonConfig');
const series = Object.keys(data).map(key => {
return {
@@ -74,6 +76,8 @@ const multiLineChart = () => {
},
yaxis: component.get('lineChartModel.yAxis') || {},
theme: component.get('lineChartModel.theme') || {},
};
const colors = component.get('lineChartModel.colors');
@@ -82,7 +86,7 @@ const multiLineChart = () => {
options['colors'] = colors
}
this.chart = new ApexCharts(this.$refs.container, options);
this.chart = new ApexCharts(this.$refs.container, mergedOptionsWithJsonConfig(options, jsonConfig));
this.chart.render();
}
}

View File

@@ -1,3 +1,4 @@
import {mergedOptionsWithJsonConfig} from './helpers'
const pieChart = () => {
return {
@@ -21,6 +22,7 @@ const pieChart = () => {
const data = component.get('pieChartModel.data')
const sparkline = component.get('pieChartModel.sparkline')
const type = component.get('pieChartModel.type')
const jsonConfig = component.get('pieChartModel.jsonConfig');
const options = {
series: data.map(item => item.value),
@@ -73,6 +75,16 @@ const pieChart = () => {
],
legend: component.get('pieChartModel.legend') || {},
theme: component.get('pieChartModel.theme') || {},
tooltip: {
y: {
formatter: function(value, series) {
return data[series.dataPointIndex].extras.tooltip || value;
}
}
},
};
const colors = component.get('pieChartModel.colors');
@@ -81,7 +93,7 @@ const pieChart = () => {
options['colors'] = colors
}
this.chart = new ApexCharts(this.$refs.container, options);
this.chart = new ApexCharts(this.$refs.container, mergedOptionsWithJsonConfig(options, jsonConfig));
this.chart.render();
}
}

View File

@@ -1,3 +1,4 @@
import { mergedOptionsWithJsonConfig } from './helpers'
const radarChart = () => {
return {
@@ -21,6 +22,7 @@ const radarChart = () => {
const data = component.get('radarChartModel.data');
const sparkline = component.get('radarChartModel.sparkline');
const colors = component.get('radarChartModel.colors');
const jsonConfig = component.get('radarChartModel.jsonConfig');
const series = Object.keys(data)
.map(seriesName => ({
@@ -88,7 +90,7 @@ const radarChart = () => {
};
this.chart = new ApexCharts(this.$refs.container, options);
this.chart = new ApexCharts(this.$refs.container, mergedOptionsWithJsonConfig(options, jsonConfig));
this.chart.render();
}
}

View File

@@ -1,3 +1,4 @@
import { mergedOptionsWithJsonConfig } from './helpers'
const treeMapChart = () => {
return {
@@ -21,6 +22,7 @@ const treeMapChart = () => {
const data = component.get('treeMapChartModel.data');
const colors = component.get('treeMapChartModel.colors');
const enableShades = component.get('treeMapChartModel.enableShades');
const jsonConfig = component.get('treeMapChartModel.jsonConfig');
const series = Object.keys(data)
.map(seriesName => ({
@@ -54,7 +56,7 @@ const treeMapChart = () => {
const block = data[series[seriesIndex].name][dataPointIndex]
component.emit(onBlockClickEventName, block)
component.call('onBlockClick', block)
},
}
},
@@ -69,7 +71,7 @@ const treeMapChart = () => {
colors: colors,
};
this.chart = new ApexCharts(this.$refs.container, options);
this.chart = new ApexCharts(this.$refs.container, mergedOptionsWithJsonConfig(options, jsonConfig));
this.chart.render();
}
}

View File

@@ -26,7 +26,7 @@ class LivewireAreaChart extends Component
return;
}
$this->emit($onPointClickEventName, $point);
$this->dispatch($onPointClickEventName, $point);
}
public function render()

View File

@@ -26,7 +26,7 @@ class LivewireColumnChart extends Component
return;
}
$this->emit($onColumnClickEventName, $column);
$this->dispatch($onColumnClickEventName, $column);
}
public function render()

View File

@@ -26,7 +26,7 @@ class LivewireLineChart extends Component
return;
}
$this->emit($onPointClickEventName, $point);
$this->dispatch($onPointClickEventName, $point);
}
public function render()

View File

@@ -26,7 +26,7 @@ class LivewirePieChart extends Component
return;
}
$this->emit($onSliceClickEventName, $slice);
$this->dispatch($onSliceClickEventName, $slice);
}
public function render()

View File

@@ -26,7 +26,7 @@ class LivewireRadarChart extends Component
return;
}
$this->emit($onPointClickEventName, $point);
$this->dispatch($onPointClickEventName, $point);
}
public function render()

View File

@@ -18,6 +18,17 @@ class LivewireTreeMapChart extends Component
$this->treeMapChartModel = $treeMapChartModel->toArray();
}
public function onBlockClick($block)
{
$onBlockClickEventName = data_get($this->treeMapChartModel, 'onBlockClickEventName');
if ($onBlockClickEventName === null) {
return;
}
$this->dispatch($onBlockClickEventName, $block);
}
public function render()
{
return view('livewire-charts::livewire-tree-map-chart');

View File

@@ -18,6 +18,8 @@ class BaseChartModel
use HasSparkline;
use HasGrid;
use HasColors;
use HasTheme;
use HasJsonConfig;
public function __construct()
{
@@ -30,6 +32,8 @@ class BaseChartModel
$this->initSparkline();
$this->initGrid();
$this->initColors();
$this->initTheme();
$this->initJsonConfig();
}
public function reactiveKey()
@@ -48,7 +52,9 @@ class BaseChartModel
$this->dataLabelsToArray(),
$this->sparklineToArray(),
$this->gridToArray(),
$this->colorsToArray()
$this->colorsToArray(),
$this->themeToArray(),
$this->jsonConfigToArray(),
);
}
@@ -63,5 +69,7 @@ class BaseChartModel
$this->sparklineFromArray($array);
$this->gridFromArray($array);
$this->colorsFromArray($array);
$this->themeFromArray($array);
$this->jsonConfigFromArray($array);
}
}

View File

@@ -11,19 +11,19 @@ namespace Asantibanez\LivewireCharts\Models;
*/
class ColumnChartModel extends BaseChartModel
{
public $opacity;
private $opacity;
public $columnWidth;
private $columnWidth;
public $horizontal;
private $horizontal;
public $isMultiColumn;
private $isMultiColumn;
public $isStacked;
private $isStacked;
public $onColumnClickEventName;
private $onColumnClickEventName;
public $data;
private $data;
public function __construct()
{
@@ -65,9 +65,16 @@ class ColumnChartModel extends BaseChartModel
return $this;
}
public function setHorizontal($value)
public function setHorizontal()
{
$this->horizontal = $value;
$this->horizontal = true;
return $this;
}
public function setVertical()
{
$this->horizontal = false;
return $this;
}

View File

@@ -0,0 +1,39 @@
<?php
namespace Asantibanez\LivewireCharts\Models;
trait HasJsonConfig
{
private $jsonConfig;
public function setJsonConfig($jsonConfig)
{
$this->jsonConfig = $jsonConfig;
return $this;
}
protected function initJsonConfig()
{
$this->jsonConfig = [];
}
private function defaultJsonConfig()
{
return [];
}
protected function jsonConfigFromArray($array)
{
$this->jsonConfig = data_get($array, 'jsonConfig', $this->defaultJsonConfig());
}
protected function jsonConfigToArray()
{
return [
'jsonConfig' => $this->jsonConfig,
];
}
}

125
src/Models/HasTheme.php Normal file
View File

@@ -0,0 +1,125 @@
<?php
namespace Asantibanez\LivewireCharts\Models;
trait HasTheme
{
private $theme;
private $mode;
private $palette;
/**
* Initialize
*/
public function initTheme()
{
$this->theme = $this->defaultTheme();
}
/**
* set default theme
*
* @return array $theme
*/
private function defaultTheme()
{
return [
'mode' => 'light',
'palette' => 'palette1'
];
}
/**
* set theme mode
*
* @param string $mode
*
* @return $this
*/
public function setThemeMode(string $mode)
{
data_set($this->theme, 'mode', $mode);
return $this;
}
/**
* set theme mode to dark theme
*
* @return $this
*/
public function darkMode()
{
data_set($this->theme, 'mode', 'dark');
return $this;
}
/**
* set theme mode to light theme
*
* @return $this
*/
public function lightMode()
{
data_set($this->theme, 'mode', 'light');
return $this;
}
/**
* set the dark mode option to on or off
*
* @param bool $status = true
*
* @return $this
*/
public function setDarkMode(bool $status = true)
{
data_set($this->theme, 'mode', $status ? 'dark' : 'light');
return $this;
}
/**
* set theme pallete
*
* @param string $pallete
*
* @return $this
*/
public function setThemePalette(string $palette)
{
data_set($this->theme, 'palette', $palette);
return $this;
}
/**
* convert the theme to array
*
* @param string $mode
*
* @return $this
*/
protected function themeToArray()
{
return [
'theme' => $this->theme,
];
}
/**
* set theme from array
*
* @param string $mode
*
* @return $this
*/
protected function themeFromArray($array)
{
$this->theme = data_get($array, 'theme', $this->defaultTheme());
}
}

View File

@@ -9,13 +9,13 @@ namespace Asantibanez\LivewireCharts\Models;
*/
class PieChartModel extends BaseChartModel
{
public $onSliceClickEventName;
private $onSliceClickEventName;
public $opacity;
private $opacity;
public $type;
private $type;
public $data;
private $data;
public function __construct()
{
@@ -44,6 +44,16 @@ class PieChartModel extends BaseChartModel
return $this;
}
public function asPie()
{
return $this->setType('pie');
}
public function asDonut()
{
return $this->setType('donut');
}
public function withOnSliceClickEvent($onSliceClickEventName)
{
$this->onSliceClickEventName = $onSliceClickEventName;

View File

@@ -3,12 +3,12 @@
namespace Asantibanez\LivewireCharts\Tests;
use Asantibanez\LivewireCharts\Charts\LivewireAreaChart;
use Livewire\Features\SupportTesting\Testable;
use Livewire\Livewire;
use Livewire\Testing\TestableLivewire;
class LivewireAreaChartTest extends TestCase
{
private function buildComponent() : TestableLivewire
private function buildComponent() : Testable
{
return Livewire::test(LivewireAreaChart::class);
}

View File

@@ -3,12 +3,12 @@
namespace Asantibanez\LivewireCharts\Tests;
use Asantibanez\LivewireCharts\Charts\LivewireColumnChart;
use Livewire\Features\SupportTesting\Testable;
use Livewire\Livewire;
use Livewire\Testing\TestableLivewire;
class LivewireColumnChartTest extends TestCase
{
private function buildComponent() : TestableLivewire
private function buildComponent() : Testable
{
return Livewire::test(LivewireColumnChart::class);
}
@@ -22,4 +22,23 @@ class LivewireColumnChartTest extends TestCase
//Assert
$this->assertNotNull($component);
}
/** @test */
public function should_emit_event_if_present()
{
//Arrange
$component = $this->buildComponent();
$columnChartModel = $component->columnChartModel;
data_set($columnChartModel, 'onColumnClickEventName', 'custom-event');
$component->set('columnChartModel', $columnChartModel);
//Act
$component->runAction('onColumnClick', []);
//Assert
$component->assertDispatched('custom-event');
}
}

View File

@@ -3,12 +3,12 @@
namespace Asantibanez\LivewireCharts\Tests;
use Asantibanez\LivewireCharts\Charts\LivewireLineChart;
use Livewire\Features\SupportTesting\Testable;
use Livewire\Livewire;
use Livewire\Testing\TestableLivewire;
class LivewireLineChartTest extends TestCase
{
private function buildComponent() : TestableLivewire
private function buildComponent() : Testable
{
return Livewire::test(LivewireLineChart::class);
}

View File

@@ -3,12 +3,12 @@
namespace Asantibanez\LivewireCharts\Tests;
use Asantibanez\LivewireCharts\Charts\LivewirePieChart;
use Livewire\Features\SupportTesting\Testable;
use Livewire\Livewire;
use Livewire\Testing\TestableLivewire;
class LivewirePieChartTest extends TestCase
{
private function buildComponent() : TestableLivewire
private function buildComponent() : Testable
{
return Livewire::test(LivewirePieChart::class);
}
@@ -22,4 +22,23 @@ class LivewirePieChartTest extends TestCase
//Assert
$this->assertNotNull($component);
}
/** @test */
public function should_emit_event_if_present()
{
//Arrange
$component = $this->buildComponent();
$pieChartModel = $component->pieChartModel;
data_set($pieChartModel, 'onSliceClickEventName', 'custom-event');
$component->set('pieChartModel', $pieChartModel);
//Act
$component->runAction('onSliceClick', []);
//Assert
$component->assertDispatched('custom-event');
}
}

View File

@@ -3,12 +3,12 @@
namespace Asantibanez\LivewireCharts\Tests;
use Asantibanez\LivewireCharts\Charts\LivewireRadarChart;
use Livewire\Features\SupportTesting\Testable;
use Livewire\Livewire;
use Livewire\Testing\TestableLivewire;
class LivewireRadarChartTest extends TestCase
{
private function buildComponent() : TestableLivewire
private function buildComponent() : Testable
{
return Livewire::test(LivewireRadarChart::class);
}

View File

@@ -3,12 +3,12 @@
namespace Asantibanez\LivewireCharts\Tests;
use Asantibanez\LivewireCharts\Charts\LivewireTreeMapChart;
use Livewire\Features\SupportTesting\Testable;
use Livewire\Livewire;
use Livewire\Testing\TestableLivewire;
class LivewireTreeMapChartTest extends TestCase
{
private function buildComponent() : TestableLivewire
private function buildComponent() : Testable
{
return Livewire::test(LivewireTreeMapChart::class);
}
@@ -22,4 +22,23 @@ class LivewireTreeMapChartTest extends TestCase
//Assert
$this->assertNotNull($component);
}
/** @test */
public function should_emit_event_if_present()
{
//Arrange
$component = $this->buildComponent();
$treeMapChartModel = $component->treeMapChartModel;
data_set($treeMapChartModel, 'onBlockClickEventName', 'custom-event');
$component->set('treeMapChartModel', $treeMapChartModel);
//Act
$component->runAction('onBlockClick', []);
//Assert
$component->assertDispatched('custom-event');
}
}