Published
Edited
Aug 8, 2021
Fork of D3 Template
Insert cell
Insert cell
Insert cell
chart = Chart().container(container).data({ message: "I am a chart" }).render()
Insert cell
function Chart() {
// Exposed variables
var attrs = {
svgWidth: 400,
svgHeight: 400,
marginTop: 5,
marginBottom: 5,
marginRight: 5,
marginLeft: 5,
container: "body",
defaultTextFill: "#2C3E50",
defaultFont: "Helvetica",
data: null
};

//Main chart object
var main = function () {
//Drawing containers
var container = d3.select(attrs.container);

//Calculated properties
var calc = {
id: null,
chartTopMargin: null,
chartLeftMargin: null,
chartWidth: null,
chartHeight: null
};

calc.chartLeftMargin = attrs.marginLeft;
calc.chartTopMargin = attrs.marginTop;
calc.chartWidth = attrs.svgWidth - attrs.marginRight - calc.chartLeftMargin;
calc.chartHeight =
attrs.svgHeight - attrs.marginBottom - calc.chartTopMargin;

//Add svg
var svg = container
._add({ tag: "svg", className: "svg-chart-container" })
.attr("width", attrs.svgWidth)
.attr("height", attrs.svgHeight)
.attr("font-family", attrs.defaultFont);

//Add container g element
var chart = svg
._add({ tag: "g", className: "chart" })
.attr(
"transform",
"translate(" + calc.chartLeftMargin + "," + calc.chartTopMargin + ")"
);

// REMOVE THIS SNIPPET AFTER YOU START THE DEVELOPMENT
chart
._add({
tag: "text",
className: "example-text",
data: [attrs.data.message]
})
.text((d) => d)
.attr("x", 10)
.attr("y", 20);

chart
._add({
tag: "circle",
className: "single-circle"
})
.attr("r", 30)
.attr("fill", "blue")
.attr("cx", 100)
.attr("cy", 100);

chart
._add({
tag: "circle",
className: "multi-circle",
data: [10, 15, 20, 25]
})
.attr("r", (d) => d)
.attr("fill", "red")
.attr("cx", (d, i) => i * 40 + 100)
.attr("cy", 150);
};

//----------- PROTOTYPE FUNCTIONS ----------------------
d3.selection.prototype._add = function ({ className, tag, data }) {
var container = this;
var localData = data || [selector];
var selector = className || selector + "-element";

// Pattern in action
let selection = container
.selectAll("." + selector)
.data(localData, (d, i) => {
if (typeof d === "object") {
return d.id || d.key || i;
}
return i;
});
selection.exit().remove();
selection = selection.enter().append(tag).merge(selection);
selection.attr("class", className);
return selection;
};

// Run visual
main["render"] = function () {
main();
return main;
};

// Run visual
main["container"] = function (container) {
attrs.container = container;
return main;
};

// Run visual
main["data"] = function (data) {
attrs.data = data;
return main;
};

return main;
}
Insert cell
d3 = require('d3')
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