D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
mcrosby114
Full window
Github gist
wca_rightPanelHistogram
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v3.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } </style> </head> <body> <script> //See: https://github.com/d3/d3-3.x-api-reference/blob/master/Histogram-Layout.md#_histogram //See: https://www.youtube.com/watch?v=0CZ7-f9wXiM // var rawData = [ 11, 56, 55, 54, 100, 111, 111, 120, 200, 211, 300, 400, 500 ]; // var rawData = [ 100, 200, 300, 400, 500 ]; var rawData = [ 15.1, 16.9 , 16.01, 15.99, 15.91, 15.98, 16.9, 16.9]; var thisProvider = [16.9]; // var rawData = [90, 199, 555, 98, 120]; // var rawData = [0.32, 0.33, 0.45, 0.46, 0.65, 0.64, 0.76, 0.78, 0.77, 0.79, 0.44]; var server_xMin = 15, server_xMax = 17, intervalLarge = 0.4, intervalSmall = 0.2, xMin = Math.floor(d3.min(rawData)), xMax = Math.ceil(d3.max(rawData)), xDomainMin = xMin, xDomainMax = xMax, numBins = Math.round((server_xMax - server_xMin) / intervalSmall) ; var rateDisplayType = "Decimal"; var scaleFormat = (function(type) { switch(type) { case 'Percent': return d3.format('%'); case 'Decimal': return d3.format('.1f'); //fixed 1 decimals case 'Duration': return d3.format(''); //TODO: set this up based on WCA time fmt case 'Integer': return d3.format('d'); case 'Text': return d3.format(''); //TODO: create ordinal scale case 'Currency': return d3.format('$'); default: return d3.format('d'); } })(rateDisplayType); console.log(scaleFormat()); var formatPercent = d3.format('%'); var chartWidth = 310, chartHeight = 100; var dims = { margin : {top: 30, right: 30, bottom: 30, left: 30}, width : function() { return 960 - this.margin.left - this.margin.right }, height : function() { return 500 - this.margin.top - this.margin.bottom } }; var canvasStyle = { 'width' : dims.width() + dims.margin.left + dims.margin.right, 'height' : dims.height() + dims.margin.top + dims.margin.bottom, }; var barStyle = { 'fill' : '#A75B1D', 'shape-rendering' : 'crispEdges', horiz_padding : 1, vert_margin: 15 }; var dotStyle = { 'fill' : '#1A61A4', 'stroke' : '#134b7f', 'stroke-width' : 3, 'r' : 8 }; var labelStyle = { 'text-anchor' : 'middle', 'font-size' : '11px', 'font-family' : 'sans-serif', 'fill' : '#FFFFFF', 'text-rendering' : 'optimizeLegibility' }; var axisStyle = { 'class' : 'x axis', 'transform' : 'translate(0,' + dims.height() + ')', 'fill' : 'none', 'stroke' : '#000', 'shape-rendering' : 'crispEdges', 'font-size' : '11px', 'font-family' : 'sans-serif' }; //Create SVG element -- the canvas var canvas = d3.select("body").append("svg") .attr(canvasStyle) .append("g") .attr('transform', 'translate(' + dims.margin.left + "," + dims.margin.top + ")") ; //Set up x-axis scale var xScale = d3.scale.linear() .domain([xDomainMin, xDomainMax]) .range([0, dims.width()]) .nice() .clamp(true) ; //Generate histogram bins from dataset var histoData = d3.layout.histogram() .bins(xScale.ticks()) (rawData) ; console.log(histoData); //Set up y-axis scale, used for the height of each bar var yScale = d3.scale.linear() .domain([0, d3.max(histoData, function(d) { return d.y; })]) .range([0, dims.height() - barStyle.vert_margin]) .nice() ; //Create x-axis var xAxis = d3.svg.axis() .scale(xScale) .tickFormat(scaleFormat) .orient("bottom") ; //Insert x-axis canvas.append("g") .attr(axisStyle) .call(xAxis) ; //Make dot for current provider var dot = canvas.selectAll("circle") .data(thisProvider) .enter() .append("g") .append("circle") .attr("cx", function(d) { return xScale(d); }) .attr("cy", 0) .attr(dotStyle) ; //Make bars to add to svg var bars = canvas.selectAll(".bar") .data(histoData) .enter() .append("g") ; //Make rects for each bar bars.append("rect") .attr("x", function(d) { //horiz positions (left of each bar) return xScale(d.x) + barStyle.horiz_padding; }) .attr("y", function(d) { //vert positions (top of each bar) return dims.height() - yScale(d.y); //how far down to start drawing each bar }) .attr("width", function(d) { return xScale(d.x + d.dx) - xScale(d.x) - barStyle.horiz_padding; }) .attr("height", function(d) { return yScale(d.y); }) .attr(barStyle) ; //Add text labels inside each bar bars.append("text") .attr("x", function(d) { return xScale(d.x) + barStyle.horiz_padding; }) .attr("dx", function(d) { return ( xScale(d.x + d.dx) - xScale(d.x) - barStyle.horiz_padding ) / 2; }) .attr("y", function(d) { return dims.height() - yScale(d.y); }) .attr("dy", function(d) { // return (height - (height - yScale(d.y))) / 8; return 30; }) .text(function(d) { return d.y; }) .attr(labelStyle) ; // //Make rects and add to svg // canvas.selectAll("rect") // .data(histoData) // .enter() // .append("rect") // .attr("x", function(d, i) { // return i * (width / histoData.length); // }) // .attr("y", function(d) { // return yScale(d.y); // }) // .attr("width", width / histoData.length - barPadding) // .attr("height", function(d) { // return yScale(d.y);; // }) // .attr("fill", function() { // return barFill; // }); // var bar = canvas.selectAll(".bar") // .data(histoData) // .enter().append("g") // .attr("class", "bar") // .attr("transform", function(d) { return "translate(" + xScale(d.x) + "," + yScale(d.y) + ")"; }); // bar.append("rect") // .attr("x", 1) // .attr("width", xScale(histoData[0].dx) + 200) // .attr("height", function(d) { return height - yScale(d.y); }); // bar.append("text") // .attr("dy", ".75em") // .attr("y", 6) // .attr("x", xScale(histoData[0].dx) / 2) // .attr("text-anchor", "middle") // .text(function(d) { return formatCount(d.y); }); // canvas.append("g") // .attr("class", "x axis") // .attr("transform", "translate(0," + height + ")") // .call(xAxis); // //In-bar value labels // canvas.selectAll("text") // .data(dataset) // .enter() // .append("text") // .text(function(d) { // return d; // }) // .attr("text-anchor", "middle") // .attr("x", function(d, i) { // return i * (w / dataset.length) + (w / dataset.length - barPadding) / 2; // }) // .attr("y", function(d) { // return h - (d * 4) + 14; // }) // .attr("font-family", "sans-serif") // .attr("font-size", "11px") // .attr("fill", "white"); </script> </body>
https://d3js.org/d3.v3.min.js