Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function drawLine() {
- // set the dimensions and margins of the graph
- var margin = {top: 20, right: 20, bottom: 30, left: 50},
- width = window.screen.availWidth * .95 - margin.left - margin.right,
- height = window.screen.availHeight * .2 - margin.top - margin.bottom;
- // parse the date / time
- var parseTime = d3.timeParse("%Y-%m-%d");
- // set the ranges
- var x = d3.scaleTime().range([0, width]);
- var y = d3.scaleLinear().range([height, 0]);
- // define the line
- var valueline = d3.line()
- .x(function(d) { return x(d.date); })
- .y(function(d) { return y(d.streams); });
- // 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("body").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 + ")");
- // Get the data
- d3.csv("./data/time.csv", function(error, data) {
- if (error) throw error;
- console.log(data)
- // format the data
- data.forEach(function(d) {
- d.date = parseTime(d.date);
- d.streams = +d.streams;
- });
- // Scale the range of the data
- x.domain(d3.extent(data, function(d) { return d.date; }));
- y.domain([100000000, d3.max(data, function(d) { return d.streams; })]);
- // Add the valueline path.
- svg.append("path")
- .data([data])
- .attr("class", "line")
- .attr("d", valueline);
- // Add the X Axis
- svg.append("g")
- .attr("transform", "translate(0," + height + ")")
- .call(d3.axisBottom(x));
- var line_group = svg.append('g');
- var line = line_group.append("line")
- .attr("class", "timeLine")
- .attr("x1", 0)
- .attr("y1", 0)
- .attr("x2", 0)
- .attr("y2", height)
- .attr("opacity", 1)
- .attr("style", "stroke:steelblue;stroke-width:1")
- var text = line_group.append("text")
- var text2 = line_group.append("text").attr("transform", "translate(0,20)")
- // line_group = d3.selectAll(".timeLine,.date")
- function getDate(mouse){
- // var mouse = d3.mouse(this)
- var xDate = x.invert(mouse[0]),
- bisect = d3.bisector(function(d) { return d.date;}).right;
- idx = bisect(data, xDate);
- return [xDate, idx];
- }
- svg.append('rect')
- .attr('width', width)
- .attr('height', height)
- .attr('opacity', 0)
- .on('mouseout', () => line_group.attr('opacity', 0))
- .on('mouseover', () => line_group.attr('opacity', 1))
- .on('mousemove', function(){
- var val = getDate(d3.mouse(this))
- var date = val[0], idx = val[1];
- if (date.getFullYear() == "2019") {
- text.attr("transform", "translate(-2,0)").attr("text-anchor","end");
- text2.attr("transform", "translate(-2,20)").attr("text-anchor","end");
- }
- else {
- text.attr("transform", "translate(0,0)").attr("text-anchor","start");
- text2.attr("transform", "translate(0,20)").attr("text-anchor","start");
- }
- line_group.attr('transform', 'translate(' + x(date) + ',0)')
- text.text("Date: " + date.toLocaleDateString("en-US"))
- text2.text("Streams: " + data[idx].streams)
- })
- .on("click", function() {
- var date = getDate(d3.mouse(this))
- drawMap(date[0])
- })
- });
- }
- drawLine();
- /* THIS PART GOES INTO STYLE.CSS
- .line {
- fill: none;
- stroke: steelblue;
- stroke-width: 2px;
- opacity: 1;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement