javascript - 忽略最后2条数据的填充

标签 javascript css svg linechart

我有一个问题,我如何绘制图表,但是来自数据的 las 2 数组忽略了来自 css 的填充。

我这里有代码:

<html>
    <head>
        <title></title>
        <script src="d3.min.js"></script>
        <style>
            body {
  font: 10px sans-serif;
}

.grid path {
    stroke-width: 0;
}

.axis path {
    fill: #E0E0E0;
    stroke: #bbb;
    shape-rendering: crispEdges;
}

.axis text {
    fill: #000;
}

.axis line {    
    stroke: #e7e7e7;
    shape-rendering: crispEdges;
}

.axis .axis-label {
    font-size: 10px;
}

.line {
    stroke-width: 1;
}
        </style>
    </head>
    <body>

    <script>
    var data =  [
    //VERD
    [{'x':15000,'y':0}, {'x':15000,'y':130},{'x':40000,'y':130},{'x':40000,'y':0},
    {'x':60000,'y':0},{'x':60000,'y':130},{'x':70000,'y':130},{'x':70000,'y':0},],

    // GRIS PARADA
    [{'x':40000,'y':0}, {'x':40000,'y':130}, {'x':60000,'y':130},{'x':60000,'y':0}],

    //TARONJA TRABAJO
    [{'x':16000,'y':40},{'x':16000,'y':80}, {'x':37000,'y':80}, {'x':37000,'y':40}],

    //BLAU RALENTI
    [{'x':17000,'y':0},{'x':17000,'y':40},{'x':35000,'y':40},{'x':35000,'y':0} ],

    //LINEA VELOCITAT
   [{'x':10000,'y':0},{'x':12000,'y':80}, {'x':15000,'y':70}, {'x':17000,'y':80},{'x':19000,'y':100},
     {'x':20000,'y':55}, {'x':27000,'y':85}, {'x':33000,'y':65}, {'x':37000,'y':25}, {'x':40000,'y':65}, {'x':45000,'y':77},
     {'x':50000,'y':47}, {'x':55000,'y':88}, {'x':59000,'y':25}, {'x':66000,'y':0}],

     //LINEA TEMPERATURA
  [{'x':10000,'y':0},{'x':12000,'y':20}, {'x':15000,'y':15}, {'x':17000,'y':18},{'x':19000,'y':17},
     {'x':20000,'y':15}, {'x':27000,'y':19}, {'x':33000,'y':12}, {'x':37000,'y':21}, {'x':40000,'y':23}, {'x':45000,'y':15},
     {'x':50000,'y':18}, {'x':55000,'y':19}, {'x':59000,'y':21}, {'x':66000,'y':20}]
];

var colors = [
    'green',
    'gray',
    'orange',
    'blue',
    'red'
]


var margin = {top: 5, right: 30, bottom: 30, left: 50},
    width = 960 - margin.left - margin.right,
    height = 200 - margin.top - margin.bottom;

var x = d3.scale.linear()
    .domain([0, 86400])
    .range([0, width]);

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

var xAxis = d3.svg.axis()
    .scale(x)
    .ticks(10)
    .tickSize(-height)
    .tickPadding(10)    
    .tickSubdivide(true)    
    .orient("bottom");  

var yAxis = d3.svg.axis()
    .scale(y)
    .ticks(5)
    .tickPadding(10)
    .tickSize(-width)
    .tickSubdivide(true)    
    .orient("left");

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

svg.append("g")
    .attr("class", "y axis")
    .append("text")
    .attr("class", "axis-label")
    .attr("transform", "rotate(-90)")
    .attr("y", (-margin.left) + 10)
    .attr("x", -height/2)
    .text('KM/H');  

svg.append("clipPath")
    .attr("id", "clip")
    .append("rect")
    .attr("width", width)
    .attr("height", height);

var line = d3.svg.line()
    .interpolate("linear")  
    .x(function(d) { return x(d.x); })
    .y(function(d) { return y(d.y); });     

svg.selectAll('.line')
    .data(data)
    .enter()
    .append("path")
    .attr("class", "line")
    .attr("clip-path", "url(#clip)")
    .attr('fill', function(d,i){ 
        return colors[i%colors.length];
    })
    .attr("d", line);       

    </script>

    </body>

</html>

这里是实际结果: Actual

我需要这个结果:

I need this

如何在没有填充的情况下显示最后一行? 真的谢谢大家。

最佳答案

当你想要<path>要排队,请使用 stroke属性而不是 fill在 SVG 中。你应该通过 index 控制特定数据在 attr 回调函数中。

 svg.selectAll('.line')
   .data(data)
   .enter()
   .append("path")
   .attr("class", "line")
   .attr("clip-path", "url(#clip)")
   .attr('fill', function(d, i) {
     return i < 4 ? colors[i % colors.length] : 'none';
   })
   .attr('stroke', function (d, i) {
     return i >= 4 ? colors[i % colors.length] : 'none';
   })
   .attr("d", line);

看看解决的 fiddle : https://jsfiddle.net/fe6jvrka/

关于javascript - 忽略最后2条数据的填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34743657/

相关文章:

Javascript灯箱未捕获类型错误: $ is not a function

html - 在 Internet Explorer 中将一个元素垂直居中于另一个元素中

javascript - 如何使用javascript获取超出其容器的元素的宽度?

javascript - 将元素附加到 d3.js 中的现有元素

Javascript onscroll 函数不起作用

Javascript: "is not a function"错误

javascript - 设置select选项框的选中显示值

html - CSS从图像条中提取图像

javascript - 如何在 Raphael SVG 中定位一个集合?

javascript - 使用javascript修改svg文件