rund3
D3.jsのオンライン実行環境
/masui/D3をScrapboxで実行する
forks
/suto3/rund3
https://github.com/takker99/rund3
preactとhtmを使えるようにした
$ https://takker99.github.io/rund3?js=:url&css=:style
:urlにJSファイルへのURLを渡す
:cssにCSSファイルへのURLを渡す
双方とも複数個指定可能
実装すること
✅preact hooksを入れる
✅外部ライブラリ読み込みとindex.jsをesmにする
examples
d3-axisを使った例
dom版
code:ex-dom.js
const width, height = 600, 100;
document.body.insertAdjacentHTML("beforeend",`
<svg xmlns="http://www.w3.org/2000/svg" width=${width} height=${height}>
<g transform="translate(0, ${height / 2})" />
</svg>
`);
const { axisBottom, select, scaleLinear } = d3;
const padding = 20;
const scale = d3.scaleLinear().domain(0, 100).range(padding, width - padding);
const axis = axisBottom(scale);
axis(select("svg g"));
htm版
code:ex-htm.js
const { render, html } = htmPreact;
const { axisBottom, select, scaleLinear } = d3;
const width, height = 600, 100;
render(html`
<svg xmlns="http://www.w3.org/2000/svg" width=${width} height=${height}>
<g transform=${translate(0, ${height / 2})} ref=${(g) => {
const padding = 20;
const scale = scaleLinear().domain(0, 100).range(padding, width - padding);
const axis = axisBottom(scale);
axis(select(g));
}}/>
</svg>
`, document.body);
/masui/D3をScrapboxで実行するをpreactで書き直す
https://takker99.github.io/rund3?js=https://scrapbox.io/api/code/takker/rund3/bar.js
code:bar.js
const { render, html } = htmPreact;
const dataset = 3, 1, 4, 1, 5, 9 ;
render(html`<style>
div.bar {
display: inline-block;
width: 40px;
background-color: teal;
}
</style>
${dataset.map(
(data) => html<div class="bar" style=${height: ${data * 10}px;} />
)}`, document.body);
#2023-02-08 21:59:13
#2023-02-07 10:57:41