Skip to content

Observable Plot with NodeJS? #847

Answered by mbostock
nshiab asked this question in Q&A
Apr 18, 2022 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

You’ll need to provide a DOM implementation to use Plot. We use JSDOM for testing, so you could crib from our approach in test/jsdom.js. For example, this script will output the generated HTML for a plot:

import * as d3 from "d3";
import * as Plot from "@observablehq/plot";
import {JSDOM} from "jsdom";
import fs from "fs/promises";

withJsdom(async () => {
  const penguins = d3.csvParse(await fs.readFile("./penguins.csv", "utf8"), d3.autoType);
  const plot = Plot.plot({
    grid: true,
    marks: [
      Plot.dot(penguins, {
        x: "culmen_depth_mm",
        y: "culmen_length_mm",
        stroke: "species"
      })
    ]
  });
  console.log(plot.outerHTML);
})();

function withJsdom(run

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@nshiab
Comment options

@Fil
Comment options

Answer selected by Fil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants