JSFiddle - React, Tailwind, and code Playground

by owen_pengtao

HTML

<script src="d3js.org/d3.v3.min.js"></script>
<div id="florida">

</div>

CSS

body {
  font: 11.5px lato;
}

path.line {
  fill: none;
  stroke: #004ecc;
  stroke-width: 4px;
}

path.line2 {
  fill: none;
  stroke: #cc0000;
  stroke-width: 4px;
}

path.area {
  fill: #e7e7e7;
}

.guideline {
  margin-right: 100px;
  float: right;
}

.axis path,
.axis line {
  fill: none;
  stroke-width: 5px;
}

.x.axis path {
  display: none;
}

.y.axis path {
  display: none;
}

.grid .tick {
  stroke: black;
  stroke-width: 0.2px;
  opacity: 5;
}

.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 2px;
}

.graph-column {
  float: left;
}

JavaScript

var margin = {
    top: 30,
    right: 100,
    bottom: 30,
    left: 50
  },
  width = 800 - margin.left - margin.right,
  height = 500 - margin.top - margin.bottom,
  padding = 1;

var parseDate = d3.time.format("%d-%b-%y").parse;


// Set the ranges
var x = d3.time.scale()
  .range([10, width - 15]);

var x2 = d3.scale.ordinal().rangePoints([0, width ], .25)

var y = d3.scale.linear()
  .range([height, 0]);

var xAxis = d3.svg.axis().scale(x2)
  .orient("bottom")
  .tickFormat(d3.time.format("%b %d"))
  .ticks(4)
  .tickPadding(2);

var yAxis = d3.svg.axis().scale(y)
  .orient("left")
  .tickPadding(8);

var valueline = d3.svg.line()
  .interpolate("basis")
  .x(function(d) {
    return x(d.date);
  })
  .y(function(d) {
    return y(d.trump);
  });

var valueline2 = d3.svg.line()
  .interpolate("basis")
  .x(function(d) {
    return x(d.date);
  })
  .y(function(d) {
    return y(d.clinton);
  });

//florida

var chart1 = d3.select("#florida")
  .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 + ")");

//needed for the grid
function make_y_axis() {
  return d3.svg.axis()
    .scale(y)
    .orient("left")
}

data1 = [{
  "date": "15-Jun-16",
  "trump": 43.4,
  "clinton": 44
}, {
  "date": "15-Jul-16",
  "trump": 43.4,
  "clinton": 44
}, {
  "date": "15-Aug-16",
  "trump": 42,
  "clinton": 45.6
}, {
  "date": "15-Sep-16",
  "trump": 45.1,
  "clinton": 44.4
}, {
  "date": "06-Oct-16",
  "trump": 43.3,
  "clinton": 46.2
}, {
  "date": "10-Oct-16",
  "trump": 49.3,
  "clinton": 49.2
}];



var color = d3.scale.ordinal().range(["#004ecc", "#cc0000"]);
//d3.csv("data.csv", function(error, data) {
data1.forEach(function(d) {
  d.date = parseDate(d.date);
  d.trump = +d.trump;
  d.clinton = +d.clinton;
});

// Scale the range of the data


x.domain(d3.extent(data1, function(d) {
  return d.date;
}));
y.domain([36,...