kevinkhill/lavacharts

Injected Lavachart

aiakane16 opened this issue · 3 comments

What Version?

3.1.11

Issue

Injected Lavacharts doesn't trigger the create chart via magic method .
i am using lavachart on my service and injecting my service to controller .

Controller Code (chart creation code)

// Service
// omitted parts of the code 

    public function __construct(Lavacharts $lavacharts){
        $this->lavacharts = $lavacharts;
    }
    public function  generateStatistics(Dataset $dataset) {
         $options = $dataset->options;

         $options['events'] = [
            'ready' => 'getImageCallback'
         ];
       //doesn't throw exception but throws error when get by view chart was not found
        $this->lavacharts->ColumnChart($dataset->title,$this->datatable, $options);
       // using facade works 
       // Lava::ColumnChart($dataset->title, $this->datatable,$options);
    }


// Controller
class DatasetController extends Controller
{

    protected $response;

    protected $statisticsService;

    public function __construct(ResponseFactory $response, StatisticsServiceInterface $statisticsService)
    {
        $this->statisticsService = $statisticsService;
        $this->response= $response;
    }

   public function show(Dataset $dataset)
    {

        $this->statisticsService->generateStatistics($dataset);

        return $this->response->view('dataset.show',[

            'dataset' => $dataset,

            'data' => $this->statisticsService->getData()

        ]);

    }
}

View Code

  <?= Lava::render('ColumnChart', $dataset->title, 'chart') ?>

<div class="body">
   <div id="chart"></div>
 </div>

Maybe it's because you are still using the facade in the view?

Try passing your lavacharts object to the view as 'lava' =>$lavacharts and replace Lava:: with $lava->

oh , i thought it is a singleton with central storage for all charts.
i just have to extract lavacharts instance from the service but it is working now, thanks.

It is designed as such, but I'm not sure why injecting doesn't work