Created
February 2, 2012 23:51
-
-
Save enthal/1726550 to your computer and use it in GitHub Desktop.
Example of continuous function graphing in d3 (using axis() for axes and grid lines)
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>D3 - Line Chart - continuous function</title> | |
| <!-- script type="text/javascript" src="https://raw.github.com/jquery/sizzle/master/sizzle.js"></script --> | |
| <script type="text/javascript" src="http://mbostock.github.com/d3/d3.min.js"></script> | |
| <script type="text/javascript" src="http://mbostock.github.com/d3/d3.time.min.js"></script> | |
| <style type="text/css"> | |
| body { | |
| font: 10px sans-serif; | |
| } | |
| .mouse_area { | |
| opacity: 0; | |
| } | |
| .rules line, .rules path { | |
| shape-rendering: crispEdges; | |
| stroke: #000; | |
| } | |
| .rules .tick { | |
| } | |
| .rules .minor { | |
| stroke: #BBB; | |
| } | |
| .rules .domain { | |
| fill: none; | |
| } | |
| .grid .tick { | |
| stroke: #CCC; | |
| } | |
| .series path { | |
| fill: none; | |
| stroke: #348; | |
| stroke-width: 5px; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <script type="text/javascript"> | |
| var w = 760; | |
| var h = 400; | |
| var x = d3.scale.linear().domain([-1, 1]).range([0,w]); | |
| var y = d3.scale.linear().domain([ 0, 1]).range([h,0]); | |
| var pad = 50; | |
| var svg = d3.select("body") | |
| .append("svg:svg") | |
| .attr("height", h + pad) | |
| .attr("width", w + pad) | |
| var vis = svg.append("svg:g") | |
| .attr("transform", "translate(40,20)") | |
| var legend = d3.select("body").append("div") | |
| .classed("legend", true) | |
| .text("Try mousing over the graph!") | |
| make_rules(); | |
| chart_line(); | |
| make_mouse_area(); | |
| function square(xi) { return Math.pow(xi,2) } | |
| function chart_line() { | |
| var g = vis.append("svg:g") | |
| .classed("series", true) | |
| g.append("svg:path") | |
| .attr("d", function(d) { return d3.svg.line()( | |
| x.ticks(100).map(function(xi) { | |
| return [ x(xi), y(square(xi)) ] | |
| }) | |
| )}) | |
| } | |
| function make_mouse_area() { | |
| vis.append("svg:rect") | |
| .classed("mouse_area", true) | |
| .attr("width", w) | |
| .attr("height", h) | |
| .on("mousemove", update_legend_values) | |
| .on("mouseout", blank_legend_values) | |
| } | |
| function update_legend_values() { | |
| xi = x.invert(d3.svg.mouse(this)[0]); | |
| legend.text("x: "+xi+ " | y: "+square(xi)); | |
| } | |
| function blank_legend_values() { | |
| legend.text(""); | |
| } | |
| function make_rules() { | |
| var rules = vis.append("svg:g").classed("rules", true) | |
| function make_x_axis() { | |
| return d3.svg.axis() | |
| .scale(x) | |
| .orient("bottom") | |
| .ticks(8) | |
| } | |
| function make_y_axis() { | |
| return d3.svg.axis() | |
| .scale(y) | |
| .orient("left") | |
| .ticks(10) | |
| } | |
| rules.append("svg:g").classed("grid x_grid", true) | |
| .attr("transform", "translate(0,"+h+")") | |
| .call(make_x_axis() | |
| .tickSize(-h,0,0) | |
| .tickFormat("") | |
| ) | |
| rules.append("svg:g").classed("grid y_grid", true) | |
| .call(make_y_axis() | |
| .tickSize(-w,0,0) | |
| .tickFormat("") | |
| ) | |
| rules.append("svg:g").classed("labels x_labels", true) | |
| .attr("transform", "translate(0,"+h+")") | |
| .call(make_x_axis() | |
| .tickSize(5) | |
| // .tickFormat(d3.time.format("%Y/%m")) | |
| ) | |
| rules.append("svg:g").classed("labels y_labels", true) | |
| .call(make_y_axis() | |
| .tickSubdivide(1) | |
| .tickSize(10, 5, 0) | |
| ) | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment