dcasia / chartjs-widget

A ChartJs widget for laravel Nova Dashboard.
MIT License
7 stars 5 forks source link

Can't get it to work - Call to a member function resolveWidgetValue() on null #14

Open ndberg opened 4 months ago

ndberg commented 4 months ago

I tried to use your dashboard and followed your example Code, but I always get this error:

"message": "Call to a member function resolveWidgetValue() on null",
"exception": "Error",
"file": "/Volumes/Work/Code/platreform/backend/vendor/digital-creative/nova-dashboard/src/Http/Controllers/WidgetController.php",
"line": 20,

When I analyze the Code in the WidgetController on Line 20, it looks like it does not find the View

   // dump(View::findView($request)) // is always null

  return response()->json([
      'value' => View::findView($request)->resolveWidgetValue($request, $widgetKey), 
  ]);

Thanks for your Help!

Versions: laravel/framework: 10.48.12 laravel/nova: 4.34.2 digital-creative/nova-dashboard: 1.1.3 digital-creative/chartjs-widget: 1.0.2

My Code:

class LeadsDashboard extends Dashboard
{
    /**
     * Get the displayable name of the dashboard.
     */
    public function label(): string
    {
        return 'Leads';
    }

    /**
     * Get the cards for the dashboard.
     */
    public function cards(): array
    {
        return [
            NovaDashboard::make()
                ->addView('Leads Example', function (View $view) {
                    return $view
                        ->icon('window')
                        ->addWidgets([
                            ExampleBarChartWidget::make(),
                        ])
                        ->addFilters([
                            DateRangeFilter::make()
                                ->dividerLabel('<>') // control the divider label in between the inputs
                                ->inputType('week') // supports any html input type
                                ->placeholder('From', 'To') // control the placeholder of the inputs
                                ->fromAttributes([ 'min' => 0 ]) // some inputs type like number accepts more attributes like min/max/step etc..
                                ->toAttributes([ 'max' => 100 ])
                        ]);
                }),
        ];
    }
}

class ExampleBarChartWidget extends BarChartWidget
{
    public function configure(NovaRequest $request): void
    {
        /**
         * These set the title and the button on the top-right if there are multiple "tabs" on this view
         */
        $this->title('Example BarChart');
        $this->buttonTitle('BarChart');
        $this->backgroundColor(dark: '#1e293b', light: '#ffffff');

        $this->padding(top: 30, bottom: 5);

        $this->tooltip([]); // https://www.chartjs.org/docs/latest/configuration/tooltip.html#tooltip
        $this->scales([]);  // https://www.chartjs.org/docs/latest/axes/#axes
        $this->legend([]);  // https://www.chartjs.org/docs/latest/configuration/legend.html#legend
        $this->elements();  // https://www.chartjs.org/docs/latest/configuration/elements.html#elements
    }

    public function value(Filters $filters): array
    {
        return [
            'labels' => Collection::range(0, 5)->map(fn () => fake()->word()),
            'datasets' => Collection::range(0, 5)->map(fn () => [
                'data' => Collection::range(0, 5)->map(fn () => fake()->numberBetween(0, 100)),
            ]),
        ];
    }
}
davide-granello commented 1 week ago

same issue here:

Versions: laravel/framework: 11.27.2 laravel/nova: 4.35.3 digital-creative/nova-dashboard: 1.1.3 digital-creative/chartjs-widget: 1.0.2

Code:

<?php

namespace App\Nova\Widgets;

use DigitalCreative\ChartJsWidget\Charts\BarChartWidget;
use DigitalCreative\NovaDashboard\Filters;
use Illuminate\Support\Collection;
use Laravel\Nova\Http\Requests\NovaRequest;

class WidgetName extends BarChartWidget
{
  public function configure(NovaRequest $request): void
  {
    /**
     * These set the title and the button on the top-right if there are multiple "tabs" on this view
     */
    $this->title(__('Number') . __('Name'));
    $this->buttonTitle('BarChart');
    $this->backgroundColor(dark: '#FFFFFF06', light: '#00000006');

    $this->padding(top: 30, bottom: 5);

    $this->tooltip([]); // https://www.chartjs.org/docs/latest/configuration/tooltip.html#tooltip
    $this->scales([]);  // https://www.chartjs.org/docs/latest/axes/#axes
    $this->legend([]);  // https://www.chartjs.org/docs/latest/configuration/legend.html#legend
    $this->elements();  // https://www.chartjs.org/docs/latest/configuration/elements.html#elements

  }

  public function value(Filters $filters): array
  {
    return [
      'labels' => Collection::range(0, 5)->map(fn () => fake()->word()),
      'datasets' => Collection::range(0, 5)->map(fn () => [
        'data' => Collection::range(0, 5)->map(fn () => fake()->numberBetween(0, 100)),
      ]),
    ];
  }
}