Added sparkline feature to charts

This commit is contained in:
Andrés Santibáñez
2020-11-09 08:29:43 -05:00
parent f5b40ec73d
commit 8e9aed484e
8 changed files with 89 additions and 31 deletions

View File

@@ -21,6 +21,7 @@
const dataLabels = component.get('areaChartModel.dataLabels') || {}; const dataLabels = component.get('areaChartModel.dataLabels') || {};
const onPointClickEventName = component.get('areaChartModel.onPointClickEventName') const onPointClickEventName = component.get('areaChartModel.onPointClickEventName')
const data = component.get('areaChartModel.data'); const data = component.get('areaChartModel.data');
const sparkline = component.get('areaChartModel.sparkline');
const categories = component.get('areaChartModel.xAxis.categories').length > 0 const categories = component.get('areaChartModel.xAxis.categories').length > 0
? component.get('areaChartModel.xAxis.categories') ? component.get('areaChartModel.xAxis.categories')
@@ -36,13 +37,13 @@
type: 'area', type: 'area',
height: '100%', height: '100%',
...sparkline,
zoom: { enabled: false }, zoom: { enabled: false },
toolbar: { show: false }, toolbar: { show: false },
animations: { animations: { enabled: animated },
enabled: animated,
},
events: { events: {
markerClick: function(event, chartContext, { dataPointIndex }) { markerClick: function(event, chartContext, { dataPointIndex }) {

View File

@@ -20,6 +20,7 @@
const onColumnClickEventName = component.get('columnChartModel.onColumnClickEventName') const onColumnClickEventName = component.get('columnChartModel.onColumnClickEventName')
const dataLabels = component.get('columnChartModel.dataLabels') || {}; const dataLabels = component.get('columnChartModel.dataLabels') || {};
const data = component.get('columnChartModel.data'); const data = component.get('columnChartModel.data');
const sparkline = component.get('columnChartModel.sparkline');
const options = { const options = {
series: [{ data: data.map(item => item.value)}], series: [{ data: data.map(item => item.value)}],
@@ -28,13 +29,11 @@
type: 'bar', type: 'bar',
height: '100%', height: '100%',
toolbar: { ...sparkline,
show: false
},
animations: { toolbar: { show: false },
enabled: animated,
}, animations: { enabled: animated },
events: { events: {
dataPointSelection: function(event, chartContext, config) { dataPointSelection: function(event, chartContext, config) {

View File

@@ -20,6 +20,7 @@
const dataLabels = component.get('lineChartModel.dataLabels') || {}; const dataLabels = component.get('lineChartModel.dataLabels') || {};
const data = component.get('lineChartModel.data'); const data = component.get('lineChartModel.data');
const onPointClickEventName = component.get('lineChartModel.onPointClickEventName'); const onPointClickEventName = component.get('lineChartModel.onPointClickEventName');
const sparkline = component.get('lineChartModel.sparkline');
const series = [{ const series = [{
name: title, name: title,
@@ -36,15 +37,15 @@
chart: { chart: {
type: 'line', type: 'line',
height: '100%', height: '100%',
zoom: {
enabled: false ...sparkline,
},
toolbar: { zoom: { enabled: false },
show: false
}, toolbar: { show: false },
animations: {
enabled: animated, animations: { enabled: animated },
},
events: { events: {
markerClick: function(event, chartContext, { dataPointIndex }) { markerClick: function(event, chartContext, { dataPointIndex }) {
if (!onPointClickEventName) { if (!onPointClickEventName) {

View File

@@ -21,6 +21,7 @@
const onColumnClickEventName = component.get('columnChartModel.onColumnClickEventName') const onColumnClickEventName = component.get('columnChartModel.onColumnClickEventName')
const dataLabels = component.get('columnChartModel.dataLabels'); const dataLabels = component.get('columnChartModel.dataLabels');
const data = component.get('columnChartModel.data'); const data = component.get('columnChartModel.data');
const sparkline = component.get('columnChartModel.sparkline');
const series = Object.keys(data) const series = Object.keys(data)
.map(seriesName => ({ .map(seriesName => ({
@@ -41,6 +42,8 @@
height: '100%', height: '100%',
stacked: stacked, stacked: stacked,
...sparkline,
toolbar: { show: false }, toolbar: { show: false },
animations: { enabled: animated }, animations: { enabled: animated },

View File

@@ -20,6 +20,7 @@
const dataLabels = component.get('lineChartModel.dataLabels') || {}; const dataLabels = component.get('lineChartModel.dataLabels') || {};
const data = component.get('lineChartModel.data'); const data = component.get('lineChartModel.data');
const onPointClickEventName = component.get('lineChartModel.onPointClickEventName'); const onPointClickEventName = component.get('lineChartModel.onPointClickEventName');
const sparkline = component.get('lineChartModel.sparkline');
const series = Object.keys(data).map(key => { const series = Object.keys(data).map(key => {
return { return {
@@ -38,15 +39,15 @@
chart: { chart: {
type: 'line', type: 'line',
height: '100%', height: '100%',
zoom: {
enabled: false ...sparkline,
},
toolbar: { zoom: { enabled: false },
show: false
}, toolbar: { show: false },
animations: {
enabled: animated, animations: { enabled: animated },
},
events: { events: {
markerClick: function(event, chartContext, { seriesIndex, dataPointIndex }) { markerClick: function(event, chartContext, { seriesIndex, dataPointIndex }) {
if (!onPointClickEventName) { if (!onPointClickEventName) {

View File

@@ -20,6 +20,7 @@
const dataLabels = component.get('pieChartModel.dataLabels') || {} const dataLabels = component.get('pieChartModel.dataLabels') || {}
const onSliceClickEventName = component.get('pieChartModel.onSliceClickEventName') const onSliceClickEventName = component.get('pieChartModel.onSliceClickEventName')
const data = component.get('pieChartModel.data') const data = component.get('pieChartModel.data')
const sparkline = component.get('pieChartModel.sparkline')
const options = { const options = {
series: data.map(item => item.value), series: data.map(item => item.value),
@@ -28,9 +29,9 @@
height: '100%', height: '100%',
type: 'pie', type: 'pie',
animations: { ...sparkline,
enabled: animated,
}, animations: { enabled: animated },
events: { events: {
dataPointSelection: function(event, chartContext, config) { dataPointSelection: function(event, chartContext, config) {

View File

@@ -15,6 +15,7 @@ class BaseChartModel
use HasStroke; use HasStroke;
use HasLegend; use HasLegend;
use HasDataLabels; use HasDataLabels;
use HasSparkline;
public function __construct() public function __construct()
{ {
@@ -24,6 +25,7 @@ class BaseChartModel
$this->initStroke(); $this->initStroke();
$this->initLegend(); $this->initLegend();
$this->initDataLabels(); $this->initDataLabels();
$this->initSparkline();
} }
public function reactiveKey() public function reactiveKey()
@@ -39,7 +41,8 @@ class BaseChartModel
$this->axisToArray(), $this->axisToArray(),
$this->strokeToArray(), $this->strokeToArray(),
$this->legendToArray(), $this->legendToArray(),
$this->dataLabelsToArray() $this->dataLabelsToArray(),
$this->sparklineToArray()
); );
} }
@@ -51,5 +54,6 @@ class BaseChartModel
$this->strokeFromArray($array); $this->strokeFromArray($array);
$this->legendFromArray($array); $this->legendFromArray($array);
$this->dataLabelsFromArray($array); $this->dataLabelsFromArray($array);
$this->sparklineFromArray($array);
} }
} }

View File

@@ -0,0 +1,48 @@
<?php
namespace Asantibanez\LivewireCharts\Models;
trait HasSparkline
{
private $sparkline;
public function setSparklineEnabled($isEnabled)
{
data_set($this->sparkline, 'sparkline.enabled', $isEnabled);
return $this;
}
public function sparklined()
{
return $this->setSparklineEnabled(true);
}
protected function initSparkline()
{
$this->sparkline = $this->defaultSparkline();
}
private function defaultSparkline()
{
return [
'sparkline' => [
'enabled' => false,
],
];
}
protected function sparklineFromArray($array)
{
$this->sparkline = data_get($array, 'sparkline', $this->defaultSparkline());
}
protected function sparklineToArray()
{
return [
'sparkline' => $this->sparkline,
];
}
}