Это странно. Это также немного долго, поэтому извиняйтесь заранее. обновление - это оказалось 2 проблемы, см. Мой ответ ниже.Ошибка d3.js или rxjs? this.svg.selectAll (...). data (...). enter не является функцией
Вот моя ошибка: EXCEPTION: this.svg.selectAll(...).data(...).enter is not a function
У меня есть угловой-кли-клиент и сервер узла апи. Я могу получить файл states.json из службы с помощью наблюдаемого (код ниже). d3 нравится файл и отображает ожидаемую карту США.
Момент, когда я изменяю цель службы на моем сервере api из файла на сервер bluemix-cloudant, я получаю ошибку выше в моем клиенте.
Когда I console.log выводит в варианте с использованием ngOnInit, изначально mapData печатает как пустой массив и генерируется ошибка. Это очевидный источник ошибки, так как данных нет, но отладчик Chrome показывает запрос на получение запроса. Когда запрос завершается, данные печатаются, как ожидалось, в консоли.
- угловой кли версия 1.0.0-beta.26
- угловая версия^2.3.1
- d3 версия^4.4.4
- rxjs версия^5.0.1
map.component.ts:
import { Component, ElementRef, Input } from '@angular/core';
import * as D3 from 'd3';
import '../rxjs-operators';
import { MapService } from '../map.service';
@Component({
selector: 'map-component',
templateUrl: './map.component.html',
styleUrls: ['./map.component.css']
})
export class MapComponent {
errorMessage: string;
height;
host;
htmlElement: HTMLElement;
mapData;
margin;
projection;
path;
svg;
width;
constructor (private _element: ElementRef, private _mapService: MapService) {
this.host = D3.select(this._element.nativeElement);
this.getMapData();
this.setup();
this.buildSVG();
}
getMapData() {
this._mapService.getMapData()
.subscribe(
mapData => this.setMap(mapData),
error => this.errorMessage = <any>error
)
}
setup() {
this.margin = {
top: 15,
right: 50,
bottom: 40,
left: 50
};
this.width = document.querySelector('#map').clientWidth - this.margin.left - this.margin.right;
this.height = this.width * 0.6 - this.margin.bottom - this.margin.top;
}
buildSVG() {
this.host.html('');
this.svg = this.host.append('svg')
.attr('width', this.width + this.margin.left + this.margin.right)
.attr('height', this.height + this.margin.top + this.margin.bottom)
.append('g')
.attr('transform', 'translate(' + this.margin.left + ',' + this.margin.top + ')');
}
setMap(mapData) {
this.mapData = mapData;
this.projection = D3.geoAlbersUsa()
.translate([this.width /2 , this.height /2 ])
.scale(650);
this.path = D3.geoPath()
.projection(this.projection);
this.svg.selectAll('path')
.data(this.mapData.features)
.enter().append('path')
.attr('d', this.path)
.style('stroke', '#fff')
.style('stroke-width', '1')
.style('fill', 'lightgrey');
}
}
map.service.ts:
import { Http, Response } from '@angular/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class MapService {
private url = 'http://localhost:3000/api/mapData';
private socket;
constructor (private _http: Http) { }
getMapData(): Observable<any> {
return this._http.get(this.url)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body.data || {};
}
private handleError(error: any) {
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg);
return Promise.reject(errMsg);
}
}
Является ли это функцией Async, и вызов данных занимает слишком много времени для d3?
У меня были надежды, что этот вопрос Uncaught TypeError: canvas.selectAll(...).data(...).enter is not a function in d3 предложит некоторое представление, но я его не вижу.
Любая помощь или понимание очень ценятся!
РЕДАКТИРОВАТЬ: Вот скриншот раздела заголовков из запроса Chrome для каждой марки. На вкладке ответа отображаются данные, которые, как правило, отображаются как объект GeoJSON. Я также скопировал этот ответ в файл локально и использовал его в качестве источника карты с положительными результатами.
Тесты данных до сих пор: GeoJSON файл (2.1MB)
- Локальный файл, локальный сервер: Success (время отклика 54ms)
- же файл, удаленный сервер: ошибки D3, прежде чем данные, возвращаемые в браузере (750ms)
- Запрос API с удаленного сервера: ошибки D3 перед возвратом данных в браузер (2.1 с)
Вы можете показать вывод отладочной 'mapData.features'? – Assan
@Assan - это части данных GeoJSON, которые определяют границы состояний. Вот пример: '" features ": [ {" type ":" Feature "," properties ": {" GEO_ID ":" 0400000US01 "," STATE ":" 01 "," NAME ":" Alabama "," LSAD ":" "," CENSUSAREA ": 50645.326000}," geometry ": {" type ":" MultiPolygon "," Coordinates ": [[[-88.124658, 30.283640], [-88.086812, 30.259864], [-88.074854 , 30.249119], [-88.075856, 30.246139], [-88.078786, 30.245039], ...}, {next state and so on ...}] ' –
Вы убедитесь, что' this.mapData.features' - это то, что вы ожидаете (должен быть массив) внутри функции setMap? – Assan