javascript - window.onresize 事件从图表对象中删除 d3 数据
问题描述
我创建了一个 js 构造函数来创建一个 D3 图表对象。
该功能似乎适用于初始负载。但是,我还添加了一个onresize
侦听器来重新绘制调整大小的图表。
但是,当 resize 事件发生时,用于制作图表的数据的 console.log() 显示该dates
字段(第一个字段)是 all null
。我没有成功使用 console.log 来查找实际发生此更改的位置。在调整大小之前和图表绘制功能运行之前,数据似乎正常。
这是一个jsfiddle,它是完整的代码。调整视口大小时,图表会消失。控制台显示myData
是null
相关代码:
HTML
var firstChart = new LineChart(myData,'chart-container','chart_area','This Is A Chart',["red","yellow","blue"],"temp","date")
//Re draw on resize
window.onresize = function(){
firstChart.drawChart();
console.log(myData);
}
数据
var myData = [
{"date":20111001,"New_York":63.4,"San_Francisco":62.7,"Austin":72.2},
{"date":20111002,"New_York":58,"San_Francisco":59.9,"Austin":67.7},
{"date":20111003,"New_York":53.3,"San_Francisco":59.1,"Austin":69.4},
{"date":20111004,"New_York":55.7,"San_Francisco":58.8,"Austin":68},
{"date":20111005,"New_York":64.2,"San_Francisco":58.7,"Austin":72.4},
{"date":20111006,"New_York":58.8,"San_Francisco":57,"Austin":77},
{"date":20111007,"New_York":57.9,"San_Francisco":56.7,"Austin":82.3},
{"date":20111008,"New_York":61.8,"San_Francisco":56.8,"Austin":78.9},
{"date":20111009,"New_York":69.3,"San_Francisco":56.7,"Austin":68.8},
{"date":20111010,"New_York":71.2,"San_Francisco":60.1,"Austin":68.7},
{"date":20111011,"New_York":68.7,"San_Francisco":61.1,"Austin":70.3},
{"date":20111012,"New_York":61.8,"San_Francisco":61.5,"Austin":75.3},
{"date":20111013,"New_York":63,"San_Francisco":64.3,"Austin":76.6},
{"date":20111014,"New_York":66.9,"San_Francisco":67.1,"Austin":66.6},
{"date":20111015,"New_York":61.7,"San_Francisco":64.6,"Austin":68},
{"date":20111016,"New_York":61.8,"San_Francisco":61.6,"Austin":70.6},
{"date":20111017,"New_York":62.8,"San_Francisco":61.1,"Austin":71.1},
{"date":20111018,"New_York":60.8,"San_Francisco":59.2,"Austin":70},
{"date":20111019,"New_York":62.1,"San_Francisco":58.9,"Austin":61.6},
{"date":20111020,"New_York":65.1,"San_Francisco":57.2,"Austin":57.4},
{"date":20111021,"New_York":55.6,"San_Francisco":56.4,"Austin":64.3}
]
在调整日期列是全部null
JS
function LineChart(data,chartContainerID,chartAreaId,chartTitle,colorArray,yaxisLabel,xaxisLabel,legend,yaxisFormat, marginsDict){
this.data = data;
this.chartContainer = document.getElementById(chartContainerID)
this.chartArea = document.getElementById(chartAreaId);
this.chartTitle = chartTitle;
this.colors = colorArray;
this.yaxisLabel=yaxisLabel;
this.xaxisLabel=xaxisLabel;
this.isLegend = legend;
this.yaxisFormat= yaxisFormat;
this.margins = marginsDict;
///....more code here in js fiddle.....
this.drawChart = function(){
///....more code here in js fiddle.....
///Console leads me here. more code above it
var items = color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
console.log(d)
return {
date: d.date,
result: +d[name]
};
})
};
});
///....more code here in js fiddle.....
}
}
解决方案
推荐阅读
- javascript - Javascript - 检查对象和数组的 null 或 undefined 的最简单有效的方法
- java - IntelliJ IDEA 花费了很长时间来编译一个简单的“Hello World!”。使用 MacBook 编写代码
- mysql - 如何使用 Nodejs 在 MySQL 中的 post 请求后插入数组项
- xml - 使用 XSLT 遍历兄弟元素
- smtp - Virtualmin 服务器通过另一个 Virtualmin 服务器发送/接收邮件?
- python-iris - 使用 Iris 计算一些季节性气候指标
- linux - Linux:仅在字符串中查找最后一个“/”的位置
- python - Python 格式:模块文档字符串和首次导入之间的正确空行数是多少?
- swift - Swift 如何处理协议是否有解决此错误的方法?
- python - PyDrive - 擦除文件的内容