basic segments impl #27
Merged
rozetko
merged 2 commits from segments-feature-#19
into main
12 months ago
5 changed files with 96 additions and 4 deletions
@ -0,0 +1,38 @@
|
||||
<!DOCTYPE html> |
||||
<html> |
||||
<head> |
||||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"> |
||||
<meta content="utf-8" http-equiv="encoding"> |
||||
|
||||
<script src="../dist/index.dev.js" type="text/javascript"></script> |
||||
</head> |
||||
<body> |
||||
<div id="chart" style="width: 100%; height: 500px;"></div> |
||||
|
||||
<script type="text/javascript"> |
||||
const startTime = 1701790172908; |
||||
const timeSerieData = [5, 6, 3, 7, 5, 6, 8, 4, 5, 6, 4, 3, 5, 7, 8] |
||||
.map((el, idx) => [startTime + idx * 1000, el]); |
||||
const segmentsData = [3, 6, 9].map(el => [startTime + el * 1000, startTime + (el + 1) * 1000]); |
||||
let options = { |
||||
renderLegend: false, |
||||
axis: { |
||||
y: { range: [0, 10] }, |
||||
x: { format: 'time' } |
||||
}, |
||||
} |
||||
var pod = new LinePod( |
||||
document.getElementById('chart'), |
||||
[ |
||||
{ datapoints: timeSerieData, color: 'black' }, |
||||
], |
||||
options, |
||||
[], |
||||
[ |
||||
{ data: segmentsData, color:'#FFE545' } |
||||
] |
||||
); |
||||
pod.render(); |
||||
</script> |
||||
</body> |
||||
</html> |
@ -0,0 +1,45 @@
|
||||
import { SegmentSerie } from "../models/segment"; |
||||
import { PodState } from "@chartwerk/core"; |
||||
import { LineTimeSerie, LineOptions } from "../types"; |
||||
|
||||
import d3 from "d3"; |
||||
|
||||
export class Segments { |
||||
// TODO: more semantic name
|
||||
private _d3Holder = null; |
||||
|
||||
constructor(private _series: SegmentSerie[], private _state: PodState<LineTimeSerie, LineOptions>) { |
||||
} |
||||
|
||||
render(metricContainer: d3.Selection<SVGGElement, unknown, null, undefined>) { |
||||
if(this._d3Holder !== null) { |
||||
this._d3Holder.remove(); |
||||
} |
||||
this._d3Holder = metricContainer.append('g').attr('class', 'markers-layer'); |
||||
for (const s of this._series) { |
||||
this.renderSerie(s); |
||||
} |
||||
} |
||||
|
||||
protected renderSerie(serie: SegmentSerie) { |
||||
serie.data.forEach((d) => { |
||||
// @ts-ignore
|
||||
const startPositionX = pod.state.xScale(d[0]) as number; |
||||
// @ts-ignore
|
||||
const endPositionX = pod.state.xScale(d[1]) as number; |
||||
const width = endPositionX - startPositionX // Math.max(endPositionX - startPositionX, MIMIMUM_SEGMENT_WIDTH);
|
||||
|
||||
this._d3Holder.append('rect') |
||||
.attr('class', 'segment') |
||||
.attr('x', startPositionX) |
||||
.attr('y', 0) |
||||
.attr('width', width) |
||||
// @ts-ignore // TODO: remove ignore but boxParams are protected
|
||||
.attr('height', this._state.boxParams.height) |
||||
.attr('opacity', 0.3) |
||||
.style('fill', serie.color) |
||||
.style('pointer-events', 'none'); |
||||
}); |
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue