gpt4 book ai didi

javascript - D3 图表问题,数据绑定(bind)不起作用

转载 作者:行者123 更新时间:2023-12-02 14:14:44 26 4
gpt4 key购买 nike

正在处理 D3 图表,并希望根据数据的变化(在范围内)更改图表的值。

到目前为止,所有日志都正确通过,我的 $scope.watch 调用正在运行并正在调用该函数。新的、更新的数据显示在日志中,但图表没有变化。

关于如何调试有什么想法吗?

  .directive('barsChart', function($parse) {
var directiveDefinitionObject = {
restrict: 'E',
scope: {data: '=chartData'},
link: function (scope, element, attrs) {
scope.$watch('data', function(newVals, oldVals) {
triggerRelink();
}, true);

var triggerRelink = function() {

console.log("this is the scope", scope.data);
var margin = {
top: 25,
right: 75,
bottom: 85,
left: 85
},
w = 600 - margin.left - margin.right,
h = 350 - margin.top - margin.bottom;
var padding = 10;

var colors = [
["Local", "#2ba8de"],
["Global", "#318db4"]
];

var dataset = [{
"keyword": "Current Home",
"global": 0,
"local": scope.data[0]
}, {
"keyword": "Extra Bathroom",
"global": 0,
"local": scope.data[1]
}
];
console.log("the dataset", dataset);
var xScale = d3.scale.ordinal()
.domain(d3.range(dataset.length))
.rangeRoundBands([0, w], 0.05);
// ternary operator to determine if global or local has a larger scale
var yScale = d3.scale.linear()
.domain([0, d3.max(dataset, function (d) {
return (d.local > d.global) ? d.local : d.global;
})])
.range([h, 0]);
var xAxis = d3.svg.axis()
.scale(xScale)
.tickFormat(function (d) {
return dataset[d].keyword;
})
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(5);

var commaFormat = d3.format(',');

// existing
var svg = d3.select(element[0])
.append("svg")
.attr("width", w + margin.left + margin.right)
.attr("height", h + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

// Graph Bars
var sets = svg.selectAll(".set")
.data(dataset)
.enter()
.append("g")
.attr("class", "set")
.attr("transform", function (d, i) {
return "translate(" + xScale(i) + ",0)";
});
sets.append("rect")
.attr("class", "local")
.attr("width", xScale.rangeBand() / 2)
.attr("y", function (d) {
return yScale(d.local);
})
.attr("x", xScale.rangeBand() / 2)
.attr("height", function (d) {
return h - yScale(d.local);
})
.attr("fill", colors[0][1]);

sets.append("rect")
.attr("class", "global")
.attr("width", xScale.rangeBand() / 2)
.attr("y", function (d) {
return yScale(d.global);
})
.attr("height", function (d) {
return h - yScale(d.global);
})
.attr("fill", colors[1][1]);
// xAxis
svg.append("g") // Add the X Axis
.attr("class", "x axis")
.attr("transform", "translate(0," + (h) + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", function (d) {
return "rotate(-25)";
});
// yAxis
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(0 ,0)")
.call(yAxis);
// xAxis label
svg.append("text")
.attr("transform", "translate(" + (w / 2) + " ," + (h + margin.bottom - 5) + ")")
.style("text-anchor", "middle");
// .text("Renovation Type");
//yAxis label
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x", 0 - (h / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Home Value");
};
triggerRelink();
}
};
return directiveDefinitionObject;
})

enter image description here

最佳答案

您需要删除在 watch 函数中调用的 triggerRelink() 中的旧 svg,如下所示:

var svg = d3.select(element[0]).select("svg").remove();
//append the new svg
var svg = d3.select(element[0])
.append("svg")
.attr("width", w + margin.left + margin.right)
.attr("height", h + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

关于javascript - D3 图表问题,数据绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39137501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com