0

When I am using this data structure from a javascript source file:

var data = [{'x': 100, 'y': '65'}, 
  {'x': 122, 'y': '57'}, 
  {'x': 144, 'y': '101'}, 
  {'x': 166, 'y': '87'}, 
  {'x': 188, 'y': '8'}, 
  {'x': 210, 'y': '180'}, 
  {'x': 232, 'y': '132'}, 
  {'x': 257, 'y': '190'}, 
  {'x': 276, 'y': '210'}, 
  {'x': 298, 'y': '150'}]

and alert the data in my main file it shows:

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

also it works fine with :

alert(d3.max(data,function(d) { return d.count;}))

But when I using a python as a source file and returns this :

 data = [{'x': 100, 'y': '65'}, 
  {'x': 122, 'y': '57'}, 
  {'x': 144, 'y': '101'}, 
  {'x': 166, 'y': '87'}, 
  {'x': 188, 'y': '8'}, 
  {'x': 210, 'y': '180'}, 
  {'x': 232, 'y': '132'}, 
  {'x': 257, 'y': '190'}, 
  {'x': 276, 'y': '210'}, 
  {'x': 298, 'y': '150'}]

abc=json.dumps(data)
print data

The alert shows also the same:

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

But this does not work:

alert(d3.max(data,function(d) { return d.count;}))

Here is my entire code

function add_py()
{
 var count = $( "#count" ).val();
    $.ajax({
        type: "get",
        url: "abc.py",
        data: {'count': count},
        datatype:"script",
        async: false,
        success: function(response) {
        var data= JSON.parse(response);

        showGraph(data);
        }, // success closed
        error:function(xhr,err)
        {
            alert("Error connecting to server, please contact system administator.");
        }
    })//ajax closed
}


function  showGraph(de)  {
  alert(de) 
  var mySVG = d3.select("body").append("svg");

  mySVG 
.attr("width", 500) 
.attr("height", 500);

  var heightScale = d3.scale.linear()
.domain([0, 963])
.range([0, 400]);

  alert(d3.max(de,function(d) { return d.count;}))
  var heightScale = d3.scale.linear()
  .domain([0, d3.max(de,function(d) { return d.count;})])
  .range([0, 400]);

  var myBars = mySVG.selectAll('circle')
.data(de)
.enter()
.append('svg:circle');

  myBars
.attr('r',20);

  myBars
.attr('cx',function(d,i) {return (i * 22) + 100;})
.attr('cy',function(d,i) {return 400 - heightScale(d.count);});

  }

I understand a data from js and from python file may behave differently , but how to make python data working same like js data. Thanks in advance.

2
  • What is the expected output? Dec 7, 2013 at 12:17
  • You have number values for x and strings for y: it could help if you change them to numbers. Dec 7, 2013 at 12:33

1 Answer 1

0

I don't get exactly how are you mixing JavaScript and Python, I'll just assume you are trying to load the data object as a string from a Python program (perhaps via a server?). In this case, you need to parse the string and convert it to a JSON object (i.e., an internal JavaScript object) by doing this:

data = JSON.parse(data)

in your main file.

EDIT: also make sure that your Python program is outputting your dictionary as a valid JSON format, just printing it won't work, you need something like json.dumps(data).

2
  • @ Emanuele Bezzi I edited my code, the alert alert(d3.max(de,function(d) { return d.count;})) is not working while data came from python, but worked fine when data came from js
    – curiousguy
    Dec 7, 2013 at 12:40
  • @ Emanuele Bezzi , Sorry my fault it should be alert(d3.max(de,function(d) { return d.x;}))
    – curiousguy
    Dec 7, 2013 at 12:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.