繁体   English   中英

D3图形上的显示区域

[英]Display area on D3 graph

我在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:类型'[number,number]'不存在属性'width'。 src / app / histogram / histogram.component.ts(49,20):错误TS2339:类型“ [number,number]”上不存在属性“ height”。 src / app / histogram / histogram.component.ts(55,20):错误TS2339:类型'[number,number]'不存在属性'width'。 src / app / histogram / histogram.component.ts(58,20):错误TS2339:类型“ [number,number]”上不存在属性“ height”。 src / app / histogram / histogram.component.ts(89,18):错误TS2345:类型'Area <[number,number]>'的参数无法分配给'ValueFn'类型的参数。 参数“数据”和“基准”的类型不兼容。 输入'{'width':number; 'height':数字; }”不可分配给类型“ [数字,数字] []”。 类型'{'width':number;中缺少属性'length'。 'height':数字; }'。 src / app / histogram / histogram.component.ts(95,18):错误TS2345:类型'Line <[number,number]>'的参数不能分配给'ValueFn'类型的参数。 参数“数据”和“基准”的类型不兼容。 输入'{'width':number; 'height':数字; }”不可分配给类型“ [数字,数字] []”。

如果您复制示例,请复制正确

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;}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM