Laravel 5.5 cant render dynamic charts using ConsoleTV's chart package

ⅰ亾dé卋堺 提交于 2019-12-10 12:24:53

问题


Why I cant generate the data coming from my database? I can render the chart but there is no data shown. But when i dump the data it shows me the array of the data. Help would be appreciated. Thanks. Chart output right here: https://imgur.com/a/eyCVxSn

PS: I want to get the data in my second database. Thats why there is 'mysql2' in the Model.

GraphController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\DB;
use View;
use App\News;
use Charts;
use App\Graph;

class GraphController extends Controller
{
    /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
    public function index()
    {



        $graph = Charts::database(Graph::find(1),'line','highcharts')
        ->title('Tokens')
        ->ElementLabel('Coins Sold')
        ->Responsive(true)
        ->dimensions(1000,500)
        ->data(Graph::find(1));

        dd($graph);

        $news = News::all();


        // //dd($news);
        return View::make('coin.news', compact('graph','news'));
    }
}

Graph.php Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Auth;
use Cache;
use Charts;


class Graph extends Model
{
    //

    protected $connection = 'mysql2';
    protected $table = 'ico_stages';



}


graph.blade.php




@section('graph')



        {!! $graph->render() !!}



@endsection

app.blade.php

stylesheets....

{!! Charts::assets() !!}

@yield('graph')

news...

scripts...

When I use dd($graph) it shows me this. I can also see the data that i want to render in my chart.

Database {#299 ▼
  +data: Graph {#306 ▼
    #connection: "mysql2"
    #table: "ico_stages"
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #attributes: array:18 [▼
      "id" => 1
      "name" => "Stage 1"
      "start_date" => "2019-05-11 21:29:00"
      "end_date" => "2019-11-30 21:29:00"
      "total_tokens" => 10000000
      "base_price" => 2.0
      "min_purchase" => 100
      "max_purchase" => 10000
      "soft_cap" => 0
      "hard_cap" => 0
      "display_mode" => "normal"
      "private" => 0
      "user_panel_display" => 0
      "sales_token" => 1496.0
      "sales_amount" => 2720.0
      "status" => "active"
      "created_at" => "2019-05-12 05:29:57"
      "updated_at" => "2019-06-19 06:02:55"
    ]
    #original: array:18 [▼
      "id" => 1
      "name" => "Stage 1"
      "start_date" => "2019-05-11 21:29:00"
      "end_date" => "2019-11-30 21:29:00"
      "total_tokens" => 10000000
      "base_price" => 2.0
      "min_purchase" => 100
      "max_purchase" => 10000
      "soft_cap" => 0
      "hard_cap" => 0
      "display_mode" => "normal"
      "private" => 0
      "user_panel_display" => 0
      "sales_token" => 1496.0
      "sales_amount" => 2720.0
      "status" => "active"
      "created_at" => "2019-05-12 05:29:57"
      "updated_at" => "2019-06-19 06:02:55"
    ]
    #changes: []
    #casts: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #hidden: []
    #visible: []
    #fillable: []
    #guarded: array:1 [▶]
  }
  +date_column: "created_at"
  +date_format: "l dS M, Y"
  +month_format: "F, Y"
  +hour_format: "D, M j, Y g A"
  +language: "en"
  +preaggregated: false
  +aggregate_column: null
  +aggregate_type: null
  +value_data: []
  +id: null
  +customId: null
  +type: "line"
  +library: "highcharts"
  +title: "Tokens"
  +element_label: "Coins Sold"
  +labels: []
  +values: []
  +colors: []
  +responsive: true
  +gauge_style: "left"
  +view: null
  +region: "world"
  #suffix: ""
  +container: ""
  +credits: false
  +loader: true
  +loader_duration: 500
  +loader_color: "#000000"
  +background_color: "inherit"
  +template: "material"
  +one_color: false
  +legend: true
  +x_axis_title: false
  +y_axis_title: null
  +script: ""
  +html: ""
  +export: null
  +"height": 500
  +"width": 1000
}

回答1:


To render data In graph template

$graph = view('graph')->with('graph', $graph); 

And to return rendered data to news

return view('coin.news', compact('graph','news'));

Final code

    $graph = Charts::database(Graph::find(1),'line','highcharts')
    ->title('Tokens')
    ->ElementLabel('Coins Sold')
    ->Responsive(true)
    ->dimensions(1000,500)
    ->data(Graph::find(1));



    $news = News::all();

    $graph = view('graph')->with('graph', $graph); 

    return view('coin.news', compact('graph','news'));


来源:https://stackoverflow.com/questions/56764929/laravel-5-5-cant-render-dynamic-charts-using-consoletvs-chart-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!