Tuesday, July 29, 2014

Re: I don't understand whats wrong with the way im passing django list to json

On Tue, Jul 29, 2014 at 12:10 AM, G Z <zukeru@gmail.com> wrote:
> so I'm trying to display some vm performance data on javascript charts. It
> needs the information as a list, I have tried passing a json list from
> django and a django list like below in the javascript i ahve the one they
> tell me to use and the one im trying the one they tell me to use works
> obviously and i kept it there as reference to see how it needs to be input.
> How do I get the variables between django and javascript in an acceptable
> way?
>
>
>
>
> for stat in vm_stats:
> cpu_values.append(stat[4])
> cpu_json = simplejson.dumps(cpu_values)
>
> context = Context({'user.is_superuser':user.is_superuser,
> 'customers':customers,
> 'vms': vms,
> 'vm_stats': vm_stats,
> 'customer_id': customer_id,
> 'cpu_json':cpu_json,
>
>
> })
>
>
> <script src="{% static 'js/chart.js' %}"></script>
> <script>
> var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
> var lineChartData = {
> labels : ["January","February","March","April","May","June","July"],
> datasets : [
> {
> label: "My First dataset",
> fillColor : "rgba(220,220,220,0.2)",
> strokeColor : "rgba(220,220,220,1)",
> pointColor : "rgba(220,220,220,1)",
> pointStrokeColor : "#fff",
> pointHighlightFill : "#fff",
> pointHighlightStroke : "rgba(220,220,220,1)",
> data : [{{cpu_json}}]
> },
> {
> label: "My Second dataset",
> fillColor : "rgba(151,187,205,0.2)",
> strokeColor : "rgba(151,187,205,1)",
> pointColor : "rgba(151,187,205,1)",
> pointStrokeColor : "#fff",
> pointHighlightFill : "#fff",
> pointHighlightStroke : "rgba(151,187,205,1)",
> data :
> [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
> }
> ]
>
> }
>
> window.onload = function(){
> var ctx = document.getElementById("canvas").getContext("2d");
> window.myLine = new Chart(ctx).Line(lineChartData, {
> responsive: true
> });
> }
>
>
> </script>
>

It doesn't seem like you have done much to debug this yet.

When you look at the generated javascript, does your "data" property
look the same type as the examples, or does it look different. Hint,
in your "My Second dataset", "data" is an array of values - is it an
array of values in "My First dataset".

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1LVRNERePuHSGFHRMtbxB%3DY%3DT3nXDtHr5MSX9Z_NNS2%2BQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment