0

I'm getting some information from my database and holding that information in an array. I've used {{dump(weights)}} in my twig template, see below the dump of the array:

enter image description here

If i use weights|json_encode() I can get this information:

enter image description here

Which is okay, but my graph needs the data in a format like this - [69,72].

For example, this is how the code should look for the graph:

var geckoWeight = {
  labels: dates,
  datasets: [
    {
      label: "Front",
      fillColor: "rgba(0, 0, 0, 0.15)",
      strokeColor: gradient,
      pointColor: gradient,
      pointStrokeColor: "#202b33",
      pointHighlightStroke: "rgba(225,225,225,0.9)",
      data: [69,72]
    }
  ]
};

What is the best way to manipulate my data to suit what is needed in the graph data?

3
  • I'm using chart.js for this Commented May 30, 2015 at 22:28
  • Either use twig to transform the data, or do it in JS Commented May 30, 2015 at 22:29
  • @EJTH can you give any information on getting twig to transform it? Commented May 30, 2015 at 22:31

3 Answers 3

2

I would do something like this (in a Model method or controller):

class MyController extends Controller
{

    public function someAction()
    {
        $data = array(
            array('weight' => 69),
            array('weight' => 72),
        );

       $graphData = array_map(function($i) { return $i['weight']; }, $data);

        return $this->render('default/index.html.twig', array(           
            'data' => $weights,
        ));
    }

On my twig template:

<script>
    var weights = {{ data|json_encode }};
</script>
Sign up to request clarification or add additional context in comments.

11 Comments

I think the way i'm doing it at the moment is the long way round of doing it. I will explore this option in a minute and let you know how i get on :)
I've tried this, however the dump just returns []
Well, It works for me. What $graphData returns in your controller?
Nothing, it's just []
hmm... So how your weight looks like? I think that's different that you have shown.
|
0

You could just do that in the JS code:

var myWeights = {{ weights|json_encode() }};
var myWeightsArr = [];
for(var i in myWeights){
   if(myWeights.hasOwnProperty(i))
   myWeightsArr.push(myWeights[i].weight);
}

3 Comments

Doesn't appear to work if i set data: myWeightsArr
Can you get me the source of what is being output ?
It's just putting 69,72 which is kind of right, but as you can see in the code example, it needs to be [69,72]
0

Based on Your input data, something like this, should give You requested output

[
{% for weightsArray in weights %}
    {% if loop.index0 > 0 %},{% endif %}
    {% for weight in weightsArray %}
        {{ weight }}
    {% endfor %}
{% endfor %}
]

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.