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

              
                <div class="" id="chart6"></div><br>
<div class="" id="chart7"></div><br>
<div class="" id="chart8"></div><br>
<div class="" id="chart9"></div><br>

              
            
!

CSS

              
                .segment-bar * {
    box-sizing: border-box;
}

.segment-bar {
    font-family: Roboto;
    display: flex;
    flex-wrap: nowrap;
    gap: 2px;
}

.segment-item-wrapper {
    display: inline-flex;
    height: 100%;
    padding: 0 4px;
    position: relative;
    overflow: hidden;
    transition: padding 0.6s cubic-bezier(0.83, 0, 0.17, 1);
}
.segment-item-wrapper:first-child {
    border-radius: 3px 0 0 3px;
}
.segment-item-wrapper:last-child {
    border-radius: 0 3px 3px 0;
}

.segment-item-percentage {
    opacity: 0;
    color: white;
    position: absolute;
    bottom: 8px;
    right: 8px;
    font-size: 12px;
    font-family: monospace;
    transition: opacity 0.6s cubic-bezier(0.83, 0, 0.17, 1);
}
.segment-item-wrapper:hover {
    z-index: 1;
    box-shadow: inset 0 0 0 60px rgba(0, 0, 0, 0.25);
    padding: 0 128px;
}

.segment-item-wrapper:hover .segment-item-percentage {
    opacity: 1;
}

.segment-item-title {
    display: inline-block;
    background-color: rgba(0, 0, 0, 0.35);
    line-height: 1;
    padding: 4px 6px;
    border-radius: 3px;
    white-space: nowrap;
}

/*
.segment-item-wrapper[title]:hover::after {
  content: attr(title);
  position: absolute;
    z-index: 1;
  top: 0%;
  left: 0;

      font-size: 16px;
  line-height: 20px;
  padding: 5px;
  background: #444;
  border: 1px solid #222;
  box-shadow: -2px 2px 5px rgba(0, 0, 0, 0.2);
  transition: opacity 0.3s, visibility 0s;
  color: white;
  min-width: 120px;
}
/**/

.segment-item-value {
    color: white;
    position: absolute;
    bottom: 8px;
    left: 8px;
    font-size: 12px;
}

.segment-item-title {
    position: absolute;
    left: 8px;
    top: 8px;
    color: white;
    font-size: 12px;
    font-weight: 500;
}

              
            
!

JS

              
                /**
 * I wrote an in-depth article and usage details here:
 * https://getbutterfly.com/segmented-horizontal-bar-chart-vanilla-javascript/
 */

const SEGMENT_WIDTH = "100%";
const SEGMENT_HEIGHT = "60px";
const palette = [
    "#c6e6ff",
    "#96d0ff",
    "#6cb6ff",
    "#539bf5",
    "#4184e4",
    "#316dca",
    "#255ab2",
    "#1b4b91",
    "#143d79",
    "#0f2d5c"
];

function buildSegmentBar(element, options) {
    let percentages = getSegmentPercentages(options.data);

    for (let i = 0; i < options.data.length; i++) {
        options.data[i].percent = +percentages[i];
    }

    element.style.width = options.width ? options.width : SEGMENT_WIDTH;
    element.style.height = options.height ? options.height : SEGMENT_HEIGHT;
    element.classList.add("segment-bar");
    let colorIt = getSegmentNextColor();

    for (let item of options.data) {
        let div = document.createElement("div");

        // Prepare wrapper
        div.style.width = `${parseFloat(item.percent * 100)}%`;
        div.style.backgroundColor = item.color
            ? item.color
            : colorIt.next().value;
        div.classList.add("segment-item-wrapper");

        // Percentage span
        let span = document.createElement("span");
        span.textContent = `${prettifySegmentPercentage(item.percent * 100)}%`;
        span.classList.add("segment-item-percentage");

        // Value span
        let valueSpan = document.createElement("span");
        valueSpan.textContent = `${item.value.toLocaleString("en-US")}`;
        valueSpan.classList.add("segment-item-value");

        // Title span
        if (item.title && item.title.length > 0) {
            let titleSpan = document.createElement("span");
            titleSpan.textContent = item.title;
            titleSpan.classList.add("segment-item-title");
            div.appendChild(titleSpan);

            div.title = `${item.title} (${item.value})`;
        }

        div.appendChild(span);
        div.appendChild(valueSpan);
        element.appendChild(div);
    }
}

function prettifySegmentPercentage(percentage) {
    let pretty = parseFloat(percentage).toFixed(2);
    let v = pretty.split(".");
    let final = 0;
    if (v[1]) {
        let digits = v[1].split("");
        if (digits[0] == 0 && digits[1] == 0) {
            final = parseFloat(`${v[0]}`);
        } else {
            final = pretty;
        }
    } else {
        final = parseFloat(v[0]);
    }
    return final;
}

// Accepts an array of chart data, returns an array of percentages
function getSegmentPercentages(data) {
    let sum = getSegmentSum(data);

    return data.map(function (item) {
        return parseFloat(item.value / sum);
    });
}

// Accepts an array of chart data, returns the sum of all values
function getSegmentSum(data) {
    return data.reduce(function (sum, item) {
        return sum + item.value;
    }, 0);
}

function* getSegmentNextColor() {
    let i = 0;
    while (true) {
        yield palette[i];
        i = (i + 1) % palette.length;
    }
}

buildSegmentBar(document.querySelector("#chart7"), {
    data: [
        { title: "First value", value: 16744, color: "#ff0000" },
        { value: 6500 },
        { value: 32750 },
        { value: 3200 }
    ]
});

buildSegmentBar(document.querySelector("#chart8"), {
    data: [
        { title: "Test 1 (s)", value: 10 },
        { title: "Test 2 (s)", value: 10 },
        { title: "Test 3 (s)", value: 20 },
        { title: "Test 4 (s)", value: 5 },
        { title: "Test 5 (s)", value: 30 },
        { title: "Test 6 (s)", value: 5 },
        { title: "Test 7 (s)", value: 20 }
    ]
});

buildSegmentBar(document.querySelector("#chart6"), {
    data: [
        { title: "Test 1", value: 50, color: '#574b90' },
        { title: "Test 2", value: 40, color: '#f78fb3' },
        { title: "Test 3", value: 30, color: '#3dc1d3' },
        { title: "Test 4", value: 20, color: '#e66767' },
        { title: "Test 5", value: 10, color: '#303952' }
    ]
});

buildSegmentBar(document.querySelector("#chart9"), {
    data: [
        { title: "Test 1", value: 10, color: '#f19066' },
        { title: "Test 2", value: 20, color: '#f5cd79' },
        { title: "Test 3", value: 30, color: '#546de5' },
        { title: "Test 4", value: 40, color: '#e15f41' },
        { title: "Test 5", value: 50, color: '#c44569' }
    ]
});

              
            
!
999px

Console