<!DOCTYPE html>
<head>
	<meta charset="utf-8" />
	<meta name="name" content="Bar chart" />
	<meta name="description" content="Simple bar chart showing frequency of tags in a playlist"/>
	<meta name="mintags" content="1" />
	<meta name="maxtags" content="1" />
	<title>%(title)s</title>

<style>

.bar {
  fill: steelblue;
}

.bar:hover {
  fill: brown;
}

.axis {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.x.axis path {
  display: none;
}

</style>
</head>
<body>
<script src="d3.min.js"></script>
<script>


//
// Generated by the Exaile Playlist Analyzer plugin.
// (C) 2014 Dustin Spicuzza <dustin@virtualroadside.com>
//
// This work is licensed under the Creative Commons Attribution 4.0
// International License. To view a copy of this license, visit
// http://creativecommons.org/licenses/by/4.0/.
//
// Copied mostly verbatim from http://bl.ocks.org/mbostock/3885304
//

var data = %(data)s;
var taglist = %(tagdata)s;

// translate data into something d3 can use.
var vals = d3.map();

// simple frequency counter. probably more interesting things
// we can do with this.. 
data.forEach(function(d){
	
	if (d == null)
		return;
	
	if (d[0] == null) d[0] = '<unknown>'
	
	// some tags are array elements
	if (Array.isArray(d[0]))
		d[0].forEach(function(d2){
			vals.set(d2, (vals.get(d2) || 0) + 1);
		});
	else
		vals.set(d[0], (vals.get(d[0]) || 0) + 1);
});

data = vals.entries().sort(function(a,b){
	return a.key < b.key ? -1 : 1;
});


// generic d3 code follows...

// cheating here because I'm lazy and don't want to calculate
// the label size
var margin = {top: 20, right: 20, bottom: 500, left: 40},
    width = 960 - margin.left - margin.right,
    height = 1000 - margin.top - margin.bottom;

var x = d3.scale.ordinal()
    .rangeRoundBands([0, width], .1);

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")
    .ticks(10);

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 + ")");

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

svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis)
    .selectAll("text")  
            .style("text-anchor", "end")
            .attr("dx", "-.8em")
            .attr("dy", ".15em")
            .attr("transform", function(d) {
                return "rotate(-75)" 
            });

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("Frequency");

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

</script>
