如何使d3折线图的虚线和实线都

时间:2019-04-01 17:59:31

标签: d3.js flask

您如何渲染d3折线图,其中的线一直固定到一个点,然后用json数据划线剩余的坐标?

例如:

{
"Time": "2017-05-03 23:17",
"BTC": 19.7
},
{
"Time": "2017-05-04 20:18",
"BTC": 21.21
},
{
"Time": "2017-05-05 19:11",
"BTC": 20.1
},
{
"Time": "2017-05-06 18:56",
"BTC": 20.28
},
{
"Time": "2017-05-09 0:50",
"BTC": 21.77
}

"Time"="2017-05-05 19:11"之后的所有图形坐标均用虚线绘制;使用实线绘制"Time"="2017-05-05 19:11"之前的所有坐标

(时间在x轴上,BTC在yaxis上)

我认为答案应该看起来像这样,但是我无法获取json数据来使用

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

  .line {
    fill: none;
    stroke: steelblue;
    stroke-width: 1.5px;
  }
</style>

<body>
  <script src="//d3js.org/d3.v4.min.js"></script>
  <script>
    var margin = {
        top: 20,
        right: 20,
        bottom: 30,
        left: 50
      },
      width = 960 - margin.left - margin.right,
      height = 500 - margin.top - margin.bottom;

    var data = d3.range(11).map(function(d, i) {
      return {
        x: i,
        y: Math.random() * 100
      };
    });

    var x = d3.scaleLinear()
      .range([0, width])
      .domain([0, 10]);

    var y = d3.scaleLinear()
      .range([height, 0])
      .domain([0, 100]);

    var xAxis = d3.axisBottom()
      .scale(x);

    var yAxis = d3.axisLeft()
      .scale(y);

    var line = d3.line()
      .x(function(d) {
        return x(d.x);
      })
      .y(function(d) {
        return y(d.y);
      })
      .curve(d3.curveBasis);

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

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

    svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)

    var p = svg.append("path")
      .datum(data)
      .attr("class", "line")
      .attr("d", line);

    // draw dashed from 2.7 to 7 in the X domain
    var dashBetweenX = [2.5, 7]
        path = p.node(),
        totalLen = path.getTotalLength();

    // find the corresponding line lengths
    var dashBetweenL = dashBetweenX.map(function(d,i){

      var beginning = 0,
          end = totalLen,
          target = null,
          d = x(d);

      // find the line lengths the correspond to our X values
      // stolen from @duopixel from http://bl.ocks.org/duopixel/3824661
      while (true){
        target = Math.floor((beginning + end) / 2);
        pos = path.getPointAtLength(target);
        if ((target === end || target === beginning) && pos.x !== d) {
            break;
        }
        if (pos.x > d) end = target;
        else if (pos.x < d) beginning = target;
        else break; //position found
      }

      return target;
    })

    // draw the dashes
    var sd =  dashBetweenL[0],
        dp = dashBetweenL[0],
        count = 0;
    while (dp < dashBetweenL[1]){
      dp += 2;
      sd += ", 2";
      count++;
    }
    // per answer below needs odd number of dash array
    if (count % 2 == 0)
      sd += ", 2";   
    sd += ", " + (totalLen - dashBetweenL[1]);
    p.attr("stroke-dasharray", sd);    
  </script>
</body>

</html>

0 个答案:

没有答案