D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
carlosfelipetorres
Full window
Github gist
Tarea3
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } .bar { fill: steelblue; } .bar:hover { fill: brown; } .axis { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .x.axis path { display: none; } </style> </head> <body> <script> var data = [ {"code":23802620, "grade":4.85}, {"code":23802825, "grade":4.865}, {"code":23801894, "grade":3.24}, {"code":23802926, "grade":5}, {"code":23800661, "grade":3.19}, {"code":23800768, "grade":3.98}, {"code":23800972, "grade":4.89}, {"code":23801922, "grade":3.73}, {"code":23805498, "grade":4.795}, {"code":23805913, "grade":4.85}, {"code":23800311, "grade":2.81}, {"code":23806395, "grade":4.72}, {"code":23808850, "grade":3.85}, {"code":23802872, "grade":2.16}, {"code":23802105, "grade":4.715}, {"code":23809880, "grade":4.92}, {"code":23802056, "grade":4.48}, {"code":23807897, "grade":5.2}, {"code":23807916, "grade":5}, {"code":23801962, "grade":3.62}, {"code":23808246, "grade":4.61}, {"code":23802600, "grade":0.11}, {"code":23808311, "grade":4.7} ]; // set the dimensions and margins of the graph var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; // set the ranges var x = d3.scaleBand() .range([0, width]) .padding(0.1); var y = d3.scaleLinear() .range([height, 0]); // var tip = d3.tip() // .attr('class', 'd3-tip') // .offset([-10, 0]) // .html(function(d) { // return "<strong>Frequency:</strong> <span style='color:red'>" + d.frequency + "</span>"; // }) // append the svg object to the body of the page // append a 'group' element to 'svg' // moves the 'group' element to the top left margin 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 + ")"); // Scale the range of the data in the domains x.domain(data.map(function(d) { return d.code; })); y.domain([0, d3.max(data, function(d) { return d.grade; })]); // append the rectangles for the bar chart svg.selectAll(".bar") .data(data) .enter().append("rect") .on("click", function(d){ console.log(d.grade); }) .attr("class", "bar") .attr("x", function(d) { return x(d.code); }) .attr("width", x.bandwidth()) .attr("y", function(d) { return y(d.grade); }) .attr("height", function(d) { return height - y(d.grade); }); // add the x Axis svg.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); // add the y Axis svg.append("g") .call(d3.axisLeft(y)); </script> </body>
https://d3js.org/d3.v4.min.js