From d2c78a05f8af7076f1442516dca1bd0a3634f3fc Mon Sep 17 00:00:00 2001 From: Navneet Rai Date: Tue, 3 Nov 2020 07:03:15 -0500 Subject: [PATCH 1/8] Add method to get a parametrized list of datasets and helper functions --- src/Traits/HasChart.php | 105 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/src/Traits/HasChart.php b/src/Traits/HasChart.php index 2e6698c..01f41db 100644 --- a/src/Traits/HasChart.php +++ b/src/Traits/HasChart.php @@ -2,6 +2,7 @@ namespace KirschbaumDevelopment\NovaChartjs\Traits; +use Illuminate\Support\Collection; use Illuminate\Database\Eloquent\Relations\MorphMany; use KirschbaumDevelopment\NovaChartjs\Models\NovaChartjsMetricValue; @@ -68,6 +69,110 @@ public function setNovaChartjsMetricValueAttribute($value): void $chartInstance->save(); } + /** + * Return a list of datapoints for each parameters. + * + * @param string $chartName + * @param null|mixed $sortBy + * @param mixed $limit + * + * @return \Illuminate\Support\Collection + */ + public static function getNovaChartjsParameterizedDataSet($chartName = 'default', $sortBy = null, $limit = 0): Collection + { + $parameters = data_get(static::getNovaChartjsSettings(), sprintf('%s.parameters', $chartName)); + + $output = collect(); + + $dataset = collect(static::getNovaChartjsComparisonData($chartName)); + + if (! empty($sortBy)) { + $dataset = $dataset->sortBy($sortBy)->values(); + } + + if ($limit > 0) { + $dataset = $dataset->slice(0, $limit); + } + + foreach ($parameters as $parameter) { + $output->put($parameter, $dataset->pluck(sprintf('novaChartjsComparisonData.%s', $parameter))); + } + + return $output; + } + + /** + * Returns a dataset consistint of max values for each parameter. + * + * @param string $chartName + * @param null|mixed $sortBy + * @param mixed $limit + * + * @return array + */ + public static function getNovaChartjsMaxDataSet($chartName = 'default', $sortBy = null, $limit = 0): array + { + $dataset = static::getNovaChartjsParameterizedDataSet($chartName, $sortBy, $limit); + + return $dataset->map(function (Collection $datpoints) { + return $datpoints->max(); + })->values()->toArray(); + } + + /** + * Returns a dataset consistint of min values for each parameter. + * + * @param string $chartName + * @param null|mixed $sortBy + * @param mixed $limit + * + * @return array + */ + public static function getNovaChartjsMinDataSet($chartName = 'default', $sortBy = null, $limit = 0): array + { + $dataset = static::getNovaChartjsParameterizedDataSet($chartName, $sortBy, $limit); + + return $dataset->map(function (Collection $datpoints) { + return $datpoints->min(); + })->values()->toArray(); + } + + /** + * Returns a dataset consistint of average values for each parameter. + * + * @param string $chartName + * @param null|mixed $sortBy + * @param mixed $limit + * + * @return array + */ + public static function getNovaChartjsAvgDataSet($chartName = 'default', $sortBy = null, $limit = 0): array + { + $dataset = static::getNovaChartjsParameterizedDataSet($chartName, $sortBy, $limit); + + return $dataset->map(function (Collection $datpoints) { + return $datpoints->avg(); + })->values()->toArray(); + } + + /** + * Returns a dataset consistint of median values for each parameter. + * + * @param string $chartName + * @param null|mixed $sortBy + * @param mixed $limit + * + * @return array + */ + public static function getNovaChartjsMedianDataSet($chartName = 'default', $sortBy = null, $limit = 0): array + { + $dataset = static::getNovaChartjsParameterizedDataSet($chartName, $sortBy, $limit); + + return $dataset->map(function (Collection $datpoints) { + return $datpoints->median(); + })->values()->toArray(); + } + /** * Return a list of all models available for comparison to root model. * From b88e7d8ee5109469c88464964be38d2f31064347 Mon Sep 17 00:00:00 2001 From: Navneet Rai Date: Tue, 3 Nov 2020 07:03:23 -0500 Subject: [PATCH 2/8] Updated readme --- README.md | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/README.md b/README.md index 7ba785c..50140c3 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,69 @@ class Employee extends Model implements Chartable // ... } ``` + +### Getting parametrized datasets + +You can get a collection of parametrized dataset for each of the parameter specified in chart settings by calling `getNovaChartjsParameterizedDataSet` method. You can specify the chartName for which you want to fetch the data. You can also sort the collected data by passing optional `field name` using which you want to sort the data and the `number of results` to be considered in sorting. We have added four static methods to provide `max`, `min`, `average` and `median` value datasets using `getNovaChartjsMaxDataSet`, `getNovaChartjsMinDataSet`, `getNovaChartjsAvgDataSet` and `getNovaChartjsMedianDataSet` method. + +You can also right your own customized method using in a similar fashion. + +```php +//... + /** + * Returns a dataset consistint of max values for each parameter. + * + * @param string $chartName + * @param null|mixed $sortBy + * @param mixed $limit + * + * @return array + */ + public static function getNovaChartjsMaxDataSet($chartName = 'default', $sortBy = null, $limit = 0): array + { + $dataset = static::getNovaChartjsParameterizedDataSet($chartName, $sortBy, $limit); + + return $dataset->map(function (Collection $datpoints) { + return $datpoints->max(); + })->values()->toArray(); + } +//... +``` + +You can use these datasets as additional datsets + +```php +use KirschbaumDevelopment\NovaChartjs\Traits\HasChart; +use KirschbaumDevelopment\NovaChartjs\Contracts\Chartable; + +class Employee extends Model implements Chartable +{ + use HasChart; + + //... + + /** + * Return a list of additional datasets added to chart + * + * @return array + */ + public function getAdditionalDatasets(): array + { + return [ + 'default' => [ + [ + 'label' => 'Average Sales', + 'borderColor' => '#f87900', + 'data' => static::getNovaChartjsAvgDataSet('default'), + ], + ] + ]; + } + + // ... +} +``` + You can read more about adding custom datasets in the [official chart.js documentation](https://www.chartjs.org/docs/latest/) ### Creating a range chart From 61ce0497db89ec9b866ab38f2fe353a3084fe199 Mon Sep 17 00:00:00 2001 From: Navneet Rai Date: Thu, 28 Jan 2021 16:09:25 -0500 Subject: [PATCH 3/8] Update src/Traits/HasChart.php Co-authored-by: David VanScott --- src/Traits/HasChart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/HasChart.php b/src/Traits/HasChart.php index 01f41db..a0a84c7 100644 --- a/src/Traits/HasChart.php +++ b/src/Traits/HasChart.php @@ -156,7 +156,7 @@ public static function getNovaChartjsAvgDataSet($chartName = 'default', $sortBy } /** - * Returns a dataset consistint of median values for each parameter. + * Returns a dataset consisting of median values for each parameter. * * @param string $chartName * @param null|mixed $sortBy From 1513f04f11456bc19c94d948edd6830ca9ee2691 Mon Sep 17 00:00:00 2001 From: Navneet Rai Date: Thu, 28 Jan 2021 16:09:30 -0500 Subject: [PATCH 4/8] Update src/Traits/HasChart.php Co-authored-by: David VanScott --- src/Traits/HasChart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/HasChart.php b/src/Traits/HasChart.php index a0a84c7..4d3b1ed 100644 --- a/src/Traits/HasChart.php +++ b/src/Traits/HasChart.php @@ -138,7 +138,7 @@ public static function getNovaChartjsMinDataSet($chartName = 'default', $sortBy } /** - * Returns a dataset consistint of average values for each parameter. + * Returns a dataset consisting of average values for each parameter. * * @param string $chartName * @param null|mixed $sortBy From 3133ab2a004aa7670af1c70b75af522a613db52c Mon Sep 17 00:00:00 2001 From: Navneet Rai Date: Thu, 28 Jan 2021 16:09:37 -0500 Subject: [PATCH 5/8] Update src/Traits/HasChart.php Co-authored-by: David VanScott --- src/Traits/HasChart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/HasChart.php b/src/Traits/HasChart.php index 4d3b1ed..cb540cb 100644 --- a/src/Traits/HasChart.php +++ b/src/Traits/HasChart.php @@ -120,7 +120,7 @@ public static function getNovaChartjsMaxDataSet($chartName = 'default', $sortBy } /** - * Returns a dataset consistint of min values for each parameter. + * Returns a dataset consisting of min values for each parameter. * * @param string $chartName * @param null|mixed $sortBy From 461d84021ef55202f400a52898391a31a13a7414 Mon Sep 17 00:00:00 2001 From: Navneet Rai Date: Thu, 28 Jan 2021 16:09:43 -0500 Subject: [PATCH 6/8] Update src/Traits/HasChart.php Co-authored-by: David VanScott --- src/Traits/HasChart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/HasChart.php b/src/Traits/HasChart.php index cb540cb..6f6f352 100644 --- a/src/Traits/HasChart.php +++ b/src/Traits/HasChart.php @@ -102,7 +102,7 @@ public static function getNovaChartjsParameterizedDataSet($chartName = 'default' } /** - * Returns a dataset consistint of max values for each parameter. + * Returns a dataset consisting of max values for each parameter. * * @param string $chartName * @param null|mixed $sortBy From f68d7326f357bd507560f7148486aed96ae016bf Mon Sep 17 00:00:00 2001 From: Navneet Rai Date: Thu, 28 Jan 2021 16:09:49 -0500 Subject: [PATCH 7/8] Update README.md Co-authored-by: David VanScott --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 50140c3..2c1fc06 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ class Employee extends Model implements Chartable ### Getting parametrized datasets -You can get a collection of parametrized dataset for each of the parameter specified in chart settings by calling `getNovaChartjsParameterizedDataSet` method. You can specify the chartName for which you want to fetch the data. You can also sort the collected data by passing optional `field name` using which you want to sort the data and the `number of results` to be considered in sorting. We have added four static methods to provide `max`, `min`, `average` and `median` value datasets using `getNovaChartjsMaxDataSet`, `getNovaChartjsMinDataSet`, `getNovaChartjsAvgDataSet` and `getNovaChartjsMedianDataSet` method. +You can get a collection of parametrized datasets for each of the parameters specified in chart settings by calling `getNovaChartjsParameterizedDataSet` method. You can specify the chartName for which you want to fetch the data. You can also sort the collected data by passing optional `field name` using which you want to sort the data and the `number of results` to be considered in sorting. We have added four static methods to provide `max`, `min`, `average` and `median` value datasets using `getNovaChartjsMaxDataSet`, `getNovaChartjsMinDataSet`, `getNovaChartjsAvgDataSet` and `getNovaChartjsMedianDataSet` method. You can also right your own customized method using in a similar fashion. From a33e391cdf43f8d343f31c4ca46d5fba705f181b Mon Sep 17 00:00:00 2001 From: Navneet Rai Date: Thu, 28 Jan 2021 16:09:58 -0500 Subject: [PATCH 8/8] Update README.md Co-authored-by: David VanScott --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2c1fc06..fdecdae 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ class Employee extends Model implements Chartable You can get a collection of parametrized datasets for each of the parameters specified in chart settings by calling `getNovaChartjsParameterizedDataSet` method. You can specify the chartName for which you want to fetch the data. You can also sort the collected data by passing optional `field name` using which you want to sort the data and the `number of results` to be considered in sorting. We have added four static methods to provide `max`, `min`, `average` and `median` value datasets using `getNovaChartjsMaxDataSet`, `getNovaChartjsMinDataSet`, `getNovaChartjsAvgDataSet` and `getNovaChartjsMedianDataSet` method. -You can also right your own customized method using in a similar fashion. +You can also write your own customized method using in a similar fashion. ```php //...