<!--
    Thank you for inspecting this page! If you find some issues, let me know!
    Library     : pip install d3blocks
    Author      : E.Taskesen
    Mail        : erdogant@gmail.com
    Github      : https://github.com/d3blocks/d3blocks
	References  : d3-graph-gallery.com
 -->

<!DOCTYPE html>
<meta charset="utf-8">

<!-- Load d3.js -->
<script src="d3.v4.js"></script>
<!-- Load color palettes -->
<script src="d3.scale.chromatic.v1.min.js"></script>

<!-- Create a div where the graph will take place -->
<div id="d3_heatmap"></div>

<script>

// set the dimensions and margins of the graph
var margin = {top: 80, right: 25, bottom: 30, left: 40},
  width = $WIDTH$ - margin.left - margin.right,
  height = $HEIGHT$ - margin.top - margin.bottom;

// append the svg object to the body of the page
var svg = d3.select("#d3_heatmap")
.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 + ")");

//Read the data
//d3.csv("$DATA_PATH$", function(data) {

//Store data in variable
var data = 
[
	$DATA_COMES_HERE$
]

  // Labels of row and columns -> unique identifier of the column called 'group' and 'variable'
  var myGroups = d3.map(data, function(d){return d.group;}).keys()
  var myVars = d3.map(data, function(d){return d.variable;}).keys()

  // Build X scales and axis:
  var x = d3.scaleBand()
    .range([ 0, width ])
    .domain(myGroups)
    .padding(0.05);
  svg.append("g")
    .style("font-size", $FONTSIZE_X$)
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x).tickSize(0))
    .select(".domain").remove()

  // Build Y scales and axis:
  var y = d3.scaleBand()
    .range([ height, 0 ])
    .domain(myVars)
    .padding(0.05);
  svg.append("g")
    .style("font-size", $FONTSIZE_Y$)
    .call(d3.axisLeft(y).tickSize(0))
    .select(".domain").remove()

  // Build color scale
  var myColor = d3.$CMAP_TYPE$()
    .interpolator(d3.$CMAP$)
    .domain([$VMIN$, $VMAX$])

  // create a tooltip
  var tooltip = d3.select("#d3_heatmap")
    .append("div")
    .style("opacity", 0)
    .attr("class", "tooltip")
    .style("background-color", "white")
    .style("border", "solid")
    .style("border-width", "2px")
    .style("border-radius", "5px")
    .style("padding", "5px")
    .style("position", "absolute") // SHOW TOOLTIP CORRECTLY WHEN HOVERING

  // Three function that change the tooltip when user hover / move / leave a cell
  var mouseover = function(d) {
    tooltip
      .style("opacity", 0.8)
    d3.select(this)
      .style("stroke", "$STROKE$")
      .style("opacity", 0.8)
  }
  var mousemove = function(d) {
    tooltip
      .html("Exact value: " + d.value)
      .style("left", (d3.mouse(this)[0]+70) + "px")
      .style("top", (d3.mouse(this)[1]) + "px")
  }
  var mouseleave = function(d) {
    tooltip
      .style("opacity", 0)
    d3.select(this)
      .style("stroke", "none")
      .style("opacity", 0.8)
  }

  // add the squares
  svg.selectAll()
    .data(data, function(d) {return d.group+':'+d.variable;})
    .enter()
    .append("rect")
      .attr("x", function(d) { return x(d.group) })
      .attr("y", function(d) { return y(d.variable) })
      .attr("rx", 4)
      .attr("ry", 4)
      .attr("width", x.bandwidth() )
      .attr("height", y.bandwidth() )
      .style("fill", function(d) { return myColor(d.value)} )
      .style("stroke-width", 4)
      .style("stroke", "none")
      .style("opacity", 0.8)
    .on("mouseover", mouseover)
    .on("mousemove", mousemove)
    .on("mouseleave", mouseleave)
//})

// Add title to graph
svg.append("text")
        .attr("x", 0)
        .attr("y", -50)
        .attr("text-anchor", "left")
        .style("font-size", "22px")
        .text("$TITLE$");

// Add subtitle to graph
svg.append("text")
        .attr("x", 0)
        .attr("y", -20)
        .attr("text-anchor", "left")
        .style("font-size", "14px")
        .style("fill", "grey")
        .style("max-width", 400)
        .text("$DESCRIPTION$");


</script>