Created
April 13, 2018 16:10
-
-
Save ebdavison/945631a03d014a60621a4fe492a2292d to your computer and use it in GitHub Desktop.
D3 code with variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.graph { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.x.axis path { | |
display: none; | |
} | |
.line { | |
fill: none; | |
stroke: steelblue; | |
stroke-width: 2px; | |
} | |
.sparkcircle { | |
fill: #f00; | |
stroke: none; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'oic/base.html' %} | |
{% load static %} | |
{% block statgraphcss %} | |
<link rel="stylesheet" type="text/css" href="{% static 'oic/css/stat_graph.css' %}" /> | |
{% endblock %} | |
{% block body_block %} | |
<!-- stat_graph.html --> | |
<div class="col-sm-8 col-sm-offset-2"> | |
<h2>Graph of {{statname}}</h2> | |
<hr> | |
<span class="graph"></span> | |
<!-- statid is a number, like 1, 2, 3, etc. --> | |
<script> | |
var statid = {{statid}}; | |
</script> | |
<script src="{% static 'oic/js/d3.v3.js' %}"></script> | |
<script src="{% static 'oic/js/stat_graph.js' %}"></script> | |
</div> | |
{% endblock %} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//function statgraph(statid) { | |
// had this hard-coded and worked just fine | |
// but with changes to HTML for "global" JS variable, no longer works and is "undefined" | |
// per the web site logs for the API call to get the data | |
// var statid = 1; | |
var margin = {top: 20, right: 20, bottom: 30, left: 50}, | |
width = 750 - margin.left - margin.right, | |
height = 400 - margin.top - margin.bottom; | |
//var parseDate = d3.time.format("%Y-%m-%d").parse; // for dates like "2014-01-01" | |
//var parseDate = d3.time.format("%Y-%m-%dT00:00:00Z").parse; // for dates like "2014-01-01T00:00:00Z" | |
var parseDate = d3.time.format("%Y-%m-%d").parse; // for dates like "2014-01-01T00:00:00Z" | |
var x = d3.time.scale() | |
.range([0, width]); | |
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"); | |
var line = d3.svg.line() | |
//.x(function(d) { return x(d.month); }) | |
.x(function(d) { return x(d.dt); }) | |
.y(function(d) { return y(d.value); }); | |
var svg = d3.select(".graph").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 + ")"); | |
//d3.json("{% url "play_count_by_month" %}", function(error, data) { | |
//d3.json("{% url "queue_count_by_month" %}", function(error, data) { | |
//d3.json("/oic/api/get_stat_values/1", function(error, data) { | |
d3.json("/oic/api/get_stat_values/"+statid, function(error, data) { | |
data.forEach(function(d) { | |
//d.month = parseDate(d.month); | |
d.dt = parseDate(d.dt); | |
}); | |
//x.domain(d3.extent(data, function(d) { return d.month; })); | |
x.domain(d3.extent(data, function(d) { return d.dt; })); | |
y.domain(d3.extent(data, function(d) { return d.value; })); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
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("Value"); | |
svg.append("path") | |
.datum(data) | |
.attr("class", "line") | |
.attr("d", line); | |
svg.selectAll(".dot") | |
.data(data) | |
.enter().append("circle") | |
.attr("class", "sparkcircle") | |
.attr("r", 2.5) | |
.attr("cx", function(d) { return x(d.dt); }) | |
.attr("cy", function(d) { return y(d.value); }); | |
}); | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment