将双变量图与线图组合在一个图表中

时间:2015-06-18 08:42:58

标签: javascript d3.js

我在将单变量区域图与线图组合在一个图d3.js中面临着艰巨的任务。如果我成功使用双变量区域图,我无法在同一图表中绘制折线图,​​反之亦然。谁都在为我工作。我提供了在x轴和y轴的双变量区域的数据。是否可以用相同的数据绘制折线图?我已经浏览了很多在线图表,我找不到一个。使用双变量面积图绘制折线图时需要帮助。

i need a graph like this

I need two lines top and bottom of bivariate area chart

<style>


body {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.x.axis path {
  display: none;
}

.area {
  fill: steelblue;
}

</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>

// data
var data = [{
      "month": "January",
      "high": "59.5",
      "low" : "57.0"
    }, {
      "month": "February",
      "high": "59.5",
      "low" : "53.4"
    }, {
      "month": "March",
      "high": "59.0",
      "low" : "53.4"
    }, {
      "month": "April",
      "high": "62.4",
      "low" : "54.7"
    }, {
      "month": "May",
      "high": "58.3",
      "low" : "52.7"
    }, {
      "month": "June",
      "high": "62.1",
      "low" : "54.5"
    }, {
      "month": "July",
      "high": "60.8",
      "low" : "53.4"
    }, {
      "month": "August",
      "high": "61.0",
      "low" : "52.5"
    }, {
      "month": "September",
      "high": "62.4",
      "low" : "52.9"
    }, {
      "month": "October",
      "high": "65.3",
      "low" : "54.0"
    }, {
      "month": "November",
      "high": "70.3",
      "low" : "55.0"
    }, {
      "month": "December",
      "high": "82.2",
      "low" : "58.6"
    }];

// margins
var margin = {top: 20, right: 30, bottom: 30, left: 50},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

// parsing data
var parseDate = d3.time.format("%B").parse;

// x-axis encoding
var x = d3.time.scale()
.range([0, width]);

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

//  x-axis scaling
var xAxis = d3.svg.axis()
    .scale(x)
    .tickFormat(d3.time.format("%b"))
    .orient("bottom");

// y-axis scaling
var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left");

// area draw
var area = d3.svg.area()
    .x(function(d) { return x(d.month); })
    .y0(function(d) { return y(d.low); })
    .y1(function(d) { return y(d.high); });

// adding the svg element
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 + ")");

//function
 function chart(error, data) {

  data.forEach(function(d) {
    d.month = parseDate(d.month);
    d.low = +d.low;
    d.high = +d.high;

  });

  // setting scales
  x.domain(d3.extent(data, function(d) { return d.month;})).range([0, 600]);
  y.domain([d3.min(data, function(d) { return d.low; }), 
    d3.max(data, function(d) { return d.high; })]);

  // area logic
  svg.append("path")
      .datum(data)
      .attr("class", "area")
      .attr("d", area);

  // appending x axis data
  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  // text label for the x axis    
  svg.append("text")      
        .attr("x", 0 )
        .attr("y",  480 )
        .style("text-anchor", "middle")
        .text("2015");

  // appending y axis data
  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Temperature (ºF)");
}
chart([],data);

/*);*/

</script>

1 个答案:

答案 0 :(得分:0)

这样做。

工作代码http://jsfiddle.net/cyril123/7a4pp6gx/2/

         <!DOCTYPE html>
    <meta charset="utf-8">
    <style>


    body {
      font: 10px sans-serif;
    }

    .axis path,
    .axis line {
      fill: none;
      stroke: #000;
      shape-rendering: crispEdges;
    }

    .x.axis path {
      display: none;
    }
    .area {
      fill: steelblue;
    }
    .line {
      fill: transparent;
      stroke:red;
      stroke-width:3px;
    }
    .line1 {
  fill: transparent;
  stroke:green;
  stroke-width:3px;
}

    </style>
    <body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
    <script>

    // data
    var data = [{
          "month": "January",
          "high": "59.5",
          "low" : "57.0"
        }, {
          "month": "February",
          "high": "59.5",
          "low" : "53.4"
        }, {
          "month": "March",
          "high": "59.0",
          "low" : "53.4"
        }, {
          "month": "April",
          "high": "62.4",
          "low" : "54.7"
        }, {
          "month": "May",
          "high": "58.3",
          "low" : "52.7"
        }, {
          "month": "June",
          "high": "62.1",
          "low" : "54.5"
        }, {
          "month": "July",
          "high": "60.8",
          "low" : "53.4"
        }, {
          "month": "August",
          "high": "61.0",
          "low" : "52.5"
        }, {
          "month": "September",
          "high": "62.4",
          "low" : "52.9"
        }, {
          "month": "October",
          "high": "65.3",
          "low" : "54.0"
        }, {
          "month": "November",
          "high": "70.3",
          "low" : "55.0"
        }, {
          "month": "December",
          "high": "82.2",
          "low" : "58.6"
        }];

    // margins
    var margin = {top: 20, right: 30, bottom: 30, left: 50},
        width = 960 - margin.left - margin.right,
        height = 500 - margin.top - margin.bottom;

    // parsing data
    var parseDate = d3.time.format("%B").parse;

    // x-axis encoding
    var x = d3.time.scale()
    .range([0, width]);

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

    //  x-axis scaling
    var xAxis = d3.svg.axis()
        .scale(x)
        .tickFormat(d3.time.format("%b"))
        .orient("bottom");

    // y-axis scaling
    var yAxis = d3.svg.axis()
        .scale(y)
        .orient("left");

    // area draw
    var area = d3.svg.area()
        .x(function(d) { return x(d.month); })
        .y0(function(d) { return y(d.low); })
        .y1(function(d) { return y(d.high); });

    var line = d3.svg.line()
        .x(function(d) { return x(d.month); })
        .y(function(d) { return y(d.high); });
var line1 = d3.svg.line()
    .x(function(d) { return x(d.month); })
    .y(function(d) { return y(d.low); });

    // adding the svg element
    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 + ")");

    //function
     function chart(error, data) {

      data.forEach(function(d) {
        d.month = parseDate(d.month);
        d.low = +d.low;
        d.high = +d.high;

      });

      // setting scales
      x.domain(d3.extent(data, function(d) { return d.month;})).range([0, 600]);
      y.domain([d3.min(data, function(d) { return d.low; }), 
      d3.max(data, function(d) { return d.high; })]);

      // area logic
      svg.append("path")
          .datum(data)
          .attr("class", "area")
          .attr("d", area);
      svg.append("path")
          .datum(data)
          .attr("class", "line")
          .attr("d", line);

  svg.append("path")
      .datum(data)
      .attr("class", "line1")
      .attr("d", line1);    

      // appending x axis data
      svg.append("g")
          .attr("class", "x axis")
          .attr("transform", "translate(0," + height + ")")
          .call(xAxis);

      // text label for the x axis    
      svg.append("text")      
            .attr("x", 0 )
            .attr("y",  480 )
            .style("text-anchor", "middle")
            .text("2015");

      // appending y axis data
      svg.append("g")
          .attr("class", "y axis")
          .call(yAxis)
        .append("text")
          .attr("transform", "rotate(-90)")
          .attr("y", 6)
          .attr("dy", ".71em")
          .style("text-anchor", "end")
          .text("Temperature (ºF)");
    }
    chart([],data);

    /*);*/

    </script>