So I have this...
import { D3Service, D3, Selection } from 'd3-ng2-service';
interface ChartData {
q: number,
p: number
}
export class GraphComponent implements OnInit{
private d3;
private x1;
private y;
constructor(element: ElementRef, d3Service: D3Service) {
this.d3 = d3Service.getD3();
let d3 = this.d3;
this.y = d3.scaleLinear()
.domain([0,100])
.range([330, 20])
;
this.x1 = d3.scaleLinear()
.range([0, 100])
;
}
render() {
let y = this.y;
let x1 = this.x1;
let data = [{p:1,q:1}, {p:0.5,q:2}]
var line = d3.line()
.x((d: ChartData) => {return x1(d.q);})
.y((d: ChartData) => {return y(d.p);});
svg.append("path")
.data(data)
.attr("class", "line")
.attr("d", line);
}
}
I'm getting an error on the .x(d: ChartData) line saying:
Argument of type '(d: ChartData) => any' is not assignable to parameter of type '(d: [number, number], index: number, data: [number, number][]) => number'. Types of parameters 'd' and 'd' are incompatible. Type '[number, number]' is not assignable to type 'ChartData'. Property 'q' is missing in type '[number, number]'.
Which I then look at the documentation for d3, which says that I need to inject my data into line() via line(data) to have a custom data.
var line = d3.line(data)
.x((d: ChartData) => {return x1(d.q);})
.y((d: ChartData) => {return y(d.p);});
Which then results in this error...
ERROR in [default] file.ts:259:13 Supplied parameters do not match any signature of call target.
...despite the d3 documentation saying that I should be able to do so. So what am I doing wrong?
Your first syntax (without passing data
) is correct. I think you just have to tell TypeScript what you are passing to d3.line
like:
var line = d3.line<ChartData>()
.x((d: ChartData) => {return x1(d.q);})
.y((d: ChartData) => {return y(d.p);});
It's attempting to compile with the default which is:
d3.line<[number, number]>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With