如何在symfony 3项目中集成Highcharts?

时间:2017-02-18 19:47:32

标签: javascript php jquery highcharts symfony

我正在尝试在我的symfony项目中集成OBHighChartsBundle。我按照教程 https://github.com/marcaube/ObHighchartsBundle 这是我的代码:

在控制器中:

 /**
 * @return \Symfony\Component\HttpFoundation\Response
 * @Route("/admin")
 * @Route("/admin", name="home_admin")
 */
public function adminAction(Request $r)
{
    $ob = new Highchart();
    $ob->chart->renderTo('piechart');
    $ob->title->text('Browser market shares at a specific website in 2010');
    $ob->plotOptions->pie(array(
        'allowPointSelect'  => true,
        'cursor'    => 'pointer',
        'dataLabels'    => array('enabled' => false),
        'showInLegend'  => true
    ));
    $data = array(
        array('Firefox', 45.0),
        array('IE', 26.8),
        array('Chrome', 12.8),
        array('Safari', 8.5),
        array('Opera', 6.2),
        array('Others', 0.7),
    );
    $ob->series(array(array('type' => 'pie','name' => 'Browser share','data' => $data)));


    return $this->render("@Tahwissa/Admin/index.html.twig", array(
        'chart' => $ob
    ));
}

在我的视图中index.html.twig:

<!-- Load jQuery from Google's CDN if needed -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" type="text/javascript"></script>

<script src="{{ asset('assets/Js/highcharts.js') }}"></script>
<script src="{{ asset('assets/Js/exporting.js') }}"></script>
<script type="text/javascript">
    {{ chart(chart) }}
</script>

<div id="linechart" style="min-width: 400px; height: 400px; margin: 0 auto"></div>

我有一个空白页面并发生了异常:

未捕获错误:Highcharts错误#13:www.highcharts.com/errors/13 Highcharts错误#13

渲染div未找到

如果chart.renderTo选项配置错误,则会发生此错误,因此Highcharts无法找到用于呈现图表的HTML元素。

0 个答案:

没有答案