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 ng-app="app">
    <section class="container" ng-controller="Controller as ctrl">
        <pie-chart portfolio="ctrl.portfolio" chart-title="Portfolio Assets"></pie-chart>
    </section>
</main>
              
            
!

CSS

              
                @import 'nib';
html
body
    height: 100%
    width: 100%
    background: #333
    font-size: 14px

main
.container
    display: flex
    align-items: center
    justify-content: center
    color: white
main
    height: 100%
    width: 100%
.container
    height: 100%
    width: 80%
    
    
$pie-chart = {
    tooltip-height: 4em
}
    
pie-chart
    position: relative
    display: block
    width: 100%
    
    .portfolio-assets-donut-chart-title
        position: absolute
        display: flex
        justify-content: center
        align-items: center
        text-align: center
        
        > h3
            max-width: 130px
            font-weight: normal
            margin: 0
            color: #c7c7c7
    
    .portfolio-assets-donut-chart
        display: flex
        justify-content: center
        height: 300px
        width: 100%
        
    .donut-chart-tooltip
        display: flex
        justify-content: center
        align-items: center
        white-space: normal
        box-sizing: border-box
        min-width: 150px
        background-color: rgba(85,85,85, 0.8)
        color: white
        border-radius: 'calc(%s/2)' % $pie-chart.tooltip-height
        
        > i
            display: inline-flex
            align-items: center
            justify-content: center
            font-style: normal
            height: $pie-chart.tooltip-height
            width: @height
            border-radius: 'calc(%s/2)' % $pie-chart.tooltip-height
            color: white
            margin-right: 10px
            
            .point-percentage
                font-size: 1.5em
        
        .point-name
            display: inline-block
            width: 10em
            padding-right: 1.3em
    
    .portfolio-assets-legend
        display: flex
        flex-wrap: wrap
        list-style: none
        padding: 0
        margin: 0
        
        li
            display: flex
            width: 33.333%
            
            &:nth-child(3n+2)
                justify-content: center
            &:nth-child(3n+3)
                justify-content: flex-end
            &:nth-child(n+4)
                margin-top: 15px
            
            .item-container
                display: inline-flex
                width: 90%
                max-width: 20em
            
        .color-label > i
            display: inline-block
            height: 1em
            width: @height
            margin-right: 20px
                
        .asset-details
            position: relative
            flex: 1
            
            h4
                font-size: 1em
                font-weight: normal
                padding-right: 2.5em
                margin: 0
            h5
                font-size: 1em
                font-weight: normal
                color: #888
                margin: 0
                margin-top: 5px
                
            .percentage
                position: absolute
                top: 0
                right: 0
                font-size: 1em
            
    
              
            
!

JS

              
                class Controller {
    constructor() {
        this.portfolio = {
            id: 123,
            portfolioAssets: [
                {
                    id: 123,
                    name: 'Asset A',
                    symbol: 'AAA',
                    percentage: 10
                },
                {
                    id: 124,
                    name: 'Asset B With a really long name from foreign country: united states',
                    name: 'Asset B with normal name',
                    symbol: 'BBB',
                    percentage: 15
                },
                {
                    id: 125,
                    name: 'Asset C',
                    symbol: 'CCC',
                    percentage: 15
                },
                {
                    id: 126,
                    name: 'Asset D',
                    symbol: 'DDD',
                    percentage: 10
                },
                {
                    id: 127,
                    name: 'Asset E',
                    symbol: 'BBB',
                    percentage: 50
                },
            ]
        };
    }
};

angular.module('app', [])
.factory('highstock', ($window) => {
    const Highcharts = $window.Highcharts;
    // Pie chart has gap bug: https://github.com/highcharts/highcharts/issues/1828
    Highcharts.wrap(Highcharts.seriesTypes.pie.prototype, 'drawPoints', function(proceed) {
        if (this.options.borderColor == null && this.options.borderWidth >= 1) {
            Highcharts.each(this.points, (point) => {
                point.pointAttr['']['stroke-width'] = 1;
                point.pointAttr[''].stroke = point.pointAttr[''].fill;
                point.pointAttr['hover']['stroke-width'] = 1;
                point.pointAttr['hover'].stroke = point.pointAttr['hover'].fill;
                point.pointAttr['select']['stroke-width'] = 1;
                point.pointAttr['select'].stroke = point.pointAttr['select'].fill;
            });
        }
        proceed.apply(this);
    });
    
    return Highcharts;
})
.controller('Controller', Controller)
.directive('pieChart', ($timeout, highstock) => {
    let colors = [
        '#CD5955',
        '#E96D66',
        '#EC876C',
        '#EFA071',
        '#EFB278',
        '#EEC27D',
        '#DEC782',
        '#BCBB87',
        '#99AD8A',
        '#7B9D8A',
        '#5E8D8A',
    ];
    let chartConfig = {
        chart: {
            type: 'pie',
            //renderTo: null,
            height: 300,
            backgroundColor: 'none',
            events: {
                //load: null
            }
        },
        title: {
            style: {display: 'none'}
        },
        subtitle: {
            style: {display: 'none'}
        },
        yAxis: {
            title: {
                text: 'Total percent market share'
            }
        },
        plotOptions: {
            pie: {
                shadow: false,
                center: ['50%', '50%'],
                colors: colors,
                borderWidth: 1,
                borderColor: null,
                stickyTracking: false,
                dataLabels: {
                    enabled: false
                },
                states: {
                    hover: {
                        brightness: 0,
                    }
                },
            }
        },
        tooltip: {
            backgroundColor: '',
            borderWidth: 0,
            useHTML: true,
            formatter() {
                console.log('this is ', this)
                let point = this.point;
                return `
                    <section class="donut-chart-tooltip">
                        <i style="background-color:${point.color}"><span class="point-percentage">${point.y+'%'}</span></i>
                        <span class="point-name" style="color:${point.color}">${point.name}</span>
                    </section>
                `;
            }
        },
        series: [{
            name: 'Assets Percentage',
            data: null, // To be set
            size: '100%',
            innerSize: '60%',
        }],
        exporting: {
            enabled: false
        },
        credits: {
            enabled: false
        },
    };
    
    return {
        restrict: 'AE',
        scope: {
            portfolio: '=',
            chartTitle: '@'
        },
        template: `
                <header class="portfolio-assets-donut-chart-title">
                    <h3 ng-bind="chartTitle"></h3>
                </header>
                <div class="portfolio-assets-donut-chart"></div>
                <ul class="portfolio-assets-legend">
                    <li ng-repeat="asset in portfolio.portfolioAssets">
                        <div class="item-container">
                        <div class="color-label"><i ng-style="{'background-color': getColorByIndex($index)}"></i></div>
                        <div class="asset-details">
                            <span ng-style="{'color': getColorByIndex($index)}" class="percentage" ng-bind="asset.percentage + '%'"></span>
                            <h4 ng-style="{'color': getColorByIndex($index)}" ng-bind="asset.name"></h4>
                            <h5 ng-bind="asset.symbol"><h5>
                        </div>
                        </div>
                    </li>
                </ul>
        `,
        link(scope, elem) {
            const titleElem = elem[0].getElementsByClassName('portfolio-assets-donut-chart-title')[0];
            const chartElem = elem[0].getElementsByClassName('portfolio-assets-donut-chart')[0];
            const portfolio = scope.portfolio;
            
            let chart = null;
            let createChart = () => {
                // Process data
                let data = portfolio.portfolioAssets.map((item) => {
                    return {
                        y: item.percentage,
                        name: item.name
                    };
                });
                
                chartConfig.series[0].data = data;
                
                chart = new highstock.Chart(chartConfig);
            };
            
            scope.getColorByIndex = (i) => {
                let len = colors.length;
                return colors[i % len];
            };
            
            const positionTitle = function() {
                console.log('positioning title', chart);
                console.log('this is ', this)

                Object.assign(titleElem.style, {
                    width: `${this.plotWidth}px`,
                    height: `${this.plotHeight}px`,
                    left: `${this.plotLeft}px`,
                    top: `${this.plotTop}px`,
                });
            };

            //chartConfig.events.load = positionTitle;
            chartConfig.chart.renderTo = chartElem;
            chartConfig.chart.events.load = positionTitle;
            chartConfig.chart.events.redraw = positionTitle;
            createChart();
        }
    };
});
              
            
!
999px

Console