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="chartArea"></div>

<h2>Simple Curved Line Chart</h2>
<h4>Built with D3 v4</h4>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Bree+Serif');

*{
  font-family: 'Bree Serif';
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #f1f2f3;
  font-size: 12px
}

#chartArea {
  margin: 4em auto 2rem;
}

.y .tick line{
  stroke: #cfd8dc;  
}

.line {
  fill: none;
  stroke: #7460ee;
  stroke-width: 3px;
}
.area{
  fill: #7460ee;
  opacity: 0.3; 
}
.meanline{
  stroke: #7460ee;
  stroke-width: 2px;
  stroke-dasharray: .25rem;
}

.line2 {
  fill: none;
  stroke: #ffbb44;
  stroke-width: 3px;
}
.area2{
  fill: #ffbb44;
  opacity: 0.3; 
}
.meanline2{
  stroke: #ffbb44;
  stroke-width: 2px;
  stroke-dasharray: .25rem;
  stroke-width: 2px;
}

.bubble2{
  fill: #ffbb44;
}
              
            
!

JS

              
                // data setup
openCloseData = [{
  "date": "1-May-16",
  "close": 558.13
}, {
  "date": "30-Apr-16",
  "close": 553.98
}, {
  "date": "27-Apr-16",
  "close": 567
}, {
  "date": "26-Apr-16",
  "close": 589.7
}, {
  "date": "25-Apr-16",
  "close": 599
}, {
  "date": "24-Apr-16",
  "close": 630.28
}, {
  "date": "23-Apr-16",
  "close": 666.7
}, {
  "date": "20-Apr-16",
  "close": 634.98
}, {
  "date": "19-Apr-16",
  "close": 645.44
}, {
  "date": "18-Apr-16",
  "close": 643.34
}, {
  "date": "17-Apr-16",
  "close": 543.7
}, {
  "date": "16-Apr-16",
  "close": 580.13
}, {
  "date": "13-Apr-16",
  "close": 605.23
}, {
  "date": "12-Apr-16",
  "close": 622.77
}, {
  "date": "11-Apr-16",
  "close": 626.2
}, {
  "date": "10-Apr-16",
  "close": 628.44
}, {
  "date": "9-Apr-16",
  "close": 636.23
}, {
  "date": "5-Apr-16",
  "close": 633.68
}, {
  "date": "4-Apr-16",
  "close": 624.31
}, {
  "date": "3-Apr-16",
  "close": 629.32
}, {
  "date": "2-Apr-16",
  "close": 618.63
}, {
  "date": "30-Mar-16",
  "close": 599.55
}, {
  "date": "29-Mar-16",
  "close": 609.86
}, {
  "date": "28-Mar-16",
  "close": 617.62
}, {
  "date": "27-Mar-16",
  "close": 614.48
}, {
  "date": "26-Mar-16",
  "close": 606.98
}];

// data setup
openCloseData2 = [{
  "date": "1-May-16",
  "close": 600
}, {
  "date": "30-Apr-16",
  "close": 590
}, {
  "date": "27-Apr-16",
  "close": 567
}, {
  "date": "26-Apr-16",
  "close": 542
}, {
  "date": "25-Apr-16",
  "close": 575
}, {
  "date": "24-Apr-16",
  "close": 564
}, {
  "date": "23-Apr-16",
  "close": 666.7
}, {
  "date": "20-Apr-16",
  "close": 650
}, {
  "date": "19-Apr-16",
  "close": 580
}, {
  "date": "18-Apr-16",
  "close": 541
}, {
  "date": "17-Apr-16",
  "close": 531
}, {
  "date": "16-Apr-16",
  "close": 575
}, {
  "date": "13-Apr-16",
  "close": 550
}, {
  "date": "12-Apr-16",
  "close": 564
}, {
  "date": "11-Apr-16",
  "close": 587
}, {
  "date": "10-Apr-16",
  "close": 579
}, {
  "date": "9-Apr-16",
  "close": 624
}, {
  "date": "5-Apr-16",
  "close": 621
}, {
  "date": "4-Apr-16",
  "close": 641
}, {
  "date": "3-Apr-16",
  "close": 557
}, {
  "date": "2-Apr-16",
  "close": 596
}, {
  "date": "30-Mar-16",
  "close": 685
}, {
  "date": "29-Mar-16",
  "close": 578
}, {
  "date": "28-Mar-16",
  "close": 565
}, {
  "date": "27-Mar-16",
  "close": 580
}, {
  "date": "26-Mar-16",
  "close": 592
}];

// set the dimensions and margins of the graph
var margin = {
    top: 20,
    right: 20,
    bottom: 30,
    left: 50
  },
  width = 420 - margin.left - margin.right,
  height = 300 - margin.top - margin.bottom;

// parse the date / time
var parseTime = d3.timeParse("%d-%b-%y");

// set the ranges
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);

// define the line
var valueline = d3.line()
  .curve(d3.curveCatmullRom)
  .x(function(d) {
    return x(d.date);
  })
  .y(function(d) {
    return y(d.close);
  });

// append the svg obgect to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("#chartArea").append("svg")
  .attr("width", width + margin.left + margin.right)
  .attr("height", height + margin.top + margin.bottom)
  .append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");


// yAxis
const yAxis = d3.axisLeft(y)
    .tickSizeInner(-width)
    .tickSizeOuter(0)
    .tickPadding(10);

const xAxis = d3.axisBottom(x).ticks(5);

const drawGraph = (data, data2) => {

  // format the data
  data.forEach(function(d) {
    d.date = parseTime(d.date);
    d.close = +d.close;
  });
  
  data2.forEach(function(d) {
    d.date = parseTime(d.date);
    d.close = +d.close;
  });
  

  const meanValue = d3.mean(data, (d) => {
    return d.close
  });
  
  const meanValue2 = d3.mean(data2, (d) => {
    return d.close
  });
  
  var meanLine = d3.line()
    .x((d) => { return x(d.date); })
    .y(() => { return y(meanValue); });
    var meanLine2 = d3.line()
    .x((d) => { return x(d.date); })
    .y(() => { return y(meanValue2); });
  

  // Scale the range of the data
  x.domain(d3.extent(data, function(d) {
    return d.date;
  }));

  y.domain([d3.min(data, function(d) {
      return d.close - margin.top;
    }),
    d3.max(data, function(d) {
      return d.close + margin.top
    })
  ]);

   // Add the X Axis
  svg.append("g")
    .attr("transform", "translate(0," + height + ")")
    .attr('class', 'x axis')
    .call(xAxis);

  // Add the Y Axis
  svg.append("g")
    .attr("class", "y axis")
    .call(yAxis);

  
  // Add the valueline path.
  svg.append("path")
    .data([data])
    .attr("class", "line")
    .attr("d", valueline);

  svg.append("path")
    .data([data])
    .attr("class", "meanline")
    .attr("d", meanLine);
  
  // Add the valueline path.
  svg.append("path")
    .data([data2])
    .attr("class", "line2")
    .attr("d", valueline);

  svg.append("path")
    .data([data2])
    .attr("class", "meanline2")
    .attr("d", meanLine2);
 
}

//drawGraph
drawGraph(openCloseData, openCloseData2);
              
            
!
999px

Console