Published
Edited
Dec 31, 2020
Insert cell
md`# sector`
Insert cell
{
let startAngle = 10
let endAngle = 20
let width = 800
let height = 800
let cx = width / 2
let cy = height / 2
let radius = 10
let degree_offset = 1
let context = DOM.context2d(width, height)
d3.select(context.canvas).call(
d3.zoom()
.scaleExtent([1/4, 4])
.on("zoom", render)
);
function render(transform = d3.zoomTransform(context.canvas)) {
context.clearRect(0, 0, width, height);
for(let i = 40; i >= 1; i = i - 1) {
for(let j = 360; j >= 0; j -= degree_offset) {
drawSector(context,
color(j / i / 360 * 17),
transform.applyX(cx),
transform.applyY(cy),
radius * transform.k * i,
(j - 10) / 180 * PI ,
j / 180 * PI)
}
}
// context.save()
}
render()
return context.canvas
}
Insert cell
md`## 等密度圆 覆盖方式`
Insert cell
{
let startAngle = 10
let endAngle = 20
let width = 800
let height = 800
let cx = width / 2
let cy = height / 2
let radius = 30
let degree_offset = 1
let context = DOM.context2d(width, height)
d3.select(context.canvas).call(
d3.zoom()
.scaleExtent([1/4, 4])
.on("zoom", render)
);
function render(transform = d3.zoomTransform(context.canvas)) {
context.clearRect(0, 0, width, height);
for(let i = 40; i >= 1; i = i - 1) {
for(let j = 360; j >= 0; j -= degree_offset) {
drawSector(context,
color(i / 360 * 17),
transform.applyX(cx),
transform.applyY(cy),
radius * transform.k * Math.sqrt(i),
(j - 10) / 180 * PI ,
j / 180 * PI)
}
}
// context.save()
}
render()
return context.canvas
}
Insert cell
{
// let context = DOM.context2d(600, 600)
let startAngle = 310 / 180 * PI
let endAngle = 320 / 180 * PI
let radius = 50
let width = 800
let height = 800
let cx = width / 2
let cy = height / 2
// let radius = 10
let degree_offset = 10
let context = DOM.context2d(width, height)

d3.select(context.canvas).call(
d3.zoom()
.scaleExtent([1/2, 2])
.on("zoom", render)
);
function render(transform = d3.zoomTransform(context.canvas)) {
context.clearRect(0, 0, width, height);
drawSector(context, color(0.2), transform.applyX(cx), transform.applyY(cy), radius * transform.k, startAngle, endAngle)
drawSector(context, color(0.1), transform.applyX(cx), transform.applyY(cy), (radius - 20) * transform.k, startAngle, endAngle)
}
render()
return context.canvas
}
Insert cell
function drawSector(ctx, color, cx, cy, radius, startAngle, endAngle, transform) {
ctx.beginPath();
ctx.moveTo(cx,cy);
ctx.arc(cx,cy, radius, startAngle, endAngle);
ctx.lineTo(cx,cy);
ctx.fillStyle = color
ctx.fill()
// ctx.save()
}
Insert cell
function drawRingSector(ctx, color, cx, cy, innerRadius, outRadius, startAngle, endAngle, transform) {
ctx.beginPath();
ctx.arc(cx,cy, innerRadius, startAngle, endAngle);
ctx.arc(cx,cy, outRadius, startAngle, endAngle);
// ctx.moveTo(cx + innerRadius * cos(startAngle), cy + innerRadius * sin(startAngle));
// ctx.lineTo(cx + outRadius * cos(startAngle),cy + outRadius * sin(startAngle));
// ctx.moveTo(cx + outRadius*cos(endAngle), cy + outRadius*sin(endAngle));
ctx.lineTo(cx + innerRadius * cos(endAngle), cy + innerRadius*sin(endAngle));
// ctx.moveTo(cx + innerRadius * cos(startAngle), cy + innerRadius * sin(startAngle));
ctx.lineTo(cx + outRadius * cos(startAngle),cy + outRadius * sin(startAngle));
// ctx.arc(cx,cy, innerRadius, startAngle, endAngle);
// ctx.arc(cx,cy, outRadius, startAngle, endAngle);
ctx.fillStyle = color
ctx.fill()
// ctx.strokeStyle = 'steelblue'
// ctx.stroke()
// ctx.save()
}
Insert cell
{
// let context = DOM.context2d(600, 600)
let startAngle = 310 / 180 * PI
let endAngle = 360 / 180 * PI
let radius = 50
let cx = width / 2
let cy = height / 2
// let radius = 10
let context = DOM.context2d(width, height)

d3.select(context.canvas).call(
d3.zoom()
.scaleExtent([1/2, 4])
.on("zoom", render)
);
function render(transform = d3.zoomTransform(context.canvas)) {
context.clearRect(0, 0, width, height);
drawRingSector(context, color(0.2), transform.applyX(cx), transform.applyY(cy), (radius - 20) * transform.k, radius * transform.k, startAngle, endAngle)
}
render()
return context.canvas
}
Insert cell
{
let width = 800
let height = 800
let cx = width / 2
let cy = height / 2
let radius = 10
let degree_offset = 1
let context = DOM.context2d(width, height)
d3.select(context.canvas).call(
d3.zoom()
.scaleExtent([1/4, 4])
.on("zoom", render)
);
function render(transform = d3.zoomTransform(context.canvas)) {
context.clearRect(0, 0, width, height);
// context.save()
for(let i = 0; i < 40; i++) {
for(let j = 360; j >= 10; j -= degree_offset) {
// if( j / 20 === 0 ) {
drawRingSector(context,
color(j / i / 360 * 17),
transform.applyX(cx),
transform.applyY(cy),
radius * transform.k * i,
radius * transform.k * (i + 1),
(j - 10) / 180 * PI ,
j / 180 * PI)
// }
}
}
// context.save()
}
render()
return context.canvas
}
Insert cell
{
let width = 800
let height = 800
let cx = width / 2
let cy = height / 2
let radius = 40
let degree_offset = 1
let context = DOM.context2d(width, height)
d3.select(context.canvas).call(
d3.zoom()
.scaleExtent([1/4, 4])
.on("zoom", render)
);
function render(transform = d3.zoomTransform(context.canvas)) {
context.clearRect(0, 0, width, height);
// context.save()
for(let i = 0; i < 40; i++) {
for(let j = 360; j >= 10; j -= degree_offset) {
// if( j / 20 === 0 ) {
drawRingSector(context,
color(i / 360 * 17),
transform.applyX(cx),
transform.applyY(cy),
radius * transform.k * Math.sqrt(i),
radius * transform.k * Math.sqrt(i + 1),
(j - 10) / 180 * PI ,
j / 180 * PI)
// }
}
}
// context.save()
}
render()
return context.canvas
}
Insert cell
color = d3.interpolateHslLong("red", "blue")
Insert cell
PI = Math.PI
Insert cell
cos = Math.cos
Insert cell
sin = Math.sin
Insert cell
width = 800
Insert cell
height = 800
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