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 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题