0

I'm making a morris line in my index page and I'm trying to pass data to it from my controller controller:

$chartData=  DB::table('transactions')
        ->select('v_type',DB::raw("MONTH(transaction_created_at)"),DB::raw('sum(v_value)'))
        ->groupby('v_type',DB::raw("MONTH(transaction_created_at)"))
        ->get();

  return view('layouts/dashboard/index', ['tickets' => $tickets , 'types'=> $types,'response'=>json_encode($all),'chartData' => $chartData]);

index.blade.php

 .......<div class="row clearfix">

                <div class="card">

                    <div class="header">
                        <h4>Charts</h4>
                    </div>
                        <div class="body">
                            <div id="pushups" class="graph-wrapper" > </div>

                        </div>
                    </div>
                </div>
            </div>
  ......
 @section('extra-script')
    {{Html::script('plugins/jquery-countto/jquery.countTo.js')}}
    {{Html::script('plugins/raphael/raphael.min.js')}}
    {{Html::script('plugins/morrisjs/morris.js')}}
    {{Html::script('plugins/chartjs/Chart.bundle.js')}}
    {{Html::script('plugins/flot-charts/jquery.flot.js')}}
    {{Html::script('plugins/flot-charts/jquery.flot.resize.js')}}
    {{Html::script('plugins/flot-charts/jquery.flot.pie.js')}}
    {{Html::script('plugins/flot-charts/jquery.flot.categories.js')}}
    {{Html::script('plugins/flot-charts/jquery.flot.time.js')}}
    {{Html::script('plugins/jquery-sparkline/jquery.sparkline.js')}}

    {{Html::script('js/pages/index.js')}}

    @endsection

and in my index.js i have this function

function initLineChart() {
Morris.Line({
    element: 'pushups',
    data: [
        { day: 'Monday', pushups: 20, beers: 2 },
        { day: 'Tuesday', pushups: 10, beers: 2 },
        { day: 'Wednesday', pushups: 5, beers: 3 },
        { day: 'Thursday', pushups: 5, beers: 4 },
        { day: 'Friday', pushups: 20, beers: 1 }
    ],
    xkey: 'day',
    parseTime: false,
    ykeys: ['pushups','beers'],
    labels: ['Pushups','Beers'],
    lineColors: ['#373651','#E65A26']
});
}

here I'm using this data just for testing however i would like to use the $chartdata to fill this morris line

the chartdata json looks like this

[
  {
    "v_type": "money_value",
    "MONTH(transaction_created_at)": 9,
    "sum(v_value)": 15.5
  },
  {
    "v_type": "tariff_switch",
    "MONTH(transaction_created_at)": 9,
    "sum(v_value)": 3
  },
  .......]

so how can i do that

Thank you in advance.

1 Answer 1

1

You can pass a php array to index.js file like this:

index.blade.php

<script type="text/javascript">var chart_data =<?php echo chartData; ?>;</script>
{{Html::script('js/pages/index.js')}}

variable chart_data will be accessible in index.js file

Sign up to request clarification or add additional context in comments.

1 Comment

Great! This help me a lot! But you can use laravelcollective {!! $charData !!} this is how I used! Thanks a lot.

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.