I want to create a scatter plot in an Angular application using plot.ly .
All the examples on line use non-typed objects, like so:
var trace = {
x: [1, 2, 3],
y: [4, 5, 6],
. . .
}
I can do that, but since I'm using TypeScript, I want to use the types defined in @types/plotly.js . One of them is ScatterData, which is defined as follows (I removed most of the fields, for brevity):
export type Data = Partial<ScatterData>;
export interface ScatterData {
type: 'bar' | 'scatter' | 'scattergl' | 'scatter3d';
x: Datum[] | Datum[][];
y: Datum[] | Datum[][];
z: Datum[] | Datum[][] | Datum[][][];
xaxis: string;
yaxis: string;
...
}
Here is something I'm trying to do that fails:
const data: Plotly.Data = {
type: 'scatter',
x: [],
y: [],
};
data.x.push(12);
I get an error on the call to push, TS2349: Cannot invoke an expression whose type lacks a call signature. Trying to place at type guard
if(data.x instanceof Ploty.Datum[]) ...
fails because it says the property Datum does not exist.
I can make everything work by not specifying Plotly.Data, but it's really something I want to resort to.