定制 tomvandenberk/chartjs-wrapper 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tomvandenberk/chartjs-wrapper

最新稳定版本:v1.0.1

Composer 安装命令:

composer require tomvandenberk/chartjs-wrapper

包简介

A wrapper for ChartJs data.

README 文档

README

This is a lightweight PHP wrapper designed to simplify the use of ChartJs in combination with APIs. It takes raw data and transforms it into a structured JSON format that can be directly used with ChartJs.

This is especially useful in scenarios where data is retrieved dynamically via an API. The wrapper ensures the data is correctly formatted and ready for use, making it easier to generate charts such as line graphs, bar charts, and pie charts without additional processing. This helps to streamline the process of connecting a PHP backend to ChartJs on the frontend.

Example usage in Laravel:

return response()->json(new Data(
    $data->pluck('label')->toArray(), [
        new DataSet($data->pluck('value')->toArray(), [
            'backgroundColor' => $data->pluck('color'),
        ])
    ]
)->addExtraValues('ids', $data->pluck('id')->toArray())->addExtraValues('person_ids', $data->pluck('person_id')->toArray()));
$datasets = [];
$items->each(function ($item) use (&$datasets, $weeks) {
    $datasets[] = new DataSet($item->pluck('total_hours')->toArray(), [
        'label' => $item->pluck('person')->first() ?? '',
    ])->withColorsBasedOnLabel();
});

return response()->json(new Data($weeks, $datasets));

output can be directly used in ChartJS

{
    "labels": [
        "2024-W52",
        "2025-W01",
        "2025-W02",
        "2025-W03",
        "2025-W04",
        "2025-W05",
        "2025-W06",
        "2025-W07",
        "2025-W08",
        "2025-W09",
        "2025-W10"
    ],
    "datasets": [
        {
            "label": "Label 1",
            "data": [
                0,
                13.2,
                13.2,
                26.4,
                13.2,
                19.8,
                6.6,
                19.8,
                33,
                6.6,
                0
            ],
            "pointRadius": 5,
            "pointHoverRadius": 20,
            "borderRadius": 5,
            "fill": true,
            "tension": 0,
            "borderColor": "#f26656",
            "backgroundColor": "rgba(242, 102, 86, 0.5)"
        },
        {
            "label": "Label 2",
            "data": [
                0,
                3.75,
                13.2,
                19.8,
                5.6,
                26.4,
                13.2,
                41.78333333333333,
                0,
                6.6,
                0
            ],
            "pointRadius": 5,
            "pointHoverRadius": 20,
            "borderRadius": 5,
            "fill": true,
            "tension": 0,
            "borderColor": "#50220a",
            "backgroundColor": "rgba(80, 34, 10, 0.5)"
        }
    ]
}
new Chart(document.getElementById('chart'), {
    type: 'line',
    data: @json($chart_data),
    options: {
        responsive: true,
        maintainAspectRatio: false,
    }
});

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unlicense
  • 更新时间: 2025-03-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固