Skip to content

Instantly share code, notes, and snippets.

@mackhankins
Created April 12, 2016 21:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mackhankins/3b63e0e583e501d6a276103796fbba32 to your computer and use it in GitHub Desktop.
Save mackhankins/3b63e0e583e501d6a276103796fbba32 to your computer and use it in GitHub Desktop.
laravel-chartjs

None of this is production code, but me trying to figure out how to work multiple datasets into a Laravel project with chartjs. Here's my table schema

        Schema::create('statistics', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->string('type');
            $table->string('table');
            $table->integer('count');
            $table->timestamps();
        });

Controller

public function grabStats($type = null, $table = null)
    {
        $first = strtotime('first day this month');
        $months = [];
        $countMonths = date('n');

        for ($i = $countMonths - 1; $i >= 0; $i--) {
            array_push($months, date('F', strtotime("-$i month", $first)));
        }

        for ($i = 0; $i <= 2; $i++) {
            $years[] = date("Y", strtotime(date('Y-m-01') . " -$i years"));
        }

        $output['labels'] = $months;

        $colors = [
            [
                'fillcolor' => 'rgba(114,131,153,0.2)',
                'strokecolor' => 'rgba(114,131,153,1)',
            ],
            [
                'fillcolor' => 'rgba(151,187,205,0.2)',
                'strokecolor' => 'rgba(151,187,205,1)',
            ],
            [
                'fillcolor' => 'rgba(220,220,220,0.2)',
                'strokecolor' => 'rgba(220,220,220,1)',
            ],
        ];

        $i = 0;

        foreach ($years as $year) {
            $array = [];
            $array['label'] = $year;
            $array['fillColor'] = $colors[$i]['fillcolor'];
            $array['strokeColor'] = $colors[$i]['strokecolor'];
            $array['pointColor'] = $colors[$i]['strokecolor'];
            $array['pointStrokeColor'] = '#fff';
            $array['pointHighlightFill'] = '#fff';
            $array['pointHighlightStroke'] = $colors[$i]['strokecolor'];
            $i++;
            $data = $this->statistic->yearlyStats($type, $table, $year, $countMonths);
            foreach ($data as $set) {
                $array['data'][] = $set['count'];
            }
            if (!empty($array['data'])) {
                $output['datasets'][] = $array;
            }
        }

        return json_encode($output);
        
    }

Output from months[]

array:4 [▼
  0 => "January"
  1 => "February"
  2 => "March"
  3 => "April"
]

Output from years[]

array:3 [▼
  0 => "2016"
  1 => "2015"
  2 => "2014"
]

Repository

    public function yearlyStats($type = null, $table = null, $year, $months)
    {
        if($year != date('Y')) {
            $subtract = date('Y') - $year;
            $start = Carbon::now()->subYears($subtract);
            $end = Carbon::now()->subYears($subtract)->subMonths($months);
        }else{
            $start = Carbon::now();
            $end = Carbon::now()->subMonths($months);
        }

        if($type)
        {
            return $this->statistic->select('count')->where('type','=',$type)->whereYear('created_at', '=', $year)->whereBetween('created_at', [$end, $start])->get();
        }

        return $this->statistic->select('count')->where('table','=',$table)->whereYear('created_at', '=', $year)->whereBetween('created_at', [$end, $start])->get();
    }

Output before json encode

array:2 [▼
  "labels" => array:4 [▼
    0 => "January"
    1 => "February"
    2 => "March"
    3 => "April"
  ]
  "datasets" => array:2 [▼
    0 => array:8 [▼
      "label" => "2016"
      "fillColor" => "rgba(114,131,153,0.2)"
      "strokeColor" => "rgba(114,131,153,1)"
      "pointColor" => "rgba(114,131,153,1)"
      "pointStrokeColor" => "#fff"
      "pointHighlightFill" => "#fff"
      "pointHighlightStroke" => "rgba(114,131,153,1)"
      "data" => array:4 [▼
        0 => 45
        1 => 2
        2 => 19
        3 => 19
      ]
    ]
    1 => array:8 [▼
      "label" => "2015"
      "fillColor" => "rgba(151,187,205,0.2)"
      "strokeColor" => "rgba(151,187,205,1)"
      "pointColor" => "rgba(151,187,205,1)"
      "pointStrokeColor" => "#fff"
      "pointHighlightFill" => "#fff"
      "pointHighlightStroke" => "rgba(151,187,205,1)"
      "data" => array:4 [▼
        0 => 45
        1 => 21
        2 => 9
        3 => 29
      ]
    ]
  ]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment