我正在尝试使用包含数字元素的一维数组制作基本折线图。
图表.html
<svg id="svg"></svg>
Run Code Online (Sandbox Code Playgroud)
图.js
lineData = [10,15,20,25,30,35,40,35,30,25,20,15,10];
let svg = D3.select("#svg").attr('width', width).attr('height', height);
let chart = svg.append('g').classed('display', true).attr('transform', 'translate(' + this.padding.l + ', ' + this.padding.t + ')');
let x = D3.scaleLinear().domain([0, lineData.length]).range([0,width]);
let y = D3.scaleLinear().domain([0, D3.max(lineData)]).range([height, 0]);
let line = D3.line().x((d,i) => {return x(i);}).y((d) => {return y(d);}).curve();
chart.selectAll('.line').data([lineData]).enter().append('path').classed('line', true).attr('stroke', 'red').attr('d', (d) => {return line(d);});
Run Code Online (Sandbox Code Playgroud)
我不断收到此错误: Error: <path> attribute d: Expected moveto path command ('M' or 'm'), "[object Object]".
----------更新(未简化的角度组件)---------
条形曲线图.component.ts
import {Component, Input, OnInit, ViewChild} from '@angular/core';
import * as D3 from "d3";
import * as D3Tooltip from "d3-tip"
@Component({
selector: 'app-bar-curve-chart',
templateUrl: './bar-curve-chart.component.html',
styleUrls: ['./bar-curve-chart.component.css']
})
export class BarCurveChartComponent implements OnInit {
@ViewChild('svg') svg:any;
private chart:any;
private padding = {t:50, r:75, b:25, l:20};
private x:any;
private y:any;
// Change Any to Data Type
@Input('data') data:number[] = [5,10,15,20,25,30,35,30,25,20,15,10,5];
@Input('lineData') lineData:any = [10,15,20,25,30,35,40,35,30,25,20,15,10];
@Input('height') height:number = 700;
@Input('width') width:number = 700;
constructor() { }
ngOnInit() {
this.prep();
this._plot();
}
private prep():void {
let svg = D3.select(this.svg.nativeElement)
.attr('width', this.width)
.attr('height', this.height);
this.chart = svg.append('g')
.classed('display', true)
.attr('transform', 'translate(' + this.padding.l + ', ' + this.padding.t + ')');
this.x = D3.scaleLinear().domain([0, this.data.length]).range([0, this.width]);
this.y = D3.scaleLinear().domain([0, D3.max(this.data)]).range([this.height, 0]);
}
private _plot():void {
let line = D3.line()
.x((d,i) => {return this.x(i);})
.y((d) => {return this.y(d);}).curve();
console.log(line(this.lineData));
this.chart.selectAll('.line')
.data([this.lineData])
.enter()
.append('path')
.classed('line', true)
.attr('stroke', 'red')
.attr('d', (d) => {return line(d);});
}
}
Run Code Online (Sandbox Code Playgroud)
条形曲线图.component.html
<svg #svg ></svg>
Run Code Online (Sandbox Code Playgroud)
您对线生成器的定义有误:
let line = D3.line()
.x((d,i) => {return this.x(i);})
.y((d) => {return this.y(d);}).curve();
Run Code Online (Sandbox Code Playgroud)
如果.curve()
不带参数使用,则它充当 getter:
如果未指定曲线,则返回当前曲线工厂,默认为curveLinear。
因此,line
将保存对曲线工厂的引用,而不是直线生成器。如果你想要一个不同于默认d3.curveLinear的曲线工厂,你需要将它作为参数传递,或者你需要省略调用来获取线生成器。.curve()
顺便说一句:上述语句可以简化/美化为:
let line = D3.line()
.x((d, i) => this.x(i))
.y(this.y);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7819 次 |
最近记录: |