HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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>
@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
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();
}
};
});
Also see: Tab Triggers