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.
<div id="chart123">
</div>
body {
background-color: #e8e8e9;
font-family: 'Roboto', sans-serif !important;
}
.custom_padding{
padding-left: 5px !important;
padding-right: 5px !important;
}
.card{
height: 125px;
border-radius: 2px;
font-weight: 300;
}
.bg{
margin-bottom: 10px;
background-color: #ffffff;
border-radius:4px;
height: 400px;
box-shadow: 1px 1px 1px -1px rgba(100,100,100,.5);
}
.shadow {
-webkit-filter: drop-shadow( 0px 2px 2px rgba(0,0,0,.3) );
filter: drop-shadow( 0px 2px 2px rgba(0,0,0,.3) );
}
.hr-custom {
border: 0;
height: 1px;
background-color: rgba(100, 100, 100, 0.2);
margin-bottom: 10px !important;
-background-image: linear-gradient(to right, rgba(0, 0, 0, 0), rgba(100, 100, 100, 0.5), rgba(0, 0, 0, 0));
}
.panel-header
{
height:50px;
background-color: #f4f4f4;
border-radius: 2px 2px 0 0;
border-bottom: 1px solid #dbdbdb;
padding:10px 20px 10px 20px;
margin-bottom: 20px;
}
.range-custom{
padding-bottom: 10px;
padding-left: 5px;
}
.range-custom-child{
line-height:30px;
}
.range-span{
color:#7f7f85;
padding-right:10px;
}
.padding-left-5{
padding-left: 5px;
}
.card_header {
padding: 10px 10px 5px 10px;
color: #ffffff;
}
.card_body{
text-align: center;
color:#ffffff;
font-size: 3em;
font-weight: 400;
}
.panel-title{
color:#6e6e74;
font-size: 1.5em;
font-weight: 300;
}
.line-height-30{
line-height: 30px;
}
.margin-below-20{
margin-bottom: 20px;
}
.line {
fill: none;
stroke: #53c79f;
stroke-width: 3px;
}
.axis {
fill: #a3a3a7;
}
.axis path,
.axis line {
fill: none;
stroke: #ececed;
shape-rendering: crispEdges;
}
.axis text {
font-size: 12px;
}
.y-grid .tick{
stroke: #ececed;
opacity: 1;
}
.y-grid path,
.y-grid line {
fill: none;
stroke: #ececed;
shape-rendering: crispEdges;
opacity: 1;
}
.dot{
fill:#FFFFFF;
stroke:#53c79f;
stroke-width:2px;
}
.area{
opacity: .9 ;
stroke: #fff;
stroke-width:2px;
}
.tooltip-bg{
fill:#000;
opacity:0.7;
}
.tooltip-text1{
font-size:15px;
fill:#ffffff;
}
.tooltip-text2{
font-size:20px;
fill:#a9f3ff;
}
var Gradient=React.createClass({
propTypes: {
id:React.PropTypes.string,
color1:React.PropTypes.string,
color2:React.PropTypes.string
},
render:function(){
return(
<defs>
<linearGradient is id={this.props.id} x1="0%" y1="100%" x2="0%" y2="0%" spreadMethod="pad">
<stop is offset="10%" stop-color={this.props.color1} stop-opacity={.4}/>
<stop is offset="80%" stop-color={this.props.color2} stop-opacity={1}/>
</linearGradient>
</defs>
);
}
});
window.D3Gradient=Gradient;
var resizeMixin={
componentWillMount:function(){
var _self=this;
$(window).on('resize', function(e) {
_self.updateSize();
});
this.setState({width:this.props.width});
},
componentDidMount: function() {
this.updateSize();
},
componentWillUnmount:function(){
$(window).off('resize');
},
updateSize:function(){
var node = ReactDOM.findDOMNode(this);
var parentWidth=$(node).width();
if(parentWidth<this.props.width){
this.setState({width:parentWidth-20});
}else{
this.setState({width:this.props.width});
}
}
};
window.resizeMixin=resizeMixin;
var Axis=React.createClass({
propTypes: {
h:React.PropTypes.number,
scale:React.PropTypes.func,
axisType:React.PropTypes.oneOf(['x','y']),
orient:React.PropTypes.oneOf(['left','top','right','bottom']),
className:React.PropTypes.string,
tickFormat:React.PropTypes.string,
//,removeFirst:React.PropTypes.bool
ticks:React.PropTypes.number
},
componentDidUpdate: function () { this.renderAxis(); },
componentDidMount: function () { this.renderAxis(); },
renderAxis: function () {
var _self=this;
this.axis = d3.svg.axis()
.scale(this.props.scale)
.orient(this.props.orient)
.ticks(this.props.ticks);
if(this.props.tickFormat!=null && this.props.axisType==='x')
this.axis.tickFormat(d3.time.format(this.props.tickFormat));
var node = ReactDOM.findDOMNode(this);
d3.select(node).call(this.axis);
},
render: function () {
var translate = "translate(0,"+(this.props.h)+")";
return (
<g className={this.props.className} transform={this.props.axisType=='x'?translate:""} >
</g>
);
}
});
window.D3Axis=Axis;
var Grid=React.createClass({
propTypes: {
h:React.PropTypes.number,
len:React.PropTypes.number,
scale:React.PropTypes.func,
gridType:React.PropTypes.oneOf(['x','y']),
orient:React.PropTypes.oneOf(['left','top','right','bottom']),
className:React.PropTypes.string,
ticks:React.PropTypes.number
},
componentDidUpdate: function () { this.renderGrid(); },
componentDidMount: function () { this.renderGrid(); },
renderGrid: function () {
this.grid = d3.svg.axis()
.scale(this.props.scale)
.orient(this.props.orient)
.ticks(this.props.ticks)
.tickSize(-this.props.len, 0, 0)
.tickFormat("");
var node = ReactDOM.findDOMNode(this);
d3.select(node).call(this.grid);
},
render: function () {
var translate = "translate(0,"+(this.props.h)+")";
return (
<g className={this.props.className} transform={this.props.gridType=='x'?translate:""}>
</g>
);
}
});
window.D3Grid=Grid;
var ToolTip=React.createClass({
propTypes: {
tooltip:React.PropTypes.object,
bgStyle:React.PropTypes.string,
textStyle1:React.PropTypes.string,
textStyle2:React.PropTypes.string,
xValue:React.PropTypes.string,
yValue:React.PropTypes.string
},
render:function(){
var visibility="hidden";
var transform="";
var x=0;
var y=0;
var width=150,height=70;
var transformText='translate('+width/2+','+(height/2-5)+')';
var transformArrow="";
if(this.props.tooltip.display==true){
var position = this.props.tooltip.pos;
x= position.x;
y= position.y;
visibility="visible";
if(y>height){
transform='translate(' + (x-width/2) + ',' + (y-height-20) + ')';
transformArrow='translate('+(width/2-20)+','+(height-.2)+')';
}else if(y<height){
transform='translate(' + (x-width/2) + ',' + (Math.round(y)+20) + ')';
transformArrow='translate('+(width/2-20)+','+0+') rotate(180,20,0)';
}
}else{
visibility="hidden"
}
return (
<g transform={transform}>
<rect class={this.props.bgStyle} is width={width} height={height} rx="5" ry="5" visibility={visibility}/>
<polygon class={this.props.bgStyle} is points="10,0 30,0 20,10" transform={transformArrow}
visibility={visibility}/>
<text is visibility={visibility} transform={transformText}>
<tspan is x="0" class={this.props.textStyle1} text-anchor="middle">{this.props.xValue +" : "+this.props.tooltip.data.key}</tspan>
<tspan is x="0" class={this.props.textStyle2} text-anchor="middle" dy="25">{this.props.yValue +" : "+this.props.tooltip.data.value}</tspan>
</text>
</g>
);
}
});
window.D3ToolTip=ToolTip;
var Dots=React.createClass({
propTypes: {
data:React.PropTypes.array,
xData:React.PropTypes.string.isRequired,
yData:React.PropTypes.string.isRequired,
x:React.PropTypes.func,
y:React.PropTypes.func,
r:React.PropTypes.string,
format:React.PropTypes.string,
removeFirstAndLast:React.PropTypes.bool
},
render:function(){
var _self=this;
//remove last & first point
var data=[];
if(this.props.removeFirstAndLast){
for(var i=1;i<this.props.data.length-1;++i){
data[i-1]=this.props.data[i];
}
}else{
data=this.props.data;
}
var circles=data.map(function(d,i){
return (<circle className="dot" r={_self.props.r} cx={_self.props.x(d[_self.props.xData])} cy={_self.props.y(d[_self.props.yData])}
key={i}
onMouseOver={_self.props.showToolTip} onMouseOut={_self.props.hideToolTip}
data-key={d3.time.format(_self.props.format)(d[_self.props.xData])} data-value={d[_self.props.yData]}/>)
});
return(
<g>
{circles}
</g>
);
}
});
window.D3Dots=Dots;
var D3TimeLineChart=React.createClass({
propTypes: {
width:React.PropTypes.number,
height:React.PropTypes.number,
id:React.PropTypes.string,
interpolations:React.PropTypes.string,
data:React.PropTypes.array.isRequired,
xData:React.PropTypes.string.isRequired,
yData:React.PropTypes.string.isRequired,
margin:React.PropTypes.object,
yMaxBuffer:React.PropTypes.number,
fill:React.PropTypes.string
},
mixins:[resizeMixin],
getDefaultProps: function() {
return {
width: 800,
height: 300,
id: 'v1_chart',
interpolations:'linear',
margin:{
top: 5, right: 5, bottom: 5, left: 5
},
yMaxBuffer:10
};
},
getInitialState:function(){
return {
tooltip:{ display:false,data:{key:'',value:''}},
width:500
};
},
createChart:function(_self){
this.w = this.state.width - (this.props.margin.left + this.props.margin.right);
this.h = this.props.height - (this.props.margin.top + this.props.margin.bottom);
this.xScale = d3.time.scale()
.domain(d3.extent(this.props.data, function (d) {
return d[_self.props.xData];
}))
.rangeRound([0, this.w]);
this.yScale = d3.scale.linear()
.domain([0,d3.max(this.props.data,function(d){
return d[_self.props.yData]+_self.props.yMaxBuffer;
})])
.range([this.h, 0]);
this.area = d3.svg.area()
.x(function (d) {
return this.xScale(d[_self.props.xData]);
})
.y0(this.h)
.y1(function (d) {
return this.yScale(d[_self.props.yData]);
}).interpolate(this.props.interpolations);
var interpolations = [
"linear",
"step-before",
"step-after",
"basis",
"basis-closed",
"cardinal",
"cardinal-closed"];
this.line = d3.svg.line()
.x(function (d) {
return this.xScale(d[_self.props.xData]);
})
.y(function (d) {
return this.yScale(d[_self.props.yData]);
}).interpolate(this.props.interpolations);
this.transform='translate(' + this.props.margin.left + ',' + this.props.margin.top + ')';
},
createElements:function(element,i){
var object;
switch(element.type){
case 'dots':
object=(<D3Dots x={this.xScale} y={this.yScale} showToolTip={this.showToolTip} hideToolTip={this.hideToolTip}
{...this.props} {...element.props} key={i}/>);
break;
case 'tooltip':
object=<D3ToolTip tooltip={this.state.tooltip} key={i} {...this.props} {...element.props}/>;
break;
case 'xGrid':
object=<D3Grid h={this.h} len={this.h} scale={this.xScale} gridType="x" key={i} {...this.props} {...element.props}/>;
break;
case 'yGrid':
object=<D3Grid h={this.h} len={this.w} scale={this.yScale} gridType="y" key={i} {...this.props} {...element.props}/>;
break;
case 'xAxis':
object=<D3Axis h={this.h} scale={this.xScale} axisType="x" key={i} {...this.props} {...element.props}/>;
break;
case 'yAxis':
object=<D3Axis h={this.h} scale={this.yScale} axisType="y" key={i} {...this.props} {...element.props}/>;
break;
case 'area':
object=<path className={element.props.className} d={this.area(this.props.data)} key={i} fill={element.props.fill}/>;
break;
case 'path':
object=<path className={element.props.className} d={this.line(this.props.data)} strokeLinecap={element.props.strokeLinecap} key={i}/>;
break;
}
return object;
},
createDefs:function(element,i){
var object;
switch(element.type){
case 'gradient':
object=(<D3Gradient id={element.props.id} color1={element.props.color1} color2={element.props.color2}/>);
break;
}
return object;
},
render:function(){
this.createChart(this);
var elements;
var defs;
var _self=this;
if(this.props.children!=null) {
if (Array.isArray(this.props.children)) {
elements=this.props.children.map(function(element,i){
if(element.type!="defs")
return _self.createElements(element,i)
});
for(var i=0;i<this.props.children.length;++i){
if(this.props.children[i].type=="defs"){
var config=this.props.children[i].props.children;
if(config!=null){
if(Array.isArray(config)){
defs=config.map(function(elem,i){
return this.createDefs(elem,i)
});
}else{
defs=this.createDefs(config,0);
}
}
}
}
}else{
elements=this.createElements(this.props.children,0)
}
}
return (
<div>
<svg id={this.props.id} width={this.state.width} height={this.props.height}>
{defs}
<g transform={this.transform}>
{elements}
</g>
</svg>
</div>
);
},
showToolTip:function(e){
e.target.setAttribute('fill', '#FFFFFF');
this.setState({tooltip:{
display:true,
data: {
key:e.target.getAttribute('data-key'),
value:e.target.getAttribute('data-value')
},
pos:{
x:e.target.getAttribute('cx'),
y:e.target.getAttribute('cy')
}
}
});
},
hideToolTip:function(e){
e.target.setAttribute('fill', '#7dc7f4');
this.setState({tooltip:{ display:false,data:{key:'',value:''}}});
}
});
window.D3TimeLineChart=D3TimeLineChart;
var Panel=React.createClass({
render:function() {
return (
<div className="bg">
{this.props.children}
</div>
);
}
});
var PanelHeader=React.createClass({
propTypes: {
title:React.PropTypes.string
},
render: function () {
return (
<div className="panel-header">
<div className="pull-left panel-title">{this.props.title}</div>
<div className="pull-right line-height-30">
{this.props.children}
</div>
</div>
);
}
});
var MainContainer=React.createClass({
render:function(){
var data=[];
var parseDate = d3.time.format("%m-%d-%Y").parse;
for(var i=0;i<15;++i){
var d={day:moment().subtract(i, 'days').format('MM-DD-YYYY'),count:Math.floor((Math.random() * 80) + 50)};
d.date = parseDate(d.day);
data[i]=d;
}
var margin={
top: 20, right: 30, bottom: 20, left: 50
};
return(
<div className="row">
<div className="col-md-12 custom_padding" >
<Panel>
<PanelHeader title="Traffic Trend">
</PanelHeader>
<D3TimeLineChart data={data} xData="date" yData="count" margin={margin}
yMaxBuffer={10} id="line-chart" width={1500}>
<defs>
<gradient color1="#fff" color2="#53c79f" id="area"/>
</defs>
{/*<xGrid orient="bottom" className="y-grid" ticks={4}/>*/}
<yGrid orient="left" className="y-grid" ticks={5}/>
<xAxis orient="bottom" className="axis" tickFormat="%d/%m" ticks={4}/>
<yAxis orient="left" className="axis" ticks={5}/>
<area className="area" fill="url(#area)"/>
<path className="line shadow" strokeLinecap="round"/>
<dots r="5" format="%b %e" removeFirstAndLast={false}/>
<tooltip textStyle1="tooltip-text1" textStyle2="tooltip-text1" bgStyle="tooltip-bg" xValue="Date" yValue="Count"/>
</D3TimeLineChart>
</Panel>
</div>
</div>
);
}
});
var Page=React.createClass({
render:function(){
return(
<div className="container">
<MainContainer />
</div>
);
}
});
ReactDOM.render(<Page/>,document.getElementById("chart123"));
Also see: Tab Triggers