function _1(md){return( md`# Example Components This notebook contains shared utilities for [D3 charts](/collection/@d3/charts).` )} function _name(Inputs){return( Inputs.text({label: "Name", value: "ExampleChart"}) )} function _3(howto,name){return( howto(name, {open: true}) )} function _4(howto,name){return( howto(name, {open: true, specifier: "@d3/example", imports: {d3: "d3", d3Sankey: "d3-sankey"}}) )} function _currentSpecifier() { const {pathname} = new URL(document.baseURI); return pathname.slice(pathname.startsWith("/d/") ? 3 : 1); } function _howto(currentSpecifier,htl,md,Inputs){return( function howto(name, options = {}) { if (typeof options === "string") options = {specifier: options}; const {open, imports = {d3: "d3"}, specifier = currentSpecifier} = options; if (!name) throw new Error("missing name"); return htl.html`
How do I use this code? 🤔
${md` **To use this chart outside of Observable,** ${specifier === currentSpecifier ? "" : `go to the ${specifier} notebook, then `}copy-paste the entire function ${name} ${specifier === currentSpecifier ? "below " : ""}including the copyright notice into your application. You’ll also need to install (*e.g.*, yarn add ${Object.values(imports).join(" ")}) and import (*e.g.*, ${Object.entries(imports).map(([name, value]) => `import \\* as ${name} from "${value}"`).join(", ")}) D3; see [D3’s README](https://github.com/d3/d3/blob/main/README.md) for details. To render a chart, pass ${name} an array of data and any desired options; it will return an SVG element that you can insert into the DOM. **To use this chart on Observable,** [import it](/@observablehq/introduction-to-imports) into your notebook: ~~~js import {${name}} from "${specifier}" ~~~ ${Inputs.button("Copy code", {reduce: () => navigator.clipboard.writeText(`import {${name}} from "${specifier}"`)})} Then call ${name}(*data*, *options*) as shown above. To customize this chart, click the Fork button at the top of the page to create your own copy of this notebook and save your changes. To run a cell, click the play button in the top-right corner of the editor, or use Command-S (⌘S) or Shift-Enter (⇧↩). If you have any questions or suggestions, please sign-in to leave a comment. Click the cell menu to the left of any cell, then click Add comment. `}
`; } )} function _7(altplot){return( altplot(`Plot.barY(alphabet, {x: "letter", y: "frequency"}).plot()`, {open: true}) )} function _altplot(htl,md,Inputs){return( function altplot(code = "", {open} = {}) { return htl.html`
Is there an easier way? 🤯
${md` Yes! While D3’s low-level abstraction is expressive, you might find it overkill for basic charts. For exploratory data analysis, or just to visualize data quickly, consider [Observable Plot](/@observablehq/plot) instead. Plot is free, [open-source](https://github.com/observablehq/plot), built on top of D3, and maintained by the same people as D3. For example, the above chart can be written as: ~~~js ${code} ~~~ ${Inputs.button("Copy code", {reduce: () => navigator.clipboard.writeText(code)})} Try pasting this code into a new cell to see. `}
`; } )} function _linkplot(htl,md){return( function linkplot(link = "", {open, title} = {}) { return htl.html`
Is there an easier way? 🤯
${md` Yes! While D3’s low-level abstraction is expressive, you might find it overkill for basic charts. For exploratory data analysis, or just to visualize data quickly, consider [Observable Plot](/@observablehq/plot) instead. Plot is free, [open-source](https://github.com/observablehq/plot), built on top of D3, and maintained by the same people as D3. For example, ${title || link} reproduces the above chart, with Plot. `}
`; } )} export default function define(runtime, observer) { const main = runtime.module(); main.variable(observer()).define(["md"], _1); main.variable(observer("viewof name")).define("viewof name", ["Inputs"], _name); main.variable(observer("name")).define("name", ["Generators", "viewof name"], (G, _) => G.input(_)); main.variable(observer()).define(["howto","name"], _3); main.variable(observer()).define(["howto","name"], _4); main.variable(observer("currentSpecifier")).define("currentSpecifier", _currentSpecifier); main.variable(observer("howto")).define("howto", ["currentSpecifier","htl","md","Inputs"], _howto); main.variable(observer()).define(["altplot"], _7); main.variable(observer("altplot")).define("altplot", ["htl","md","Inputs"], _altplot); main.variable(observer("linkplot")).define("linkplot", ["htl","md"], _linkplot); return main; }