TS2345 and TS2339 errors are thrown for simple code in Angular
up vote
1
down vote
favorite
I am trying to learn to use D3 with angular. As a beginner I am trying to implement a simple project written by William Liu a few years ago (http://bl.ocks.org/WilliamQLiu/76ae20060e19bf42d774)
I could replicate most of the code but get compilation errors as below :
error TS2345: Argument of type 'BaseType' is not assignable to parameter of type 'ContainerElement'.
Type 'Element' is not assignable to type 'ContainerElement'.
Type 'Element' is not assignable to type 'SVGGElement'.
Property 'farthestViewportElement' is missing in type 'Element'.
error TS2339: Property 'dataset' does not exist on type 'BaseType'.
Property 'dataset' does not exist on type 'Element'.
error TS2339: Property 'dataset' does not exist on type 'BaseType'.
Property 'dataset' does not exist on type 'Element'.
I have created a component called dragex. The code is as below
import { Component, OnInit } from '@angular/core';
import * as d3 from 'd3';
@Component({
selector: 'dragex',
templateUrl: './dragex.component.html',
styleUrls: ['./dragex.component.css']
})
export class DragexComponent implements OnInit {
private dataset = [
{ x: 100, y: 110 },
{ x: 83, y: 43 },
{ x: 92, y: 28 },
{ x: 49, y: 74 },
{ x: 51, y: 10 },
{ x: 25, y: 98 },
{ x: 77, y: 30 },
{ x: 20, y: 83 },
{ x: 11, y: 63 },
{ x: 4, y: 55 },
{ x: 0, y: 0 },
{ x: 85, y: 100 },
{ x: 60, y: 40 },
{ x: 70, y: 80 },
{ x: 10, y: 20 },
{ x: 40, y: 50 },
{ x: 25, y: 31 }
];
constructor() { }
ngOnInit() {
this.drawchart();
}
private drawchart() {
const margin = {
top: 40,
right: 20,
bottom: 20,
left: 40
},
width = 960, // window.innerWidth,
height = 500, // window.innerHeight,
radius = 6;
let svg = d3.select('body').append('svg')
.attr('width', width )
.attr('height', height);
let xScale = d3.scaleLinear()
.domain([0, d3.max(this.dataset, function(d) { return d.x + 10; })])
.range([margin.left, width - margin.right]);
let yScale = d3.scaleLinear()
.domain([0, d3.max(this.dataset, function(d) { return d.y + 10; })])
.range([margin.top, width - margin.bottom]);
let xAxis = d3.axisTop(xScale);
let yAxis = d3.axisLeft(yScale);
let circleAttrs = {
cx: function(d) { return xScale(d.x); },
cy: function(d) { return yScale(d.y); },
r: radius
} ;
svg.append('g')
.attr('class', 'axis')
.attr( 'transform', 'translate(' + [margin.left, 0] + ')')
.call(yAxis);
svg.selectAll('circle')
.data(this.dataset)
.enter().append('circle')
.attr('cx', circleAttrs.cx)
.attr('cy', circleAttrs.cy)
.attr('r', circleAttrs.r)
.on('mouseover', handleMouseOver)
.on('mouseout', handleMouseOut);
svg.on('click', function() {
let coords = d3.mouse(this);
let newData = {
x: Math.round(xScale.invert(coords[0])),
y: Math.round(yScale.invert(coords[1]))
};
this.dataset.push(newData);
svg.selectAll('circle')
.data(this.dataset)
.enter().append('circle')
.attr('cx', circleAttrs.cx)
.attr('cy', circleAttrs.cy)
.attr('r', circleAttrs.r)
.on('mouseover', handleMouseOver)
.on('mouseout', handleMouseOut);
});
function handleMouseOver(d, i) {
d3.select(this)
.attr('fill', 'green')
.attr('r', radius * 2 );
svg.append('text')
.attr('id', 't' + d.x + '-' + d.y + '-' + i)
.attr('x', function() { return xScale(d.x) - 30 ; })
.attr('y', function() { return yScale(d.y) - 15 ; })
.text(d.x + ',' + d.y );
}
function handleMouseOut(d, i) {
d3.select(this)
.attr('fill', 'black')
.attr( 'r', radius);
d3.select('id').remove();
}
}
}
The errors are thrown on
let coords = d3.mouse(this);
this.dataset.push(newData);
.data(this.dataset)
The plunker for the same is available at
https://plnkr.co/edit/pM5ZsVw7EOm4bMI94JKj?p=info
On hovering over this in the above lines it shows d3.BaseType whereas ideally it should show this: this
I have had a look at other articles but none of them is pointing at the same solution.
Help!
===========================================================================
I shifted out the functions in that code into a new function (as below) and the errors go away.
Just have a DOM error now that shows this.dataset.push is not a function.
function onClickIn(d, i) {
let coords = d3.mouse(this);
console.log('here in onclick function');
let newData = {
x: Math.round(xScale.invert(coords[0])),
y: Math.round(yScale.invert(coords[1]))
};
console.log('new point- x:'+ newData.x + ',' + newData.y);
this.dataset.push(newData);
svg.select('circle')
.data(this.dataset)
.enter().append('circle')
.attr('cx', circleAttrs.cx)
.attr('cy', circleAttrs.cy)
.attr('r', circleAttrs.r)
.on('mouseover', handleMouseOver)
.on('mouseout', handleMouseOut);
}
angular d3.js
add a comment |
up vote
1
down vote
favorite
I am trying to learn to use D3 with angular. As a beginner I am trying to implement a simple project written by William Liu a few years ago (http://bl.ocks.org/WilliamQLiu/76ae20060e19bf42d774)
I could replicate most of the code but get compilation errors as below :
error TS2345: Argument of type 'BaseType' is not assignable to parameter of type 'ContainerElement'.
Type 'Element' is not assignable to type 'ContainerElement'.
Type 'Element' is not assignable to type 'SVGGElement'.
Property 'farthestViewportElement' is missing in type 'Element'.
error TS2339: Property 'dataset' does not exist on type 'BaseType'.
Property 'dataset' does not exist on type 'Element'.
error TS2339: Property 'dataset' does not exist on type 'BaseType'.
Property 'dataset' does not exist on type 'Element'.
I have created a component called dragex. The code is as below
import { Component, OnInit } from '@angular/core';
import * as d3 from 'd3';
@Component({
selector: 'dragex',
templateUrl: './dragex.component.html',
styleUrls: ['./dragex.component.css']
})
export class DragexComponent implements OnInit {
private dataset = [
{ x: 100, y: 110 },
{ x: 83, y: 43 },
{ x: 92, y: 28 },
{ x: 49, y: 74 },
{ x: 51, y: 10 },
{ x: 25, y: 98 },
{ x: 77, y: 30 },
{ x: 20, y: 83 },
{ x: 11, y: 63 },
{ x: 4, y: 55 },
{ x: 0, y: 0 },
{ x: 85, y: 100 },
{ x: 60, y: 40 },
{ x: 70, y: 80 },
{ x: 10, y: 20 },
{ x: 40, y: 50 },
{ x: 25, y: 31 }
];
constructor() { }
ngOnInit() {
this.drawchart();
}
private drawchart() {
const margin = {
top: 40,
right: 20,
bottom: 20,
left: 40
},
width = 960, // window.innerWidth,
height = 500, // window.innerHeight,
radius = 6;
let svg = d3.select('body').append('svg')
.attr('width', width )
.attr('height', height);
let xScale = d3.scaleLinear()
.domain([0, d3.max(this.dataset, function(d) { return d.x + 10; })])
.range([margin.left, width - margin.right]);
let yScale = d3.scaleLinear()
.domain([0, d3.max(this.dataset, function(d) { return d.y + 10; })])
.range([margin.top, width - margin.bottom]);
let xAxis = d3.axisTop(xScale);
let yAxis = d3.axisLeft(yScale);
let circleAttrs = {
cx: function(d) { return xScale(d.x); },
cy: function(d) { return yScale(d.y); },
r: radius
} ;
svg.append('g')
.attr('class', 'axis')
.attr( 'transform', 'translate(' + [margin.left, 0] + ')')
.call(yAxis);
svg.selectAll('circle')
.data(this.dataset)
.enter().append('circle')
.attr('cx', circleAttrs.cx)
.attr('cy', circleAttrs.cy)
.attr('r', circleAttrs.r)
.on('mouseover', handleMouseOver)
.on('mouseout', handleMouseOut);
svg.on('click', function() {
let coords = d3.mouse(this);
let newData = {
x: Math.round(xScale.invert(coords[0])),
y: Math.round(yScale.invert(coords[1]))
};
this.dataset.push(newData);
svg.selectAll('circle')
.data(this.dataset)
.enter().append('circle')
.attr('cx', circleAttrs.cx)
.attr('cy', circleAttrs.cy)
.attr('r', circleAttrs.r)
.on('mouseover', handleMouseOver)
.on('mouseout', handleMouseOut);
});
function handleMouseOver(d, i) {
d3.select(this)
.attr('fill', 'green')
.attr('r', radius * 2 );
svg.append('text')
.attr('id', 't' + d.x + '-' + d.y + '-' + i)
.attr('x', function() { return xScale(d.x) - 30 ; })
.attr('y', function() { return yScale(d.y) - 15 ; })
.text(d.x + ',' + d.y );
}
function handleMouseOut(d, i) {
d3.select(this)
.attr('fill', 'black')
.attr( 'r', radius);
d3.select('id').remove();
}
}
}
The errors are thrown on
let coords = d3.mouse(this);
this.dataset.push(newData);
.data(this.dataset)
The plunker for the same is available at
https://plnkr.co/edit/pM5ZsVw7EOm4bMI94JKj?p=info
On hovering over this in the above lines it shows d3.BaseType whereas ideally it should show this: this
I have had a look at other articles but none of them is pointing at the same solution.
Help!
===========================================================================
I shifted out the functions in that code into a new function (as below) and the errors go away.
Just have a DOM error now that shows this.dataset.push is not a function.
function onClickIn(d, i) {
let coords = d3.mouse(this);
console.log('here in onclick function');
let newData = {
x: Math.round(xScale.invert(coords[0])),
y: Math.round(yScale.invert(coords[1]))
};
console.log('new point- x:'+ newData.x + ',' + newData.y);
this.dataset.push(newData);
svg.select('circle')
.data(this.dataset)
.enter().append('circle')
.attr('cx', circleAttrs.cx)
.attr('cy', circleAttrs.cy)
.attr('r', circleAttrs.r)
.on('mouseover', handleMouseOver)
.on('mouseout', handleMouseOut);
}
angular d3.js
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to learn to use D3 with angular. As a beginner I am trying to implement a simple project written by William Liu a few years ago (http://bl.ocks.org/WilliamQLiu/76ae20060e19bf42d774)
I could replicate most of the code but get compilation errors as below :
error TS2345: Argument of type 'BaseType' is not assignable to parameter of type 'ContainerElement'.
Type 'Element' is not assignable to type 'ContainerElement'.
Type 'Element' is not assignable to type 'SVGGElement'.
Property 'farthestViewportElement' is missing in type 'Element'.
error TS2339: Property 'dataset' does not exist on type 'BaseType'.
Property 'dataset' does not exist on type 'Element'.
error TS2339: Property 'dataset' does not exist on type 'BaseType'.
Property 'dataset' does not exist on type 'Element'.
I have created a component called dragex. The code is as below
import { Component, OnInit } from '@angular/core';
import * as d3 from 'd3';
@Component({
selector: 'dragex',
templateUrl: './dragex.component.html',
styleUrls: ['./dragex.component.css']
})
export class DragexComponent implements OnInit {
private dataset = [
{ x: 100, y: 110 },
{ x: 83, y: 43 },
{ x: 92, y: 28 },
{ x: 49, y: 74 },
{ x: 51, y: 10 },
{ x: 25, y: 98 },
{ x: 77, y: 30 },
{ x: 20, y: 83 },
{ x: 11, y: 63 },
{ x: 4, y: 55 },
{ x: 0, y: 0 },
{ x: 85, y: 100 },
{ x: 60, y: 40 },
{ x: 70, y: 80 },
{ x: 10, y: 20 },
{ x: 40, y: 50 },
{ x: 25, y: 31 }
];
constructor() { }
ngOnInit() {
this.drawchart();
}
private drawchart() {
const margin = {
top: 40,
right: 20,
bottom: 20,
left: 40
},
width = 960, // window.innerWidth,
height = 500, // window.innerHeight,
radius = 6;
let svg = d3.select('body').append('svg')
.attr('width', width )
.attr('height', height);
let xScale = d3.scaleLinear()
.domain([0, d3.max(this.dataset, function(d) { return d.x + 10; })])
.range([margin.left, width - margin.right]);
let yScale = d3.scaleLinear()
.domain([0, d3.max(this.dataset, function(d) { return d.y + 10; })])
.range([margin.top, width - margin.bottom]);
let xAxis = d3.axisTop(xScale);
let yAxis = d3.axisLeft(yScale);
let circleAttrs = {
cx: function(d) { return xScale(d.x); },
cy: function(d) { return yScale(d.y); },
r: radius
} ;
svg.append('g')
.attr('class', 'axis')
.attr( 'transform', 'translate(' + [margin.left, 0] + ')')
.call(yAxis);
svg.selectAll('circle')
.data(this.dataset)
.enter().append('circle')
.attr('cx', circleAttrs.cx)
.attr('cy', circleAttrs.cy)
.attr('r', circleAttrs.r)
.on('mouseover', handleMouseOver)
.on('mouseout', handleMouseOut);
svg.on('click', function() {
let coords = d3.mouse(this);
let newData = {
x: Math.round(xScale.invert(coords[0])),
y: Math.round(yScale.invert(coords[1]))
};
this.dataset.push(newData);
svg.selectAll('circle')
.data(this.dataset)
.enter().append('circle')
.attr('cx', circleAttrs.cx)
.attr('cy', circleAttrs.cy)
.attr('r', circleAttrs.r)
.on('mouseover', handleMouseOver)
.on('mouseout', handleMouseOut);
});
function handleMouseOver(d, i) {
d3.select(this)
.attr('fill', 'green')
.attr('r', radius * 2 );
svg.append('text')
.attr('id', 't' + d.x + '-' + d.y + '-' + i)
.attr('x', function() { return xScale(d.x) - 30 ; })
.attr('y', function() { return yScale(d.y) - 15 ; })
.text(d.x + ',' + d.y );
}
function handleMouseOut(d, i) {
d3.select(this)
.attr('fill', 'black')
.attr( 'r', radius);
d3.select('id').remove();
}
}
}
The errors are thrown on
let coords = d3.mouse(this);
this.dataset.push(newData);
.data(this.dataset)
The plunker for the same is available at
https://plnkr.co/edit/pM5ZsVw7EOm4bMI94JKj?p=info
On hovering over this in the above lines it shows d3.BaseType whereas ideally it should show this: this
I have had a look at other articles but none of them is pointing at the same solution.
Help!
===========================================================================
I shifted out the functions in that code into a new function (as below) and the errors go away.
Just have a DOM error now that shows this.dataset.push is not a function.
function onClickIn(d, i) {
let coords = d3.mouse(this);
console.log('here in onclick function');
let newData = {
x: Math.round(xScale.invert(coords[0])),
y: Math.round(yScale.invert(coords[1]))
};
console.log('new point- x:'+ newData.x + ',' + newData.y);
this.dataset.push(newData);
svg.select('circle')
.data(this.dataset)
.enter().append('circle')
.attr('cx', circleAttrs.cx)
.attr('cy', circleAttrs.cy)
.attr('r', circleAttrs.r)
.on('mouseover', handleMouseOver)
.on('mouseout', handleMouseOut);
}
angular d3.js
I am trying to learn to use D3 with angular. As a beginner I am trying to implement a simple project written by William Liu a few years ago (http://bl.ocks.org/WilliamQLiu/76ae20060e19bf42d774)
I could replicate most of the code but get compilation errors as below :
error TS2345: Argument of type 'BaseType' is not assignable to parameter of type 'ContainerElement'.
Type 'Element' is not assignable to type 'ContainerElement'.
Type 'Element' is not assignable to type 'SVGGElement'.
Property 'farthestViewportElement' is missing in type 'Element'.
error TS2339: Property 'dataset' does not exist on type 'BaseType'.
Property 'dataset' does not exist on type 'Element'.
error TS2339: Property 'dataset' does not exist on type 'BaseType'.
Property 'dataset' does not exist on type 'Element'.
I have created a component called dragex. The code is as below
import { Component, OnInit } from '@angular/core';
import * as d3 from 'd3';
@Component({
selector: 'dragex',
templateUrl: './dragex.component.html',
styleUrls: ['./dragex.component.css']
})
export class DragexComponent implements OnInit {
private dataset = [
{ x: 100, y: 110 },
{ x: 83, y: 43 },
{ x: 92, y: 28 },
{ x: 49, y: 74 },
{ x: 51, y: 10 },
{ x: 25, y: 98 },
{ x: 77, y: 30 },
{ x: 20, y: 83 },
{ x: 11, y: 63 },
{ x: 4, y: 55 },
{ x: 0, y: 0 },
{ x: 85, y: 100 },
{ x: 60, y: 40 },
{ x: 70, y: 80 },
{ x: 10, y: 20 },
{ x: 40, y: 50 },
{ x: 25, y: 31 }
];
constructor() { }
ngOnInit() {
this.drawchart();
}
private drawchart() {
const margin = {
top: 40,
right: 20,
bottom: 20,
left: 40
},
width = 960, // window.innerWidth,
height = 500, // window.innerHeight,
radius = 6;
let svg = d3.select('body').append('svg')
.attr('width', width )
.attr('height', height);
let xScale = d3.scaleLinear()
.domain([0, d3.max(this.dataset, function(d) { return d.x + 10; })])
.range([margin.left, width - margin.right]);
let yScale = d3.scaleLinear()
.domain([0, d3.max(this.dataset, function(d) { return d.y + 10; })])
.range([margin.top, width - margin.bottom]);
let xAxis = d3.axisTop(xScale);
let yAxis = d3.axisLeft(yScale);
let circleAttrs = {
cx: function(d) { return xScale(d.x); },
cy: function(d) { return yScale(d.y); },
r: radius
} ;
svg.append('g')
.attr('class', 'axis')
.attr( 'transform', 'translate(' + [margin.left, 0] + ')')
.call(yAxis);
svg.selectAll('circle')
.data(this.dataset)
.enter().append('circle')
.attr('cx', circleAttrs.cx)
.attr('cy', circleAttrs.cy)
.attr('r', circleAttrs.r)
.on('mouseover', handleMouseOver)
.on('mouseout', handleMouseOut);
svg.on('click', function() {
let coords = d3.mouse(this);
let newData = {
x: Math.round(xScale.invert(coords[0])),
y: Math.round(yScale.invert(coords[1]))
};
this.dataset.push(newData);
svg.selectAll('circle')
.data(this.dataset)
.enter().append('circle')
.attr('cx', circleAttrs.cx)
.attr('cy', circleAttrs.cy)
.attr('r', circleAttrs.r)
.on('mouseover', handleMouseOver)
.on('mouseout', handleMouseOut);
});
function handleMouseOver(d, i) {
d3.select(this)
.attr('fill', 'green')
.attr('r', radius * 2 );
svg.append('text')
.attr('id', 't' + d.x + '-' + d.y + '-' + i)
.attr('x', function() { return xScale(d.x) - 30 ; })
.attr('y', function() { return yScale(d.y) - 15 ; })
.text(d.x + ',' + d.y );
}
function handleMouseOut(d, i) {
d3.select(this)
.attr('fill', 'black')
.attr( 'r', radius);
d3.select('id').remove();
}
}
}
The errors are thrown on
let coords = d3.mouse(this);
this.dataset.push(newData);
.data(this.dataset)
The plunker for the same is available at
https://plnkr.co/edit/pM5ZsVw7EOm4bMI94JKj?p=info
On hovering over this in the above lines it shows d3.BaseType whereas ideally it should show this: this
I have had a look at other articles but none of them is pointing at the same solution.
Help!
===========================================================================
I shifted out the functions in that code into a new function (as below) and the errors go away.
Just have a DOM error now that shows this.dataset.push is not a function.
function onClickIn(d, i) {
let coords = d3.mouse(this);
console.log('here in onclick function');
let newData = {
x: Math.round(xScale.invert(coords[0])),
y: Math.round(yScale.invert(coords[1]))
};
console.log('new point- x:'+ newData.x + ',' + newData.y);
this.dataset.push(newData);
svg.select('circle')
.data(this.dataset)
.enter().append('circle')
.attr('cx', circleAttrs.cx)
.attr('cy', circleAttrs.cy)
.attr('r', circleAttrs.r)
.on('mouseover', handleMouseOver)
.on('mouseout', handleMouseOut);
}
angular d3.js
angular d3.js
edited Mar 22 at 16:15
asked Mar 22 at 15:04
Krishnan Iyer
63
63
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
Try replacing:
let coords = d3.mouse(this);
For
let aux:any = this
let coords = d3.mouse(aux);
This way you fool the Typescript Compiler.
add a comment |
up vote
0
down vote
Use type assertion
const coords = d3.mouse(this as SVGGElement);
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Try replacing:
let coords = d3.mouse(this);
For
let aux:any = this
let coords = d3.mouse(aux);
This way you fool the Typescript Compiler.
add a comment |
up vote
0
down vote
Try replacing:
let coords = d3.mouse(this);
For
let aux:any = this
let coords = d3.mouse(aux);
This way you fool the Typescript Compiler.
add a comment |
up vote
0
down vote
up vote
0
down vote
Try replacing:
let coords = d3.mouse(this);
For
let aux:any = this
let coords = d3.mouse(aux);
This way you fool the Typescript Compiler.
Try replacing:
let coords = d3.mouse(this);
For
let aux:any = this
let coords = d3.mouse(aux);
This way you fool the Typescript Compiler.
answered Oct 20 at 15:41
Esteve Tarragó
1
1
add a comment |
add a comment |
up vote
0
down vote
Use type assertion
const coords = d3.mouse(this as SVGGElement);
add a comment |
up vote
0
down vote
Use type assertion
const coords = d3.mouse(this as SVGGElement);
add a comment |
up vote
0
down vote
up vote
0
down vote
Use type assertion
const coords = d3.mouse(this as SVGGElement);
Use type assertion
const coords = d3.mouse(this as SVGGElement);
answered 3 hours ago
Ekaterina Tokareva
43635
43635
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49432032%2fts2345-and-ts2339-errors-are-thrown-for-simple-code-in-angular%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown