Dan W
Dan W

Reputation: 365

D3 dynamic JSON labelling

I have an AJAX call which brings me back to data in JSON format.

[{"KEY":"IA","VALUE":"8"},{"KEY":"GE","VALUE":"1"}]

However, the labelling may change for this data depending on some user interaction (selecting from a drop down may invoke a search for some other data) leading to :

[{"NAME":"STEVE","AGE":"54"},{"NAME":"PETE","AGE":"22"}]

So I need some way to just get the first label and data and push it to the X axis like:

|
|
|
|
|_____________
Steve    Pete
Name

and then stick the second label and data up the Y Axis.

so most of the code examples I have seen use some form of d3.name to identify the labels in the returned data but as I need it to dynamically name the axis keys/values Im not sure how I can achieve this.

Also, the JSON data I have is stored in a variable called jdata so I wouldnt use the d3.json method.

The examples im working from is on : http://codepen.io/mrev/pen/waKvbw

JS:

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

// scale to ordinal because x axis is not numerical
var x = d3.scale.ordinal().rangeRoundBands([0, width], .1);

//scale to numerical value by height
var y = d3.scale.linear().range([height, 0]);

var chart = d3.select("#chart")  
              .append("svg")  //append svg element inside #chart
              .attr("width", width+(2*margin.left)+margin.right)    //set width
              .attr("height", height+margin.top+margin.bottom);  //set height
var xAxis = d3.svg.axis()
              .scale(x)
              .orient("bottom");  //orient bottom because x-axis will appear below the bars

var yAxis = d3.svg.axis()
              .scale(y)
              .orient("left");

d3.json("http://codepen.io/superpikar/pen/kcJDf.js", function(error, data){
  x.domain(data.map(function(d){ return d.letter}));
  y.domain([0, d3.max(data, function(d){return d.frequency})]);

  var bar = chart.selectAll("g")
                    .data(data)
                  .enter()
                    .append("g")
                    .attr("transform", function(d, i){
                      return "translate("+x(d.letter)+", 0)";
                    });

  bar.append("rect")
      .attr("y", function(d) { 
        return y(d.frequency); 
      })
      .attr("x", function(d,i){
        return x.rangeBand()+(margin.left/2);
      })
      .attr("height", function(d) { 
        return height - y(d.frequency); 
      })
      .attr("width", x.rangeBand());  //set width base on range on ordinal data

  bar.append("text")
      .attr("x", x.rangeBand()+margin.left )
      .attr("y", function(d) { return y(d.frequency) -10; })
      .attr("dy", ".75em")
      .text(function(d) { return d.frequency; });

  chart.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate("+margin.left+","+ height+")")        
        .call(xAxis);

  chart.append("g")
        .attr("class", "y axis")
        .attr("transform", "translate("+margin.left+",0)")
        .call(yAxis)
        .append("text")
        .attr("transform", "rotate(-90)")
        .attr("y", 6)
        .attr("dy", ".71em")
        .style("text-anchor", "end")
        .text("Frequency");
});

function type(d) {
    d.letter = +d.letter; // coerce to number
    return d;
  }

HTML :

<div id="chart"></div> 

EDITED CODE

 var jsplit =  jdata.split('"');
                    var keyX = jsplit[1];
                    var keyY = "";
                    var data = JSON.parse(jdata);
                    data[0].keys().forEach(function(k) {
                        if (k!=keyX) keyY=k;
                    });


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

// scale to ordinal because x axis is not numerical
                    var x = d3.scale.ordinal().rangeRoundBands([0, width], .1);

//scale to numerical value by height
                    var y = d3.scale.linear().range([height, 0]);

                    var chart = d3.select("#chart")
                        .append("svg")  //append svg element inside #chart
                        .attr("width", width+(2*margin.left)+margin.right)    //set width
                        .attr("height", height+margin.top+margin.bottom);  //set height
                    var xAxis = d3.svg.axis()
                        .scale(x)
                        .orient("bottom");  //orient bottom because x-axis will appear below the bars

                    var yAxis = d3.svg.axis()
                        .scale(y)
                        .orient("left");



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

                        var bar = chart.selectAll("g")
                            .data(data)
                            .enter()
                            .append("g")
                            .attr("transform", function(d, i){
                                return "translate("+x(d[keyX])+", 0)";
                            });

                        bar.append("rect")
                            .attr("y", function(d) {
                                return y(d[keyY]);
                            })
                            .attr("x", function(d,i){
                                return x.rangeBand()+(margin.left/2);
                            })
                            .attr("height", function(d) {
                                return height - y(d[keyY]);
                            })
                            .attr("width", x.rangeBand());  //set width base on range on ordinal data

                        bar.append("text")
                            .attr("x", x.rangeBand()+margin.left )
                            .attr("y", function(d) { return y(d[keyY]) -10; })
                            .attr("dy", ".75em")
                            .text(function(d) { return d[keyY]; });

                        chart.append("g")
                            .attr("class", "x axis")
                            .attr("transform", "translate("+margin.left+","+ height+")")
                            .call(xAxis);

                        chart.append("g")
                            .attr("class", "y axis")
                            .attr("transform", "translate("+margin.left+",0)")
                            .call(yAxis)
                            .append("text")
                            .attr("transform", "rotate(-90)")
                            .attr("y", 6)
                            .attr("dy", ".71em")
                            .style("text-anchor", "end")
                            .text("Frequency");


                    function type(d) {
                        d[keyX] = +d[keyX]; // coerce to number
                        return d;
                    }

Upvotes: 0

Views: 414

Answers (1)

tarulen
tarulen

Reputation: 2100

Quick & dirty trick to find the names of the first and second keys: simply split the json text data around ":

var jsplit =  jdata.split('"');
var keyX = jsplit[1];
var keyY = jsplit[5];

this is assuming your data format doesn't change, and that the " character does not appear within the values


Edit: taking comments into account:

var jsplit =  jdata.split('"');
var keyX = jsplit[1];
var keyY = "";
var data = JSON.parse(jdata);
for (k in data[0]) { 
   if (k!=keyX) keyY=k; 
}

Note that all this code, as well as the rest of the graph building parts, should appear in the callback function from your ajax method. You need to use d[keyX] and d[keyY], respectively, instead of d.letter and d.frequency in your example.


For the labels, .text("Frequency") should be .text(keyY), and you need to add an x label, maybe with (untested):

    .call(xAxis) //add the following lines:
    .append("text")
    .attr("y", 6)
    .attr("dy", ".71em")
    .style("text-anchor", "end")
    .text(keyX);

Upvotes: 1

Related Questions