Skip to content

Commit

Permalink
Add sparkline popchart on server pages
Browse files Browse the repository at this point in the history
  • Loading branch information
amoeba committed Feb 10, 2021
1 parent 15bfc7c commit 31d0455
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
67 changes: 67 additions & 0 deletions assets/javascripts/sparkline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var sparkline = function (selector, data_url) {
var margin = { top: 5, right: 5, bottom: 5, left: 5 },
width = 200 - margin.left - margin.right,
height = 25 - margin.top - margin.bottom;

var parseDate = d3.time.format("%Y%m%d").parse;

d3.json(data_url, function (error, data) {
var data = data["Coldeve"];
var data = data.map(function (d) {
return {
date: parseDate(d.date),
count: +d.count,
};
});

var x = d3.time
.scale()
.domain(
d3.extent(
data.map(function (d) {
return d.date;
})
)
)
.range([0, width]);

var y = d3.scale
.linear()
.domain(
d3.extent(
data.map(function (d) {
return d.count;
})
)
)
.range([height, 0]);

var line = d3.svg
.line()
.interpolate("linear")
.x(function (d) {
return x(d.date);
})
.y(function (d) {
return y(d.count);
});

var svg = d3
.select(selector)
.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 + ")");

svg
.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "green")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line);
});
};
8 changes: 8 additions & 0 deletions views/server.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
= @online[:count]
characters online as of
= AppHelper.relative_time(@online[:created_at])
#sparkline
%tr
%td
Uploaded
Expand Down Expand Up @@ -54,3 +55,10 @@
%td
%a{:href => "/#{character[:server]}/#{character[:name]}"}= character[:name]
%td= AppHelper.relative_time(character[:updated_at])


%script{:src => javascript_path('d3.min')}
%script{:src => javascript_path('sparkline')}

:javascript
sparkline("#sparkline", "/player_counts.json?range=3mo&servers=#{params[:server]}");

0 comments on commit 31d0455

Please sign in to comment.