Published
Edited
Nov 19, 2020
Insert cell
md`# rotate shape`
Insert cell
width = 300
Insert cell
height = 300
Insert cell
o = [0, 0]
Insert cell
start = [300, 0]
Insert cell
md`## 正方形`
Insert cell
chart = {
let svg = d3.create('svg').attr("viewBox", [0, 0, width, height]);
let group = svg.append('g')
.attr('id', 'lines')
.attr('transform', `translate(${width/2}, ${height/2})`);
let PI = Math.PI;
let x = 100;
let y = 0;
for(let i = 0; i <= 3; i ++) {
for(let alpha = (2 * i - 1) * PI / 4 ; alpha < (PI / 4 * (i * 2 + 1)); alpha += offset_degree) {
if(i === 0) {
x = 100;
y = Math.tan(alpha) * x;
} else if(i === 1) {
y = 100;
x = y / Math.tan(alpha);
} else if(i === 2) {
x = -100;
y = Math.tan(alpha) * x;
} else if(i === 3) {
y = -100;
x = y / Math.tan(alpha);
}
group
.append("line")
.attr('x1', 0)
.attr('y1', 0)
.attr('x2', x)
.attr('y2', y)
// .attr('stroke', d3.interpolateRainbow(alpha * 2 / PI ))
.attr('stroke', 'steelblue')
.attr('stroke-width', 0.2);
}
}
return svg.node();
}
Insert cell
md`## 圆形`
Insert cell
chart1 = {
let svg = d3.create('svg').attr("viewBox", [0, 0, width, height]);
let group = svg.append('g')
.attr('id', 'lines')
.attr('transform', `translate(${width/2}, ${height/2})`);
let PI = Math.PI;
let w = 100;
let y = 0;
for(let i = 0; i <= 3; i ++) {
for(let alpha = (2 * i - 1) * PI / 4 ; alpha < (PI / 4 * (i * 2 + 1)); alpha += offset_degree) {
let x2 = Math.sin(alpha) * w;
let y2 = Math.cos(alpha) * w;
group
.append("line")
.attr('x1', 0)
.attr('y1', 0)
.attr('x2', x2)
.attr('y2', y2)
.attr('stroke', d3.interpolateRainbow(alpha / PI / 2 ))
// .attr('stroke', 'steelblue')
.attr('stroke-width', 0.2);
}
}
return svg.node();
}
Insert cell
md `# cos(cos(s))`
Insert cell
chart2 = {
let svg = d3.create('svg').attr("viewBox", [0, 0, width, height]);
let group = svg.append('g')
.attr('id', 'lines')
.attr('transform', `translate(${width/2}, ${height/2})`);
let PI = Math.PI;
let w = 100;
let y = 0;
for(let alpha = -PI; alpha < PI; alpha += offset_degree) {
let x2 = sin(sin(alpha / 10) * PI ) * w ;
let y2 = cos(cos(alpha * 2)) * w ;
group
.append("line")
.attr('x1', 0)
.attr('y1', 0)
.attr('x2', x2)
.attr('y2', y2)
.attr('stroke', d3.interpolateRainbow(alpha / PI / 2 ))
// .attr('stroke', 'steelblue')
.attr('stroke-width', 0.2);
}
return svg.node();
}
Insert cell
offset_degree = 0.005;
Insert cell
PI = Math.PI;
Insert cell
sin = Math.sin;
Insert cell
cos = Math.cos;
Insert cell
d3 = require('d3@5')
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more