2015-11-05 55 views
0

我正在试图制作一个图形,它有条形图和一条线,但都遵循相同的Y轴。 Nvd3.js有一个Line +条形图,但每个系列都依赖于单独的Y轴,它不会给我的数据带来真正的效果,因为它需要在轴相互抵消时显示。线条和条形图组合在NVD3中

如果可能的话,我真的不希望从D3.js + NVD3.js走开,如果我能帮助它。

有什么办法创造nvd3D3依赖于同一轴上的酒吧和线路图?

这是我想在NVD3中得到的结果的图像。

enter image description here

对不起,我没有真正得到任何示例代码,因为我还没有发现任何东西,从尚未创造的东西,因此为什么我诉诸问这个问题。但我确实认为这个问题对社区非常有用。

感谢

+0

是不是你在寻找什么http://nvd3.org/examples/linePlusBar.html – Cyril

+0

不是,我解释了这个问题。 – SK2017

+0

从粘贴的图像看来,条形图和折线图有不同的数据集,但是对于两者来说y轴都是一样的...... – Cyril

回答

2

既然你说你需要一个nvd3或D3解决方案我张贴唯一D3的解决方案。

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

 
var x = d3.scale.ordinal() 
 
    .rangeRoundBands([0, width], .1); 
 

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

 
var xAxis = d3.svg.axis() 
 
    .scale(x) 
 
    .orient("bottom"); 
 

 
var yAxis = d3.svg.axis() 
 
    .scale(y) 
 
    .orient("left") 
 
    .ticks(10, "%"); 
 

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

 
var barData = [{ 
 
    "letter": "A", 
 
    "frequency": 0.08167 
 
}, { 
 
    "letter": "B", 
 
    "frequency": 0.01492 
 
}, { 
 
    "letter": "C", 
 
    "frequency": 0.02782 
 
}, { 
 
    "letter": "D", 
 
    "frequency": 0.04253 
 
}, { 
 
    "letter": "E", 
 
    "frequency": 0.12702 
 
}, { 
 
    "letter": "F", 
 
    "frequency": 0.02288 
 
}, { 
 
    "letter": "G", 
 
    "frequency": 0.02015 
 
}, { 
 
    "letter": "H", 
 
    "frequency": 0.06094 
 
}, { 
 
    "letter": "I", 
 
    "frequency": 0.06966 
 
}, { 
 
    "letter": "J", 
 
    "frequency": 0.00153 
 
}, { 
 
    "letter": "K", 
 
    "frequency": 0.00772 
 
}, { 
 
    "letter": "L", 
 
    "frequency": 0.04025 
 
}, { 
 
    "letter": "M", 
 
    "frequency": 0.02406 
 
}, { 
 
    "letter": "N", 
 
    "frequency": 0.06749 
 
}, { 
 
    "letter": "O", 
 
    "frequency": 0.07507 
 
}, { 
 
    "letter": "P", 
 
    "frequency": 0.01929 
 
}, { 
 
    "letter": "Q", 
 
    "frequency": 0.00095 
 
}, { 
 
    "letter": "R", 
 
    "frequency": 0.05987 
 
}]; 
 

 
var lineData = [{ 
 
    "letter": "A", 
 
    "frequency": 0.07 
 
}, { 
 
    "letter": "B", 
 
    "frequency": 0.05492 
 
}, { 
 
    "letter": "C", 
 
    "frequency": 0.05782 
 
}, { 
 
    "letter": "D", 
 
    "frequency": 0.07253 
 
}, { 
 
    "letter": "E", 
 
    "frequency": 0.092702 
 
}, { 
 
    "letter": "F", 
 
    "frequency": 0.062288 
 
}, { 
 
    "letter": "G", 
 
    "frequency": 0.07015 
 
}, { 
 
    "letter": "H", 
 
    "frequency": 0.07094 
 
}, { 
 
    "letter": "I", 
 
    "frequency": 0.07966 
 
}, { 
 
    "letter": "J", 
 
    "frequency": 0.00453 
 
}, { 
 
    "letter": "K", 
 
    "frequency": 0.00972 
 
}, { 
 
    "letter": "L", 
 
    "frequency": 0.05025 
 
}, { 
 
    "letter": "M", 
 
    "frequency": 0.03406 
 
}, { 
 
    "letter": "N", 
 
    "frequency": 0.04749 
 
}, { 
 
    "letter": "O", 
 
    "frequency": 0.07507 
 
}, { 
 
    "letter": "P", 
 
    "frequency": 0.01929 
 
}, { 
 
    "letter": "Q", 
 
    "frequency": 0.00095 
 
}, { 
 
    "letter": "R", 
 
    "frequency": 0.05987 
 
}]; 
 
//concatenating the 2 set to get the full data for calculating teh max and min of teh domain. 
 
var fullData = [].concat.apply([], [barData, lineData]); 
 

 
x.domain(fullData.map(function (d) { 
 
    return d.letter; 
 
})); 
 
y.domain([0, d3.max(fullData, function (d) { 
 
    return d.frequency; 
 
})]); 
 
var line = d3.svg.line() 
 
    .x(function(d) { return x(d.letter)+x.rangeBand()/2; }) 
 
    .y(function(d) { return y(d.frequency); }); 
 

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

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

 
svg.selectAll(".bar") 
 
    .data(barData) 
 
    .enter().append("rect") 
 
    .attr("class", "bar") 
 
    .attr("x", function (d) { 
 
    return x(d.letter); 
 
}) 
 
    .attr("width", x.rangeBand()) 
 
    .attr("y", function (d) { 
 
    return y(d.frequency); 
 
}) 
 
    .attr("height", function (d) { 
 
    return height - y(d.frequency); 
 
}); 
 
svg.append("path") 
 
     .datum(lineData) 
 
     .attr("class", "line") 
 
     .attr("d", line);
.bar { 
 
    fill: steelblue; 
 
} 
 
.bar:hover { 
 
    fill: brown; 
 
} 
 
.axis { 
 
    font: 10px sans-serif; 
 
} 
 
.axis path, .axis line { 
 
    fill: none; 
 
    stroke: #000; 
 
    shape-rendering: crispEdges; 
 
} 
 
.x.axis path { 
 
    display: none; 
 
} 
 
.line { 
 
    fill: none; 
 
    stroke: red; 
 
    stroke-width: 1.5px; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

希望这有助于!

+0

这是完美的,我应该可以自己添加NVD3功能。非常感谢你! – SK2017