Chart
The <x-chart> component is a wrapper around Chart.js charts.
<x-chart
type="line"
:labels="$labels"
:datasets="$data"
:options="$options"
/>
labels = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];
data = [
[
'label' => 'Dataset 1',
'cubicInterpolationMode' => 'monotone',
'data' => [65, 59, 80, 84, 56, 55, 40],
'borderColor' => 'rgb(50, 132, 255)',
'backgroundColor' => 'rgb(50, 132, 255)',
'borderWidth' => 1
],
[
'label' => 'Dataset 2',
'data' => [38, 65, 75, 60, 75, 22, 48],
'borderColor' => 'rgb(255, 99, 132)',
'backgroundColor' => 'rgb(255, 99, 132)',
'borderWidth' => 1,
'tension' => 0.4
],
[
'label' => 'Dataset 3',
'cubicInterpolationMode' => [],
'data' => [10, 44, 30, 35, 50, 80, 75],
'borderColor' => 'rgb(75, 192, 192)',
'backgroundColor' => 'rgb(75, 192, 192)',
'borderWidth' => 1
]
];
options = [
'plugins' => [
'legend' => ['display' => true],
'title' => [
'display' => true,
'text' => 'Data in a chart',
'padding' => ['top' => 10, 'bottom' => 30]
]
],
'scales' => [
'y' => ['beginAtZero' => true]
]
];
<x-chart> has a
label, datasets and options attributes, each being a translation into a PHP array of the properties supported by Chart.js. You should visit chartjs.org for the full documentation.
#Include addin scripts
Use the include / plugins attributes to add scripts to your chart, such as the one in charge of adding labels to the below piechart:
includeis an array of script URLs to import into the page.pluginsis the list of plugin names to register into your chart.
<x-chart
type="pie"
dark-class
:plugins="$plugins"
:include="$include"
:labels="$labels"
:datasets="$data"
:options="$options"
/>
include = ['https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2'];
plugins = ['ChartDataLabels'];
labels = ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet'];
data = [
[
'data' => [184, 191, 154, 78, 44, 92, 166],
'backgroundColor' => [
'rgba(255, 99, 132, 0.6)',
'rgba(255, 159, 64, 0.6)',
'rgba(255, 205, 86, 0.6)',
'rgba(75, 192, 192, 0.6)',
'rgba(54, 162, 235, 0.6)',
'rgba(70, 120, 255, 0.6)',
'rgba(201, 100, 207, 0.6)'
]
]
];
options = [
'plugins' => [
'datalabels' => [
'labels' => [
'value' => [
'font' => ['size' => 24]
]
]
],
'legend' => ['display' => true],
'title' => [
'display' => true,
'text' => 'Colored pie chart',
'padding' => [
'top' => 10,
'bottom' => 30
]
],
'tooltip' => ['enabled' => false]
],
'tooltip' => ['enabled' => false]
];
The above example uses the
You can also use
dark-class attribute, used to generate a class for the dark mode of your website. Without it, the labels would hardly be readable.You can also use
dark-class={CSS class} to tweak the behavior of your chart when in dark mode.