You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel ConsoleTVs Charts 6.x版本如何设置图表颜色?

How to Set Chart Colors in ConsoleTVs Charts 6.x for Pie Charts

Hey there! I’ve dealt with this exact color issue when upgrading to ConsoleTVs Charts 6.x — the old ->colors() method doesn’t work anymore because the library switched to using Chart.js 3.x under the hood, which has a different API structure.

Here’s how you can fix your pie chart colors:

Option 1: Set Colors Directly on the Dataset

You can define the backgroundColor (the fill color for pie slices) directly when creating your dataset:

$chart = new SampleChart;
$chart->displayAxes(false);
$chart->dataset('W1', 'pie', [10, 80, 10])
    ->backgroundColor([
        '#ff6384', // Red-pink
        '#36a2eb', // Blue
        '#cc65fe'  // Purple
    ]);

Each color in the array will map to a corresponding value in your dataset (so the first color matches the first value 10, second matches 80, etc.).

Option 2: Global Color Configuration via Options

If you want to set default colors for all pie charts or use more advanced Chart.js settings, use the ->options() method to pass native Chart.js configuration:

$chart = new SampleChart;
$chart->displayAxes(false);
$chart->dataset('W1', 'pie', [10, 80, 10]);

$chart->options([
    'datasets' => [
        'pie' => [
            'backgroundColor' => [
                '#ff6384',
                '#36a2eb',
                '#cc65fe'
            ],
            // Optional: Add border colors if needed
            'borderColor' => '#ffffff',
            'borderWidth' => 2
        ]
    ]
]);

Why the Old ->colors() Method Doesn’t Work

ConsoleTVs Charts 6.x upgraded to Chart.js 3.x, which deprecated many of the wrapper-specific helper methods like colors(). Instead, you now use the native Chart.js properties directly — for pie charts, backgroundColor is the key property to set slice colors.

You can use any valid color format supported by Chart.js: hex codes, RGB/RGBA values, or even named colors (like 'red').

内容的提问来源于stack exchange,提问作者marcus n

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:27:59