Liveweave: Generative AI Web Editor & HTML/CSS/JS Playground
Initializing
Liveweave
Web
expand_more
home
Home
Palette
Color Explorer
arrow_outward
Polyline
Graphics Editor
arrow_outward
frame_source
Python Playground
arrow_outward
build
Tools
expand_more
restart_alt
Load "Hello Weaver!"
post_add
Generate Lorem ipsum...
code
Format HTML
code_blocks
Format CSS
data_object
Format JavaScript
library_add
Library
expand_more
A
Algolia JS
Animate CSS
Apex Charts JS
B
Bulma CSS
Bootstrap
C
Chart JS
Chartist
Create JS
D
D3
Dojo
F
Foundation
Fullpage JS
G
Granim JS
Google Charts
H
Halfmoon
J
jQuery
M
Materialize
Moment JS
Masonry JS
Milligram CSS
P
Pure CSS
Primer CSS
Popper JS
Pattern CSS
Picnic CSS
R
React JS
Raphael JS
Raisin CSS
S
Semantic UI
Skeleton CSS
Spectre CSS
Tachyons CSS
T
Tailwind
Three JS
U
UI Kit
Vis JS
W
Water CSS
download
Download
expand_more
developer_mode
Download as HTML
folder_zip
Download as .ZIP
cloud_upload
Fork
account_circle
Login
settings
Settings
expand_more
Editor font
14
px
A
A
Line number
Mini map
Word wrap
sync_alt
Reset Settings
smart_display
Run
left_panel_close
AI Assistant
dashboard
All
HTML
CSS
JS
visibility
Preview
bolt
Live Preview
terminal
JS Console
<!DOCTYPE html> <html> <head> <title>Simple D3 Histogram</title> <script src="https://d3js.org/d3.v4.js"></script> </head> <body> <!-- Start your code here --> <!-- Create a div where the graph will take place --> <div id="my_dataviz"></div> <!-- End your code here --> </body> </html>
// Write JavaScript here // set the dimensions and margins of the graph var margin = {top: 10, right: 30, bottom: 30, left: 40}, width = 460 - margin.left - margin.right, height = 400 - margin.top - margin.bottom; // append the svg object to the body of the page var svg = d3.select("#my_dataviz") .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 + ")"); // get the data d3.csv("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/1_OneNum.csv", function(data) { // X axis: scale and draw: var x = d3.scaleLinear() .domain([0, 1000]) // can use this instead of 1000 to have the max of data: d3.max(data, function(d) { return +d.price }) .range([0, width]); svg.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); // set the parameters for the histogram var histogram = d3.histogram() .value(function(d) { return d.price; }) // I need to give the vector of value .domain(x.domain()) // then the domain of the graphic .thresholds(x.ticks(70)); // then the numbers of bins // And apply this function to data to get the bins var bins = histogram(data); // Y axis: scale and draw: var y = d3.scaleLinear() .range([height, 0]); y.domain([0, d3.max(bins, function(d) { return d.length; })]); // d3.hist has to be called before the Y axis obviously svg.append("g") .call(d3.axisLeft(y)); // append the bar rectangles to the svg element svg.selectAll("rect") .data(bins) .enter() .append("rect") .attr("x", 1) .attr("transform", function(d) { return "translate(" + x(d.x0) + "," + y(d.length) + ")"; }) .attr("width", function(d) { return x(d.x1) - x(d.x0) -1 ; }) .attr("height", function(d) { return height - y(d.length); }) .style("fill", "#69b3a2") });
terminal
JavaScript Console
Preview
Clear
close
›
Run