Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <main>
    <h1>A static graph</h1>
    <div class="graph" id="graphContainer">
        <div class="axis-label x-axis-label">x axis label</div>
        <div class="axis x-axis" data-orientation="horizontal">
            <div class="tick">Category 1</div>
            <div class="tick">Category 2</div>
            <div class="tick">Category 3</div>
            <div class="tick">Category 4</div>
        </div>
        <div class="axis-label y-axis-label">y axis label</div>
        <div class="axis y-axis">
            <div class="tick">0</div>
            <div class="tick">100</div>
            <div class="tick">200</div>
            <div class="tick">300</div>
            <div class="tick">400</div>
            <div class="tick">500</div>
            <div class="tick">600</div>
            <div class="tick">700</div>
            <div class="tick">800</div>
        </div>
        <div class="graph-area">
            <div class="datum">
                <div class="value">200</div>
                <div class="bar" style="height: 25%"></div>
            </div>
            <div class="datum">
                <div class="value">400</div>
                <div class="bar" style="height: 50%"></div>
            </div>
            <div class="datum">
                <div class="value">600</div>
                <div class="bar" style="height: 75%"></div>
            </div>
            <div class="datum">
                <div class="value">800</div>
                <div class="bar" style="height: 100%"></div>
            </div>
        </div>
    </div>
    <h1>A graph from dynamic data</h1>
    <div id="buildings"></div>
</main>

              
            
!

CSS

              
                .graph {
    display: grid;
    grid-template-columns: auto auto 1fr;
    grid-template-rows: 1fr auto auto;
    grid-template-areas:
        "y-axis-label y-axis graph-area"
        ".            .      x-axis"
        ".            .      x-axis-label";
}

.graph .axis-label {
    --margin: 8px;
    font-weight: bold;
    text-align: center;
}

.graph .x-axis-label {
    grid-area: x-axis-label;
    margin-top: var(--margin);
}

.graph .y-axis-label {
    grid-area: y-axis-label;
    margin-block-end: var(--margin);
    writing-mode: vertical-lr;
    transform: rotate(180deg);
}

.graph .axis {
    display: flex;
    font-size: .8rem;
    --border: 1px solid black;
}

.graph .tick {
    --tick-lengeth: 10px;
    --tick-thickness: 1px;
    --tick-color: darkgray;

    display: flex;
    align-items: center;
    gap: 2px;
    white-space: nowrap;
}

.graph .y-axis {
    grid-area: y-axis;
    flex-direction: column-reverse;
    justify-content: space-between;
    align-items: end;
    border-inline-end: var(--border);
}

.graph .y-axis .tick {
    height: 0;
}

.graph .y-axis .tick::after {
    content: "";
    height: var(--tick-thickness);
    width: var(--tick-lengeth);
    background-color: var(--tick-color);
}

.graph .x-axis {
    grid-area: x-axis;
    justify-content: space-around;
    border-top: var(--border);
}

.graph .x-axis .tick {
    width: 0;
    flex-direction: column;
    text-align: center;
}

.graph .x-axis[data-orientation=vertical] .tick {
    writing-mode: vertical-lr;
    flex-direction: row-reverse;
    transform: rotate(180deg);
}

.graph .x-axis .tick::before {
    content: "";
    width: var(--tick-thickness);
    height: var(--tick-lengeth);
    background-color: var(--tick-color);
}

.graph .graph-area {
    grid-area: graph-area;
    aspect-ratio: 5 / 4;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(1px, .25fr));
    justify-content: space-around;
}

.graph .datum {
    margin-inline: 5%;
    display: flex;
    flex-direction: column;
    justify-content: end;
}

.graph .datum .value {
    align-self: center;
    z-index: 1;
}

.graph .datum .bar {
    flex-shrink: 0;
    background-color: rebeccapurple;
}



body {
    font-family: system-ui, sans-serif;
}

main {
    width: 80%;
    max-width: 40rem;
    margin-inline: auto;
}

*, *::before, *::after {
    box-sizing: border-box;
}

h1 {
    text-align: center;
}

              
            
!

JS

              
                import { select, selectAll } from "https://cdn.skypack.dev/d3-selection@3.0.0";
import { max } from "https://cdn.skypack.dev/d3-array@3.2.2";
import { scaleLinear } from "https://cdn.skypack.dev/d3-scale@4.0.2";

class BarGraph {
    constructor(container, options) {
        this.container = select(container);
        this.xAxisLabel = options.xAxisLabel ?? "";
        this.yAxisLabel = options.yAxisLabel ?? "";
        this.xAxisOrientation = options.xAxisOrientation ?? "horizontal";

        this.container.classed("graph", true);
        this.container.html(`
            <div class="axis-label x-axis-label">${this.xAxisLabel}</div>
            <div class="axis x-axis" data-orientation=${this.xAxisOrientation}></div>
            <div class="axis-label y-axis-label">${this.yAxisLabel}</div>
            <div class="axis y-axis"></div>
            <div class="graph-area"></div>
        `);
        this.xAxis = this.container.select(".x-axis");
        this.yAxis = this.container.select(".y-axis");
        this.graphArea = this.container.select(".graph-area");

        this.y = scaleLinear().range([0, 100]);
    }

    get data() {
        return this._data;
    }
    set data(d) {
        this._data = d;
        this.y.domain([0, max(this.data, d => d.value)]).nice();
        this.update();
    }

    update() {
        this.xAxis.selectAll(".tick").data(this.data, d => d.category)
            .join("div")
                .attr("class", "tick")
                .text(d => d.category);

        this.yAxis.selectAll(".tick").data(this.y.ticks())
            .join("div")
                .attr("class", "tick")
                .text(d => d);

        let datums = this.graphArea.selectAll(".datum").data(this.data, d => d.category);
        const datumsEnter = datums.enter().append("div").attr("class", "datum");
        datumsEnter.append("div").attr("class", "value");
        datumsEnter.append("div").attr("class", "bar");
        datums = datums.merge(datumsEnter);
        datums.select(".value").text(d => d.value);
        datums.select(".bar").style("height", d => `${this.y(d.value)}%`);
    }
}


const data = [
    { building: "Burj Khalifa", height: 828 },
    { building: "Merdeka 118", height: 678.9 },
    { building: "Shanghai Tower",height: 632 },
    { building: "Abraj Al-Bait Clock Tower", height: 601 },
    { building: "Ping An International Finance Centre", height: 599.1 }
];

const formattedData = data.map(d => ({ category: d.building, value: d.height }));

const myGraph = new BarGraph("#buildings", {
    xAxisLabel: "Building",
    yAxisLabel: "Height (m)",
    xAxisOrientation: "vertical"
});
myGraph.data = formattedData;

              
            
!
999px

Console