百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

laravel-charts

> 编程语言
开源

使用 Chart.js 在 Laravel 中绘制图表

553 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

使用 Chart.js 在 Laravel 中绘制图表

Laravel Charts

Package to generate Chart.js charts directly from Laravel/Blade, without interacting with JavaScript.


Simple Usage

If you want to generate a chart above, grouping users records by the month of created_at value, here's the code.

Controller:

php
use LaravelDaily\LaravelCharts\Classes\LaravelChart;

// ...

$chart_options = [
    'chart_title' => 'Users by months',
    'report_type' => 'group_by_date',
    'model' => 'App\Models\User',
    'group_by_field' => 'created_at',
    'group_by_period' => 'month',
    'chart_type' => 'bar',
];
$chart1 = new LaravelChart($chart_options);

return view('home', compact('chart1'));

View File

blade
@extends('layouts.app')

@section('content')

                

            
        
    

@endsection

@section('javascript')
{!! $chart1->renderChartJsLibrary() !!}
{!! $chart1->renderJs() !!}
@endsection

Installation

bash
composer require laraveldaily/laravel-charts

No additional configuration or other parameters yet.


Usage

You need to create LaravelChart object in your Controller, passing array of options.

php
$chart = new LaravelChart($options);

Then pass it to the View, as a variable:

php
return view('dashboard', compact('chart'));

Available Reports and Options

Currently package support three types of charts/reports:

  • group_by_date - amount of records from the same table, grouped by time period - day/week/month/year;
  • group_by_string - amount of records from the same table, grouped by any string field, like name;
  • group_by_relationship - amount of records from the same table, grouped by belongsTo relationship's field

NOTE: From Laravel 8, all its models are placed in a folder called Models (App\Models)

Example with all options

…
  • chart_title (required) - just a text title that will be shown as legend;
  • chart_type (required) - possible values: "line", "bar", "pie";
  • report_type (required) - see above, can be group_by_date, group_by_string or group_by_relationship;
  • model (required) - name of Eloquent model, where to take the data from;
  • name (optional) - just a text title that will be shown as title, otherwise the legend is used;
  • conditions (optional, only for line chart type) - array of conditions (name + raw condition + color) for multiple datasets;
  • group_by_field (required) - name of database field that will be used in group_by clause;
  • group_by_period (optional, only for group_by_date report type) - possible values are "day", "week", "month", "year";
  • relationship_name (optional, only for group_by_relationship report type) - the name of model's method that contains belongsTo relationship.
  • aggregate_function (optional) - you can view not only amount of records, but also their SUM() or AVG(). Possible values: "count" (default), "avg", "sum".
  • aggregate_field (optional) - see aggregate_function above, the name of the field to use in SUM() or AVG() functions. Irrelevant for COUNT().
  • aggregate_transform (optional) - callback function for additional transformation of aggregate number
  • filter_field (optional) - show only data filtered by that datetime field (see below)
  • filter_days (optional) - see filter_field above - show only last filter_days days of that field. Example, last 30 days by created_at field.
  • filter_period (optional) - another way to filter by field, show only record from last week / month / year. Possible values are "week", "month", "year".
  • continuous_time (optional) - show all dates on chart, including dates without data.
  • show_blank_data (optional) - show date even if the data is blank based on filter_days.
  • range_date_start (optional) - show data in from a date range by filter_field, this is the start date.
  • range_date_end (optional) - show data in from a date range by filter_field, this is the end date.
  • field_distinct (optional) - field name required, it will apply a distinct(fieldname)
  • style_class (optional) - add class css in canvas
  • date_format (optional) - add the date format, by default: American format Y-m-d
  • where_raw (optional) - Condition in multiple consultation situations
  • chart_height (optional) - add the height in options, default 300px
  • date_format_filter_days (optional) - add the date format for Filter days
  • withoutGlobalScopes (optional) - removes global scope restriction from queried model
  • with_trashed (optional) - includes soft deleted models
  • only_trashed (optional) - only displays soft deleted models
  • top_results (optional, integer) - limit number of results shown, see Issue #49
  • chart_color (optional, value in rgba, like "0,255,255") - defines the color of the chart
  • labels (optional, array with key and value) - defines key value array mapping old and new values
  • hidden (optional, boolean) hides the current dataset. Useful when having multiple datasets in one chart
  • stacked (optional, boolean, only for bar chart) stacks the chart data when dates or strings match instead of showing it next to eachother

Example with relationship

php
$chart_options = [
    'chart_title' => 'Transactions by user',
    'chart_type' => 'line',
    'report_type' => 'group_by_relationship',
    'model' => 'App\Models\Transaction',

    'relationship_name' => 'user', // represents function user() on Transaction model
    'group_by_field' => 'name', // users.name

    'aggregate_function' => 'sum',
    'aggregate_field' => 'amount',
    
    'filter_field' => 'transaction_date',
    'filter_days' => 30, // show only transactions for last 30 days
    'filter_period' => 'week', // show only transactions for this week
];

Rendering Charts in Blade

After you passed $chart variable, into Blade, you can render it, by doing three actions:

Action 1. Render HTML.

Wherever in your Blade, call this:

blade
{!! $chart1->renderHtml() !!}

It will generate something like this:

xml

Action 2. Render JavaScript Library

Package is using Chart.js library, so we need to initialize it somewhere in scripts section:

blade
{!! $chart1->renderChartJsLibrary() !!}

It will generate something like this:

xml

Action 3. Render JavaScript of Specific Chart

After Chart.js is loaded, launch this:

blade
{!! $chart1->renderJs() !!}

Using Multiple Charts

You can show multiple charts on the same page, initialize them separately.

Controller:

…

View:

blade
@extends('layouts.app')

@section('content')

                

            
        
    

@endsection

@section('javascript')
{!! $chart1->renderChartJsLibrary() !!}

{!! $chart1->renderJs() !!}
{!! $chart2->renderJs() !!}
{!! $chart3->renderJs() !!}
@endsection

Multiple Datasets

This is a new feature from v0.1.27. You can provide multiple arrays of settings to the LaravelChart constructor, and they will be drawn on the same chart.

…

License

The MIT License (MIT). Please see License File for more information.


More from our LaravelDaily Team

  • Enroll in our Laravel Online Courses
  • Check out our adminpanel generator QuickAdminPanel
  • Subscribe to our YouTube channel Laravel Daily

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言