简体   繁体   English

D3.js更新了json的条形图

[英]D3.js update bar chart from json

I'm working on a bar chart using d3.js, the bar shoud be able correspond to a new json data. 我正在使用d3.js在条形图上工作,条形图能够对应于新的json数据。

var m = [30, 5, 5, 5],
    w = 375 - m[1] - m[3],
    h = 260 - m[0] - m[2];

var format = d3.format(",.0f");

var wdthnew = d3.scale.linear().range([0, w]),
    wdth = d3.scale.linear().range([0, w]).domain([0, 25000]),
    hght = d3.scale.ordinal().rangeRoundBands([0, h], .1);

var svg = d3.select("body").append("svg")
    .attr("width", w + m[1] + m[3])
    .attr("height", h + m[0] + m[2])
  .append("g")
    .attr("transform", "translate(" + m[3] + "," + m[0] + ")");

d3.json("barchartjson.php", function(data) {

  data.sort(function(a, b) { return b.value - a.value; });
  hght.domain(data.map(function(d) { return d.orientation; }));

  var bar = svg.selectAll("g.bar")
      .data(data)
    .enter().append("g")
      .attr("class", "bar")
      .attr("transform", function(d) { return "translate(0," + hght(d.orientation) + ")"; });

  bar.append("rect")
      .attr("id", "activebar") 
      .attr("width", 0)
      .attr("height", hght.rangeBand())
    .transition()
    .delay(100)
    .duration(1000)    
      .attr("width", function(d) { return wdth(d.value); });    
});

function makebar(date1, date2) {
var barchartjson = "barchartjson.php?df="+date1+"&dl="+date2;

d3.json(barchartjson , function(datanew) { 

  datanew.sort(function(a, b) { return b.jumlah - a.jumlah; });

  wdthnew.domain([0, d3.sum(datanew, function(d) { return d.value; })]);

  console.log("SUM datanew: "+ d3.sum(datanew, function(d) { return d.value; }));
  console.log("Array datanew: ");
  console.log(datanew);
  var updatebar = svg.selectAll("#activebar");

  updatebar.transition().duration(1000)
        .attr("width", function(a) { console.log("update width: wdthnew("+a.value+") = "+wdthnew(a.value));
        return wdthnew(a.value); });  
});
}

Here is the json from barchartjson.php used at the first time: 这是第一次使用barchartjson.php的json:

[{"orientation":"negatif","value":"15964"},{"orientation":"netral","value":"2788"},{"orientation":"positif","value":"9701"}]

The bar is showing properly for the first time. 酒吧第一次正确显示。 The problem is when makebar() is called, updating the bar width with the new json data. 问题是调用makebar()时,用新的json数据更新条宽。 Each bar width is updated, but in some case they are out of range. 每个条形宽度都会更新,但在某些情况下,它们会超出范围。 Here is the output from console.log : 这是console.log的输出:

SUM datanew: 2520
Array datanew: 
[Object
value: "1411"
orientation: "negatif"
__proto__: Object
, 
Object
value: "846"
orientation: "positif"
__proto__: Object
, 
Object
value: "263"
orientation: "netral"
__proto__: Object
]
update width: wdthnew(15964) = 2312.246031746032
update width: wdthnew(9701) = 1405.1051587301588
update width: wdthnew(2788) = 403.81746031746036

a new json successfully called , but the log showing that wdthnew is keep scaling the old json. 一个新的json成功调用,但是日志显示wdthnew继续缩放旧的json。

 updatebar.transition().duration(1000)
        .attr("width", function(a) { console.log("update width: wdthnew("+a.value+") = "+wdthnew(a.value));
        return wdthnew(a.value); }); 

resulting: 导致:

update width: wdthnew(15964) = 2312.246031746032

despite 15694 is from the old json, the wdthnew(d.value) is actually return a value from wdthnew(1411) wich is 2312 and it is out of range.. 尽管15694是来自旧的json,但是wdthnew(d.value)实际上是从wdthnew(1411)返回的值是2312并且它超出了范围。

Can someone help me solve this problem? 有人可以帮我解决这个问题吗? Or point out where I'm doing wrong? 或者指出我做错了什么? I'm new to d3.js and javascript. 我是d3.js和javascript的新手。 I'm sorry for the bad grammar. 对不起语法我很抱歉。 English is not my native language. 英语不是我的母语。

jsFiddle here: http://jsfiddle.net/saido/5mREA/ jsFiddle这里: http//jsfiddle.net/saido/5mREA/

Turns out the problem was in function makebar(): at this line I supposed to attach the new data: 原来问题出在function makebar():在这一行我应该附加新数据:

var updatebar = svg.selectAll("#activebar").data(datanew);

Now the console.log showing and calculating the proper values. 现在console.log显示并计算正确的值。

I'm sorry I'm not sure how to explain the technical issue in much more detail. 对不起,我不知道如何更详细地解释技术问题。

Perhaps create a buffer array/object that will store the data prior to using it... Then create an update interval that won't update before new data arrives. 也许创建一个缓冲区数组/对象,在使用之前存储数据...然后创建一个更新间隔,在新数据到达之前不会更新。

Update: Here's an example 更新:这是一个例子

var data = [{x: 1, y: 3},
            {x: 2, y: 4},
            {x: 3, y: 5},
            ...
            ];

var incomingData = [];

/* incoming data gets .pushed(...) in every 1 or
    2 seconds, depending on network traffic */

var updateDelay = 3000;

setInterval(function() {
        for (var index = 0; index < data.length; index++) {
            seriesData.shift();
            seriesData.push(incomingData.shift());
            redrawGraph();
        }
    }, updateDelay);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM