带Ajax数据的D3条形图 [英] D3 barchart with ajax data

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

问题描述

我正在使用 http://bl.ocks.org/mbostock/3885304 中的示例图表a>而不是使用tsv示例,而是希望从api调用中使用json.

I am using the example chart from http://bl.ocks.org/mbostock/3885304 and rather than use the tsv example, I wish to use json from api call.

图表的确显示了某种意义上的大斑点,就像看起来是单个条形图.

The chart does display, in the sense that it gives a large blob of what looks like a single bar.

从api.php调用返回的JSON看起来像这样

The JSON returned from the api.php call looks like this

[{"id":"2","name":"wombat","total":"98000","record_date":"2016-01-21 00:00:00"},{"id":"5","name":"wombat","total":"96000","record_date":"2016-02-21 00:00:00"},{"id":"8","name":"wombat","total":"93000","record_date":"2016-03-21 00:00:00"},{"id":"11","name":"wombat","total":"91000","record_date":"2016-04-21 00:00:00"},{"id":"14","name":"wombat","total":"92000","record_date":"2016-05-21 00:00:00"},{"id":"17","name":"wombat","total":"83000","record_date":"2016-06-21 00:00:00"},{"id":"20","name":"wombat","total":"81000","record_date":"2016-07-21 00:00:00"}]

柱塞

谢谢 凯文

推荐答案

您的json数据数组具有record_date属性而不是event_time.但是您的代码引用了event_time属性.尝试用record_date替换它们.

Your json data array has the record_date property instead of event_time. But your code refers to event_time property. Try replacing them with the record_date.

我在您的代码中找不到其他错误.

I could not find no other errors in your code.

以下是有效的代码段:

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 data = [{
  "id": "2",
  "name": "wombat",
  "total": "98000",
  "record_date": "2016-01-21 00:00:00"
}, {
  "id": "5",
  "name": "wombat",
  "total": "96000",
  "record_date": "2016-02-21 00:00:00"
}, {
  "id": "8",
  "name": "wombat",
  "total": "93000",
  "record_date": "2016-03-21 00:00:00"
}, {
  "id": "11",
  "name": "wombat",
  "total": "91000",
  "record_date": "2016-04-21 00:00:00"
}, {
  "id": "14",
  "name": "wombat",
  "total": "92000",
  "record_date": "2016-05-21 00:00:00"
}, {
  "id": "17",
  "name": "wombat",
  "total": "83000",
  "record_date": "2016-06-21 00:00:00"
}, {
  "id": "20",
  "name": "wombat",
  "total": "81000",
  "record_date": "2016-07-21 00:00:00"
}];

var k = [];
data.forEach(function(d) {
  d.record_date = d.record_date;
  d.total = +d.total;
  k.push(d.record_date)
});

x.domain(data.map(function(d) {
  return d.record_date;
}));
y.domain([0, d3.max(data, function(d) {
  return d.total;
})]);

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

svg.selectAll(".bar")
  .data(data)
  .enter().append("rect")
  .attr("class", "bar")
  .attr("x", function(d) {
    return x(d.record_date);
  })
  .attr("width", x.rangeBand())
  .attr("y", function(d) {
    return y(d.total);
  })
  .attr("height", function(d) {
    return height - y(d.total);
  });


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

.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;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

这篇关于带Ajax数据的D3条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆