rozetko
2 weeks ago
12 changed files with 358 additions and 126 deletions
@ -0,0 +1,42 @@
|
||||
<!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: 50%; height: 500px;"></div> |
||||
|
||||
<script type="text/javascript"> |
||||
let options = { |
||||
renderLegend: false, usePanning: false, |
||||
axis: { |
||||
x: { format: 'numeric', range: [0, 100] }, |
||||
y: { invert: true, range: [0, 100] }, |
||||
y1: { isActive: true, format: 'numeric', range: [0, 1000] }, |
||||
}, |
||||
zoomEvents: { |
||||
mouse: { zoom: { isActive: false, orientation: 'horizontal' } }, |
||||
scroll: { zoom: { isActive: false, orientation: 'horizontal' } } |
||||
}, |
||||
} |
||||
const data1 = [[0,0], [35, 40], [65, 60], [100, 100]]; |
||||
const data2 = [[0,0], [35, 50], [65, 65], [80, 100]]; |
||||
const data3 = [[0,0], [35, 20], [65, 50], [100, 80]]; |
||||
const data4 = [[0,900], [35, 800], [65, 700], [100, 600]]; |
||||
var pod = new LinePod( |
||||
document.getElementById('chart'), |
||||
[ |
||||
{ target: 'test1', datapoints: data1, color: 'green', renderArea: 'Above' }, |
||||
{ target: 'test2', datapoints: data2, color: 'blue', renderArea: 'Below' }, |
||||
{ target: 'test3', datapoints: data3, color: 'orange', renderArea: 'Below', yOrientation: 'right' }, |
||||
{ target: 'test4', datapoints: data4, color: 'purple', renderArea: 'Above', yOrientation: 'right' }, |
||||
], |
||||
options |
||||
); |
||||
pod.render(); |
||||
</script> |
||||
</body> |
||||
</html> |
@ -0,0 +1,33 @@
|
||||
<!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: 50%; height: 500px;"></div> |
||||
<script type="text/javascript"> |
||||
const startTime = 1590590148; |
||||
const data = Array.from( |
||||
{ length: 20 }, |
||||
(el, idx) => [startTime + idx * 10000, Math.floor(Math.random() * 30)] |
||||
); |
||||
let options = { |
||||
renderLegend: false, usePanning: false, |
||||
axis: { y: { range: [0, 50] } }, |
||||
zoomEvents: { mouse: { |
||||
zoom: { isActive: false }, |
||||
pan: { isActive: false }, |
||||
} }, |
||||
eventsCallbacks: { contextMenu: (position) => console.log('contextMenu', position) } |
||||
} |
||||
var pod = new LinePod( |
||||
document.getElementById('chart'), |
||||
[{ datapoints: data }], |
||||
options |
||||
); |
||||
pod.render(); |
||||
</script> |
||||
</body> |
||||
</html> |
@ -1,59 +1,139 @@
|
||||
import { MarkersConf, MarkerSerie } from "../models/marker"; |
||||
import { PodState } from "@chartwerk/core"; |
||||
import { LineTimeSerie, LineOptions } from "../types"; |
||||
import { MarkerElem, MarkersConf, MarkerSerie } from '../models/marker'; |
||||
import { LineTimeSerie, LineOptions } from '../types'; |
||||
|
||||
import d3 from "d3"; |
||||
import { Margin, PodState } from '@chartwerk/core'; |
||||
|
||||
import d3 from 'd3'; |
||||
|
||||
export class Markers { |
||||
// TODO: more semantic name
|
||||
private _d3Holder = null; |
||||
private _layerContainer = null; |
||||
private _chartHeight = 0; |
||||
|
||||
constructor(private _markerConf: MarkersConf, private _state: PodState<LineTimeSerie, LineOptions>) { |
||||
constructor( |
||||
private _chartContainer: d3.Selection<HTMLElement, unknown, null, undefined>, |
||||
private _markerConf: MarkersConf, |
||||
private _state: PodState<LineTimeSerie, LineOptions>, |
||||
private _margin: Margin, |
||||
) { } |
||||
|
||||
clear() { |
||||
if(this._layerContainer !== null) { |
||||
this._layerContainer.remove(); |
||||
} |
||||
this._chartContainer.selectAll('.marker-content').remove(); |
||||
} |
||||
|
||||
render(metricContainer: d3.Selection<SVGGElement, unknown, null, undefined>) { |
||||
if(this._d3Holder !== null) { |
||||
this._d3Holder.remove(); |
||||
render(metricContainer: d3.Selection<SVGGElement, unknown, null, undefined>, chartHeight: number) { |
||||
this._chartHeight = chartHeight; |
||||
this._layerContainer = metricContainer |
||||
.append('g') |
||||
.attr('class', 'markers-layer'); |
||||
for(const serie of this._markerConf.series) { |
||||
this.renderSerie(serie); |
||||
} |
||||
this._d3Holder = metricContainer.append('g').attr('class', 'markers-layer'); |
||||
for (const ms of this._markerConf.series) { |
||||
this.renderSerie(ms); |
||||
} |
||||
|
||||
private _getLinePosition(marker: MarkerElem): number { |
||||
return this._state.xScale(marker.x); |
||||
} |
||||
|
||||
private _renderCircle(marker: MarkerElem) { |
||||
const linePosition = this._getLinePosition(marker); |
||||
|
||||
let circle = this._layerContainer.append('circle') |
||||
.attr('class', 'gap-circle') |
||||
.attr('stroke', marker.color) |
||||
.attr('stroke-width', '2px') |
||||
.attr('r', 4) |
||||
.attr('cx', linePosition) |
||||
.attr('cy', 5) |
||||
|
||||
circle |
||||
.attr('pointer-events', 'all') |
||||
.style('cursor', 'pointer') |
||||
.on('mousemove', () => { |
||||
const onMouseMove = this._markerConf.events?.onMouseMove; |
||||
if(onMouseMove) { |
||||
onMouseMove(marker); |
||||
return |
||||
} |
||||
if(marker.alwaysDisplay) { |
||||
return; |
||||
} |
||||
this._chartContainer |
||||
.selectAll(`.marker-content-${marker.x}`) |
||||
.style('visibility', 'visible') |
||||
.style('z-index', 9999); |
||||
}) |
||||
.on('mouseout', () => { |
||||
const onMouseOut = this._markerConf.events?.onMouseOut; |
||||
if(onMouseOut) { |
||||
onMouseOut() |
||||
return |
||||
} |
||||
if(marker.alwaysDisplay) { |
||||
return; |
||||
} |
||||
this._chartContainer |
||||
.selectAll(`.marker-content-${marker.x}`) |
||||
.style('visibility', 'hidden') |
||||
.style('z-index', 1); |
||||
}); |
||||
} |
||||
|
||||
private _renderLine(marker: MarkerElem) { |
||||
const linePosition = this._getLinePosition(marker); |
||||
|
||||
this._layerContainer.append('line') |
||||
.attr('class', 'gap-line') |
||||
.attr('stroke', marker.color) |
||||
.attr('stroke-width', '1px') |
||||
.attr('stroke-opacity', '0.3') |
||||
.attr('stroke-dasharray', '4') |
||||
.attr('x1', linePosition) |
||||
.attr('x2', linePosition) |
||||
.attr('y1', 0) |
||||
// @ts-ignore // TODO: remove ignore but boxParams are protected
|
||||
.attr('y2', this._state.boxParams.height) |
||||
.attr('pointer-events', 'none'); |
||||
} |
||||
|
||||
private _renderTooltip(marker: MarkerElem) { |
||||
if(marker.html === undefined) { |
||||
return; |
||||
} |
||||
|
||||
const linePosition = this._getLinePosition(marker); |
||||
|
||||
const div = this._chartContainer |
||||
.append('div') |
||||
.attr('class', `marker-content marker-content-${marker.x}`) |
||||
// @ts-ignore // TODO: remove ignore but boxParams are protected
|
||||
.style('top', `${this._state.boxParams.height - this._chartHeight}px`) |
||||
.style('visibility', marker.alwaysDisplay ? 'visible' : 'hidden') |
||||
.style('position', 'absolute') |
||||
.style('border', '1px solid black') |
||||
.style('background-color', 'rgb(33, 37, 41)') |
||||
.style('color', 'rgb(255, 255, 255)') |
||||
.style('line-height', '1.55') |
||||
.style('font-size', '0.875rem') |
||||
.style('border-radius', '0.5rem') |
||||
.style('padding', 'calc(0.3125rem) 0.625rem') |
||||
.style('position', 'absolute') |
||||
.style('white-space', 'nowrap') |
||||
.style('pointer-events', 'none') |
||||
.style('z-index', 1) |
||||
.html(marker.html); |
||||
|
||||
// align tooltip: center (we need it to be rendered first)
|
||||
div.style('left', `${linePosition + this._margin.left - div.node().getBoundingClientRect().width / 2}px`) |
||||
} |
||||
|
||||
protected renderSerie(serie: MarkerSerie) { |
||||
serie.data.forEach((d) => { |
||||
let linePosition = this._state.xScale(d[0]) as number; |
||||
this._d3Holder.append('line') |
||||
.attr('class', 'gap-line') |
||||
.attr('stroke', serie.color) |
||||
.attr('stroke-width', '1px') |
||||
.attr('stroke-opacity', '0.3') |
||||
.attr('stroke-dasharray', '4') |
||||
.attr('x1', linePosition) |
||||
.attr('x2', linePosition) |
||||
.attr('y1', 0) |
||||
// @ts-ignore // TODO: remove ignore but boxParams are protected
|
||||
.attr('y2', this._state.boxParams.height) |
||||
.attr('pointer-events', 'none'); |
||||
let circle = this._d3Holder.append('circle') |
||||
.attr('class', 'gap-circle') |
||||
.attr('stroke', serie.color) |
||||
.attr('stroke-width', '2px') |
||||
.attr('r', 4) |
||||
.attr('cx', linePosition) |
||||
.attr('cy', 5) |
||||
|
||||
if(this._markerConf !== undefined) { |
||||
circle |
||||
.attr('pointer-events', 'all') |
||||
.style('cursor', 'pointer') |
||||
.on('mousemove', () => this._markerConf.events.onMouseMove(d)) |
||||
.on('mouseout', () => this._markerConf.events.onMouseOut()) |
||||
} |
||||
|
||||
serie.data.forEach((marker: MarkerElem) => { |
||||
this._renderLine(marker); |
||||
this._renderCircle(marker); |
||||
this._renderTooltip(marker); |
||||
}); |
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue