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="chart123">

</div>
              
            
!

CSS

              
                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;
}
              
            
!

JS

              
                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,
        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 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 StackChart=React.createClass({
    propTypes: {
        width:React.PropTypes.number,
        height:React.PropTypes.number,
        chartId:React.PropTypes.string,
        data:React.PropTypes.array.isRequired,
        xData:React.PropTypes.string.isRequired,
        margin:React.PropTypes.object,
        keys:React.PropTypes.array,
        color:React.PropTypes.array,
        twoColorScheme:React.PropTypes.bool
    },

    mixins:[resizeMixin],

    getDefaultProps: function() {
        return {
            width: 800,
            height: 300,
            chartId: 'v1_chart',
            interpolations:'linear',
            margin:{
                top: 5, right: 5, bottom: 5, left: 5
            },
            color:[]
        };
    },
    getInitialState:function(){
        return {
            tooltip:{ display:false,data:{key:'',value:''}},
            width:500
        };
    },

    createChart:function(_self){

        if(this.props.color.length>0){
            this.color=d3.scale.ordinal()
                .range(this.props.color);
        }else{
            this.color=d3.scale.category20();
        }


        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.stacked = d3.layout.stack()(_self.props.keys.map(function(key,i){
            return _self.props.data.map(function(d,j){
                return {x: d[_self.props.xData], y: d[key] };
            })
        }));

        this.xScale = d3.scale.ordinal()
            .rangeRoundBands([0, this.w], .35)
            .domain(this.stacked[0].map(function(d) { return d.x; }));



        this.yScale = d3.scale.linear()
            .range([this.h, 0])
            .domain([0, d3.max(this.stacked[this.stacked.length - 1], function(d) { return d.y0 + d.y; })])
            .nice();

        this.transform='translate(' + this.props.margin.left + ',' + this.props.margin.top + ')';

    },

    createElements:function(element,i){
        var object;
        var _self=this;

        switch(element.type){

            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;
        }
        return object;
    },
    render:function(){
        this.createChart(this);

        var elements;
        var _self=this;

        if(this.props.children!=null) {
            if (Array.isArray(this.props.children)) {
                elements=this.props.children.map(function(element,i){
                    return _self.createElements(element,i)
                });
            }else{
                elements=this.createElements(this.props.children,0)
            }
        }


        var bars=_self.stacked.map(function(data,i){

            var rects=data.map(function(d,j){

                var fill="";

                if(_self.props.twoColorScheme) {

                    fill = _self.color(j);
                    if (i > 0) {
                        fill = "#e8e8e9";
                    }

                }

                return (<rect x={_self.xScale(d.x)}  y={_self.yScale(d.y + d.y0)} fill={fill}
                              height={_self.yScale(d.y0) - _self.yScale(d.y + d.y0)} width={_self.xScale.rangeBand()} key={j}/>);

            });

            var fill;
            if(!_self.props.twoColorScheme){
                fill=_self.color(i);
            }

            return <g key={i} fill={fill}>
                {rects}
            </g>
        });



        return (
            <div>
                <svg id={this.props.chartId} width={this.state.width} height={this.props.height}>
                    <g transform={this.transform}>
                        {elements}

                        {bars}

                    </g>

                </svg>
            </div>
        );
    }

});

window.StackChart=StackChart;

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 keys=['new','old'];
      var color=['#53c79f','#e58c72','#7a6fca','#ca6f96','#64b0cc','#e5c072'];

        var margin={
            top: 20, right: 30, bottom: 40, left: 50
        };
      var dataBar=[
            { month:'Jan', new:20, old:30 },
            { month:'Feb', new:29, old:83 },
            { month:'Mar', new:86, old:75 },
            { month:'Apr', new:13, old:57 },
            { month:'May', new:30, old:23 },
            { month:'Jun', new:50, old:27 }

        ];

        for(var i=0,j=5;i<6;++i,--j){

            var d=dataBar[i];
            d.new=Math.floor((Math.random() * 200) + 5);
            d.old=Math.floor((Math.random() * 200) + 5);


            dataBar[i]=d;
        }

        return(
            <div className="row">
                <div className="col-md-12 custom_padding" >
                    <Panel>
                        <PanelHeader title="Traffic Source">
                            
                        </PanelHeader>

                        <div className="text-center">
                            <StackChart data={dataBar} xData="month" margin={margin}
                                        id="stacked-bar" keys={keys} color={color} twoColorScheme={true}>
                                <yGrid orient="left" className="y-grid" ticks={5}/>
                                <xAxis orient="bottom" className="axis" ticks={5}/>
                                <yAxis orient="left" className="axis" ticks={5}/>
                            </StackChart>
                        </div>

                    </Panel>
                </div>                
            </div>
        );
    }
});


var Page=React.createClass({
    render:function(){
        return(
            <div className="container">
                <MainContainer />
            </div>
        );
    }
});

ReactDOM.render(<Page/>,document.getElementById("chart123"));
              
            
!
999px

Console