Line Graph using D3 is not starting from the date i want it to

让人想犯罪 __ 提交于 2019-12-11 10:55:20

问题


Below is my line graph where I am using 2 lines to show different values,

One line is blue and the other is red.

however the red line i want to start from 'Dec 27'(half way of the graph) instead of the start of the graph. I have been looking ways to do this but honestly I'm not sure how I can.

if someone is able to show me how i would appreciate that a lot.

Thanks you in advance!

This is my css:

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;
}

.tablebad thead tr {
    background-color: #eee;
}

.tablegood thead tr th {
    background-color: #eee;
}

This is my d3.js:

   <script>

var margin = {
    top: 0,
    right: 90,
    bottom: 30,
    left: 50
  },
  width = 1200 - 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([10, width], .10)

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

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");

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

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

//florida

var chart1 = d3.select("#LineChart")
  .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": "24-Dec-12",
    "XMAS": 2,
    "JANSALES": 0
  }, {
    "date": "25-Dec-12",
    "XMAS": 3,
    "JANSALES": 0
  },

  {
    "date": "26-Dec-12",
    "XMAS": 1,
    "JANSALES": 0
  },

  {
    "date": "27-Dec-12",
    "XMAS": 2.0,
    "JANSALES": 0
  },

  {
    "date": "28-Dec-12",
    "XMAS": 4.0,
    "JANSALES": 0
  },

  {
    "date": "29-Dec-12",
    "XMAS": 4.0,
    "JANSALES": 0
  }
  ,

  {
    "date": "29-Dec-12",
    "XMAS": 5,
    "JANSALES": 0
  },

  {
    "date": "30-Dec-12",
    "JANSALES": 3.0
  },

  {
    "date": "31-Dec-12",

    "JANSALES": 2.0
  },

  {
    "date": "01-Jan-13",

    "JANSALES": 3.0
  },

  {
    "date": "02-Jan-13",

    "JANSALES": 1.0
  },

  {
    "date": "03-Jan-13",

    "JANSALES": 3.0
  },

  {
    "date": "04-Jan-13",

    "JANSALES": 2.0
  },

  {
    "date": "05-Jan-13",

    "JANSALES": 2.0
  },

  {
    "date": "06-Jan-13",

    "JANSALES": 1.0
  },

  {
    "date": "07-Jan-13",

    "JANSALES": 2.0
  },

  {
    "date": "08-Jan-13",

    "JANSALES": 2.0
  },

  {
    "date": "09-Jan-13",

    "JANSALES": 3.0
  },

  {
    "date": "10-Jan-13",

    "JANSALES": 2.0
  },

  {
    "date": "11-Jan-13",

    "JANSALES": 3.0
  },

  {
    "date": "12-Jan-13",

    "JANSALES": 3.0
  },

  {
    "date": "13-Jan-13",

    "JANSALES": 2.0
  },

  {
    "date": "14-Jan-13",

    "JANSALES": 1.0
  }
];


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.XMAS = +d.XMAS;
  d.JANSALES = +d.JANSALES;
});

// Scale the range of the data


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

x2.domain(data1.map(function(d) {
  return d.date;
}));

//add the grid
chart1.append("g")
  .attr("class", "grid")
  .call(make_y_axis()
    .tickSize(-width, 0, 0)
    .tickFormat("")
  )

chart1.append("path")
  .attr("class", "line")
  .attr("stroke", "red")
  .attr("d", valueline(data1));

chart1.append("path")
  .attr("class", "line2")
  .attr("d", valueline2(data1.filter(function(d) {
    return d.date > parseDate("29-Dec-12");
  })));

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

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

chart1.append("text")
  .attr("transform", "translate(" + (width + 3) + "," + y(data1[0].JANSALES) + ")")
  .attr("x", ".1em")
  .attr("y", "-40")
  .attr("text-anchor", "start")
  .style("fill", "red")
  .style("font-size", "15")
  .style("font-weight", "bold")
  .text("JAN SALES");

chart1.append("text")
  .attr("transform", "translate(" + (width + 3) + "," + y(data1[0].XMAS) + ")")
  .attr("x", ".1em")
  .attr("y", "10")
  .attr("text-anchor", "start")
  .style("fill", "steelblue")
  .style("font-size", "15")
  .style("font-weight", "bold")
  .text("XMAS");

//plus 1: animation

var curtain = chart1.append('rect')
  .attr('x', -1 * width)
  .attr('y', -1 * height - 2)
  .attr('height', height)
  .attr('width', width)
  .attr('class', 'curtain')
  .attr('transform', 'rotate(180)')
  .style('fill', '#ffffff')

/* Optionally add a guideline */
var guideline = chart1.append('line')
  .attr('stroke', '#333')
  .attr('stroke-width', 0.4)
  .attr('class', 'guide')
  .attr('x1', 1)
  .attr('y1', 1)
  .attr('x2', 1)
  .attr('y2', height)

var t = chart1.transition()
  .delay(120)
  .duration(500)
  .ease('linear')
  .each('end', function() {
    d3.select('line.guide')
      .transition()
      .style('opacity', 0)
      .remove()
  });

t.select('rect.curtain')
  .attr('width', 0);
t.select('line.guide')
  .attr('transform', 'translate(' + width + ', 0)')

d3.select("#show_guideline").on("change", function(e) {


});
</script>

And finally this is my Div tag where i am calling out the d3.js:

    <div id="florida"></div>

回答1:


Right now the blue line and the red line use the same data set.

If you want the red line using a different data set, you can filter out the unwanted objects:

.attr("d", valueline2(data1.filter(function(d) {
    return d.date > parseDate("26-Dec-12");
})));

Here is your code with that change:

var margin = {
    top: 30,
    right: 100,
    bottom: 30,
    left: 50
  },
  width = 800 - margin.left - margin.right,
  height = 280 - 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");

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": "24-Dec-12",
    "trump": 2.0,
    "clinton": 0
  }, {
    "date": "25-Dec-12",
    "trump": 3.0,
    "clinton": 0
  },

  {
    "date": "26-Dec-12",
    "trump": 1.0,
    "clinton": 2
  },

  {
    "date": "27-Dec-12",
    "trump": 2.0,
    "clinton": 0
  },

  {
    "date": "28-Dec-12",
    "trump": 4.0,
    "clinton": 0
  }

  ,

  {
    "date": "29-Dec-12",
    "trump": 1.0,
    "clinton": null
  }
];


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([0, 5]);

x2.domain(data1.map(function(d) {
  return d.date;
}));

//add the grid
chart1.append("g")
  .attr("class", "grid")
  .call(make_y_axis()
    .tickSize(-width, 0, 0)
    .tickFormat("")
  )

chart1.append("path")
  .attr("class", "line")
  .attr("stroke", "red")
  .attr("d", valueline(data1));

chart1.append("path")
  .attr("class", "line2")
  .attr("d", valueline2(data1.filter(function(d) {
    return d.date > parseDate("26-Dec-12");
  })));

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

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

chart1.append("text")
  .attr("transform", "translate(" + (width + 3) + "," + y(data1[0].clinton) + ")")
  .attr("x", ".1em")
  .attr("y", "-40")
  .attr("text-anchor", "start")
  .style("fill", "red")
  .style("font-size", "15")
  .style("font-weight", "bold")
  .text("JAN SALES");

chart1.append("text")
  .attr("transform", "translate(" + (width + 3) + "," + y(data1[0].trump) + ")")
  .attr("x", ".1em")
  .attr("y", "10")
  .attr("text-anchor", "start")
  .style("fill", "steelblue")
  .style("font-size", "15")
  .style("font-weight", "bold")
  .text("XMAS");

//plus 1: animation

var curtain = chart1.append('rect')
  .attr('x', -1 * width)
  .attr('y', -1 * height - 2)
  .attr('height', height)
  .attr('width', width)
  .attr('class', 'curtain')
  .attr('transform', 'rotate(180)')
  .style('fill', '#ffffff')

/* Optionally add a guideline */
var guideline = chart1.append('line')
  .attr('stroke', '#333')
  .attr('stroke-width', 0.4)
  .attr('class', 'guide')
  .attr('x1', 1)
  .attr('y1', 1)
  .attr('x2', 1)
  .attr('y2', height)

var t = chart1.transition()
  .delay(120)
  .duration(500)
  .ease('linear')
  .each('end', function() {
    d3.select('line.guide')
      .transition()
      .style('opacity', 0)
      .remove()
  });

t.select('rect.curtain')
  .attr('width', 0);
t.select('line.guide')
  .attr('transform', 'translate(' + width + ', 0)')

d3.select("#show_guideline").on("change", function(e) {
  guideline.attr('stroke-width', this.checked ? 1 : 0);
  curtain.attr("opacity", this.checked ? 0.75 : 1);
});
    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;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="florida"></div>

This is only one of the ways to do it. There are another approaches, such as using line.defined and null values.



来源:https://stackoverflow.com/questions/43797864/line-graph-using-d3-is-not-starting-from-the-date-i-want-it-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!