0 Replies Latest reply on Jan 22, 2014 8:02 AM by Runkle

    d3js graph within Edge Animate

    Runkle

      Hi World,

       

      I am trying insert a d3js graph within a div(named 'placeholder') in Edge, plus with the d3js function drawing data from an external tsv file. With the d3js graph drawn within the placeholder then I can animate the placeholder div. However, I'm having trouble of making the d3js function work within edge and thus graph never appears.

       

      I have seen Flot working within Edge but I'm hoping to get d3 working. Can anyone offer some guidance?

       

      I'll be honest, coding is not my strongest skill so I might be making some dumb mistake somewher.

      Below is the code I have got so far:

       

       

      yepnope(
      {
      nope:[
      'js/d3.v3.js',
      ],
      complete: init
      }
      );
      //when yepnope has loaded everything execute init();
      
      
      
      
      function init (){
      //initialise your variables and Edge comp here
      var margin = {top: 20, right: 50, bottom: 30, left: 50},
          width = 840 - margin.left - margin.right,
          height = 500 - margin.top - margin.bottom;
      
      
      var parseDate = d3.time.format("%d-%b-%y").parse,
          bisectDate = d3.bisector(function(d) { return d.date; }).left,
          formatValue = d3.format(",.2f"),
          formatCurrency = function(d) { return "£" + formatValue(d); };
      
      
      var x = d3.time.scale()
          .range([0, width]);
      
      
      var y = d3.scale.linear()
          .range([height, 0]);
      
      
      var xAxis = d3.svg.axis()
          .scale(x)
          .orient("bottom");
      
      
      var yAxis = d3.svg.axis()
          .scale(y)
          .orient("left");
      
      
      var line = d3.svg.line()
          .x(function(d) { return x(d.date); })
          .y(function(d) { return y(d.close); });
      
      
      var svg = d3.select("placeholder").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 + ")");
      
      
      
      
      //external tsv data 
      d3.tsv("data2.tsv", function(error, data) {
        data.forEach(function(d) {
          d.date = parseDate(d.date);
          d.close = +d.close;
        });
      
      
        data.sort(function(a, b) {
          return a.date - b.date;
        });
      
      
        x.domain([data[0].date, data[data.length - 1].date]);
        y.domain(d3.extent(data, function(d) { return d.close; }));
      
      
        svg.append("g")
            .attr("class", "x axis")
            .attr("transform", "translate(0," + height + ")")
            .call(xAxis);
      
      
        svg.append("g")
            .attr("class", "y axis")
            .call(yAxis)
          .append("text")
            .attr("transform", "rotate(-90)")
            .attr("y", 6)
            .attr("dy", ".71em")
            .style("text-anchor", "end")
            .text("Price (£)");
      
      
        svg.append("path")
            .datum(data)
            .attr("class", "line")
            .attr("d", line);
      
      
                //d3 graph ui
        var focus = svg.append("g")
            .attr("class", "focus")
            .style("display", "none");
      
      
        focus.append("circle")
            .attr("r", 6.5);
      
      
        focus.append("text")
            .attr("x", 12)
            .attr("dy", ".35em");
      
      
        svg.append("rect")
            .attr("class", "overlay")
            .attr("width", width)
            .attr("height", height)
            .on("mouseover", function() { focus.style("display", null); })
            .on("mouseout", function() { focus.style("display", "none"); })
            .on("mousemove", mousemove);
      
      
        function mousemove() {
          var x0 = x.invert(d3.mouse(this)[0]),
              i = bisectDate(data, x0, 1),
              d0 = data[i - 1],
              d1 = data[i],
              d = x0 - d0.date > d1.date - x0 ? d1 : d0;
          focus.attr("transform", "translate(" + x(d.date) + "," + y(d.close) + ")");
          focus.select("text").text(formatCurrency(d.close));
        }