angular2中的d3.js折线图从url(服务器)而非本地数据获取数据
我在D3V4线形图中遇到困难,因为我想使用angular 2中服务器的一些虚拟数据绘制线形图。 我已经从这个链接获得了帮助。 我对angular2和d3不熟悉。请帮忙,我们将感谢你的帮助。 有人在plunker上这样做,但它必须从url(服务器)获取数据,而不是本地数据和angular2 有人能帮忙吗angular2中的d3.js折线图从url(服务器)而非本地数据获取数据,angular,d3.js,Angular,D3.js,我在D3V4线形图中遇到困难,因为我想使用angular 2中服务器的一些虚拟数据绘制线形图。 我已经从这个链接获得了帮助。 我对angular2和d3不熟悉。请帮忙,我们将感谢你的帮助。 有人在plunker上这样做,但它必须从url(服务器)获取数据,而不是本地数据和angular2 有人能帮忙吗 Data in Url:- -------------- [{"title":"A","value":123},{"title":"B","value":445},{"title":"C
Data in Url:-
--------------
[{"title":"A","value":123},{"title":"B","value":445},{"title":"C","value":666},{"title":"D","value":123},{"title":"E","value":876},{"title":"F","value":234},{"title":"G","value":567},{"title":"H","value":987},{"title":"I","value":357},{"title":"J","value":865},{"title":"K","value":245},{"title":"L","value":999},{"title":"M","value":111},{"title":"N","value":222},{"title":"O","value":333},{"title":"P","value":444},{"title":"Q","value":555},{"title":"R","value":666},{"title":"S","value":777},{"title":"T","value":888},{"title":"U","value":999},{"title":"V","value":232},{"title":"X","value":757},{"title":"Y","value":234},{"title":"Z","value":876}]
LineChartdata-service.ts
---------------------------------
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
@Injectable()
export class LineChartDataService {
constructor(private http: Http) { }
getRemotedata(){
return this.http.get('http://-------:8088/restfullDataApi/jsondata')
.map((response:Response) => response.json());
}
LineChartComponent.ts
-------------------------------
import { Component, OnInit} from '@angular/core';
import { LineChartDataService } from './------';
import * as d3 from "d3";
@Component({
selector: 'app-line-chart',
templateUrl: './line-chart.component.html',
styleUrls: ['./line-chart.component.css']
})
export class LineChartComponent implements OnInit {
private data: any;
private jsondata: any;
private margin = {top: 20, right: 20, bottom: 30, left: 50};
private y: any;
private x: any;
private svg:any;
private width: any;
private height: any;
private margin:any;
private g:any;
private line: d3.Line<[number, number]>;
constructor(private LinechartService: LineChartDataService) {
this.width = 900 - this.margin.left - this.margin.right ;
this.height = 500 - this.margin.top - this.margin.bottom;
}
ngOnInit() {
this.LinechartService.getRemotedata()
.subscribe(
success => this.drawLineChart(success),
error => this.jsonData = error
);}
private drawLineChart(jsonData) {
console.log("Json Remote Data :: ");
console.log(JSON.stringify(jsonData));
//this.Data = jsonData;
this.svg = d3.select("svg"),
this.margin = {top: 20, right: 20, bottom: 30, left: 50},
this.g = this.svg.append("g").attr("transform", "translate(" + this.margin.left + "," + this.margin.top + ")");
this.x = d3.scaleLinear()
.rangeRound([0, this.width]);
this.y = d3.scaleLinear()
.rangeRound([this.height, 0]);
let line = d3.line()
.x(function(d) { return x(d.title); })
.y(function(d) { return y(d.value); });
this.x.domain(d3.extent(this.jsondata, function (d) { return d.title; }));
this.y.domain(d3.extent(this.jsondata, function (d) { return d.value; }));
this.g.append("g")
.attr("transform", "translate(0," + this.height + ")")
.call(d3.axisBottom(this.x))
.select(".domain")
.remove();
this.g.append("g")
.call(d3.axisLeft(this.y))
.append("text")
.attr("fill", "#000")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("value");
this.g.append("path")
.datum(this.jsondata)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 1.5)
.attr("d", this.line);
}}
Url中的数据:-
--------------
[{“标题”:“A”,“值”:123},{“标题”:“B”,“值”:445},{“标题”:“C”,“值”:666},{“标题”:“D”,“值”:123},{“标题”:“E”,“值”:876},{“标题”:“F”,“值”:234},{“标题”:“G”,“值”:567},{“标题”:“H”,“值”:987},{“标题”:“I”,“值”:357},{“标题”:“J”,“值”:865,{“标题”:“K”,“值”:245“N”,“value”:222},{“title”:“O”,“value”:333},{“title”:“P”,“value”:444},{“title”:“Q”,“value”:555},{“title”:“R”,“value”:666},{“title”:“S”,“value”:777},{“title”:“T”,“value”:888},{“title”:“U”,“value”:999},{“title”:“V”,“value”:232},{“title”:“X”,“value”:757},{“title”:“Y”,“value”:234},{“title”:“title”:“Y”,“value”:876}]
LineChartdata-service.ts
---------------------------------
从“@angular/core”导入{Injectable};
从'@angular/Http'导入{Http,Response};
@可注射()
导出类LineChartDataService{
构造函数(私有http:http){}
getRemotedata(){
返回此.http.get('http://-------:8088/restfullDataApi/jsondata')
.map((response:response)=>response.json());
}
LineChartComponent.ts
-------------------------------
从“@angular/core”导入{Component,OnInit};
从“/----”导入{LineChartDataService};
从“d3”导入*作为d3;
@组成部分({
选择器:“应用程序折线图”,
templateUrl:'./linechart.component.html',
样式URL:['./折线图.component.css']
})
导出类LineChartComponent实现OnInit{
私人资料:任何;
私人jsondata:任何;
私人保证金={顶部:20,右侧:20,底部:30,左侧:50};
私人y:任何;
私人x:任何;
私人svg:任何;
私人宽度:任意;
私人身高:任意;
私人保证金:任何;
私人g:任何;
专线:d3线;
构造函数(专用LinechartService:LineChartDataService){
this.width=900-this.margin.left-this.margin.right;
this.height=500-this.margin.top-this.margin.bottom;
}
ngOnInit(){
此文件名为.LinechartService.getRemotedata()
.订阅(
success=>this.drawLineChart(success),
error=>this.jsonData=error
);}
专用绘制线图(jsonData){
log(“Json远程数据::”);
log(JSON.stringify(jsonData));
//this.Data=jsonData;
this.svg=d3.select(“svg”),
this.margin={top:20,right:20,bottom:30,left:50},
this.g=this.svg.append(“g”).attr(“transform”、“translate”(+this.margin.left+)、“+this.margin.top+”);
this.x=d3.scaleLinear()
.rangeRound([0,此.width]);
this.y=d3.scaleLinear()
.rangeRound([this.height,0]);
设line=d3.line()
.x(函数(d){返回x(d.title);})
.y(函数(d){返回y(d.value);});
this.x.domain(d3.extent(this.jsondata,函数(d){返回d.title;}));
this.y.domain(d3.extent(this.jsondata,函数(d){返回d.value;}));
本附录g(“g”)
.attr(“变换”、“平移(0,+this.height+”))
.call(d3.axisBottom(this.x))
.select(“.domain”)
.remove();
本附录g(“g”)
.call(d3.axisLeft(this.y))
.append(“文本”)
.attr(“填充”、“千”)
.attr(“变换”、“旋转(-90)”)
.attr(“y”,6)
.attr(“dy”,“0.71em”)
.attr(“文本锚定”、“结束”)
.文本(“价值”);
这个.g.append(“路径”)
.datum(此.jsondata)
.attr(“填充”、“无”)
.attr(“笔划”、“钢蓝”)
.attr(“笔划线条连接”、“圆形”)
.attr(“笔划线头”、“圆形”)
.attr(“笔划宽度”,1.5)
.attr(“d”,这一行);
}}
我找到了一个答案,而且效果很好。 如果有什么问题,我需要一些建议
line-chart.component.ts
------------------------
import { Component, ElementRef, AfterViewInit } from '@angular/core';
import { LineChartData } from '../../providers/linechart-data';
import * as d3 from "d3";
@Component({
selector: 'app-line-chart',
templateUrl: './line-chart.component.html',
styleUrls: ['./line-chart.component.css']
})
export class LineChartComponent implements AfterViewInit {
title: string = 'd3.js with Angular 2!';
subtitle: string = 'Line Chart';
host;
path;
private xAxis :any;;
private yAxis :any;
data: any;
private xScale :any;
private yScale :any;
private margin = {top: 20, right: 20, bottom: 30, left: 50};
private width: number;
private height: number;
private svg: any;
private line: any;
constructor(private LinechartService: PieChartData,private element: ElementRef) {
this.width = 900 - this.margin.left - this.margin.right ;
this.height = 500 - this.margin.top - this.margin.bottom;
this.xScale = d3.scaleBand().range([0,this.width]);
this.yScale = d3.scaleLinear().range([this.height, 0]).domain([123,999]);
}
ngAfterViewInit(): void {
this.getGlobalData();
}
getGlobalData() {
console.log("getglobaldata method");
this.LinechartService.getGlobalData()
.subscribe(
success => this.buildSVG(success),
error => this.data = error
);
this.host = d3.select(this.element.nativeElement);
}
private buildSVG(data) {
console.log('In setMap(mapData), mapData getting assigned');
this.data = data;
console.log('mapData assigned as '+JSON.stringify(this.data));
console.log('In buildSVG()');
this.xAxis = d3.axisBottom(this.xScale)
this.yAxis = d3.axisLeft(this.yScale)
this.host.html('');
let self = this;
// let area = d3.area()
let line = d3.line()
.curve(d3.curveBasis)
.x(function(d: any) { return self.xScale(d.title); })
.y(function(d: any) { return self.yScale(d.value); });
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 + ')');
self.xScale.domain(d3.extent(self.data, function(d: any) { console.log("title:::"+d.title);return 0; }));
self.yScale.domain([0,d3.max(self.data, function(d: any) {console.log("value::::" +d.value); return 0; })]);
self.xScale.domain(self.data.map(function(d: any) { return d.title }));
self.yScale.domain([0,d3.max(self.data, function(d: any) { return d.value; })]);
// this.area.yAxis0(this.yAxis(0));
self.svg.append('g')
.attr("class", "axis axis-x")
.attr('transform', 'translate(0,' + self.height + ')')
.call(self.xAxis)
self.svg.append('g')
.attr("class", "axis axis-y")
.call(self.yAxis)
.append('text')
.attr("class", "axis-title")
.attr('transform', 'rotate(-90)')
.attr('y', 6)
.attr('dy', '.71em')
.style('text-anchor', 'end')
.style("fill","black")
.text('Value()');
//console.log(data)
self.svg.append('path')
.datum(data)
.attr("fill","none")
.attr("stroke", "steelblue")
.attr('class', 'line')
.attr('d', line);
// .attr('d', area);
}
}
line-chart.component.css
--------------------------
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
/*shape-rendering: crispEdges;*/
}
.axis-title {
fill: none;
stroke: black;
stroke-width: 0.5px;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
linechart-data.ts
--------------------
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
import { Observable } from "rxjs/Observable";
@Injectable()
export class LineChartData {
// data : string;
constructor(private http: Http) {
getGlobalData(): Observable<any> {
return this.http.get('http://.........:8088/restfullDataApi/UserService/jsondata')
.map(this.extractData)
// .catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body;
}
}
line-chart.component.ts
------------------------
从'@angular/core'导入{Component,ElementRef,AfterViewInit};
从“../../providers/linechart data”导入{LineChartData};
从“d3”导入*作为d3;
@组成部分({
选择器:“应用程序折线图”,
templateUrl:'./linechart.component.html',
样式URL:['./折线图.component.css']
})
导出类LineChartComponent实现AfterViewInit{
标题:string='d3.js,带角度2!';
副标题:字符串='折线图';
主办
路径
私人xAxis:任何;;
私人雅克斯:任何;
资料:有;
私人xScale:任何;
私人规模:任何;
私人保证金={顶部:20,右侧:20,底部:30,左侧:50};
私有宽度:数字;
私人身高:数字;
私人svg:任何;
专线:任何;
构造函数(专用LinechartService:PieChartData,专用元素:ElementRef){