Line And Bar Chart Combo in NVD3

给你一囗甜甜゛ 提交于 2019-12-12 02:20:57

问题


I am trying to make a graph that has bars and one single line but both obey the same Y axis. Nvd3.js have a Line+Bar chart but each series relies on separate Y Axis which doesn't give my data the real effect that it needs to show as the axis are offset to one another.

If possible, I don't really want to go away from D3.js + NVD3.js if I can help it.

Is there any way to create a bar and line chart in nvd3 or d3 that rely on the same axis?

Here is an image of the result I would like in NVD3.

Sorry that I haven't really got any example code as I haven't found any thing to create something from yet, hence why I am resorting to ask this question. But I do think this question would be very useful to the community.

Thanks


回答1:


Since you said you need a nvd3 or d3 solution I am posting a sole d3 solution.

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 the max and min of the 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>


来源:https://stackoverflow.com/questions/33541939/line-and-bar-chart-combo-in-nvd3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!