mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* fix some e2e flows types * some type fixes * must... fix... more... * MOAR * MOREMOREMORE * 1 more
61 lines
1.4 KiB
TypeScript
61 lines
1.4 KiB
TypeScript
import { Feature } from 'ol';
|
|
import { Geometry, LineString, Point } from 'ol/geom';
|
|
import VectorSource from 'ol/source/Vector';
|
|
|
|
import { DataFrame, Field } from '@grafana/data';
|
|
|
|
import { getGeometryField, LocationFieldMatchers } from './location';
|
|
|
|
export interface FrameVectorSourceOptions {}
|
|
|
|
export class FrameVectorSource<T extends Geometry = Geometry> extends VectorSource<T> {
|
|
constructor(public location: LocationFieldMatchers) {
|
|
super({});
|
|
}
|
|
|
|
update(frame: DataFrame) {
|
|
this.clear(true);
|
|
const info = getGeometryField(frame, this.location);
|
|
if (!info.field) {
|
|
this.changed();
|
|
return;
|
|
}
|
|
|
|
for (let i = 0; i < frame.length; i++) {
|
|
this.addFeatureInternal(
|
|
new Feature({
|
|
frame,
|
|
rowIndex: i,
|
|
geometry: info.field.values[i] as T,
|
|
})
|
|
);
|
|
}
|
|
|
|
// only call this at the end
|
|
this.changed();
|
|
}
|
|
|
|
updateLineString(frame: DataFrame) {
|
|
this.clear(true);
|
|
const info = getGeometryField(frame, this.location);
|
|
if (!info.field) {
|
|
this.changed();
|
|
return;
|
|
}
|
|
|
|
//eslint-disable-next-line
|
|
const field = info.field as unknown as Field<Point>;
|
|
const geometry: Geometry = new LineString(field.values.map((p) => p.getCoordinates()));
|
|
this.addFeatureInternal(
|
|
new Feature({
|
|
frame,
|
|
rowIndex: 0,
|
|
geometry: geometry as T,
|
|
})
|
|
);
|
|
|
|
// only call this at the end
|
|
this.changed();
|
|
}
|
|
}
|