{
const width = 800;
const height = 200;
const margin = 25;
const planets = [
{
name: "Mercury",
size: 12878,
distance: 0.39,
outerColor: "#666699",
coreColor: "#cccccc"
},
{
name: "Venus",
size: 12104,
distance: 0.723,
outerColor: "#ffcc00",
coreColor: "#995522"
},
{
name: "Earth",
size: 12756,
distance: 1,
outerColor: "#33ccff",
coreColor: "#228800"
},
{
name: "Mars",
size: 6787,
distance: 1.524,
outerColor: "#ff6666",
coreColor: "#774422"
},
{
name: "Jupiter",
size: 142796,
distance: 5.203,
outerColor: "#ff9977",
coreColor: "#997755"
},
{
name: "Saturn",
size: 120660,
distance: 9.539,
outerColor: "#ddcc66",
coreColor: "#666666"
},
{
name: "Uranus",
size: 51118,
distance: 19.18,
outerColor: "#99ccff",
coreColor: "#6688ff"
},
{
name: "Neptune",
size: 48600,
distance: 30.06,
outerColor: "#33cccc",
coreColor: "#55ffff"
}
];
const svg = (d3.create("svg").attr("viewBox", [0 , 0 , width , height]));
const bg = svg
.append ("rect")
.attr ("x" , 0)
.attr ("y" , 0)
.attr ("width" , width)
.attr ("height", height)
.attr ("fiil", "#000");
for (let i = 0; i < 100; i++) {
const star = svg
.append ("circle")
.attr ("cx", Math.random() * width)
.attr ("cy", Math.random() * height)
.attr ("r", Math.random() * 2)
.attr ("opacity", Math.random())
.attr ("fill", "#fff")
.attr ("class", "stars");
}
const defs = svg.append("defs");
for (let i = 0; i < planets.length; i++) {
const gradient = defs.append("radialGradient").attr("id", planets[i].name);
gradient
.append ("stop")
.attr ("offset", "0%")
.attr ("stop-color", planets[i].coreColor);
gradient
.append ("stop")
.attr ("offset", "100%")
.attr ("stop-color", planets[i].outerColor);
}
const sizeScale = d3
.scaleLinear ()
.domain (d3.extent(planets, (d) => d.size))
.range ([3,50]);
const distanceScale = d3
.scaleLinear ()
.domain (d3.extent(planets, (d) => d.distance))
.range ([margin, width-margin]);
for (let i = 0; i < planets.length; i++) {
const planet = svg
.append ("circle")
.attr ("cx", distanceScale(planets[i].distance))
.attr ("cy", height / 2)
.attr ("r", sizeScale(planets[i].size))
.attr ("id", "name")
.style ("fill", "url(#" + planets[i].name + ")");
}
const sol = svg
.append ("rect")
.attr ("x", "0")
.attr ("y", "0")
.attr ("width", "5")
.attr ("height", height)
.attr ("fill", "#fd0");
return svg.node();
}