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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动