无法在使用Lavacharts创建的Laravel 5.1中渲染LineChart

时间:2015-12-16 07:56:05

标签: php laravel laravel-5.1

我已按照教程https://github.com/kevinkhill/lavacharts在我的应用程序中基于Laravel 5.1呈现图表。但无法呈现它。它给了我错误 在View 中找不到LineChart('Stocks')。以下是我的控制器代码&查看代码。修复此问题需要进行哪些更改?

ChartController.php

public function generatecharts()
{

$lava = new Lavacharts;
$stocksTable = $lava->DataTable();

 $stocksTable->addDateColumn('Day of Month')
            ->addNumberColumn('Projected')
            ->addNumberColumn('Official');




// Random Data For Example
for ($a = 1; $a < 30; $a++)
{
    $rowData = array(
      "2014-8-$a", rand(800,1000), rand(800,1000)
    );

    $stocksTable->addRow($rowData);
}

  $Chart = $lava->LineChart('Stocks')
                ->setOptions(array(
                    'datatable' => $stocksTable,
                    'title' => 'Stock Market Trends'
                  ));

return view('pages.analytics');

}

analytics.blade.php

@extends('layouts.app')
@section('title') analytics:: @parent @stop
@section('content')



<div class="row">
    <div class="page-header">
        <h2>Welcome to analytics</h2>
    </div>

</div>
<div id="stock-div"></div>
echo $lava->render('LineChart', 'Stocks', 'stock-div');
@linechart('Stocks', 'stocks-div');


@endsection

2 个答案:

答案 0 :(得分:4)

1)在documentation中说明如果您使用的是Laravel,则需要更改

$lava-> to \Lava:: (the slash is important)

2)实际上这行代码:

 echo $lava->render('LineChart', 'Stocks', 'stock-div');

相当于这一行:

 @linechart('Stocks', 'stocks-div');

所以你在这里有一个重复的代码(只删除一行)。

您可以在视图中添加3个选项:

您可以使用PHP标记:

<?php echo \Lava::render('LineChart', 'Stocks', 'stock-div'); ?>

或者你可以使用它:

{!! \Lava::render('LineChart', 'Stocks', 'stock-div') !!}

或此简化版

@linechart('Stocks', 'stocks-div');

解决方案:

<强> ChartController.php

public function generatecharts()
{

$stocksTable = \Lava::DataTable();

 $stocksTable->addDateColumn('Day of Month')
            ->addNumberColumn('Projected')
            ->addNumberColumn('Official');




// Random Data For Example

for ($a = 1; $a < 30; $a++)
{
    $rowData = array(
      "2014-8-$a", rand(800,1000), rand(800,1000)
    );

    $stocksTable->addRow($rowData);
}

  $Chart = \Lava::LineChart('Stocks')
                ->setOptions(array(
                    'datatable' => $stocksTable,
                    'title' => 'Stock Market Trends'
                  ));

return view('pages.analytics');

}

<强> analytics.blade.php

@extends('layouts.app')
@section('title') analytics:: @parent @stop
@section('content')



<div class="row">
    <div class="page-header">
        <h2>Welcome to analytics</h2>
    </div>

</div>
<div id="stock-div"></div>

{!! \Lava::render('LineChart', 'Stocks', 'stock-div') !!}

@endsection

答案 1 :(得分:1)

这是 LAVACHARTS 3.0和LAVAVAR 5.2的代码

<强>控制器

    $stocksTable = \Lava::DataTable();
    $stocksTable->addDateColumn('Day of Month')
        ->addNumberColumn('Projected')
        ->addNumberColumn('Official');

    // Random Data For Example
    for ($a = 1; $a < 30; $a++) {
        $stocksTable->addRow(["2014-8-$a", rand(800, 1000), rand(800, 1000)]);
    }

    //DON'T pass $Chart object to view, you get it via its label
    //options here: http://lavacharts.com/#datatables
    $Chart = \Lava::LineChart('this_is_the_label', $stocksTable, [
        'title'    => 'This works in laravel 5.2',
        'fontSize' => 24,
    ]);

查看

<div id="stock-div"></div>
{!! \Lava::render('LineChart', 'this_is_the_label', 'stock-div') !!}