Published
Edited
Jan 19, 2019
4 forks
31 stars
Insert cell
Insert cell
html`

<canvas id="mapCanvas" width="800" height=500 ></canvas>

`
Insert cell
mapDrawing = {
var selector = 'mapCanvas';
clear(selector);
var rc = getrc(selector);


d3.json("https://gist.githubusercontent.com/rveciana/181f41663ae9cef7c00ad80cd085f413/raw/63a2b578f5d9536ca51c49e71f3d6a429ae93392/us.json")
.then(us=>{
var projection = d3.geoAlbersUsa();
var mappath = d3.geoPath()
.projection(projection);
var colorScale = d3.scaleOrdinal(d3.schemeCategory10);
var features = topojson.feature(us, us.objects.states).features;
features.forEach(function(d,j){
var path = rc.path(mappath(d),{
fill : colorScale(d.id),
simplification:0.2,
fillWeight: 1.5
});
});
});
// return html(" ");
}
Insert cell
data = [1,5,3,4,5,1]
Insert cell
Insert cell
html`

<canvas id="canvasText" width="700" ></canvas>

`
Insert cell
textDrawing = {
// Set interval is needed, because font is loaded asynchronously
setInterval(function(){
var ctx = getContext('canvasText');
clear('canvasText')
ctx.font="30px 'Indie Flower'";
ctx.fillText("This is hand drawn like text inside canvas",10,50);
},1000)
return html(" ");
}
Insert cell
Insert cell
html`<canvas id="canvas" width="700" ></canvas>`
Insert cell
rectDrawing = {
var rc = getrc('canvas');
clear('canvas');
var max = d3.max(data);
var pix = 20;
data.forEach((d,i)=>{
rc.rectangle(
i*50+10,
max*pix-d*pix,
10,
d*pix,
{fill:'green'})
})
return html(" ");
}
Insert cell
Insert cell
html`<canvas id="circleCanvas" width="700" ></canvas>`
Insert cell
bubbleDrawing = {
var rc = getrc('circleCanvas');
var max = d3.max(data);
var pix = 10;
clear('circleCanvas');
data.forEach((d,i)=>{
rc.circle(
i*60+20,
40,
d*pix,
{
fill:'green',
fillWeight: 0.5 // thicker lines for hachure
})
})
return html(" ");
}
Insert cell
Insert cell
html`<canvas id="pathCanvas" width="700" height="200" ></canvas>`
Insert cell
pieDrawing = {
var selector = 'pathCanvas';
var context = getContext(selector)
var rc = getrc(selector);
var max = d3.max(data);
var pix = 10;
clear(selector);

var pie = d3.pie()
.value(d=>d)
.sort(null)
var piedData = pie(data);
piedData.forEach((d,i)=>{
var a = rc.arc(100, 100, 100, 100, d.startAngle - Math.PI/2, d.endAngle - Math.PI/2, true,{
fill : 'green'
});
})
return html(" ");
}
Insert cell
Insert cell
html`<canvas id="donutCanvas" width="700" height="200" ></canvas>`
Insert cell
donutDrawing = {
var selector = 'donutCanvas';
var context = getContext(selector)
var rc = getrc(selector);
var max = d3.max(data);
var pix = 10;
clear(selector);

var pie = d3.pie()
.value(d=>d)
.sort(null)
var piedData = pie(data);
piedData.forEach((d,i)=>{
var a = rc.arc(100, 100, 100, 100, d.startAngle - Math.PI/2, d.endAngle - Math.PI/2, true,{
fill : 'green'
});
})
rc.circle(
100,
100,
40,
{
fill:'white',
fillWeight: 10
})
return html(" ");
}
Insert cell
Insert cell
html`<canvas id="lineCanvas" width="700" height="200" ></canvas>`
Insert cell
lineDrawing = {
var selector = 'lineCanvas';

var rc = getrc(selector);
var pix = 50;
var max = d3.max(data);
var scale = d3.scaleLinear().domain([0,max]).range([100,10])
clear(selector);
var points = data.map((d,i)=>{
return [i*pix,scale(d)]
})
rc.curve(points,{
stroke:'green',
strokeWidth:2
})
return html(" ");
}
Insert cell
Insert cell
html`<canvas id="areaCanvas" width="700" height="200" ></canvas>`
Insert cell
areaDrawing = {
var selector = 'areaCanvas';
var rc = getrc(selector);
var pix = 50;
var max = d3.max(data);
var scale = d3.scaleLinear().domain([0,max]).range([100,10])
clear(selector);

var area = d3.area()
.x((d,i)=> i*pix)
.y1((d,i)=> scale(d))
.y0(150)
.curve(d3.curveCardinal)
rc.path(area(data), {fill:'green'});
return html(" ");
}
Insert cell
Insert cell
Insert cell
Insert cell
function getrc(selector){
var canvas = getCanvas(selector);
return rough.canvas(canvas);
}

Insert cell
Insert cell
function getCanvas(selector){
return document.getElementById(selector)
}
Insert cell
Insert cell
function getContext(selector){
var canvas = getCanvas(selector);
return canvas.getContext('2d');
}
Insert cell
Insert cell
function clear(selector){
var canvas = getCanvas(selector);
var context = getContext(selector)
context.clearRect(0, 0, canvas.width, canvas.height);
}
Insert cell
Insert cell
Insert cell
rough = require('roughjs@3.0.0/dist/rough.js').catch(() => window.rough)
Insert cell
Insert cell
d3=require('d3@5')
Insert cell
Insert cell
topojson = require("topojson")
Insert cell
Insert cell
stylesheet = html`<link href="https://fonts.googleapis.com/css?family=Indie+Flower" rel="stylesheet">`
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