2016-01-11 119 views
0

嗨,大家好,我在做這個圖表,我需要用var顏色填充相同顏色的數據。填充線D3 js

在這裏,我把一個截圖從實際的折線圖:

Line chart without fill

下面的代碼:

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

.grid .tick { 
    stroke: lightgrey; 
    opacity: 0.7; 
    shape-rendering: crispEdges; 
} 

.grid path { 
    stroke-width: 0; 
} 

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

.axis text { 
    fill: #555; 
} 

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

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

.line { 
    fill: none; 
    stroke-width: 1.5px; 
} 

.dot { 

    stroke: transparent; 
    stroke-width: 10px; 
    cursor: pointer; 
} 

.dot:hover { 
    stroke: rgba(68, 127, 255, 0.3); 
} 
     </style> 
    </head> 
    <body> 


    <div id="graph" class="aGraph" style="position:absolute;top:0px;left:0; float:left;"></div> 


    <script> 
    var data = [ 
    [{'x':0,'y':5},{'x':9,'y':5}, {'x':9,'y':-1}, {'x':12,'y':-1},{'x':12,'y':5}, {'x':17,'y':5}, {'x':17,'y':-1}], 
    [{'x':1,'y':-1},{'x':1,'y':1},{'x':6,'y':1},{'x':6,'y':-1} ], 
    [{'x':3,'y':-1},{'x':3,'y':3}, {'x':7,'y':3}, {'x':7,'y':-1}] 
]; 

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


//************************************************************ 
// Create Margins and Axis and hook our zoom function 
//************************************************************ 
var margin = {top: 20, right: 30, bottom: 30, left: 50}, 
    width = 960 - margin.left - margin.right, 
    height = 300 - margin.top - margin.bottom; 

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

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

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

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

var zoom = d3.behavior.zoom() 
    .x(x) 
    .y(y) 
    .scaleExtent([1, 10]) 
    .on("zoom", zoomed);  





//************************************************************ 
// Generate our SVG object 
//************************************************************ 
var svg = d3.select("body").append("svg") 
    .call(zoom) 
    .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); 





//************************************************************ 
// Create D3 line object and draw data on our SVG object 
//************************************************************ 
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('stroke', function(d,i){   
     return colors[i%colors.length]; 
    }) 
    .attr("d", line);  




//************************************************************ 
// Draw points on SVG object based on the data given 
//************************************************************ 
var points = svg.selectAll('.dots') 
    .data(data) 
    .enter() 
    .append("g") 
    .attr("class", "dots") 
    .attr("clip-path", "url(#clip)"); 

points.selectAll('.dot') 
    .data(function(d, index){  
     var a = []; 
     d.forEach(function(point,i){ 
      a.push({'index': index, 'point': point}); 
     });  
     return a; 
    }) 
    .enter() 
    .append('circle') 
    .attr('class','dot') 
    .attr("r", 2.5) 
    .attr('fill', function(d,i){  
     return colors[d.index%colors.length]; 
    }) 
    .attr("transform", function(d) { 
     return "translate(" + x(d.point.x) + "," + y(d.point.y) + ")"; } 
    ); 






//************************************************************ 
// Zoom specific updates 
//************************************************************ 
function zoomed() { 
    svg.select(".x.axis").call(xAxis); 
    svg.select(".y.axis").call(yAxis); 
    svg.selectAll('path.line').attr('d', line); 

    points.selectAll('circle').attr("transform", function(d) { 
     return "translate(" + x(d.point.x) + "," + y(d.point.y) + ")"; } 
    ); 
} 
    </script> 



    </body> 
</html> 

我只需要從圖表填充矩形。

但不是所有的行,只是第一個和第二個數組。

我需要這樣一個結果: I need this result

在此行中我tryied更改與填充行程,但是這並不工作...我闖到大有更多的想法...

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

解決了問題。只是css拖動我...

+1

即時通訊不完全瞭解目標。你想要在行後添加矩形,給出填充。另外,您可以使用填充和描邊的多邊形。 – heavyhorse

回答

1

你想修改你的圖表。在你的照片中,你正在使用線條,但你可能想要使用rect對象。然後,你可以只設置它們填充:

d3.select("svg") 
 
    .append("rect") 
 
    .attr({ x: 10, y: 10, height: 50, width: 100}) 
 
    .style("fill", "blue");
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 
 
<svg width="500" height="500"></svg>

如果你想保持你的線/點,那麼要確保你繪製矩形第一,以確保它在低了下去z-順序與SVG根據文檔中的外觀跟蹤z順序。

+0

但我需要很多的精神與圖表上的數據顯示... 在這篇文章中,我只有0到30,但最終的x是0到86400. 我可以用rects和acurracy畫這個嗎? –

+0

我想我必須做一些與車道的填充,但我沒有想法,我可以... –

+0

@ SebasBejenaru:這是一個不同的問題。你不會想要遠遠超過幾千個SVG元素,否則你的表現會慢下來。 – Ian