douyun3631 2018-11-22 10:24
浏览 62
已采纳

Chart.js传递一个以1开头而不是0问题的数组

I try to make a Chart.js chart in Laravel/PHP with the plugin Fxcosta. So I am quite new to this community, as well as the programming languages. I try to make a dynamic chart with all the days of the actual month. (In November = 30 days). With this task I created a function (calcDaysOfActualMonth) which returns an array with all the days of the actual month. So my current problem is, that I want to start my chart with day 1 and not 0. So when I change the loop index to 1 I can't pass the array $labelsArr to my chart because it's not starting with 0.

        // CHART LAST MONTH
        $labelsArr = $this->calcDaysOfActualMonth();
        $chartjs = app()
        ->chartjs
        ->name('lineChartTest')
        ->type('line')
        ->size(['width' => 400, 'height' => 200])
        ->labels($labelsArr)
        ->datasets([
            [
                "label" => "Actual Month",
                'backgroundColor' => "rgba(38, 185, 154, 0.31)",
                'borderColor' => "rgba(38, 185, 154, 0.7)",
                "pointBorderColor" => "rgba(38, 185, 154, 0.7)",
                "pointBackgroundColor" => "rgba(38, 185, 154, 0.7)",
                "pointHoverBackgroundColor" => "#fff",
                "pointHoverBorderColor" => "rgba(220,220,220,1)",
                'data' => [65, 59, 55, 12, 33, 65, 59, 80, 81, 56,65, 12, 55, 12, 33, 55, 59, 80, 81, 56,65, 59, 80, 81, 56,65, 59, 80, 81, 56],
            ]
        ])
        ->options([]);

My function trying to create an array indexing with 1, starting with 0 is working, starting with 1 is not working:

private function calcDaysOfActualMonth(){
    $days = date("t");
    $daysArr = [];

    for($i=0; $i<$days; $i++){
        $daysArr[$i] = $i;
    }

    return $daysArr;
}

My Chart is this one, staring currently with 0: My Chart of actual month

Excuse me sincerly when it's a real trivial problem. And I hope I gave enough information! Thank you very much in advance.

  • 写回答

1条回答 默认 最新

  • douxiong3245 2018-11-22 10:37
    关注

    Yes it was a trivial problem. So the answer from Magnus Eriksson helped me solving this:

    It was: $daysArr[] = $i + 1;

    With dd() the $daysArr

    The chart with the index 1

    Thank you very much for the fast answer!

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

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题