<!DOCTYPE html>
<html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link type="text/css" rel="stylesheet" href="/static/style.css">
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>
    <style type="text/css">
.chart {
  display: block;
  margin: auto;
  margin-top: 40px;
}

text {
  font-size: 11px;
}

rect {
  fill: none;
}
    </style>
  </head>
  <body>
     <div id="body"></div>
    <!--div id="body">
      <div id="footer">
        d3.layout.treemap
        <div class="hint">click or option-click to descend or ascend</div>
        <div><select>
          <option selected="selected" value="size">Size</option>
          <option value="count">Count</option>
        </select></div>
      </div>
    </div-->
<h4>{{ title }}</h4>
{{ time }} 
  <a href="{{ top }}">top</a>
  <a href="{{ json_url }}">json</a>
 {% for item in items %}
  <a href="{{ item.href }}">{{ item.key }}</a>
 {% endfor %}<br/>
  Built with <a href="http://d3js.org">D3.js</a>. Based on
  an <a href="http://bl.ocks.org/tgk/6044254">example</a>
  by <a href="http://bost.ocks.org/mike">Mike Bostock</a>.
  <a href="http://www.ecmwf.int" class="test">ecmwf</a>
  <input name="updateButton" 
         type="button" 
         value="Update" 
         onclick="updateData()" /><br/>
<div id="timer"></div>
<script type="text/javascript">
var seconds_left = 1;
 document.getElementById('timer').innerHTML = 300;
var interval = setInterval(function() {
    document.getElementById('timer').innerHTML -= seconds_left;
    if (document.getElementById('timer').innerHTML <= 10)
    {
        document.getElementById('timer').innerHTML = '!!!';
        clearInterval(interval);
    }
}, 1000);

var jsondata = {{ jsondata | safe }} , 
  WIDTH=640, 
  HEIGHT= 740;

var w = 1280 - 80,
    h = 800 - 180,
    x = d3.scale.linear().range([0, w]),
    y = d3.scale.linear().range([0, h]),
    color = d3.scale.category20c(),
    root,
    node;

var treemap = d3.layout.treemap()
    .round(false)
    .size([w, h])
    .sticky(true)
    .value(function(d) { return d.size; });

var mousemove = function(d) {
  var xPosition = d3.event.pageX + 2;
  var yPosition = d3.event.pageY + 2;

  d3.select("#tooltip")
    .style("left", xPosition + "px")
    .style("top", yPosition + "px");
  d3.select("#tooltip #fullname")
    .text(d["name"])
    .text(d["_status"]);
  d3.select("#tooltip").classed("hidden", false);
};

var mouseout = function() {
  d3.select("#tooltip").classed("hidden", true);
};

var mouseover = function(d) {
  var tok = d;
  var fullname = [tok.name + "\t" + tok._status];
  while (typeof tok.parent === 'object') {
    tok = tok.parent;
    fullname.unshift(tok.name);
  }
  fullname = fullname.join('/');
  $('#tooltip').text(fullname);
}

var svg = d3.select("#body").append("div")
    .attr("class", "chart")
    .style("width", w + "px")
    .style("height", h + "px")
  .append("svg:svg")
    .attr("width", w)
    .attr("height", h)
  .append("svg:g")
    .attr("transform", "translate(.5,.5)");

// d3.json("/static/flare.json", function(data) {
d3.json(jsondata, function(data) {
  node = root = data;

  var nodes = treemap.nodes(root)
      .filter(function(d) { return !d.children; });

  var cell = svg.selectAll("g")
      .data(nodes)
    .enter().append("svg:g")
      .attr("class", "cell")
      .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
      .on("click", function(d) { return zoom(node == d.parent ? root : d.parent); });

  cell.append("svg:rect")
      .attr("width", function(d) { return d.dx - 1; })
      .attr("height", function(d) { return d.dy - 1; })
      .style("fill", function(d) { return color(d.parent.name); });

  cell.on('mouseover', mouseover) // map
      .on("mousemove", mousemove)
      .on("mouseout", mouseout);

  cell.append("svg:text")
      .attr("x", function(d) { return d.dx / 2; })
      .attr("y", function(d) { return d.dy / 2; })
      .attr("dy", ".35em")
      .attr("text-anchor", "middle")
      .text(function(d) { return d.name; })
      .style("opacity", function(d) { d.w = this.getComputedTextLength(); return d.dx > d.w ? 1 : 0; });

  d3.select(window).on("click", function() { zoom(root); });

  d3.select("select").on("change", function() {
    treemap.value(this.value == "size" ? size : count).nodes(root);
    zoom(node);
  });
});

function size(d) {
  return d.size;
}

function count(d) {
  return 1;
}

function zoom(d) {
  var kx = w / d.dx, ky = h / d.dy;
  x.domain([d.x, d.x + d.dx]);
  y.domain([d.y, d.y + d.dy]);

  var t = svg.selectAll("g.cell").transition()
      .duration(d3.event.altKey ? 7500 : 750)
      .attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; });

  t.select("rect")
      .attr("width", function(d) { return kx * d.dx - 1; })
      .attr("height", function(d) { return ky * d.dy - 1; })

  t.select("text")
      .attr("x", function(d) { return kx * d.dx / 2; })
      .attr("y", function(d) { return ky * d.dy / 2; })
      .style("opacity", function(d) { return kx * d.dx > d.w ? 1 : 0; });

  node = d;
  d3.event.stopPropagation();
}

    </script>
  
<div id="tooltip" class="hidden"><p><strong id="fullname"></strong></p></div>

</body></html>
