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 id="app">
    <div>
        <mds-section bold :title="chartAriaLabel" :aria-label="chartAriaLabel" aria-describedby="aria-description">
            <span id="aria-description" class="accessibly-hidden"> {{ chartAriaDescription }}</span>

            <mce-hover-flag v-if="focusedData" :x="mousePosition.pageX" :y="mousePosition.pageY">
                <mce-legend hide-top-border hide-bottom-border>
                    <mce-legend-item :color="chartColors[mouseData._itemId]" :label="mouseData.label" :value="mouseData.value | format" key-type="donut"></mce-legend-item>
                </mce-legend>
            </mce-hover-flag>
            <div class="wrapper" ref="wrapper">
                <mce-layout :plot-width="plotWidth" :plot-height="plotHeight" :legend-right-width="legendRightWidth">
                    <template #legend-right v-if="!showTopLegend">
                        <mce-legend>
                            <mce-legend-group title="Asset Allocation">
                                <mce-legend-item v-for="row in fullData" :key="row.id" :label="row.label" :value="row.value | format" :color="chartColors[row.id]" key-type="donut"></mce-legend-item>
                            </mce-legend-group>
                        </mce-legend>
                    </template>

                    <!-- Legend top slot for horizontal legend (small viewports) -->
                    <template #legend-top v-if="showTopLegend">
                        <mce-legend horizontal>
                            <mce-legend-item v-for="row in fullData" :key="row.id" :label="row.label" :value="row.value | format" :color="chartColors[row.id]" key-type="donut"></mce-legend-item>
                        </mce-legend>
                    </template>

                    <!-- Main plot (default slot) -->
                    <mce-donut-plot :color="chartColors" :data="chartData" :data-focused="focusedData" :height="plotHeight" :width="plotWidth" @mousemove-plot="onMousemovePlot" @mouseleave-plot="onMouseleavePlot"></mce-donut-plot>

                </mce-layout>
            </div>
        </mds-section>

        <!-- provide an accessible alternative for screen-readers -->
        <mds-table class="table">
            <div class="accessibly-hidden">
                {{
                    chartAriaDescription
                }}
            </div>
            <mds-thead>
                <mds-th>Asset Class</mds-th>
                <mds-th right-aligned>Allocation %</mds-th>
            </mds-thead>
            <mds-tbody>
                <mds-tr v-for="row in fullData" :key="row.id">
                    <mds-td> {{ row.label }} </mds-td>
                    <mds-td right-aligned> {{ row.value | format }} </mds-td>
                </mds-tr>
            </mds-tbody>
        </mds-table>
    </div>
</div>
              
            
!

CSS

              
                // All mixins, constants, and typography from MDS are available for use

@mixin mds-accessibly-hidden() {
    clip: rect(0 0 0 0);
    left: 0;
    position: absolute;
    z-index: -1;
}

body {
    margin: 0;
    padding: 0;
}

#app {
    margin: $mds-space-1-x;

    .table {
        margin-top: $mds-space-4-x;
        max-width: 400px;
    }

    .accessibly-hidden {
        @include mds-accessibly-hidden();
    }

    .wrapper {
        height: 50vh;
    }
}

              
            
!

JS

              
                import * as d3 from "https://cdn.skypack.dev/[email protected]";
const { assetAllocation } = window.utilities.color;

/*
Donut is a chart example built using MDS Chart Elements based on the Asset Allocation breakdown chart from the SAL (Signature Asset Library) team for the Vanguard Global Wellington Fund Admiral Shares (VGWAX) fund.

The chart shows the allocation breakdown across asset classes for the Vanguard Global Wellington Fund Admiral Shares (VGWAX) fund. The fund is well diversified across asset classes with 37% allocated to U.S. Equity, 29% Fixed Income, 27% Non-U.S. Equity, and around 5% Cash.
*/

// Prepare chart source data
const rawData = [
    {
        id: "AssetAllocCash",
        label: "Cash",
        value: 4.74688,
    },
    {
        id: "AssetAllocNotClassified",
        label: "Not Classified",
        value: 1.53654,
    },
    {
        id: "AssetAllocNonUSEquity",
        label: "Non-U.S. Equity",
        value: 27.24799,
    },
    {
        id: "AssetAllocOther",
        label: "Other",
        value: 0.0,
    },
    {
        id: "AssetAllocUSEquity",
        label: "U.S. Equity",
        value: 37.11866,
    },
    {
        id: "AssetAllocBond",
        label: "Fixed Income",
        value: 29.34991,
    }
];

const chartColors = {
    AssetAllocCash: assetAllocation.cash,
    AssetAllocNotClassified: assetAllocation['not-classified'],
    AssetAllocNonUSEquity: assetAllocation.alternative,
    AssetAllocOther: assetAllocation.other,
    AssetAllocUSEquity: assetAllocation.equity,
    AssetAllocBond: assetAllocation['fixed-income'],
};

// Initialize and render chart
const app = new Vue({
    el: "#app",
    data() {
        return {
            plotHeight: 450,
            plotWidth: 450,
            legendRightWidth: 170,
            chartColors,
            mouseData: {},
            mousePosition: {},
            focusedData: null,
            flagVisible: false,
            showTopLegend: false,

            // Descriptive Labels
            chartAriaLabel:
                "Vanguard Global Wellington Fund Admiral Shares (VGWAX)",
            chartAriaDescription:
                "The allocation breakdown across asset classes for the Vanguard Global Wellington Fund Admiral Shares (VGWAX) fund. The fund is well diversified across asset classes with 37% allocated to U.S. Equity, 29% Fixed Income, 27% Non-U.S. Equity, and around 5% Cash."
        };
    },
    computed: {
        fullData() {
            // Show all values, including zero, in the legend
            return rawData.sort((a, b) => b.value - a.value);
        },
        chartData() {
            // Ignore zero values to avoid unwanted pie padAngle slices
            return this.fullData.filter((d) => d.value > 0);
        },
    },
    filters: {
        format(value) {
            return d3.format(",.2%")(value / 100);
        }
    },
    methods: {
        setSize() {
            const donutMinHeight = 150;
            const donutMaxHeight = 400;
            const donutMinWidth = 200;
            const donutMaxWidth = 400;
            const elem = this.$refs.wrapper;
            const { offsetWidth, offsetHeight } = elem;
            const tooSkinny =
                offsetWidth - this.legendRightWidth < donutMinWidth;
            const tooWide = offsetWidth - this.legendRightWidth > donutMaxWidth;
            const tooShort = offsetHeight < donutMinHeight;
            const tooTall = offsetHeight > donutMaxHeight;
            const targetWidth = tooSkinny
                ? donutMinWidth
                : tooWide
                ? donutMaxWidth
                : offsetWidth - this.legendRightWidth;
            const targetHeight = tooShort
                ? donutMinHeight
                : tooTall
                ? donutMaxHeight
                : offsetHeight;
            const targetDimension = Math.min(targetWidth, targetHeight);

            // 12 = left padding on right legend
            if (offsetWidth < donutMinWidth + 12 + this.legendRightWidth) {
                this.showTopLegend = true;
            } else {
                this.showTopLegend = false;
            }

            this.plotWidth = targetDimension;
            this.plotHeight = targetDimension;
        },
        onResize() {
            this.setSize();
        },
        onMousemovePlot(e) {            
            const { data, position } = e;

            this.mouseData = data;
            this.mousePosition = position;
            this.focusedData = [data._itemId];
        },
        onMouseleavePlot() {
            this.focusedData = null;
        }
    },
    mounted() {
        // Add event listener for resize
        window.addEventListener("resize", () => {
            this.onResize();
        });

        // Calls setSize method once chart is mounted
        this.setSize();
    }
});

              
            
!
999px

Console