Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console