Published
Edited
Nov 2, 2021
Fork of Simple D3
2 forks
1 star
Insert cell
Insert cell
Insert cell
Insert cell
import {desiredData} from "@spepechen/dataprep"
Insert cell
desiredData
Insert cell
Insert cell
numberOfFlowers = 50
Insert cell
//opacity = 0.06
opacity = 0.06
Insert cell
strokeWidth = 0.2
Insert cell
Insert cell
dummyData
Insert cell
// sin cos
magicNumber = 2.7
Insert cell
//outerRadius_ = 160
outerRadius_ = 180
Insert cell
rScaleNum = 3.8
Insert cell
regions = desiredData[0].map(d=>d.region)
Insert cell
Insert cell
regions
Insert cell
Insert cell
import {Scrubber} from "@mbostock/scrubber"
Insert cell
Insert cell
Insert cell
viewof i = Scrubber(Array.from({length: desiredData.length}, (_, i) => i), {delay: 100}, {autoplay: false})
Insert cell
{
const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height]);

svg
.append('g')
.selectAll("ellipse")
.data(desiredData[i])

.join("ellipse")
.attr("cx", 0)
.attr("cy", 0)
.attr("ry", d => rScale(d.deaths))
.attr("rx", d => rScale(d.deaths)/2)
.attr("class", "rotate")
.attr("fill", d => colorByContinent3(d.region, 0.6))
.attr("stroke", "white")
.attr("stroke-width", strokeWidth)
.attr('transform', function(d, i) {
var rx = Math.cos(xcos_(i)) * outerRadius_;
var ry = Math.sin(ysin_(i)) * outerRadius_;
return `translate(${rx},${ry})
rotate(${xcos_(i) * 180 /Math.PI + 90})
scale(${0.8})`;
})
return svg.node()
}
Insert cell
arr__ = [[0], [0,1], [0,1,2]]
Insert cell
arr_sample = [[50], [50,51], [50,51,52]]
Insert cell
arrCreator(3)
Insert cell
arr = arrCreator(desiredData.length)
Insert cell
desiredData.length
Insert cell
function arrInner(n) {
let inner= []
for(let i=0; i < n; i++){
inner.push(i)
}
return inner
}
Insert cell
function arrCreator(n) {
let outer= []
for(let i=0; n >= i; i++){
outer.push(arrInner(i))
}
return outer.splice(1, outer.length - 1)
}
Insert cell
Insert cell
Insert cell
Insert cell
{
const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height]);

// for(let i=0; i < arr.length; i++){
for(let j=0; j < arr[s].length; j++){
svg
.append('g')
.selectAll("ellipse")
.data(desiredData[arr[s][j]])
.join("ellipse")
.attr("cx", 0)
.attr("cy", 0)
.attr("ry", d => rScale(d.deaths))
.attr("rx", d => rScale(d.deaths)/2)
.attr("class", "rotate")
.attr("fill", d => colorByContinent3(d.region, 0.06))
.attr("stroke", "white")
.attr("stroke-width", strokeWidth)
.attr('transform', function(d, i) {
var rx = Math.cos(xcos_(i)) * outerRadius_;
var ry = Math.sin(ysin_(i)) * outerRadius_;
return `translate(${rx},${ry})
rotate(${xcos_(i) * 180 /Math.PI + 90})
scale(${0.8})`;
})
}
// }
return svg.node()
}
Insert cell
{
const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height]);

for(let i=0; i < dummyData.length; i++){
svg
.append('g')
.selectAll("ellipse")
// .data(dummyData[i])
.data(desiredData[i])

.join("ellipse")
.attr("cx", 0)
.attr("cy", 0)
.attr("ry", d => rScale(d.deaths))
.attr("rx", d => rScale(d.deaths)/2)
.attr("class", "rotate")
.attr("fill", d => colorByContinent3(d.region, 0.1))
.attr("stroke", "white")
.attr("stroke-width", strokeWidth)
.attr('transform', function(d, i) {
var rx = Math.cos(xcos_(i)) * outerRadius_;
var ry = Math.sin(ysin_(i)) * outerRadius_;
return `translate(${rx},${ry})
rotate(${xcos_(i) * 180 /Math.PI + 90})
scale(${0.8})`;
})
}
return svg.node()
}
Insert cell
Insert cell
dummyData = new Array(numberOfFlowers)
Insert cell
{
for(let i=0; i< dummyData.length; i++){
let r = [
{region: "Oceania"},
{region: "Central America and the Caribbean"},
{region: "South America"},
{region: "Sub-saharan Africa"},
{region: "Northern Africa & Middle East"},
{region: "North America"},
{region: "Europe"},
{region: "Asia"},
]
r.forEach(element => {element.deaths = Math.floor((Math.random()*5000 ) + 1)})
dummyData[i] = r;
}
return dummyData
}
Insert cell
dummyData
Insert cell
sampleData = FileAttachment("container.json").json()
Insert cell
sampleData.map( d => parseInt(d.deaths))
Insert cell
https://stackoverflow.com/questions/46443710/placing-elements-on-circle-line-and-rotating-them-towards-the-center-using-d3-js
Insert cell
xcos_ = d3.scaleLinear()
.domain([0, sampleData.length])
.range([Math.PI * 0, Math.PI * magicNumber]);
//.range([Math.PI * 0, Math.PI * 1.98]);
Insert cell
ysin_= d3.scaleLinear()
.domain([0, sampleData.length])
.range([Math.PI * 0, Math.PI * magicNumber]);
//.range([Math.PI * 0, Math.PI * 1.98]);
Insert cell
//outerRadius_ = 160
// outerRadius_ = 400
Insert cell
rScale(4000)
Insert cell
rScale= d3.scaleLog()
.domain([minDeaths, maxDeaths])
.range([0, height / rScaleNum])
Insert cell
minDeaths = parseInt(sampleDataSorted[0].deaths)
Insert cell
maxDeaths = parseInt(sampleDataSorted[sampleDataSorted.length -1].deaths)
Insert cell
sampleDataSorted = sampleData.sort(function(a, b) {
return a.deaths - b.deaths;
});
Insert cell
data = d3.range(7)
Insert cell
Insert cell
d3.schemePastel1.map(d => opacityTweak(d,opacity))
Insert cell
function colorByContinent2(expr){
var c = d3.schemePastel1.map(d => opacityTweak(d,opacity))
switch (expr) {
case 'Asia':
return c[0]
break;
case 'Europe':
return c[1]
break;
case 'North America':
return c[2]
break;
case 'Northern Africa & Middle East':
return c[3]
break;
case 'Sub-saharan Africa':
return c[4]
break;
case 'South America':
return c[5]
break;
case 'Central America and the Caribbean':
return c[6]
break;
case 'Oceania':
return c[7]
break;
default:
return c[8]
}}
Insert cell
d3.schemePastel1.map(d => opacityTweak(d,opacity))
Insert cell
color = d3.scaleOrdinal().domain(data)
.range(d3.schemePastel1.map(d => opacityTweak(d,opacity)));
Insert cell
d3.lch('green')
Insert cell
function opacityTweak(color, number) {
const {l, c, h, opacity} = d3.lch(color);
return d3.lch(l, c, h,opacity* number);
}
Insert cell
Insert cell
xcos = d3.scaleLinear()
.domain([0, data.length])
.range([Math.PI * 0, Math.PI * 1.97]);
Insert cell
ysin = d3.scaleLinear()
.domain([0, data.length])
.range([Math.PI * 0, Math.PI * 1.97]);
Insert cell
outerRadius = 120;
Insert cell
html`
<style type="text/css">

.rotate {
transform-box: fill-box;
transform-origin: center;
}

// #svg {
// background-color: red;
// }
</style>
`
Insert cell
d3.range(5)
Insert cell
height = 700
Insert cell
//width = 200
Insert cell
margin = 50
Insert cell
https://docs.aspose.com/svg/net/drawing-basics/svg-path-data/
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more