0

我正在使用 d3 制作条形图。从示例中我看到他们使用 tsv 文件作为数据。就我而言,我有动态数据。这是代码,我想使用 var data1 来制作图表

    <!DOCTYPE html>
    <meta charset="utf-8">
    <style>

    body {
      font: 10px sans-serif;
    }

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

    .bar {
      fill: steelblue;
    }

    .x.axis path {
      display: none;
    }

    </style>
    <body>
        <div id="bar"></div>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>
    var $window = $(window);
    var wWidth  = $window.width();
    var wHeight = $window.height();

    var margin = {top: 10, right: 0, bottom: 30, left: 40},
        width = 300 - margin.left - margin.right,
        height = 300 - margin.top - margin.bottom;

    var formatPercent = d3.format(".0%");

    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")
        .tickFormat(formatPercent);
    //var data = [[1,1],[2,3],[3,2],[4,5],[5,4]];
    var svg = d3.select("#bar").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 data1 = [
        {letter:"A",frequency:"58.13"},
        {letter:"B",frequency:"53.98"},
        {letter:"C",frequency:"67.00"},
        {letter:"D",frequency:"89.70"},
        {letter:"E",frequency:"99.00"}
    ];
    d3.tsv("data.tsv", type, function(error, data) {


      x.domain(data.map(function(d) { return d.letter; }));
      y.domain([0, d3.max(data, function(d) { return 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(data)
        .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); });

    });

    function type(d) {
      d.frequency = +d.frequency;
      return d;
    }

    </script>

数据.tsv

  letter    frequency
A   .08167
B   .01492
C   .02780
D   .04253
E   .12702
F   .02288
G   .02022
H   .06094
I   .06973
J   .00153
4

1 回答 1

1

更改变量名,

    data1 To data

并删除这些行

 1.  d3.tsv("data.tsv", type, function(error, data) {
 2.  });

在此处查看演示

于 2013-09-24T07:22:31.203 回答