D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
foolosopher
Full window
Github gist
Russian military conflicts (demo)
<!DOCTYPE html> <html> <meta charset="utf-8"> <!-- Example based on https://bl.ocks.org/mbostock/3887118 --> <!-- Tooltip example from https://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html --> <style> body { font: 11px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .x.axis path { stroke: #bdbdbd; stroke-width:2; } .x.axis .tick line { stroke: #bdbdbd; stroke-width:2; } .dot { stroke: #000; } .hlines { stroke: #f0f0f0; opacity: 1; stroke-width:2; } .tooltip { position: absolute; width: 200px; height: 28px; pointer-events: none; } </style> <body> <h1>История военных конфликтов России (demo)</h1> <script src="https://d3js.org/d3.v3.min.js"></script> <script> var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; /* * value accessor - returns the value to encode for a given data object. * scale - maps value to a visual display encoding, such as a pixel position. * map function - maps from data value to display value * axis - sets up axis */ // setup x var xValue = function(d) { return d.Beg%100;}, // data -> value xScale = d3.scale.linear().range([0, width]), // value -> display xMap = function(d) { return xScale(xValue(d));}, // data -> display xAxis = d3.svg.axis().scale(xScale).orient("bottom"); // setup y var yValue = function(d) { return -Math.trunc(d.Beg/100);}, // data -> value yScale = d3.scale.linear().range([height, 0]), // value -> display yMap = function(d) { return yScale(yValue(d));}, // data -> display yAxis = d3.svg.axis().scale(yScale).orient("left").tickValues([-14, -15, -16, -17, -18, -19]).tickFormat(function(d) { return -d; }).tickSize(10,1); var colors = ["#e41a1c","#AAAAAA","#C9A61A"]; // setup fill color var cValue = function(d) { return d.Result;}; var color = function(d) { return colors[+d]; }; //color = d3.scale.category10(); // add the graph canvas to the body of the webpage 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 + ")"); // add the tooltip area to the webpage var tooltip = d3.select("body").append("div") .attr("class", "tooltip") .style("opacity", 0); // load data d3.csv("wars.csv", function(error, data) { // change string (from CSV) into number format data.forEach(function(d) { d.Beg = +d.Beg; d.End = +d.End; d.Length = +d.Length; d.Murdered = +d.Murdered; }); data = data.sort(function(a, b){ return b["Murdered"]-a["Murdered"]; }); // don't want dots overlapping axis, so add in buffer to data domain //xScale.domain([d3.min(data, xValue)-1, d3.max(data, xValue)+1]); xScale.domain([-1, 101]); yScale.domain([d3.min(data, yValue)-0.5, d3.max(data, yValue)+1]); // x-axis svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .append("text") .attr("class", "label") .attr("x", width) .attr("y", -6) .style("text-anchor", "end") .text("Годы"); // y-axis svg.append("g") .attr("class", "y axis") .attr("transform", "translate(0,-10)") .style("stroke-width",0) .call(yAxis) .append("text") .attr("class", "label") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end"); // draw lines years = [14,15,16,17,18,19]; svg.selectAll(".hlines") .data(years) .enter() .append("line") .classed("hlines",true) .attr('x1', xScale(-5)) .attr('y1',function(d) { return yScale(-d); }) .attr('x2',xScale(100)) .attr('y2',function(d) { return yScale(-d); }); console.log(data); // draw dots svg.selectAll(".dot") .data(data) .enter().append("circle") .attr("class", "dot") .attr("r", function(d) { return 3*d.Murdered; }) .style("stroke-width", 0) .attr("cx", xMap) .attr("cy", yMap) .style("fill", function(d) { return color(cValue(d));}) .style("opacity",0.5) .on("mouseover", function(d) { d3.select(this).transition().duration(200) .style("opacity", .7); tooltip.transition() .duration(200) .style("opacity", .8); tooltip.html(d.Conflict + "(" + d.Beg + "-" + d.End + ")" +"<br/> Погибло: " +Math.trunc(d.Murdered*10000)) .style("left", (d3.event.pageX + 5) + "px") .style("top", (d3.event.pageY - 28) + "px"); }) .on("mouseout", function(d) { d3.select(this).transition().duration(200) .style("opacity", .5); tooltip.transition() .duration(500) .style("opacity", 0); }); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js