draxq02664 2018-05-18 10:34
浏览 71
已采纳

性能问题,是什么原因造成的?

I have performance issue on a chart loaded with Chartjs on a web page

It is a stacked bar chart and I am doing a lot of calculs on the data fetched in my database.

What I want to know is where do I need to change my code to improve the time of execution?

Thanks to stackoverflow, I've put this to calculate the execution time :

$time_start = microtime(true); 
echo 'Total execution time in seconds: ' . (microtime(true) - $time_start);

around my two major loops in my php file.

The result is here :

Total execution time in seconds: 0.01149320602417
Total execution time2 in seconds: 0.0041401386260986

So apparently it is not due to the loops right?

I've also tracked performance with Chrome :

enter image description here

And also :

enter image description here

Does this mean that the slow loading time is due to Chartjs or does it prove nothing ?

I initially thought because of this post : Chart.js too slow rendering vertical stacked bars chart that it was the way to give the values to the chart in a loop but now I'm not sure anymore.

My way to pass vars to my chart (2nd loop calculated) :

for ($cd=0; $cd <= $curday; $cd++) {
    for ($i=0; $i <$maxy; $i++) {
        if (!isset($dataset[$i][$cd])) break; 
        //$dummydata = array(NULL, $dataset[$i][$cd]);
        $dummydata = array();
        // add empty values for previous days
        for ($zz=0; $zz < $cd; $zz++){
            $dummydata[]=null;
        }
        // add current value
        $dummydata[]=$dataset[$i][$cd];
        $dummy=array('type' => "bar",
                   'label' => "Intensity " . $intensity[$i][$cd],
                   'backgroundColor' => $intColors[$intensity[$i][$cd]],
                   'data' => ($dummydata));
        array_push($arrDatasets, $dummy);
    }         
}

EDIT : data to json :

$arrReturn = (array('labels' => $labels, 
                               'datasets' => $arrDatasets));
            $mydata = json_encode(($arrReturn));

EDIT : Javascript part :

$html .= "<script>";
    $html .= "var ctx=document.getElementById('myChart').getContext('2d');";
    $html .= "var myChart = new Chart(ctx, {";
    $html .= "type:'". $type . "',";
    $html .= "data:" . $mydata . ",";
    $html .= "options: " . $options;
    $html .= "});";
    $html .= "</script>";

This is the output of $mydata : enter image description here

  • 写回答

1条回答 默认 最新

  • donglang8008 2018-05-23 07:16
    关注

    It was due to Chartjs. I changed library and used one with a chart more appropriate, it nows load almost instantly

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错