本文介绍了D3 v4条形图X轴带有负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此示例 https://gist.github.com/mbostock/2368837绘制带有负值和正值的条形图,但我无法正确设置负x轴.有人可以告诉我我的错误是什么吗?

I was using this example https://gist.github.com/mbostock/2368837 to plot a bar chart with negative and positive values but i am not able to set the negative x axis correctly. Can someone please tell me what my error is?

var margin = {top: 30, right: 10, bottom: 50, left: 50},
    width = $('.col-lg-12').width(),
    height = 420;

// Add svg to
var svg = d3.select('#id').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 + ')');

// set the ranges
var y = d3.scaleBand()
    .range([height, 0])
    .padding(0.1);

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

// Scale the range of the data in the domains
x.domain([0, d3.max(data, function (d) {
    return Math.abs(d.value);
})]);
y.domain(data.map(function (d) {
    return d.dataset;
}));

// append the rectangles for the bar chart
svg.selectAll(".bar")
    .data(data)
    .enter().append("rect")
    .attr("class", function (d) {
        return "bar bar--" + (d.value < 0 ? "negative" : "positive");
    })
    .attr("x", function (d) {
        return x(Math.min(0, d.value));
    })
    .attr("y", function (d) {
        return y(d.dataset);
    })
    .attr("width", function (d) {
        return Math.abs(x(d.value) - x(0));
    })
    .attr("height", y.bandwidth());

// add the x Axis
svg.append("g")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x));

// add the y Axis
svg.append("g")
    .attr("class", "y axis")
    .attr("transform", "translate(" + x(0) + ",0)")
    .call(d3.axisRight(y));

推荐答案

问题仅仅是x域.由于您具有负值,因此:

The problem is just the x domain. Since you have negative values, instead of:

x.domain([0, d3.max(data, function (d) {
    return Math.abs(d.value);
})]);

应该是:

x.domain(d3.extent(data, function (d) {
    return d.value;
}));

这里是一个使用您的代码处理伪数据的演示:

Here is a demo using your code with fake data:

var margin = {top: 30, right: 10, bottom: 50, left: 50},
    width = 500,
    height = 300;

var data = [{value: 10, dataset:"barbaz"},
{value: 40, dataset:"barbar"},
{value: -10, dataset:"foobaz"},
{value: 50, dataset:"foobar"},
{value: 30, dataset:"baz"},
{value: -20, dataset:"bar"},
{value: 70, dataset:"foo"}];

// Add svg to
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 + ')');

// set the ranges
var y = d3.scaleBand()
    .range([height, 0])
    .padding(0.1);

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

// Scale the range of the data in the domains
x.domain(d3.extent(data, function (d) {
    return d.value;
}));
y.domain(data.map(function (d) {
    return d.dataset;
}));

// append the rectangles for the bar chart
svg.selectAll(".bar")
    .data(data)
    .enter().append("rect")
    .attr("class", function (d) {
        return "bar bar--" + (d.value < 0 ? "negative" : "positive");
    })
    .attr("x", function (d) {
        return x(Math.min(0, d.value));
    })
    .attr("y", function (d) {
        return y(d.dataset);
    })
    .attr("width", function (d) {
        return Math.abs(x(d.value) - x(0));
    })
    .attr("height", y.bandwidth());

// add the x Axis
svg.append("g")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x));

// add the y Axis
svg.append("g")
    .attr("class", "y axis")
    .attr("transform", "translate(" + x(0) + ",0)")
    .call(d3.axisRight(y));
.bar--positive {
  fill: steelblue;
}

.bar--negative {
  fill: darkorange;
}
<script src="https://d3js.org/d3.v4.min.js"></script>

这篇关于D3 v4条形图X轴带有负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 06:32