简体   繁体   English

D3图形上的显示区域

[英]Display area on D3 graph

I am having problem displaying area on D3 graph. 我在D3图形上显示区域时遇到问题。 I'm guessing problem is in data format(?) but I'm not sure. 我猜问题出在数据格式(?)中,但我不确定。 I'm total d3 library beginner. 我是d3库的初学者。 I am using d3 v5 version. 我正在使用d3 v5版本。 I tried to find some simple examples but most of code I found is really complex and for some old versions. 我试图找到一些简单的示例,但是我发现的大多数代码对于某些旧版本来说确实很复杂。

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

Area is not showing at all and I'm getting following error messages: 该区域完全没有显示,并且我收到以下错误消息:

ERROR in src/app/histogram/histogram.component.ts(45,20): error TS2339: Property 'width' does not exist on type '[number, number]'. src / app / histogram / histogram.component.ts(45,20)中的错误:错误TS2339:类型'[number,number]'不存在属性'width'。 src/app/histogram/histogram.component.ts(49,20): error TS2339: Property 'height' does not exist on type '[number, number]'. src / app / histogram / histogram.component.ts(49,20):错误TS2339:类型“ [number,number]”上不存在属性“ height”。 src/app/histogram/histogram.component.ts(55,20): error TS2339: Property 'width' does not exist on type '[number, number]'. src / app / histogram / histogram.component.ts(55,20):错误TS2339:类型'[number,number]'不存在属性'width'。 src/app/histogram/histogram.component.ts(58,20): error TS2339: Property 'height' does not exist on type '[number, number]'. src / app / histogram / histogram.component.ts(58,20):错误TS2339:类型“ [number,number]”上不存在属性“ height”。 src/app/histogram/histogram.component.ts(89,18): error TS2345: Argument of type 'Area<[number, number]>' is not assignable to parameter of type 'ValueFn'. src / app / histogram / histogram.component.ts(89,18):错误TS2345:类型'Area <[number,number]>'的参数无法分配给'ValueFn'类型的参数。 Types of parameters 'data' and 'datum' are incompatible. 参数“数据”和“基准”的类型不兼容。 Type '{ 'width': number; 输入'{'width':number; 'height': number; 'height':数字; }' is not assignable to type '[number, number][]'. }”不可分配给类型“ [数字,数字] []”。 Property 'length' is missing in type '{ 'width': number; 类型'{'width':number;中缺少属性'length'。 'height': number; 'height':数字; }'. }'。 src/app/histogram/histogram.component.ts(95,18): error TS2345: Argument of type 'Line<[number, number]>' is not assignable to parameter of type 'ValueFn'. src / app / histogram / histogram.component.ts(95,18):错误TS2345:类型'Line <[number,number]>'的参数不能分配给'ValueFn'类型的参数。 Types of parameters 'data' and 'datum' are incompatible. 参数“数据”和“基准”的类型不兼容。 Type '{ 'width': number; 输入'{'width':number; 'height': number; 'height':数字; }' is not assignable to type '[number, number][]'. }”不可分配给类型“ [数字,数字] []”。

If you copy an example copy it correct 如果您复制示例,请复制正确

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

Edit 编辑

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

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

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