Skip to content

Instantly share code, notes, and snippets.

@skhanker
Last active March 3, 2016 19:17
Show Gist options
  • Select an option

  • Save skhanker/d9d4e284e5e92db500c8 to your computer and use it in GitHub Desktop.

Select an option

Save skhanker/d9d4e284e5e92db500c8 to your computer and use it in GitHub Desktop.
faculty-pubs-chart
<!DOCTYPE html>
<meta charset="utf-8">
<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>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 200, left: 40},
width = 1600 - margin.left - margin.right,
height = 800 - 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(7);
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 + ")");
<?php
# get query
$query = file_get_contents('https://gist.githubusercontent.com/skhanker/41ad18d122586c18a899/raw/aa4dcc6f2ec9a7fbc8bdbfd5554dd84fd238bb2c/ubc-faculty-pubs-histogram.json');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://oc-index.library.ubc.ca/search",
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => trim(preg_replace('/\s+/', ' ', $query)),
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 3437f71f-a94b-8696-fe39-d78498a8ec48"
),
));
# get data from API
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
} else {
$data = json_decode($response, true);
# we are only concerned with aggregations
$data = $data['data']['data']['aggregations'];
/*
return array(
[ 'term' => $bucket['key'] , 'docCount' => $bucket['doc_count'] ],
[ 'term' => $bucket['key'] , 'docCount' => $bucket['doc_count'] ]
);
*/
$dataset = [];
foreach ($data['subject']['buckets'] as $bucket) {
if (!empty($bucket['key']) && !empty($bucket['doc_count'])) {
$dataset [] = [
'term' => $bucket['key']
, 'docCount' => $bucket['doc_count']
];
}
}
echo "var data = " . json_encode($dataset) . ";";
}
?>
x.domain(data.map(function(d) { return d.term; }));
y.domain([0, d3.max(data, function(d) { return d.docCount; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.attr("y", 0)
.attr("x", 7)
.attr("transform", "rotate(90)")
.style("text-anchor", "start");
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("Count");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.term); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.docCount); })
.attr("height", function(d) { return height - y(d.docCount); });
function type(d) {
d.docCount = +d.docCount;
return d;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment