2

I followed the guide on https://charts.erik.cat/guide/ to install Chartisan with echarts in my Laravel 7.4 framework. Everything works fine and I receive a chart.

The chart is triggered with this handler function:

public function handler(Request $request): Chartisan
    {
        return Chartisan::build()
            ->labels(['First', 'Second', 'Third'])
            ->dataset('Sample', [1, 2, 3])
            ->dataset('Sample 2', [3, 2, 1]);
    }

The dataset shall be pulled from my database therefore I modify the code to:

public function handler(Request $request): Chartisan
    {
        $data = DB::Users->where('id',$id)->find(1); //$id is not defined

        return Chartisan::build()
            ->labels(['First', 'Second', 'Third'])
            ->dataset('Sample', [1, 2, 3])
            ->dataset('Sample 2', [3, 2, 1]);
    }

Of course this doesn't work because $id is not defined. How and where can I pass data to the handler function? How can I use the $request variable if it is not a form?

The chart is called via the api/chart/SampleChart route. Where is this route defined? Could I pass data to the route? It is not included in routes/api.php or web.php.

All Laravel/Chart examples with the handler function are using ALL data from the database table without restrictions. I did not find an example which passes data.

global $id does not work to get access to $id.

0

2 Answers 2

3

You can easily pass parameters from your view to your chart url with "?":

http://localhost/public/api/chart/sample_chart?id=1

like this:

const chart = new Chartisan({
        el: '#chart',
        url: "@chart('sample_chart')" + "?id=1"
});

this part "?id=1" I would do something like this "?id={{ $id_came_from_controller }}"

and then in your handle(Request $request) method you can access your $id like this:

$id = $request->id;
$data = DB::Users->where('id', $id)->first();

P.S.

if you need several parameters, you can pass like this:

http://localhost/public/api/chart/sample_chart?first_param=123&second_param=456&third_param=789

and retrieve:

$request->first_param;
$request->second_param;
$request->third_param;
Sign up to request clarification or add additional context in comments.

Comments

0

The handler method is used to show static example in the docs.

You can directly instantiate the SampleChart, by adding it into the controller, If you have SampleChart inside App->Charts->SampleChart.php, you can add use App\Charts\SampleChart

Add a contructor to your sample chart, not know if they provide in recent versions by default,

class SampleChart extends Basechart
    public function __construct(){
        parent::__contruct();
    }

Then create instance

$samplechart = new SampleChart;
$data = DB::Users->find(1); // either use DB::Users->where('id',$id)->first();
/**
* let $data looks like, $data->name = "bhucho";$data->id = 2;$data->hobby = stoverflow; 
*/

$samplechart->labels = (array_keys($data));
$samplechart->dataset = (array_values($data));

return view('yourview', compact('samplechart'));

Also add these to your SampleChart as mentioned in chart_configuration.

 public ?string $name = 'custom_chart_name';
 public ?string $routeName = 'chart_route_name';

3 Comments

I am getting an error Call to undefined method ConsoleTVs\Charts\BaseChart::__contruct()
It works for version < 5.4, the code changes drastically in version 5, 6 & 7,
use this doc for version 6, v6.charts.erik.cat/… , honestly this doc of version 7 chartisan.dev/documentation has very less info, version 6 should solve your problem, I would update the answer but quite busy these days so can't update answer right now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.