【发布时间】:2017-03-09 19:12:36
【问题描述】:
我目前有一个下拉菜单和日期选择器,单击它会返回一个新的数据源。类似的方法见here。
此数据源用于创建我的图表并按比例放大/缩小显示的数据量。
一般来说,我的源代码遵循这个结构:
var updateGraphData = function(error, json) {
updateGraph(json);
}
// datepicker code goes here
// url param update code as seen in linked jsfiddle goes here
function MakeUrl() {
var urlSource = BaseURL + param1 + param2; /*these values are defined in the param code mentioned */
d3.json(urlSource, updateGraphData);
}
//set dimensions
var margin = {top: 30,right: 60,bottom: 30,left: 60
};
var width = 800 - margin.left - margin.right;
var height = 400 - margin.top - margin.bottom;
var color = d3.scale.category10();
function updateGraph(json_data) {
/* everything I want drawn goes here, i.e. axes, lines, points, labels, etc.*/
};
现在,我附加的 url 源代码可以工作了——该图确实会使用正确的数据集重新呈现。但仅当我在 updateGraph 函数中定义我的 SVG 元素时。当我这样做时,会以正确的方式呈现一个新图形,但它会在旧图形下方弹出一个新图形。这不是我想要的。我希望我的新图表替换旧图表。
当我尝试在 updateGraph 函数之前拉出我的 SVG 元素时(在定义边距和尺寸的地方如此定义),该图没有在下拉更改时重复,但它以一种我无法做到的方式分层数据查看任何有用的数据。
基本上,我无法在新的数据请求中删除我的所有图表元素。您建议如何解决这个问题?我觉得它应该像在我的 updateGraphData() 函数中使用 svg.selectAll("*").remove() 一样简单,但这只会阻止任何数据在 SVG 中呈现。
对我有什么建议吗?
非常感谢!如果我不清楚,请告诉我。
【问题讨论】:
标签: javascript jquery d3.js svg