【问题标题】:Display area on D3 graphD3图形上的显示区域
【发布时间】:2019-01-17 09:05:58
【问题描述】:

我在 D3 图表上显示区域时遇到问题。我猜问题出在数据格式上(?),但我不确定。我是 d3 图书馆的初学者。我正在使用 d3 v5 版本。我试图找到一些简单的例子,但我发现的大部分代码都非常复杂,而且对于一些旧版本。

some_data = [
  {
    'width': 10,
    'height': 0.2,
  },
  {
    'width': 11,
    'height': 0.3,
  },
  {
    'width': 12,
    'height': 0.4,
  },
  {
    'width': 13,
    'height': 0.5,
  }
];

var margin = { top: 20, right: 20, bottom: 30, left: 50 },
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var x = d3.scaleLinear().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);

// define the area
var area = d3.area()
  .x(function (d) {
      console.log('x');
      return x(d.width);
  })
  .y0(height)
  .y1(function (d) {
      return y(d.height);
  });

// define the line
var valueline = d3.line()
  .x(function (d) {
      return x(d.width);
  })
  .y(function (d) {
      return y(d.height);
  });


var svg = d3.select('body').append('svg')
  .attr('width', width + margin.left + margin.right)
  .attr('height', height + margin.top + margin.bottom)
  .append('g')
  .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');


// format the data
this.some_data.forEach(function (d) {
    d.width = d.width;
    d.height = +d.height;
});

// scale the range of the data
x.domain(d3.extent(this.some_data, function (d) {
    return d.width;
}));
y.domain([0, d3.max(this.some_data, function (d) {
    return d.height;
})]);


// add the area
svg.append('path')
  .data(this.some_data)
  .attr('class', 'area')
  .attr('d', area);

// add the valueline path.
svg.append('path')
  .data(this.some_data)
  .attr('class', 'line')
  .attr('d', valueline);

// add the X Axis
svg.append('g')
  .attr('transform', 'translate(0,' + height + ')')
  .call(d3.axisBottom(x));

// add the Y Axis
svg.append('g')
  .call(d3.axisLeft(y));

区域根本没有显示,我收到以下错误消息:

src/app/histogram/histogram.component.ts(45,20) 中的错误:错误 TS2339:类型“[数字,数字]”上不存在属性“宽度”。 src/app/histogram/histogram.component.ts(49,20): 错误 TS2339: 类型“[数字,数字]”上不存在属性“高度”。 src/app/histogram/histogram.component.ts(55,20): 错误 TS2339: 类型“[数字,数字]”上不存在属性“宽度”。 src/app/histogram/histogram.component.ts(58,20): 错误 TS2339: 类型“[数字,数字]”上不存在属性“高度”。 src/app/histogram/histogram.component.ts(89,18): 错误 TS2345: 'Area' 类型的参数不可分配给 “ValueFn”类型的参数。种类 参数 'data' 和 'datum' 不兼容。 输入'{'宽度':数字; “高度”:数字; }' 不可分配给类型 '[number, number][]'。 类型 '{ 'width': number; 中缺少属性 'length' “高度”:数字; }'。 src/app/histogram/histogram.component.ts(95,18): 错误 TS2345:'Line' 类型的参数不是 可分配给“ValueFn”类型的参数。种类 参数 'data' 和 'datum' 不兼容。 输入'{'宽度':数字; “高度”:数字; }' 不可分配给类型 '[number, number][]'。

【问题讨论】:

标签: javascript d3.js


【解决方案1】:

如果你复制一个例子,复制它是正确的

svg.append('path')
  .data([this.some_data])
  .attr('class', 'area')
  .attr('d', area);

svg.append('path')
  .data([this.some_data])
  .attr('class', 'line')
  .attr('d', valueline);

编辑

.line {fill:none; stroke:red;}

【讨论】:

  • 谢谢,这很有帮助,但我还是有一些麻烦。 jsfiddle.net/o1up890k/1
  • 将线条的填充样式设置为none
猜你喜欢
  • 2015-09-03
  • 2013-08-31
  • 1970-01-01
  • 2014-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多