Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get parameterized list of datapoints #46

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
navneetrai marked this conversation as resolved.
Show resolved Hide resolved

You can also right your own customized method using in a similar fashion.
navneetrai marked this conversation as resolved.
Show resolved Hide resolved

```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
Expand Down
105 changes: 105 additions & 0 deletions src/Traits/HasChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace KirschbaumDevelopment\NovaChartjs\Traits;

use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use KirschbaumDevelopment\NovaChartjs\Models\NovaChartjsMetricValue;

Expand Down Expand Up @@ -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.
navneetrai marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string $chartName
* @param null|mixed $sortBy
* @param mixed $limit
*
* @return array
*/
public static function getNovaChartjsMaxDataSet($chartName = 'default', $sortBy = null, $limit = 0): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about trying to make these calls a little more dynamic? Is there a way we could use __callStatic for these since they're almost identical code? Do you think that would be a good idea here or should we avoid that for reasons I'm not thinking about?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dvanscott These calls are mostly convenience methods for end users. These are not used anywhere in our code directly.

These are also supposed to act as a template for end users it they want to add more such methods for their own use. That was one of the reason I wanted to keep it more readable.

{
$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.
navneetrai marked this conversation as resolved.
Show resolved Hide resolved
*
* @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.
navneetrai marked this conversation as resolved.
Show resolved Hide resolved
*
* @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.
navneetrai marked this conversation as resolved.
Show resolved Hide resolved
*
* @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.
*
Expand Down