cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

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.

            
              <div id="svg-container">
  <div class="linegraph js-linegraph">
      <div>
          <button class="info-switcher js-info-switcher" data-year="1950" data-index="0"><span>1950</span></button>
          <button class="info-switcher js-info-switcher" data-year="1960" data-index="1"><span>1960</span></button>
          <button class="info-switcher js-info-switcher" data-year="1970" data-index="2"><span>1970</span></button>
          <button class="info-switcher js-info-switcher" data-year="1980" data-index="3"><span>1980</span></button>
          <button class="info-switcher js-info-switcher" data-year="1990" data-index="4"><span>1990</span></button>
      </div>
  </div>
</div>

            
          
!
            
              #svg-container{
  padding: 20px;
}
.stage{
  .axis{
    path{
        fill : none;
        stroke: black;
        shape-rendering: crispEdges;
    }
    line {
        display: none;
    }
    text {
        font-size: 10px;
    }
  }
  .grid{
    line{
        stroke: rgba(0,0,0,0.15);
        stroke-width: 1;
        shape-rendering: crispEdges;
    }
  }
}
.info-switcher{
    display: inline-block;
    padding: 0;
    border: none;
    outline: none;
    background: none;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
    > span{
        display: inline-block;
        padding: 12px 20px;
        line-height: 1;
        background: #e6e7e8;
        border-radius: 5px;
        -webkit-transition: background 250ms, color 250ms;;
        transition: background 250ms, color 250ms;;
    }
    &:hover{
      > span{
          background: slategray; 
          -webkit-transition: background 250ms;
          transition: background 250ms;
      }
    }
    &.active{
      > span{
          color: #fff;
          background: slategray; 
          -webkit-transition: background 250ms, color 250ms;
          transition: background 250ms, color 250ms;
      }
    }
}

.linegraph {
  .graphLine{
    stroke: rgba(112, 128, 144, 0.4);
    stroke-width: 2;
    shape-rendering: crispEdges;
    fill: none;
  }
}
            
          
!
            
              class base{
    constructor(target, width, height, translate) {
        /*** general ***/
        this.target = target;
        this.$target = $(this.target);
        this.$switcher = this.$target.find('.js-info-switcher');
        this.svg = {};
        /*** property for stage ***/
        this.width = width;
        this.height = height;
        this.translate = translate;
        this.color = d3.scale.category10();
        /*** property for axis/grid ***/
        this.maxX = 0;
        this.maxY = 0;
        this.maxR = 0;
        this.step = 50;
        this.xScale = 0;
        this.yScale = 0;
        this.rangeX = [];
        this.rangeY = [];
        this.gridRangeX = [];
        this.gridRangeY = [];
        /*** dataset ***/
        this.dataset = {};
    }
    init() {
        const scope = this;
        scope.generate();
    }
    generateComponent() {
        const scope = this;
        scope.generateStage();
        scope.generateGrid();
        scope.generateAxis();
    }
    generateStage() {
        const scope = this;
        scope.svg = d3.selectAll(scope.target)
            .append('svg')
            .attr({
                'width': scope.width,
                'height': scope.height,
                'class': 'stage'
            });
        scope.getRange();
        scope.getScale();
    }
    getRange() {
        const scope = this;
        let _valueX = [];
        let _valueY = [];
        let _valueR = [];
        let _dataset = {};
        _.each(scope.dataset, function(d) {
            _dataset = d;
            _.each(_dataset, function(map) {
                _valueX.push(map.x);
                _valueY.push(map.y);
            });
        });
        scope.maxX = Math.floor((d3.max(_valueX) + 50) / scope.step) * scope.step;
        scope.maxY = Math.floor((d3.max(_valueY) + 50) / scope.step) * scope.step;
        scope.maxX += scope.step / 2;
        scope.maxY += scope.step / 2;
        scope.rangeX = d3.range(scope.step, scope.maxX, scope.step);
        scope.rangeY = d3.range(scope.step, scope.maxY, scope.step);
        scope.gridRangeX = d3.range(0, scope.maxX, scope.step / 4);
        scope.gridRangeY = d3.range(0, scope.maxY, scope.step / 4);
    }
    getScale() {
        const scope = this;
        scope.xScale = d3.scale.linear()
            .domain([0, scope.maxX])
            .range([scope.translate, scope.width - scope.translate]);

        scope.yScale = d3.scale.linear()
            .domain([0, scope.maxY])
            .range([scope.height - scope.translate, scope.translate]);
    }
    generateGrid() {
        const scope = this;
        const gridX = scope.svg.append('g').attr('class', 'grid grid--x');
        const gridY = scope.svg.append('g').attr('class', 'grid grid--y');
        scope.svg.selectAll('g.grid--x')
            .selectAll('line.x')
            .data(scope.gridRangeX)
            .enter()
            .append('line')
            .attr({
                x1: function(d, i) {
                    return scope.xScale(d);
                },
                y1: function(d, i) {
                    return scope.yScale(0);
                },
                x2: function(d, i) {
                    return scope.xScale(d);
                },
                y2: function(d, i) {
                    return scope.yScale(scope.maxY);
                }
            });

        scope.svg.selectAll('g.grid--y')
            .selectAll('line.y')
            .data(scope.gridRangeY)
            .enter()
            .append('line')
            .attr({
                x1: function(d, i) {
                    return scope.xScale(0);
                },
                y1: function(d, i) {
                    return scope.yScale(d);
                },
                x2: function(d, i) {
                    return scope.xScale(scope.maxX);
                },
                y2: function(d, i) {
                    return scope.yScale(d);
                }
            });
    }
    generateAxis() {
        const scope = this;
        const _xAxis = d3.svg.axis()
            .scale(scope.xScale)
            .tickValues(scope.rangeX)
            .outerTickSize(0)
            .orient('bottom');

        const _yAxis = d3.svg.axis()
            .scale(scope.yScale)
            .tickValues(scope.rangeY)
            .outerTickSize(0)
            .orient('left');

        scope.svg.append('g')
            .attr({
                class: 'axis axis--x',
                transform: 'translate(0, ' + (scope.height - scope.translate) + ')'
            })
            .call(_xAxis);

        scope.svg.append('g')
            .attr({
                class: 'axis axis--y',
                transform: 'translate(' + scope.translate + ', 0)'
            })
            .call(_yAxis);
    }
}

class linegraph extends base {
    constructor() {
        const _target = '.js-linegraph';
        const _width = 560;
        const _height = 300;
        const _translate = 30;
        super(_target, _width, _height, _translate);
        this.index = 0;
        this.year = 1950;
        this.walker = {};
        this.graphLine = {};
        this.animTimer;
        this.dataset = {
            "1950": [
                { "x": 50, "y": 100 }, 
                { "x": 200, "y": 400 }, 
                { "x": 320, "y": 100 }, 
                { "x": 420, "y": 200 },
                { "x": 520, "y": 100 }
            ],
            "1960": [
                { "x": 70, "y": 80 },               
                { "x": 200, "y": 300 }, 
                { "x": 330, "y": 100 }, 
                { "x": 430, "y": 480 }, 
                { "x": 550, "y": 100 }
            ],
            "1970": [
                { "x": 50, "y": 100 }, 
                { "x": 150, "y": 200 },
                { "x": 250, "y": 100 }, 
                { "x": 370, "y": 150 }, 
                { "x": 500, "y": 100 }
            ],
            "1980": [
                { "x": 50, "y": 100 }, 
                { "x": 150, "y": 150 },
                { "x": 250, "y": 100 }, 
                { "x": 370, "y": 350 }, 
                { "x": 500, "y": 100 }
            ],
            "1990": [
                { "x": 50, "y": 150 },
                { "x": 150, "y": 50 },
                { "x": 300, "y": 350 },
                { "x": 450, "y": 250 },                             
                { "x": 550, "y": 450}               
            ]
        }
        this.init();
    }
    generate() {
        const scope = this;
        scope.generateComponent();
        scope.draw();
        scope.buttonInit();
    }
    draw() {
        const scope = this;
        let i = scope.index;
        let _dataset = [];
        let _line = d3.svg.line()
              .x(function(d,i) {return scope.xScale(d[0]);})
              .y(function(d,i) {return scope.yScale(d[1]);})
              .interpolate('cardinal');

        _.each(scope.dataset[scope.year], function(d){
            let _map = d;
            _dataset.push([_map.x, _map.y])
        });

        scope.graphLine = scope.svg.append('path')
                  .attr({
                    'd': _line(_dataset)
                  })
                  .classed('graphLine', true);
      
        scope.marker = scope.svg.selectAll('circle.marker')
                  .data(_dataset)
                  .enter()
                  .append('circle')
                  .attr({
                    'cx' : function(d){
                        return scope.xScale(d[0]);
                    },
                    'cy' : function(d){
                        return scope.yScale(d[1]);
                    },
                    'r' : 5,
                    'fill' : 'rgba(112, 128, 144, 0.7)',
                    'data-index': function(d,i){
                        return i;
                    }
                  })
                  .classed('marker', true);
    }
    buttonInit() {
        const scope = this;
        scope.$switcher.on('click', function(){
            const $this = $(this);
            const _key = $this.data('year');
            const _index = $this.data('index');
            let _dataset = [];
            scope.$switcher.filter('.active').removeClass('active');
            $this.addClass('active');

            let _line = d3.svg.line()
                  .x(function(d,i) { return scope.xScale(d[0]); })
                  .y(function(d,i) { return scope.yScale(d[1]); })
                  .interpolate('cardinal');

            _.each(scope.dataset[_key], function(d){
                let _map = d;
                _dataset.push([_map.x, _map.y])
            });

            scope.svg.selectAll('path.graphLine')
                .transition()
                .attr({
                    'd': _line(_dataset)
                });

            scope.svg.selectAll('circle.marker')
                .data(_dataset)
                .transition()
                .attr({
                    'cx': function(d){
                        return scope.xScale(d[0]);
                    },
                    'cy': function(d){
                        return scope.yScale(d[1]);
                    }
                });

        });

    }
}

document.addEventListener('DOMContentLoaded', function() {
    new linegraph();
}, false);
            
          
!
999px
Loading ..................

Console