Is there a way to add an array of objects to chartjs? In the controller of the view, an array is passed to the Twig template using the variable {{cg}}. However, I am encountering an error that suggests the array I am working with is actually an array of arrays. My aim is to populate the labels attribute with a list of months and the data attribute with the corresponding amount. Any suggestions on how to achieve this? Below is the snippet from the controller:
/** Controller
* @Route("/cg1", name="cg1")
*/
public function cg1Action()
{
$conn=$this->get('database_connection');
$consulta="SELECT consumo_combustible.importe as imp,MONTH(consumo_combustible.fecha) as mes FROM consumo_combustible WHERE YEAR(consumo_combustible.fecha)=2018";
$sql=$conn->fetchAll($consulta);
return $this->render('default/consultag1.html.twig', array('cg' => $sql));
}
//-------------- //- AREA CHART - //--------------
// Get context with jQuery - using jQuery's .get() method.
var areaChartCanvas = $('#areaChart').get(0).getContext('2d')
// This will get the first returned node in the jQuery collection.
var areaChart = new Chart(areaChartCanvas)
var datames = []
datames={{ cg.mes }} ;
var dataimp = [] ;
dataimp={{ cg.imp }} ;
var areaChartData = {
labels : datames,
datasets: [
{
label : 'Electronics',
fillColor : 'rgba(210, 214, 222, 1)',
strokeColor : 'rgba(210, 214, 222, 1)',
pointColor : 'rgba(210, 214, 222, 1)',
pointStrokeColor : '#c1c7d1',
pointHighlightFill : '#fff',
pointHighlightStroke: 'rgba(220,220,220,1)',
data : dataimp
},
{
label : 'Digital Goods',
fillColor : 'rgba(60,141,188,0.9)',
strokeColor : 'rgba(60,141,188,0.8)',
pointColor : '#3b8bba',
pointStrokeColor : 'rgba(60,141,188,1)',
pointHighlightFill : '#fff',
pointHighlightStroke: 'rgba(60,141,188,1)',
data : dataimp
}
]
}
An error is thrown indicating:
Key "mes" for array with keys "0, 1, 2, 3, 4, 5" does not exist in default\consultag1.html.twig at line 55