1

I'm using this script to have this chart 1 .

<script>

$(function () {
$('#container1').highcharts({
    chart: {
        type: 'column',
        options3d: {
            enabled: true,
            alpha: 10,
            beta: 25,
            depth: 70
        }
    },
    title: {
        text: 'Nombre De Demande Par Categorie'
    },
    subtitle: {

    },
    plotOptions: {
        column: {
            depth: 25
        }
    },
    xAxis: {
        categories: ['Education','Beauté Et Santé','Livraison','Maison Et               Jardin','Mission Et Affaire','Evénement Et Restauration','Travaux Informatique'] 
    },
    yAxis: {
        title: {
            text: null
        }
    },
    series: [{
        name: 'Demandes',
        data: [1,5,7,0,null,1,2]
    }]
});
});
</script>

But the line "data" must have variable from my data base:

series: [{
        name: 'Demandes',
        data: [1,5,7,0,null,1,2]
    }]

so i created a new function in my repository:

class DemandeRepository extends \Doctrine\ORM\EntityRepository
{
public function getNb($categorie_id)
{
    $query = $this->createQueryBuilder('u');
    $query->SELECT ('COUNT(u)');
    $query->join('u.service','s');
    $query->where('s.Categorie = :id');
    $query->setParameter('id',$categorie_id);

    return $query->getQuery()->getSingleScalarResult();

}

}

then in the controller i included the function:

class DashbroadController extends Controller
{
public function dashbroadAction()
{   
    $em = $this->getDoctrine()->getManager();

    $Categorie = $em->getRepository("tutoBackofficeBundle:Categorie")->findAll();
    $nb=array();
    foreach ($Categorie as $categorie){
        $nb[]=array(
            'categorie'=>$categorie,
            'count'=>$em->getRepository("tutoBackofficeBundle:Demande")->getNb($categorie)
            );
    }

    return $this->render('tutoBackofficeBundle:Dashbroad:dashbroad.html.twig',array('nb'=>$nb));
}
}

And in the twig :

{% for n in nb %}
{{ n.categorie }}  -> {{ n.count }}<br>

Now I want to have something like that:

data: [{{ n.count }}]

But I don't know how to pass my variable from Twig to Java Script .

3
  • You should describe the content of the nb variable. It's hard to guess with images only. Do you want to put all the data on the chart or only one value? Commented May 13, 2016 at 21:37
  • i have edited my question @A.L Commented May 13, 2016 at 22:02
  • and no i'm not from Epitech @AlainTiemblo Commented May 13, 2016 at 22:03

1 Answer 1

1

As far as I understand, data contains the list of count without any label.

So I guess this will do the job:

series: [{
    name: 'Demandes',
    data: [{% for n in nb %}{% if not loop.first %},{% endif %}{{ n.count }}{% endfor %}]
}]
Sign up to request clarification or add additional context in comments.

3 Comments

no it doesn't work even the chart didn't display @ Alain Tiemblo
@AlainTiemblo it looks like you forgot to put {{ }} around n.count.
thanks for your answers, the chart is now rendering exact values but when i try to get to any other entity ,i get this error . Variable "nb" does not exist in ":back:base.html.twig" at line 606 (500 Internal Server Error)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.