I am sending my data to the front-end of ConsoleTvs 7 Chartisan chart using the built in back-end advancedDataset
method. As the third parameter I decided to send extra dataset configurations in array data type:
// Example of extra info data that forms part of chartData return
$data['extra'] = ['type' => 'line', 'fill' => false, 'borderColor' => '#FF6F6F', 'backgroundColor' => '#FF6F6F', 'hidden' => true,
'datalabels' => ['display' => true], 'borderDash' => [5, 5]];
//--> within handler method
public function handler(Request $request): Chartisan
{
$data = $this->chartData($request); // Trait method chartData return
$chart = Chartisan::build()
->labels($data["labels"]);
foreach ($data['dataset'] as $key => $values) {
$chart->advancedDataset($values['legend'], $values['data'], $values['extra']);
// ^
//----------------------------- -------------------------------------| extra arg
}
return $chart;
}
This extra values are not applied to the datasets configuration. In fact the chart renders its default bar chart if I remove the ChartisanHooks.datasets(...[ configurations ]...)
method.
How can I apply the extra dataset configs to the front-end without having to resort to double work? Is there a setting I am missing or how do I access the extra
argument?
Laravel 8
Chart.js v2.9.4
chartjs-plugin-datalabels v1.0.0
All of the configuration files for the Laravel framework are stored in the config directory. Each option is documented, so feel free to look through the files and get familiar with the options available to you.
we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command: Now In this step, install consoletvs/charts:6 package in laravel 8 app via following command.
In fact, if you review the Laravel configuration files, you will notice many of the options are already using this helper: The second value passed to the env function is the "default value". This value will be returned if no environment variable exists for the given key.
If you are working with Laravel 5.5 or higher thats all you need to install the package thanks by autodiscover feature. If you work with Laravel lower than 5.5 you will need to register a service provider, add this line into the config/app.php file in the providers section:
At last I found the answer in the not so obscure Chartisan documentation.
The custom()
method of the front-end const hooks = new ChartisanHooks()
instance contains 3 hook parameters namely :
hooks.custom(({ data, merge, server }) => {
// data -> Contains the current chart configuration
// data that will be passed to the chart instance.
// merge -> Contains a function that can be called to merge
// two javascript objects and returns its merge.
// server -> Contains the server information in case you need
// to acces the raw information provided by the server.
// This is mostly used to access the `extra` field.
// ...
// The function must always return the new chart configuration.
return data
})
The aptly named server
key contains a datasets
key that consists of an array of objects where one key is named extra
i.e.
server.datasets[0].extra // all array key : values passed from server
Hoorah!
So to access this extra
key : values I pass them to the front-end hooks.custom()
method data.datasets[0]
object or create new ones e.g.
const chart = new Chartisan({
el: '#test_chart',
hooks: new ChartisanHooks()
.colors()
.custom(function({ data, merge, server }) { // server param
//---> loop through extra from server
for (let i = 0; i < server.datasets.length; i++) {
const extras = server.datasets[i].extra; // extra object
for (const [key, value] of Object.entries(extras)) { // loop through extras
data.data.datasets[i][key] = value; // add extras to data
}
}
return merge(data, { // merge data
options: {
layout: {
padding: {
left: 0,
right: 0,
top: 50,
bottom: 0
},
},
aspectRatio: 1,
maintainAspectRatio: false,
responsive: true,
legend: {
display: true,
position: 'top',
labels: {
usePointStyle: true,
fontSize: 12,
},
},
elements: {
point: {
pointStyle: 'circle',
}
},
scales: {
xAxes: [{
maxBarThickness: 120,
scaleLabel: {
display: true,
labelString: "xAxes_label"
},
gridLines: {
display: false
},
ticks: {
fontSize: 10,
maxRotation: 80,
minRotation: 80,
padding: 2
},
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: "yAxes_label"
},
gridLines: {
display: false,
drawBorder: false
},
ticks: {
display: true,
fontSize: 10,
suggestedMin: 0
},
}],
},
plugins: {
datalabels: {
color: '#ff0a6c',
labels: {
title: {
font: {
weight: 'bold',
size: 11,
}
},
value: {
color: 'green'
}
},
formatter: function(value, context) {
return (value != '' && value !== undefined) ? Math.round(value * 100) / 100 : value;
},
anchor: 'end',
align: 'start',
display: 'auto',
clamp: false
}
}
}
});
}),
});
Of course this is very rough and needs checking if client side supports some of these methods. Also server.datasets[i].extra !== null
, object[key] !== undefined
etcetera checks is also needed.
This of course makes this more dynamic from back-end. The question is, is ConsoleTvs package dead as a back-end safe Laravel package and is it still supported by its developer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With