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 Ridgeline Plot</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>
// set the dimensions and margins of the graph var margin = {top: 60, right: 30, bottom: 20, left:110}, 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 + ")"); //read data d3.csv("https://raw.githubusercontent.com/zonination/perceptions/master/probly.csv", function(data) { // Get the different categories and count them var categories = data.columns var n = categories.length // Add X axis var x = d3.scaleLinear() .domain([-10, 140]) .range([ 0, width ]); svg.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); // Create a Y scale for densities var y = d3.scaleLinear() .domain([0, 0.4]) .range([ height, 0]); // Create the Y axis for names var yName = d3.scaleBand() .domain(categories) .range([0, height]) .paddingInner(1) svg.append("g") .call(d3.axisLeft(yName)); // Compute kernel density estimation for each column: var kde = kernelDensityEstimator(kernelEpanechnikov(7), x.ticks(40)) // increase this 40 for more accurate density. var allDensity = [] for (i = 0; i < n; i++) { key = categories[i] density = kde( data.map(function(d){ return d[key]; }) ) allDensity.push({key: key, density: density}) } // Add areas svg.selectAll("areas") .data(allDensity) .enter() .append("path") .attr("transform", function(d){return("translate(0," + (yName(d.key)-height) +")" )}) .datum(function(d){return(d.density)}) .attr("fill", "#69b3a2") .attr("stroke", "#000") .attr("stroke-width", 1) .attr("d", d3.line() .curve(d3.curveBasis) .x(function(d) { return x(d[0]); }) .y(function(d) { return y(d[1]); }) ) }) // This is what I need to compute kernel density estimation function kernelDensityEstimator(kernel, X) { return function(V) { return X.map(function(x) { return [x, d3.mean(V, function(v) { return kernel(x - v); })]; }); }; } function kernelEpanechnikov(k) { return function(v) { return Math.abs(v /= k) <= 1 ? 0.75 * (1 - v * v) / k : 0; }; }
terminal
JavaScript Console
Preview
Clear
close
›
Run