在symfony中使用HighCharts绘制循环图 [英] Draw charts with a Loop using HighCharts in symfony

查看:110
本文介绍了在symfony中使用HighCharts绘制循环图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在symfony中使用Highcharts。当我输入这样的静态数据(array1 / array2 / array3 / array4)时它很好用:

  $ ob1 = new Highchart ); 
$ ob1->图表 - > renderTo('barchart');
$ ob1->标题 - >文字('图表1');
$ ob1-> xAxis->类别($ arrayResult);
$ ob1-> plotOptions-> pie(array(
'allowPointSelect'=> true,
'cursor'=>'pointer',
'dataLabels' => array('enabled'=> false),
'showInLegend'=> true
));

$ ob1-> series(array(array('type'=>'column','name'=>'bar1','data'=> $ array1),$ ('type'=>'column','name'=>'bar2','data'=> $ array2)),
(array('type'=> 'column','name'=>'bar3','data'=> $ array3)),
(array('type'=>'column','name'=>'bar4 ','data'=> $ array4))


));

但我需要的是在循环中输入数据,因为我的数组数量不规则。我试过这个,但是当''(T_WHILE)'出现了'意外'的错误时,有什么我在这里错过了吗?

这里是我的代码,用while来添加图表的数据系列:

  $ i = 1; 
$ number = 4;
$ b $ ob1-> series(array(

$ myarray = array();
while($ i <= $ number)):array_push( $ myarray,array(0 =>'value',1 => $ i));
$ i ++;
array('type'=>'column','name'=> ;'bar'。$ i,'data'=> $ myarray)
endwhile;
),

));

我也试过这个,只显示while的最后一个迭代

  $ i = 1; 
$ number = 4;
$ myarray = array();

while($ i <= $ number):
array_push($ myarray,array(0 =>'value',1 => $ i));
$ i ++;
$ ob1-> series(array(array('type'=>'column','name'=>'bar'。$ i,'data'=> $ myarray)));
endwhile;


解决方案

您的php语句无效,语法无效。为了避免这种类型的错误,请不要在函数参数中创建一个循环。简化你的语法和使用时间变量,很容易阅读和理解你在做什么,记住,每个优秀的开发人员总是会说:分而治之:)

  $ i = 1; 
$ number = 4;

$ chartData = [];
while($ i <= $ number){
$ chartData [] = [
'type'=> '列',
'name'=> 'bar'。$ i,
'data'=> [
'值',
$ i,
],
];
$ i ++;
}
$ ob1->系列($ chartData);


I am currently using Highcharts in symfony. It works good when I enter static data (array1/array2/array3/array4) like this:

$ob1 = new Highchart();
$ob1->chart->renderTo('barchart');
$ob1->title->text('Chart 1');
$ob1->xAxis->categories($arrayResult);
$ob1->plotOptions->pie(array(
    'allowPointSelect'  => true,
    'cursor'    => 'pointer',
    'dataLabels'    => array('enabled' => false),
    'showInLegend'  => true
));

$ob1->series(array(array('type' => 'column','name' => 'bar1', 'data' => $array1),
                    (array('type' => 'column','name' => 'bar2', 'data' => $array2)),
                    (array('type' => 'column','name' => 'bar3', 'data' => $array3)),
                    (array('type' => 'column','name' => 'bar4', 'data' => $array4))


    ));

but what I need is to enter data within a loop because I have an irregular number of arrays. I tried this but I got an error "unexpected 'while' (T_WHILE)" Is there anything I have missed here?

Here is my code using while to add Chart's Data Series:

      $i=1;
      $number=4;

    $ob1->series(array( 
        (   
         $myarray = array();
         while($i <= $number): array_push($myarray, array(0 => 'value', 1=> $i));
          $i++;
          array('type' => 'column','name' => 'bar'.$i, 'data' => $myarray)
          endwhile;
        ),

));

I also tried this and displays only the last iteration of the while

     $i=1;
     $number=4;
     $myarray = array();

 while($i <= $number): 
     array_push($myarray, array(0 => 'value', 1=> $i));
     $i++;
     $ob1->series(array (array('type' => 'column','name' => 'bar'.$i, 'data' => $myarray)));
 endwhile;

解决方案

Your php statement is not valid, has invalid syntax. To avoid this type of error, never create a loop inside the function argument. Simplify your syntax and use temporal variables, is easy to read and to understand what are you doing, remember, every good developer always say: "divide and conquer" :)

$i = 1;
$number = 4;

$chartData = [];
while ($i <= $number) {
    $chartData[] = [
        'type' => 'column',
        'name' => 'bar'.$i,
        'data' => [
            'value',
            $i,
        ],
    ];
    $i++;
}
$ob1->series($chartData);

这篇关于在symfony中使用HighCharts绘制循环图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆