norgeit/nova-bar-metrics

A Laravel Nova tool for bar chart metrics and frequency distributions.

1.02 2020-10-01 04:04 UTC

This package is auto-updated.

Last update: 2024-10-29 06:00:10 UTC


README

License Latest Stable Version Scrutinizer Code Quality Total Downloads Monthly Downloads

This Nova tool lets you:

  • create bar charts
  • create frequency distribution charts (using pie charts and/or bar charts)

Bar Chart Metric Screenshot

screenshot of the backup tool

Frequency Distribution Metric Screenshot (Bar Chart)

screenshot of the backup tool

Frequency Distribution Metric Screenshot (Pie Chart)

screenshot of the backup tool

Bar Chart Metrics

For a bar chart metric, just create a metric class like you normally would for a Partition metric. All the available methods in a Partition metric are also available for BarChartMetric! Instead of extending Partition you would just need to extend BarChartMetric like so:

use Insenseanalytics\NovaBarMetrics\BarChartMetric;

class BrandsPerCategory extends BarChartMetric
{
  public function calculate(Request $request)
  {
    return $this->count($request, BrandCategory::class, 'category_name');
  }
}

You can also use the suffix, prefix, dollars and euros methods like in a TrendMetric in Laravel Nova. Besides this, we also have a precision method to set the precision of the avg metric shown in the top right corner of the bar chart.

Frequency Distributions for Bar Chart Metrics and Partition Metrics

To create a frequency distributions chart, either extend the BarChartMetric class or extend the Partition class and use the trait HasFrequencyDistributions. You can use the distributions helper method to create the frequency distribution chart like so:

Example for BarChartMetric

use Insenseanalytics\NovaBarMetrics\BarChartMetric;

class BrandFacebookFollowers extends BarChartMetric
{
  public function calculate(Request $request)
  {
    return $this->distributions($request, Brand::class, 'facebook_followers', 100000);
  }
}

In the example above, 100000 is the step size to use for the ranges in the frequency distribution and facebook_followers is the column to distribute by ranges.

Instead of providing the step size, you may provide the max number of steps instead using the distributionsWithSteps method and the package would automatically calculate the step size like so:

use Insenseanalytics\NovaBarMetrics\BarChartMetric;

class BrandFacebookFollowers extends BarChartMetric
{
  public function calculate(Request $request)
  {
    return $this->distributionsWithSteps($request, Brand::class, 'facebook_followers', 15);
  }
}

For friendly formatted ranges (K for thousands, M for millions, B for billions), you can use the withFormattedRangeLabels method like so:

public function calculate(Request $request)
{
  return $this->distributions($request, Brand::class, 'facebook_followers', 100000)
              ->withFormattedRangeLabels();
}

Example for Partition Metric

use Laravel\Nova\Metrics\Partition;
use Insenseanalytics\NovaBarMetrics\HasFrequencyDistributions;

class BrandFacebookFollowers extends Partition
{
  use HasFrequencyDistributions;
  
  public function calculate(Request $request)
  {
    return $this->distributions($request, Brand::class, 'facebook_followers', 100000);
  }
}

Requirements & Dependencies

There are no PHP dependencies except the Laravel Nova package. On the frontend JS, this package uses vue, chartist, chartist-plugin-tooltips and laravel-nova, all of which are also used by Nova itself.

Installation

You can install this tool into a Laravel app that uses Nova via composer:

composer require insenseanalytics/nova-bar-metrics

Next, if you do not have package discovery enabled, you need to register the provider in the config/app.php file.

'providers' => [
    ...,
    Insenseanalytics\NovaBarMetrics\NovaBarMetricsServiceProvider::class,
]

Usage

You can use the console command php artisan nova:barmetric <classname> to create new BarMetric classes

Contributing

Contributions are welcome and will be fully credited as long as you use PSR-2, explain the issue/feature that you want to solve/add and back your code up with tests. Happy coding!

License

The MIT License (MIT). Please see License File for more information.