javascript - D3 简单折线图 : Error: <path> attribute d: Expected moveto path command ('M' or 'm' ), "[object Object]"

标签 javascript d3.js

我正在尝试使用包含数字元素的一维数组制作基本折线图。

chart.html

<svg id="svg"></svg>

graph.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);});

我不断收到此错误:Error: <path> attribute d: Expected moveto path command ('M' or 'm'), "[object Object]".

------------UPDATE(未简化的 Angular 组件)------------

bar-curve-chart.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);});
  }

}

bar-curve-chart.component.html

<svg #svg ></svg>

最佳答案

您对线生成器的定义有误:

let line = D3.line()
  .x((d,i) => {return this.x(i);})
  .y((d) => {return this.y(d);}).curve();

如果您使用 .curve()没有参数,它充当 setter/getter :

If curve is not specified, returns the current curve factory, which defaults to curveLinear.

因此,line 将保存对曲线工厂而不是线生成器的引用。如果你想要 curve factory不同于默认 d3.curveLinear ,您需要将其作为参数传递,或者您需要省略对 .curve() 的调用以获取线生成器。


顺便说一句:上面的语句可以简化/美化为:

let line = D3.line()
  .x((d, i) => this.x(i))
  .y(this.y);

关于javascript - D3 简单折线图 : Error: <path> attribute d: Expected moveto path command ('M' or 'm' ), "[object Object]",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46453473/

相关文章:

javascript - 获取Facebook好友的真实ID

javascript - 如何在 d3js 中制作分组堆叠条形图?

javascript - 将数据发布到弹出窗口

javascript - d3线不画

javascript - D3.js - 围绕点旋转 svg 文本

javascript - 使用新数据更新 d3 图表

javascript - 从 D3 转换后 Canvas 的定位

javascript - JS获取逗号分隔的单词长度+最后一个单词

javascript - Ember CLI 阵列 Controller 未对每个循环进行排序

javascript - 在 CRM 4.0 的实体 View 中添加外部链接