JS d3条形图没有显示数据

时间:2018-04-02 10:49:18

标签: javascript html d3.js

我正在尝试使用d3 JS库创建条形图。 我试图实现提交here的代码略有改动,但没有成功。

以下是我的完整代码示例:

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Bar Chart</title>

        <!-- Reference style.css -->
        <link rel = "stylesheet" type="text/css" href="style.css">

        <!-- Reference minified version of D3 -->
        <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
      </head>

      <body>
        <div id="barplot" style='width: 600px; height: 500px; padding-left: 5px;'></div>
        <script>
var width = 400, height = 350;
var margin = {top: 20, right: 20, left: 40, bottom: 20};

var svg2 = d3.select("#barplot").append("svg")
            .append("g")
      .attr("width", width + margin.left + margin.right)
            .attr("height", height + margin.top + margin.bottom)
            .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

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

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

createBarPlot();
updateBarPlot();

function createBarPlot(){
    svg2.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis)
        .selectAll("text")
        .style("text-anchor", "end")
        .attr("dx", "-.8em")
        .attr("dy", "-.55em")
        .attr("transform", "rotate(-90)" );

    svg2.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("Cn0");
}

function updateBarPlot(){
    // Create data array of values to visualize
  var Cn0 = [{"sat": 2, "val": 15}, {"sat": 4, "val": 20}, {"sat": 10, "val": 11}];

    x.domain(Cn0.map(function(d) { return d.sat;}));
    y.domain([0, d3.max(Cn0, function(d) { return d.val;})]);

    svg2.selectAll("bar")
        .data(Cn0)
        .enter().append("rect")
        .style("fill", "steelblue")
        .attr("x", function(d) { return x(d.sat);})
        .attr("width", x.rangeBand())
        .attr("y", function(d) { return y(d.val);})
        .attr("height", function(d) { return height - y(d.value);});
}

</script>
      </body>
    </html>

当我运行它时,我能够为条形图创建轴,但我在图表上看不到任何数据。 我怀疑问题出在updateBarPlot()方法中,但我无法弄清楚它有什么问题。 知道如何解决这个问题吗?任何帮助,将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

就x轴和y轴域的标记而言,轴的调用应在设置各个域之后进行,即.call(xAxis).call(yAxis)应该是域名设置后调用(在 updateBarPlot 函数中),如下所示:

x.domain(Cn0.map(function(d) { return d.sat;}));
y.domain([0, d3.max(Cn0, function(d) { return d.val;})]);

svg2.select('.x.axis')
  .call(xAxis);

svg2.select('.y.axis')
  .call(yAxis);

此外,我在轴的路径和刻度上添加了一些样式:

<style>

.axis {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

</style> 

这是一个结合上述内容的片段(以及@Gerardo在评论中提到的拼写错误):

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Bar Chart</title>

        <!-- Reference style.css -->

        <!-- Reference minified version of D3 -->
	<style>

	.axis {
	  font: 10px sans-serif;
	}

	.axis path,
	.axis line {
	  fill: none;
	  stroke: #000;
	  shape-rendering: crispEdges;
	}

	</style>        
        <script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
      </head>

      <body>
        <div id="barplot" style='width: 600px; height: 500px; padding-left: 5px;'></div>
        <script>
var width = 400, height = 350;
var margin = {top: 20, right: 20, left: 40, bottom: 20};

var svg2 = d3.select("#barplot").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 x = d3.scale.ordinal().rangeRoundBands([0, width], .05);

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

createBarPlot();
updateBarPlot();

function createBarPlot(){
    svg2.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")");

    svg2.append("g")
        .attr("class", "y axis")
        .append("text")
        .attr("transform", "rotate(-90)")
        .attr("y", 6)
        .attr("dy", ".71em")
        .style("text-anchor", "end")
        .text("Cn0");
}

function updateBarPlot(){
    // Create data array of values to visualize
  var Cn0 = [{"sat": 2, "val": 15}, {"sat": 4, "val": 20}, {"sat": 10, "val": 11}];

    x.domain(Cn0.map(function(d) { return d.sat;}));
    y.domain([0, d3.max(Cn0, function(d) { return d.val;})]);

		svg2.select('.x.axis')
      	.call(xAxis);

		svg2.select('.y.axis')
      	.call(yAxis);

    svg2.selectAll("bar")
        .data(Cn0)
        .enter().append("rect")
        .style("fill", "steelblue")
        .attr("x", function(d) { return x(d.sat);})
        .attr("width", x.rangeBand())
        .attr("y", function(d) { return y(d.val);})
        .attr("height", function(d) { return height - y(d.val);});
}

</script>
      </body>
    </html>