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 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: