0

views.py is

def index(request):

fm_ds=Details.objects.filter(Gender='Female',Dept='Software',Shift='Day').count()
m_ds=Details.objects.filter(Gender='Male',Dept='Software',Shift='Day').count()
Female_dict={}
Female_dict['Gender']='Female'
Female_dict['count']=fm_ds
Male_dict={}
Male_dict['Gender']='Male'
Male_dict['count']=m_ds
print Female_dict
print Male_dict
di=[Male_dict,Female_dict]
context = {'data_json': json.dumps(di)}
print context
return render(request, 'index.html', context=context)

output is: [{'count': 1, 'Gender': 'Male'}, {'count': 2, 'Gender': 'Female'}] now i need to pass this json to d3.js template to plot a graph of link of this kind. that is feMALE on x axis and count on y axis.

index.html is

<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */

.bar { fill: steelblue; }

</style>
<body>


<script src="//d3js.org/d3.v4.min.js"></script>
<script>


var margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;


var x = d3.scaleBand()
          .range([0, width])
          .padding(0.1);
var y = d3.scaleLinear()
          .range([height, 0]);

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform",
          "translate(" + margin.left + "," + margin.top + ")");

// get the data
var data_json = {{ data_json|safe }};
var data=[];
data.push(temp);


  x.domain(data.map(function(d) { return d.gender; }));
  y.domain([0, d3.max(data, function(d) { return d.count; })]);


  svg.selectAll(".bar")
      .data(data)
    .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(d.gender); })
      .attr("width", x.bandwidth())
      .attr("y", function(d) { return y(d.count); })
      .attr("height", function(d) { return height - y(d.count); });


  svg.append("g")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));


  svg.append("g")
      .call(d3.axisLeft(y));



</script>
</body>

im getting only either male/female graph with their count...not both!

Preethi
  • 11
  • 7
  • your context is dictionary within a dictionary, so you cannot access it with d.gender, and check the Gender, its Gender and not gender, – Exprator Jul 17 '17 at 07:31
  • how do i access it? can you please help me if you know it! – Preethi Jul 17 '17 at 08:03
  • I think you don't need this: `d3.json( data_json, function(error, data_json) {` since `data_json` will be available - no need to load it through d3. I don't remember and I can't test it but In case `data_json` is a string, you can use JSON.parse to load it as json. After that, you can define your data as let's say`var data = data_json["data_json"]` but I am not sure the json structure you provide is correct for what you want. Don't you want something like `{'data_json': [{"count": 2, "Gender": "Female"}, {"count": 2, "Gender": "Male"}]'}`? – mkaran Jul 17 '17 at 09:16
  • {"count":2 , "Gender":"Male"} is fine. I tried with JSON.parse also...its the same...no output! – Preethi Jul 17 '17 at 09:40
  • Can you `console.log(data_json);` after `var data_json = {{ data_json|safe }};`? Does it output anything? – mkaran Jul 17 '17 at 09:43
  • Also, this might help: https://stackoverflow.com/a/25538871/3433323 – mkaran Jul 17 '17 at 09:46
  • @mkaran thankyou will check it out!:) – Preethi Jul 17 '17 at 10:52

1 Answers1

0

i tried this and got the answer in index.html

<script>

    var temp = {{ data_json|safe }};
    var data=[]
    data.push(temp);
    console.log(JSON.stringify(data))

</script>
Preethi
  • 11
  • 7