gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property 'append' of null

转载 作者:行者123 更新时间:2023-11-30 00:04:25 25 4
gpt4 key购买 nike

我在这里重新创建了 ploytly.js 下拉图:

https://plot.ly/javascript/dropdowns/#bind-dropdown-events-to-plotlyjs-charts

我将代码复制并粘贴到我的应用程序中,它运行良好。

现在我只想复制图表并将其放在原始图表下方。这是 HTML:

<div class="showcase__section" id="bubble">
<div class="spacer --small"></div>
<div id="bubbleplots">
<div class="bubbleplot" data-num="0">
<div class="plot" id="plotdiv"></div>
<div class="control-row">
Country: <select class="countrydata">
</select>
</div>
</div>
</div>
</div>

<div class="showcase__section" id="bubble">
<div class="spacer --small"></div>
<div id="bubbleplots">
<div class="bubbleplot" data-num="0">
<div class="plot1" id="plotdiv1"></div>
<div class="control-row">
Country: <select class="countrydata1">
</select>
</div>
</div>
</div>

我更改了 3 个 div,以便 javascript 可以区分它们。

下面是javascript。同样,我更改了第二张图的一些变量的名称。否则,第一张图和第二张图的 javascript 是相同的。

第一个图表在我的应用程序中完美显示,但第二个图表存在问题。第二个图表出现,图表上的数据正确,弹出菜单出现但没有国家名称。我对第二张图进行了三次 console.log('currentOption1')。前两次控制台按预期返回,但第三次显示“Uncaught TypeError: Cannot read property 'append' of null”。所以问题出在

selector.appendChild(currentOption1);

再次,

selector.appendChild(currentOption);

与第一张图完美配合。

所以 currentOption1 为空。为什么,我该如何解决?

这是 two graphs 的链接

Plotly.d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv', function(err, rows){

function unpack(rows, key) {
return rows.map(function(row) { return row[key]; });
}

var allCountryNames = unpack(rows, 'country'),
allYear = unpack(rows, 'year'),
allGdp = unpack(rows, 'gdpPercap'),
listofCountries = [],
currentCountry,
currentGdp = [],
currentYear = [];

for (var i = 0; i < allCountryNames.length; i++ ){
if (listofCountries.indexOf(allCountryNames[i]) === -1 ){
listofCountries.push(allCountryNames[i]);
}
}

function getCountryData(chosenCountry) {
currentGdp = [];
currentYear = [];
for (var i = 0 ; i < allCountryNames.length ; i++){
if ( allCountryNames[i] === chosenCountry ) {
currentGdp.push(allGdp[i]);
currentYear.push(allYear[i]);
}
}
};

// Default Country Data
setBubblePlot('Afghanistan');

function setBubblePlot(chosenCountry) {
getCountryData(chosenCountry);

var trace1 = {
x: currentYear,
y: currentGdp,
mode: 'lines+markers',
marker: {
size: 12,
opacity: 0.5
}
};

var data = [trace1];

var layout = {
title: 'GDP per cap according to Country<br>'+ chosenCountry + ' GDP'
};

Plotly.newPlot('plotdiv', data, layout);
};

var innerContainer = document.querySelector('[data-num="0"'),
plotEl = innerContainer.querySelector('.plot'),
countrySelector = innerContainer.querySelector('.countrydata');

function assignOptions(textArray, selector) {
for (var i = 0; i < textArray.length; i++) {
var currentOption = document.createElement('option');
currentOption.text = textArray[i];
selector.appendChild(currentOption);
}
}

assignOptions(listofCountries, countrySelector);

function updateCountry(){
setBubblePlot(countrySelector.value);
}

countrySelector.addEventListener('change', updateCountry, false);
});


Plotly.d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv', function(err, rows){

function unpack(rows, key) {
return rows.map(function(row) { return row[key]; });
}

var allCountryNames = unpack(rows, 'country'),
allYear = unpack(rows, 'year'),
allGdp = unpack(rows, 'gdpPercap'),
listofCountries = [],
currentCountry,
currentGdp = [],
currentYear = [];

for (var i = 0; i < allCountryNames.length; i++ ){
if (listofCountries.indexOf(allCountryNames[i]) === -1 ){
listofCountries.push(allCountryNames[i]);
}
}

function getCountryData(chosenCountry) {
currentGdp = [];
currentYear = [];
for (var i = 0 ; i < allCountryNames.length ; i++){
if ( allCountryNames[i] === chosenCountry ) {
currentGdp.push(allGdp[i]);
currentYear.push(allYear[i]);
}
}
};

// Default Country Data
setBubblePlot('Brazil');

function setBubblePlot(chosenCountry) {
getCountryData(chosenCountry);

var trace1 = {
x: currentYear,
y: currentGdp,
mode: 'lines+markers',
marker: {
size: 12,
opacity: 0.5
}
};

var data = [trace1];

var layout = {
title: 'GDP per cap according to Country<br>'+ chosenCountry + ' GDP'
};

Plotly.newPlot('plotdiv1', data, layout);
};

var innerContainer = document.querySelector('[data-num="0"'),
plotEl = innerContainer.querySelector('.plot1'),
countrySelector = innerContainer.querySelector('.countrydata1');

function assignOptions(textArray, selector) {
for (var i = 0; i < textArray.length; i++) {
var currentOption1 = document.createElement('option');
console.log('currentOption1')
currentOption1.text = textArray[i];
console.log('currentOption1')
selector.appendChild(currentOption1);
console.log('currentOption1')
}
}

assignOptions(listofCountries, countrySelector);

function updateCountry(){
setBubblePlot(countrySelector.value);
}

countrySelector.addEventListener('change', updateCountry, false);
});

最佳答案

var innerContainer = document.querySelector('[data-num="0"')

调用第一个下拉框所以我把它改成了

var innerContainer = document.getElementById('bubble1')

在我在 html 中创建“bubble1”id 以区分两者后,它从第二组相同的数据中检索了正确的数据。

关于javascript - 未捕获的类型错误 : Cannot read property 'append' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39068452/

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