Compare commits

...

116 Commits

Author SHA1 Message Date
Coin de Gamma bd598d6a0b Merge pull request '0.6.19' (#64) from 0.6.19 into main 4 weeks ago
glitch4347 4620ef7aa9 0.6.19 4 weeks ago
rozetko 44a4e6ce74 Merge pull request 'segment select' (#63) from segment-select into main 4 weeks ago
glitch4347 04a01b75a1 opacity param 4 weeks ago
glitch4347 bf3aab2db1 segment select 4 weeks ago
Coin de Gamma 348a289f1a Merge pull request '0.6.18' (#61) from 0.6.18 into main 3 months ago
glitch4347 17a2fdf94e 0.6.18 3 months ago
Coin de Gamma dc89f6cbb9 Merge pull request 'basic react component implementatino is sep project' (#59) from better-react-component-#58 into main 3 months ago
glitch4347 547c15c0b6 optimise excludes in libs in webpack 3 months ago
glitch4347 32d122ba4d back dev build 3 months ago
glitch4347 9c165f4e1f basic react component implementatino is sep project 3 months ago
Coin de Gamma ec2bd27acd Merge pull request '0.6.17' (#54) from 0.6.17 into main 3 months ago
glitch4347 815144d209 update core 0.6.23 3 months ago
glitch4347 1a80ca2cab 0.6.17 3 months ago
Coin de Gamma ee08db2f54 Merge pull request 'use events instead of eventsCallbacks' (#53) from core-0.6.23 into main 3 months ago
glitch4347 884fed6582 use events instead of eventsCallbacks 3 months ago
glitch4347 8aca72ce04 zoom out with ranges logic 3 months ago
Coin de Gamma c62a1c386f Merge pull request 'rename demo->basic, live and vertical && update readme' (#47) from rename-examples into main 3 months ago
glitch4347 de6237891c rename demo->basic, live and vertical && update readme 3 months ago
rozetko 7bcd0d298a 0.6.16 4 months ago
rozetko ee2f02be96 upd core 4 months ago
rozetko 456b888fe1 0.6.15 5 months ago
rozetko a34c0574e4 hotfix 5 months ago
rozetko 73dbf0ea67 0.6.14 5 months ago
rozetko ff5973b106 Merge pull request 'updateData: update segments and markers' (#44) from update-segments-and-markers-on-update-data into main 5 months ago
rozetko 878e86d849 updateData: update segments and markers 5 months ago
vargburz 1e8f8d0cb3 0.6.13 5 months ago
rozetko 39412e5f65 Merge pull request 'roken shared crosshair #42' (#43) from broken-shared-crosshair-#42 into main 5 months ago
rozetko 37da147343 Merge pull request '0.6.19 core usdage' (#41) from build-fail-on-core-0.6.18-udpate-#40 into main 5 months ago
glitch4347 0dac1c390e fix 5 months ago
glitch4347 07f454fe51 0.6.19 core usdage 5 months ago
vargburz 88d6b2b24a Merge pull request 'click-event' (#39) from click-event into main 5 months ago
glitch4347 837004c2ad onclick 5 months ago
glitch4347 e46c7bdfb6 some commit 5 months ago
rozetko ee7a57685f 0.6.12 6 months ago
rozetko 963011b274 Merge pull request 'pod fix' (#37) from strange-updates into main 6 months ago
glitch4347 283f0995e8 pod fix 6 months ago
rozetko 19689a68cd Merge pull request 'mouse-over-and-react' (#36) from mouse-over-and-react into main 6 months ago
glitch4347 79b759569f simpler example 6 months ago
glitch4347 d66da50c1a rm line 6 months ago
glitch4347 d9d1b5e501 example with mouse over and refactor react component events 6 months ago
Coin de Gamma b8cd14e4b8 Merge pull request 'marker-callback-#25' (#35) from marker-callback-#25 into main 6 months ago
glitch4347 e470d58611 fx Marker callback type 6 months ago
glitch4347 26d5956c17 markers conf in react 6 months ago
glitch4347 43241bbcb7 markers conf + events 6 months ago
glitch4347 82fc9a10f1 markers conf + new example 6 months ago
rozetko 57ae85c20e Merge pull request 'react-component-marker-and-segment-feature-#28' (#33) from react-component-marker-and-segment-feature-#28 into main 6 months ago
glitch4347 98e4e42fc4 callbacks 6 months ago
glitch4347 6fbf833150 event callback back 6 months ago
glitch4347 a0e27d5efd rm My_CONST 6 months ago
glitch4347 727f8cac86 codefix 6 months ago
glitch4347 869488d405 codestyle 6 months ago
glitch4347 d97b0e9b0c markers++ 6 months ago
glitch4347 11ab01a7d2 markers++ 6 months ago
glitch4347 326c666435 component refactorinmg begin 6 months ago
rozetko 33cf970d5f Merge pull request 'basic segments impl' (#27) from segments-feature-#19 into main 6 months ago
glitch4347 03a562c0d2 codestytle fix 6 months ago
glitch4347 38b6eb2326 basic segments impl 6 months ago
rozetko af9aaf6d82 Merge pull request 'markers-feature-#18' (#23) from markers-feature-#18 into main 6 months ago
glitch4347 bf679019fc payload comment 6 months ago
glitch4347 86099c57b3 pointe events 6 months ago
glitch4347 9a6c5cd7f4 markers++ 6 months ago
glitch4347 aadcd013bf markers++ 6 months ago
glitch4347 a5c737fa81 marker data continue 6 months ago
rozetko c451df6e3b 0.6.11 6 months ago
rozetko 3ff6f7b8e8 crosshair hotfix 6 months ago
glitch4347 1d1a34f9bf markers begin 6 months ago
glitch4347 8346bceedf markers examples begin 6 months ago
vargburz 1ca84c53f8 Merge pull request '0.6.10 version' (#21) from version-0.6.10 into main 6 months ago
vargburz 85793a7965 0.6.10 version 6 months ago
vargburz 7e4a895e93 Merge pull request 'add copy-webpack-=plugin' (#20) from update-copy-webpack into main 6 months ago
glitch4347 f1c3ef6f1d add copy-webpack-=plugin 6 months ago
rozetko 6a0ea4303e Merge pull request 'add react component and build for that' (#16) from react-component-attempt-2 into main 6 months ago
glitch4347 08b128ea91 codestyle fixes 6 months ago
glitch4347 f1582fc610 rm comment 6 months ago
glitch4347 e98997995f add react component and build for that 6 months ago
rozetko e809046098 Merge pull request 'Developement how to and d3 resolution' (#14) from dev-how-to-#4 into main 6 months ago
glitch4347 833780105a fx type in conf 6 months ago
glitch4347 2e156ad46b add node_resolutinos for linking case & update readme 6 months ago
vargburz f100af0ee8 Merge pull request 'hotfix: call mouseOver and mouseMove to render crosshair on init and zoomin' (#10) from hidden-crosshair-on-first-render-and-zoomin into main 7 months ago
vargburz 927855ad45 Merge pull request 'set git repo in package.json' (#12) from set-git-package into main 7 months ago
glitch4347 a6841a83b0 set git repo in package.json 7 months ago
glitch4347 5c39f741ff hotfix: call mouseOver and mouseMove to render crosshair on init and zoomin 7 months ago
rozetko a9f5d074ce Merge pull request 'Update dependencies' (#3) from upd-deps into main 1 year ago
rozetko e3d261aa30 0.6.9 1 year ago
rozetko 4dc9c5290d upd dependencies 1 year ago
vargburz d3597763c5 0.6.8 2 years ago
Alexander Velikiy 517c014629 Merge branch 'hightlight-datapoint-for-right-y-axis' into 'main' 2 years ago
vargburz d763d8591b use right y series on mouse move 2 years ago
vargburz 7d9f38fa3c 0.6.7 2 years ago
Alexander Velikiy 60904493dd Merge branch 'make-y-oriented-series-work' into 'main' 2 years ago
vargburz 903817cb9c apply yOriented series for line pod 2 years ago
vargburz 8825f8b0ba Merge pull request '#DOC [Readme] add browser including' (#2) from doc/add_browser_including into main 2 years ago
Eugene Petukhov db30589898 #DOC [Readme] add browser including v2 2 years ago
Eugene Petukhov f701b29bab #DOC [Readme] add browser including 2 years ago
vargburz b7c6bb7fd8 Merge pull request 'sync-gitlab 0.6.6' (#1) from sync-gitlab into main 2 years ago
rozetko fa632d35a9 Merge branch 'do-not-highlight-nil-metric' into 'main' 2 years ago
rozetko a448f62fc9 minor fix 2 years ago
rozetko cf5722484a 0.6.6 2 years ago
rozetko 49c5c3d462 fix non-existing metrics being highlighted on hover 2 years ago
vargburz 0a04633b43 return latest core vesrion 2 years ago
vargburz dd6485bef5 static core version 2 years ago
vargburz b0b2b48f4d 0.6.3 2 years ago
vargburz b082e71822 fix parent class 2 years ago
vargburz c93bcddee4 0.6.2 2 years ago
vargburz 7cc885e2c7 0.6.1 2 years ago
vargburz 550ca9a807 test 2 years ago
vargburz 15587d9bfa 0.6.0 2 years ago
Alexander Velikiy 9bf20c4169 Merge branch 'update-line-with-models' into 'main' 2 years ago
vargburz b65b04c574 use core 060 2 years ago
vargburz 70966b323f fix live demo 2 years ago
vargburz ca3dc0b8f1 remame 2 years ago
vargburz f9d505fd92 remove strange options 2 years ago
vargburz 463bce47c6 fix defaults 2 years ago
vargburz f593206a98 fix type 2 years ago
vargburz e7248806b0 models 2 years ago
  1. 41
      README.md
  2. 14
      build/webpack.base.conf.js
  3. 11
      examples/basic.html
  4. 16
      examples/live.html
  5. 42
      examples/markers.html
  6. 47
      examples/markers_select.html
  7. 33
      examples/mouse_click.html
  8. 29
      examples/mouse_move.html
  9. 41
      examples/segments.html
  10. 46
      examples/segments_select.html
  11. 0
      examples/vertical.html
  12. 37
      examples/zoom_out.html
  13. 32
      package.json
  14. 26
      react/build/webpack.base.conf.js
  15. 8
      react/build/webpack.dev.conf.js
  16. 6
      react/build/webpack.prod.conf.js
  17. 23
      react/package.json
  18. 74
      react/src/index.tsx
  19. 23
      react/tsconfig.json
  20. 1322
      react/yarn.lock
  21. 59
      src/components/markers.ts
  22. 73
      src/components/segments.ts
  23. 420
      src/index.ts
  24. 21
      src/models/line_series.ts
  25. 15
      src/models/marker.ts
  26. 11
      src/models/segment.ts
  27. 30
      src/types.ts
  28. 5
      tsconfig.json
  29. 4878
      yarn.lock

41
README.md

@ -1,2 +1,43 @@
# Chartwerk Line Pod # Chartwerk Line Pod
## Including
### Browser
#### Script tag
```html
<script src="https://unpkg.com/@chartwerk/line-pod@latest/dist/index.dev.js" type="text/javascript"></script>
```
#### Example
```html
<div id="line-chart" class="chart-block" style="width: 100%; height: 400px;"></div>
<script src="https://unpkg.com/@chartwerk/line-pod@latest/dist/index.dev.js" type="text/javascript"></script>
<script>
new LinePod(document.getElementById('yourDivId', yourData, yourOptions)).render();
</script>
```
#### Other examples
* [Basic](https://code.corpglory.net/chartwerk/line-pod/src/branch/main/examples/basic.html)
* [Live](https://code.corpglory.net/chartwerk/line-pod/src/branch/main/examples/live.html)
* [Vertical](https://code.corpglory.net/chartwerk/line-pod/src/branch/main/examples/vertical.html)
* [Segments](https://code.corpglory.net/chartwerk/line-pod/src/branch/main/examples/segments.html)
* [Markers](https://code.corpglory.net/chartwerk/line-pod/src/branch/main/examples/markers.html)
### Development
If you want to link `core` then clone it to same directory and then run
```
yarn link ../core
```
but don't add `resolutions` logic to `package.json`
then run
```
yarn install
yarn dev
```

14
build/webpack.base.conf.js

@ -1,4 +1,5 @@
const path = require('path'); const path = require('path');
const CopyPlugin = require("copy-webpack-plugin");
function resolve(dir) { function resolve(dir) {
@ -8,7 +9,14 @@ function resolve(dir) {
module.exports = { module.exports = {
context: resolve('src'), context: resolve('src'),
entry: './index.ts', entry: './index.ts',
plugins: [], plugins: [
new CopyPlugin({
patterns: [
{ from: "../react/dist/index.js", to: "react/index.js" },
{ from: "../react/dist/index.d.ts", to: "react/index.d.ts" },
],
})
],
module: { module: {
rules: [ rules: [
{ {
@ -25,6 +33,10 @@ module.exports = {
}, },
resolve: { resolve: {
extensions: ['.ts', '.js'], extensions: ['.ts', '.js'],
// this is necessary for resolution of external libs like d3 in dev mode
// when core is linked: webpack will take d3 from this node_modules but not from
// internal so you get one version of d3
modules: [path.resolve(__dirname, '../node_modules'), 'node_modules']
}, },
output: { output: {
filename: 'index.js', filename: 'index.js',

11
examples/demo.html → examples/basic.html

@ -13,12 +13,17 @@
const startTime = 1590590148; const startTime = 1590590148;
const arrayLength = 20; const arrayLength = 20;
const data1 = Array.from({ length: arrayLength }, (el, idx) => [startTime + idx * 10000, Math.floor(Math.random() * 40)]); const data1 = Array.from({ length: arrayLength }, (el, idx) => [startTime + idx * 10000, Math.floor(Math.random() * 40)]);
const data2 = Array.from({ length: arrayLength }, (el, idx) => [startTime + idx * 10000, Math.floor(Math.random() * 100)]); const data2 = Array.from({ length: arrayLength }, (el, idx) => [startTime + idx * 10000, Math.floor(Math.random() * 10)]);
const data3 = Array.from({ length: arrayLength }, (el, idx) => [startTime + idx * 10000, Math.floor(Math.random() * 20) + 90]); const data3 = Array.from({ length: arrayLength }, (el, idx) => [startTime + idx * 10000, Math.floor(Math.random() * 20) + 90]);
const zoomIn = (ranges) => { const xRange = ranges[0]; options.axis.x.range = xRange; pod.updateData(undefined, options); } const zoomIn = (ranges) => { const xRange = ranges[0]; options.axis.x.range = xRange; pod.updateData(undefined, options); }
const panningEnd = (ranges) => { const xRange = ranges[0]; options.axis.x.range = xRange; pod.updateData(undefined, options); } const panningEnd = (ranges) => { const xRange = ranges[0]; options.axis.x.range = xRange; pod.updateData(undefined, options); }
let options = { let options = {
renderLegend: false, usePanning: false, axis: { y: { invert: false, range: [0, 350] }, x: { format: 'time' } }, renderLegend: false, usePanning: false,
axis: {
y: { invert: false, range: [0, 350] },
y1: { isActive: true, range: [0, 10], ticksCount: 8 },
x: { format: 'time' }
},
zoomEvents: { zoomEvents: {
mouse: { zoom: { isActive: true, orientation: 'horizontal' } }, mouse: { zoom: { isActive: true, orientation: 'horizontal' } },
scroll: { zoom: { isActive: true, orientation: 'horizontal' } } scroll: { zoom: { isActive: true, orientation: 'horizontal' } }
@ -29,7 +34,7 @@
document.getElementById('chart'), document.getElementById('chart'),
[ [
{ target: 'test1', datapoints: data1, color: 'green', dashArray: '5,3', class: 'first', renderArea: true }, { target: 'test1', datapoints: data1, color: 'green', dashArray: '5,3', class: 'first', renderArea: true },
{ target: 'test2', datapoints: data2, color: 'blue' }, { target: 'test2', datapoints: data2, color: 'blue', yOrientation: 'right' },
{ target: 'test3', datapoints: data3, color: 'orange' }, { target: 'test3', datapoints: data3, color: 'orange' },
], ],
options options

16
examples/demo_live.html → examples/live.html

@ -15,9 +15,9 @@
const startTime = 1590590148; const startTime = 1590590148;
const arrayLength = 100; const arrayLength = 100;
this.isZoomed = false; // TODO: temporary hack to have zoomin|zoomout with `appendData`. It will be moved to Pod. this.isZoomed = false; // TODO: temporary hack to have zoomin|zoomout with `appendData`. It will be moved to Pod.
const data1 = Array.from({ length: arrayLength }, (el, idx) => [startTime + idx * 10000, Math.floor(Math.random() * 40)]); var data1 = Array.from({ length: arrayLength }, (el, idx) => [startTime + idx * 10000, Math.floor(Math.random() * 40)]);
const data2 = Array.from({ length: arrayLength }, (el, idx) => [startTime + idx * 10000, Math.floor(Math.random() * 100)]); var data2 = Array.from({ length: arrayLength }, (el, idx) => [startTime + idx * 10000, Math.floor(Math.random() * 100)]);
const data3 = Array.from({ length: arrayLength }, (el, idx) => [startTime + idx * 10000, Math.floor(Math.random() * 20) + 90]); var data3 = Array.from({ length: arrayLength }, (el, idx) => [startTime + idx * 10000, Math.floor(Math.random() * 20) + 90]);
const zoomIn = (ranges) => { const xRange = ranges[0]; options.axis.x.range = xRange; pod.updateData(undefined, options); this.isZoomed = true } const zoomIn = (ranges) => { const xRange = ranges[0]; options.axis.x.range = xRange; pod.updateData(undefined, options); this.isZoomed = true }
const zoomOut = (ranges) => { options.axis.x.range = undefined; pod.updateData(undefined, options); this.isZoomed = false } const zoomOut = (ranges) => { options.axis.x.range = undefined; pod.updateData(undefined, options); this.isZoomed = false }
let options = { renderLegend: false, axis: { y: { invert: false, range: [0, 350] }, x: { format: 'time' } }, eventsCallbacks: { zoomIn: zoomIn, zoomOut } }; let options = { renderLegend: false, axis: { y: { invert: false, range: [0, 350] }, x: { format: 'time' } }, eventsCallbacks: { zoomIn: zoomIn, zoomOut } };
@ -37,9 +37,17 @@
const d1 = [startTime + rerenderIdx * 10000, Math.floor(Math.random() * 20) + 90]; const d1 = [startTime + rerenderIdx * 10000, Math.floor(Math.random() * 20) + 90];
const d2 = [startTime + rerenderIdx * 10000, Math.floor(Math.random() * 100)]; const d2 = [startTime + rerenderIdx * 10000, Math.floor(Math.random() * 100)];
const d3 = [startTime + rerenderIdx * 10000, Math.floor(Math.random() * 20) + 90]; const d3 = [startTime + rerenderIdx * 10000, Math.floor(Math.random() * 20) + 90];
console.log('d1', data1)
const shouldRerender = !this.isZoomed; const shouldRerender = !this.isZoomed;
console.time('rerender'); console.time('rerender');
pod.appendData([d1, d2, d3], shouldRerender); data1.push(d1);
data2.push(d2);
data3.push(d3);
pod.updateData([
{ target: 'test1', datapoints: data1, color: 'green', maxLength: arrayLength + 30, renderDots: false },
{ target: 'test2', datapoints: data2, color: 'blue', maxLength: arrayLength + 30, renderDots: false },
{ target: 'test3', datapoints: data3, color: 'orange', maxLength: arrayLength + 30, renderDots: false },
]);
console.timeEnd('rerender'); console.timeEnd('rerender');
if(rerenderIdx > arrayLength + 100) { if(rerenderIdx > arrayLength + 100) {
clearInterval(test); clearInterval(test);

42
examples/markers.html

@ -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: 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]);
// TODO: make this one-dimensinal data when implemented
const markersData1 = [3, 6, 9].map(el => [startTime + el * 1000]);
const markersData2 = [4, 11].map(el => [startTime + el * 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,
{
series: [
{ data: markersData1, color: 'red' },
{ data: markersData2, color: 'blue' },
]
}
);
pod.render();
</script>
</body>
</html>

47
examples/markers_select.html

@ -0,0 +1,47 @@
<!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]);
// TODO: make this one-dimensinal data when implemented
const markersData = [3, 6, 9].map(el => [
startTime + el * 1000,
{ el }
]);
let options = {
renderLegend: false,
axis: {
y: { range: [0, 10] },
x: { format: 'time' }
},
}
var pod = new LinePod(
document.getElementById('chart'),
[
{ datapoints: timeSerieData, color: 'black' },
],
options,
{
series: [
{ data: markersData, color: 'red' },
],
events: {
onMouseMove: (el) => { console.log(el); },
onMouseOut: () => { console.log('mouse out'); }
}
}
);
pod.render();
</script>
</body>
</html>

33
examples/mouse_click.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: { mouseClick: console.log }
}
var pod = new LinePod(
document.getElementById('chart'),
[{ datapoints: data }],
options
);
pod.render();
</script>
</body>
</html>

29
examples/mouse_move.html

@ -0,0 +1,29 @@
<!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] } },
eventsCallbacks: { mouseMove: console.log }
}
var pod = new LinePod(
document.getElementById('chart'),
[{ datapoints: data }],
options
);
pod.render();
</script>
</body>
</html>

41
examples/segments.html

@ -0,0 +1,41 @@
<!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) * 1100]);
let options = {
renderLegend: false,
axis: {
y: { range: [0, 10] },
x: { format: 'time' }
},
}
var pod = new LinePod(
document.getElementById('chart'),
[
{ datapoints: timeSerieData, color: 'black' },
],
options,
undefined,
[
{
data: segmentsData,
color:'#FFE545'
}
]
);
pod.render();
</script>
</body>
</html>

46
examples/segments_select.html

@ -0,0 +1,46 @@
<!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) * 1100]);
let options = {
renderLegend: false,
axis: {
y: { range: [0, 10] },
x: { format: 'time' }
},
}
var pod = new LinePod(
document.getElementById('chart'),
[
{ datapoints: timeSerieData, color: 'black' },
],
options,
undefined,
[
{
data: segmentsData,
color:'#FFE545',
select: true,
opacity: 0.4,
opacitySelect: 0.8,
onSelect: console.log,
onUnselect: console.log
}
]
);
pod.render();
</script>
</body>
</html>

0
examples/demo_vertical.html → examples/vertical.html

37
examples/zoom_out.html

@ -0,0 +1,37 @@
<!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: true, orientation: "horizontal" },
pan: { isActive: false },
}},
events: {
zoomOut: function(centers, ranges) {
console.log('zoomOut', centers, ranges);
}
}
}
var pod = new LinePod(
document.getElementById('chart'),
[{ datapoints: data }],
options
);
pod.render();
</script>
</body>
</html>

32
package.json

@ -1,29 +1,37 @@
{ {
"name": "@chartwerk/line-pod", "name": "@chartwerk/line-pod",
"version": "0.5.1", "version": "0.6.19",
"description": "Chartwerk line chart", "description": "Chartwerk line chart",
"main": "dist/index.js", "main": "dist/index.js",
"files": [
"/dist"
],
"scripts": { "scripts": {
"build": "webpack --config build/webpack.prod.conf.js && webpack --config build/webpack.dev.conf.js", "build": "rm -rf dist && cd react && yarn build && cd .. && webpack --config build/webpack.prod.conf.js && webpack --config build/webpack.dev.conf.js",
"dev": "webpack --watch --config build/webpack.dev.conf.js", "dev": "webpack --watch --config build/webpack.dev.conf.js",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1",
"update-core": "yarn up @chartwerk/core && yarn up @chartwerk/core@latest"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://gitlab.com/chartwerk/line-pod.git" "url": "http://code.corpglory.net/chartwerk/line-pod.git"
}, },
"author": "CorpGlory", "author": "CorpGlory",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@chartwerk/core": "latest" "@chartwerk/core": "^0.6.23"
}, },
"devDependencies": { "devDependencies": {
"css-loader": "^3.4.2", "copy-webpack-plugin": "^11.0.0",
"style-loader": "^1.1.3", "css-loader": "^6.8.1",
"ts-loader": "^6.2.1", "style-loader": "^3.3.3",
"typescript": "^3.8.3", "ts-loader": "^9.4.3",
"webpack": "^4.42.0", "typescript": "^5.1.3",
"webpack-cli": "^3.3.11" "webpack": "^5.87.0",
"webpack-cli": "^5.1.4"
}, },
"packageManager": "yarn@3.2.1" "packageManager": "yarn@3.2.1",
"workspaces": [
"react/*"
]
} }

26
react/build/webpack.base.conf.js

@ -0,0 +1,26 @@
const path = require('path');
module.exports = {
entry: './src/index.tsx',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
filename: 'index.js',
path: path.resolve(__dirname, '../dist'),
libraryTarget: 'umd',
umdNamedDefine: true,
},
externals: [
'@chartwerk/line-pod', 'react'
]
};

8
react/build/webpack.dev.conf.js

@ -0,0 +1,8 @@
const baseWebpackConfig = require('./webpack.base.conf');
var conf = baseWebpackConfig;
conf.devtool = 'inline-source-map';
conf.mode = 'development';
conf.output.filename = 'index.dev.js';
module.exports = conf;

6
react/build/webpack.prod.conf.js

@ -0,0 +1,6 @@
const baseWebpackConfig = require('./webpack.base.conf');
var conf = baseWebpackConfig;
conf.mode = 'production';
module.exports = baseWebpackConfig;

23
react/package.json

@ -0,0 +1,23 @@
{
"name": "line-pod-react",
"version": "0.0.1",
"description": "React wrapper around line-pod",
"main": "dist/index.js",
"repository": "http://code.corpglory.net/chartwerk/line-pod.git",
"author": "CorpGlory Inc.",
"license": "ISC",
"scripts": {
"build": "webpack --config build/webpack.prod.conf.js",
"dev": "webpack --config build/webpack.dev.conf.js"
},
"dependencies": {
"@chartwerk/line-pod": "workspace:*",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"ts-loader": "^9.5.1",
"typescript": "^5.4.3",
"webpack": "^5.87.0"
}
}

74
react/src/index.tsx

@ -0,0 +1,74 @@
import { LineTimeSerie, LineOptions, LinePod } from '@chartwerk/line-pod';
import { MarkersConf } from '@chartwerk/line-pod/dist/models/marker';
import { SegmentSerie } from '@chartwerk/line-pod/dist/models/segment';
import React, { useEffect, useRef, useState } from 'react';
import _ from 'lodash';
export type ChartwerkLinePodProps = {
id?: string;
series: LineTimeSerie[];
options?: LineOptions;
markersConf?: MarkersConf,
segments?: SegmentSerie[],
className?: string;
}
export function ChartwerkLinePod(props: ChartwerkLinePodProps) {
const [pod, setPod] = useState<LinePod | null>(null);
const [hack, setHack] = useState<number | null>(null);
const chartRef = useRef(null);
const chart = chartRef.current;
useEffect(() => {
// this function will be called on component unmount
return () => {
if(pod === null) { return; }
// @ts-ignore
pod.removeEventListeners();
}
}, []);
useEffect(() => {
if(chart === null) { return; }
if(pod === null) {
const newPod = new LinePod(
// @ts-ignore
chart,
props.series,
props.options,
props.markersConf,
props.segments
);
setPod(newPod);
newPod.render();
} else {
if(props.markersConf) {
pod.updateMarkers(props.markersConf);
}
if(props.segments) {
pod.updateSegments(props.segments);
}
// TODO: actually it's wrong logic with updates
// because it creates new pod anyway
pod.updateData(props.series, props.options);
}
}, [chart, props.id, props.options, props.markersConf, props.segments]);
// TODO: it's a hack to render the LinePod right after the div appears in DOM
setTimeout(() => {
if(hack === null) {
setHack(1);
}
}, 1);
return (
<div id={props.id} className={props.className} ref={chartRef}></div>
);
}
export default ChartwerkLinePod;

23
react/tsconfig.json

@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "es5",
"rootDir": "./src",
"module": "esnext",
"moduleResolution": "node",
"declaration": true,
"declarationDir": "dist",
"allowSyntheticDefaultImports": true,
"inlineSourceMap": false,
"sourceMap": true,
"noEmitOnError": false,
"emitDecoratorMetadata": false,
"experimentalDecorators": true,
"noImplicitReturns": true,
"noImplicitThis": false,
"noImplicitUseStrict": false,
"noImplicitAny": false,
"noUnusedLocals": false,
"baseUrl": "./src",
"jsx": "react"
}
}

1322
react/yarn.lock

File diff suppressed because it is too large Load Diff

59
src/components/markers.ts

@ -0,0 +1,59 @@
import { MarkersConf, MarkerSerie } from "../models/marker";
import { PodState } from "@chartwerk/core";
import { LineTimeSerie, LineOptions } from "../types";
import d3 from "d3";
export class Markers {
// TODO: more semantic name
private _d3Holder = null;
constructor(private _markerConf: MarkersConf, 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 ms of this._markerConf.series) {
this.renderSerie(ms);
}
}
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())
}
});
}
}

73
src/components/segments.ts

@ -0,0 +1,73 @@
import { SegmentSerie, SegmentElement } from "../models/segment";
import { PodState } from "@chartwerk/core";
import { LineTimeSerie, LineOptions } from "../types";
import * as d3 from "d3";
const OPACITY = 0.3;
const OPACITY_SELECT = 0.3;
export class Segments {
// TODO: more semantic name
private _d3Holder = null;
private _metricCon = null;
constructor(
private _series: SegmentSerie[],
private _state: PodState<LineTimeSerie, LineOptions>
) {
}
render(metricContainer: d3.Selection<SVGGElement, unknown, null, undefined>, chartContainer: 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(chartContainer, s);
}
}
protected renderSerie(chartContainer: d3.Selection<SVGGElement, unknown, null, undefined>, serie: SegmentSerie) {
// TODO: it's hack with core, need to find a better way
const overlay = chartContainer.select('.overlay');
serie.data.forEach((d) => {
// @ts-ignore
const startPositionX = this._state.xScale(d[0]) as number;
// @ts-ignore
const endPositionX = this._state.xScale(d[1]) as number;
const width = endPositionX - startPositionX // Math.max(endPositionX - startPositionX, MIMIMUM_SEGMENT_WIDTH);
const opacity = serie.opacity || OPACITY;
const opacitySelect = serie.opacitySelect || OPACITY_SELECT;
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', opacity)
.style('fill', serie.color)
.on('mouseover', function() {
if(serie.select === true) {
d3.select(this).attr('opacity', opacitySelect);
if(serie.onSelect) {
serie.onSelect(d);
}
}
})
.on('mouseout', function(e) {
if(serie.select === true) {
d3.select(this).attr('opacity', opacity);
if(serie.onUnselect) {
serie.onUnselect(d);
}
}
})
.on('mousemove', function(e) {
var event = new MouseEvent('mousemove', d3.event);
overlay.node().dispatchEvent(event)
})
});
}
}

420
src/index.ts

@ -1,5 +1,11 @@
import { ChartwerkPod, VueChartwerkPodMixin, TickOrientation, TimeFormat, CrosshairOrientation, BrushOrientation } from '@chartwerk/core'; import { ChartwerkPod, VueChartwerkPodMixin, TimeFormat, CrosshairOrientation, BrushOrientation, yAxisOrientation } from '@chartwerk/core';
import { LineTimeSerie, LineOptions, Mode } from './types'; import { LineTimeSerie, LineOptions, MouseObj } from './types';
import { Markers } from './components/markers';
import { Segments } from './components/segments';
import { LineSeries } from './models/line_series';
import { MarkersConf } from './models/marker';
import { SegmentSerie } from './models/segment';
import * as d3 from 'd3'; import * as d3 from 'd3';
import * as _ from 'lodash'; import * as _ from 'lodash';
@ -9,51 +15,48 @@ const CROSSHAIR_CIRCLE_RADIUS = 3;
const CROSSHAIR_BACKGROUND_RAIDUS = 9; const CROSSHAIR_BACKGROUND_RAIDUS = 9;
const CROSSHAIR_BACKGROUND_OPACITY = 0.3; const CROSSHAIR_BACKGROUND_OPACITY = 0.3;
export class LinePod extends ChartwerkPod<LineTimeSerie, LineOptions> {
class LinePod extends ChartwerkPod<LineTimeSerie, LineOptions> {
lineGenerator = null; lineGenerator = null;
areaGenerator = null; areaGenerator = null;
lineGeneratorY1 = null;
constructor(_el: HTMLElement, _series: LineTimeSerie[] = [], _options: LineOptions = {}) { areaGeneratorY1 = null;
private _markersLayer: Markers = null;
private _segmentsLayer: Segments = null;
constructor(
_el: HTMLElement,
_series: LineTimeSerie[] = [],
_options: LineOptions = {},
private _markersConf?: MarkersConf,
private _segmentSeries: SegmentSerie[] = [],
) {
super(_el, _series, _options); super(_el, _series, _options);
this.series = new LineSeries(_series);
} }
renderMetrics(): void { override renderMetrics(): void {
this.clearAllMetrics(); this.clearAllMetrics();
this.updateCrosshair(); this.updateCrosshair();
this.initLineGenerator(); this.initLineGenerator();
this.initAreaGenerator(); this.initAreaGenerator();
// TODO: seems that renderMetrics is not correct name if(!this.series.isSeriesAvailable) {
if(this.series.length === 0) {
this.renderNoDataPointsMessage(); this.renderNoDataPointsMessage();
return; return;
} }
for(const serie of this.series.visibleSeries) {
for(let idx = 0; idx < this.series.length; ++idx) { this._renderMetric(serie);
if(this.series[idx].visible === false) {
continue;
}
// TODO: use _.defaults same as in core
const confidence = this.series[idx].confidence || 0;
const mode = this.series[idx].mode || Mode.STANDARD;
const target = this.series[idx].target;
const renderDots = this.series[idx].renderDots !== undefined ? this.series[idx].renderDots : false;
const renderLines = this.series[idx].renderLines !== undefined ? this.series[idx].renderLines : true;
this._renderMetric(
this.series[idx].datapoints,
{
color: this.getSerieColor(idx),
confidence,
target,
mode,
serieIdx: idx,
renderDots,
renderLines,
}
);
} }
if(this._markersConf !== undefined) {
this._markersLayer = new Markers(this._markersConf, this.state);
this._markersLayer.render(this.metricContainer);
}
this._segmentsLayer = new Segments(this._segmentSeries, this.state);
this._segmentsLayer.render(this.metricContainer, this.chartContainer);
} }
clearAllMetrics(): void { clearAllMetrics(): void {
@ -63,141 +66,69 @@ export class LinePod extends ChartwerkPod<LineTimeSerie, LineOptions> {
initLineGenerator(): void { initLineGenerator(): void {
this.lineGenerator = d3.line() this.lineGenerator = d3.line()
.x(d => this.xScale(d[0])) .x(d => this.state.xScale(d[0]))
.y(d => this.yScale(d[1])); .y(d => this.state.yScale(d[1]));
this.lineGeneratorY1 = d3.line()
.x(d => this.state.xScale(d[0]))
.y(d => this.state.y1Scale(d[1]));
} }
initAreaGenerator(): void { initAreaGenerator(): void {
this.areaGenerator = d3.area() this.areaGenerator = d3.area()
.x(d => this.xScale(d[0])) .x(d => this.state.xScale(d[0]))
.y1(d => this.yScale(d[1])) .y1(d => this.state.yScale(d[1]))
.y0(d => this.height);
this.areaGeneratorY1 = d3.area()
.x(d => this.state.xScale(d[0]))
.y1(d => this.state.y1Scale(d[1]))
.y0(d => this.height); .y0(d => this.height);
} }
getRenderGenerator(serieIdx: number): any { getRenderGenerator(renderArea: boolean, yOrientation: yAxisOrientation): any {
const renderArea = this.series[serieIdx].renderArea;
if(renderArea) { if(renderArea) {
return this.areaGenerator; return yOrientation === yAxisOrientation.LEFT ? this.areaGenerator : this.areaGeneratorY1;
}
return this.lineGenerator;
}
public appendData(data: [number, number][], shouldRerender = true): void {
for(let idx = 0; idx < this.series.length; ++idx) {
if(this.series[idx].visible === false) {
continue;
}
this.series[idx].datapoints.push(data[idx]);
const maxLength = this.series[idx].maxLength;
if(maxLength !== undefined && this.series[idx].datapoints.length > maxLength) {
this.series[idx].datapoints.shift();
}
}
for(let idx = 0; idx < this.series.length; ++idx) {
this.metricContainer.select(`.metric-path-${idx}`)
.datum(this.series[idx].datapoints)
.attr('d', this.getRenderGenerator(idx));
if(this.series[idx].renderDots === true) {
this.metricContainer.selectAll(`.metric-circle-${idx}`)
.data(this.series[idx].datapoints)
.attr('cx', d => this.xScale(d[0]))
.attr('cy', d => this.yScale(d[1]));
this._renderDots([data[idx]], idx);
}
}
if(shouldRerender) {
const rightBorder = _.last(data)[0];
this.state.xValueRange = [this.state.getMinValueX(), rightBorder];
this.renderXAxis();
this.renderYAxis();
this.renderGrid();
} }
return yOrientation === yAxisOrientation.LEFT ? this.lineGenerator : this.areaGeneratorY1;
} }
_renderDots(datapoints: number[][], serieIdx: number): void { _renderDots(serie: LineTimeSerie): void {
const customClass = this.series[serieIdx].class || '';
this.metricContainer.selectAll(null) this.metricContainer.selectAll(null)
.data(datapoints) .data(serie.datapoints)
.enter() .enter()
.append('circle') .append('circle')
.attr('class', `metric-circle-${serieIdx} metric-el ${customClass}`) .attr('class', `metric-circle-${serie.idx} metric-el ${serie.class}`)
.attr('fill', this.getSerieColor(serieIdx)) .attr('fill', serie.color)
.attr('r', METRIC_CIRCLE_RADIUS) .attr('r', METRIC_CIRCLE_RADIUS)
.style('pointer-events', 'none') .style('pointer-events', 'none')
.attr('cx', d => this.xScale(d[0])) .attr('cx', d => this.state.xScale(d[0]))
.attr('cy', d => this.yScale(d[1])); .attr('cy', d => this.state.yScale(d[1]));
} }
_renderLines(datapoints: number[][], serieIdx: number): void { _renderLines(serie: LineTimeSerie): void {
const dashArray = this.series[serieIdx].dashArray !== undefined ? this.series[serieIdx].dashArray : '0'; const fillColor = serie.renderArea ? serie.color : 'none';
const customClass = this.series[serieIdx].class || ''; const fillOpacity = serie.renderArea ? 0.5 : 'none';
const fillColor = this.series[serieIdx].renderArea ? this.getSerieColor(serieIdx) : 'none';
const fillOpacity = this.series[serieIdx].renderArea ? 0.5 : 'none';
this.metricContainer this.metricContainer
.append('path') .append('path')
.datum(datapoints) .datum(serie.datapoints)
.attr('class', `metric-path-${serieIdx} metric-el ${customClass}`) .attr('class', `metric-path-${serie.idx} metric-el ${serie.class}`)
.attr('fill', fillColor) .attr('fill', fillColor)
.attr('fill-opacity', fillOpacity) .attr('fill-opacity', fillOpacity)
.attr('stroke', this.getSerieColor(serieIdx)) .attr('stroke', serie.color)
.attr('stroke-width', 1) .attr('stroke-width', 1)
.attr('stroke-opacity', 0.7) .attr('stroke-opacity', 0.7)
.attr('pointer-events', 'none') .attr('pointer-events', 'none')
.style('stroke-dasharray', dashArray) .style('stroke-dasharray', serie.dashArray)
.attr('d', this.getRenderGenerator(serieIdx)); .attr('d', this.getRenderGenerator(serie.renderArea, serie.yOrientation));
} }
_renderMetric(
datapoints: number[][],
metricOptions: {
color: string,
confidence: number,
target: string,
mode: Mode,
serieIdx: number,
renderDots: boolean,
renderLines: boolean,
}
): void {
if(_.includes(this.seriesTargetsWithBounds, metricOptions.target)) {
return;
}
if(metricOptions.mode === Mode.CHARGE) {
const dataPairs = d3.pairs(datapoints);
this.metricContainer.selectAll(null)
.data(dataPairs)
.enter()
.append('line')
.attr('x1', d => this.xScale(d[0][0]))
.attr('x2', d => this.xScale(d[1][0]))
.attr('y1', d => this.yScale(d[0][1]))
.attr('y2', d => this.yScale(d[1][1]))
.attr('stroke-opacity', 0.7)
.style('stroke-width', 1)
.style('stroke', d => {
if(d[1][0] > d[0][0]) {
return 'green';
} else if (d[1][0] < d[0][0]) {
return 'red';
} else {
return 'gray';
}
});
return;
}
if(metricOptions.renderLines === true) { _renderMetric(serie: LineTimeSerie): void {
this._renderLines(datapoints, metricOptions.serieIdx); if(serie.renderLines === true) {
this._renderLines(serie);
} }
if(metricOptions.renderDots === true) { if(serie.renderDots === true) {
this._renderDots(datapoints, metricOptions.serieIdx); this._renderDots(serie);
} }
} }
@ -209,7 +140,7 @@ export class LinePod extends ChartwerkPod<LineTimeSerie, LineOptions> {
appendCrosshairCircles(): void { appendCrosshairCircles(): void {
// circle for each serie // circle for each serie
this.series.forEach((serie: LineTimeSerie, serieIdx: number) => { this.series.visibleSeries.forEach((serie: LineTimeSerie, serieIdx: number) => {
this.appendCrosshairCircle(serieIdx); this.appendCrosshairCircle(serieIdx);
}); });
} }
@ -222,7 +153,7 @@ export class LinePod extends ChartwerkPod<LineTimeSerie, LineOptions> {
.attr('cx', -CROSSHAIR_BACKGROUND_RAIDUS) .attr('cx', -CROSSHAIR_BACKGROUND_RAIDUS)
.attr('cy', -CROSSHAIR_BACKGROUND_RAIDUS) .attr('cy', -CROSSHAIR_BACKGROUND_RAIDUS)
.attr('clip-path', `url(#${this.rectClipId})`) .attr('clip-path', `url(#${this.rectClipId})`)
.attr('fill', this.getSerieColor(serieIdx)) .attr('fill', this.series.visibleSeries[serieIdx].color)
.style('opacity', CROSSHAIR_BACKGROUND_OPACITY) .style('opacity', CROSSHAIR_BACKGROUND_OPACITY)
.style('pointer-events', 'none'); .style('pointer-events', 'none');
@ -232,33 +163,21 @@ export class LinePod extends ChartwerkPod<LineTimeSerie, LineOptions> {
.attr('cy', -CROSSHAIR_CIRCLE_RADIUS) .attr('cy', -CROSSHAIR_CIRCLE_RADIUS)
.attr('class', `crosshair-circle-${serieIdx}`) .attr('class', `crosshair-circle-${serieIdx}`)
.attr('clip-path', `url(#${this.rectClipId})`) .attr('clip-path', `url(#${this.rectClipId})`)
.attr('fill', this.getSerieColor(serieIdx)) .attr('fill', this.series.visibleSeries[serieIdx].color)
.attr('r', CROSSHAIR_CIRCLE_RADIUS) .attr('r', CROSSHAIR_CIRCLE_RADIUS)
.style('pointer-events', 'none'); .style('pointer-events', 'none');
} }
public renderSharedCrosshair(values: { x?: number, y?: number }): void {
this.onMouseOver(); // TODO: refactor to use it once
const eventX = this.xScale(values.x);
const eventY = this.yScale(values.y);
this.moveCrosshairLine(eventX, eventY);
const datapoints = this.findAndHighlightDatapoints(values.x, values.y);
if(this.options.eventsCallbacks === undefined || this.options.eventsCallbacks.sharedCrosshairMove === undefined) {
return;
}
this.options.eventsCallbacks.sharedCrosshairMove({
datapoints: datapoints,
eventX, eventY
});
}
public hideSharedCrosshair(): void { public hideSharedCrosshair(): void {
this.crosshair.style('display', 'none'); this.crosshair.style('display', 'none');
} }
// TODO: refactor to make xPosition and yPosition optional
// and trough error if they are provided for wrong orientation
moveCrosshairLine(xPosition: number, yPosition: number): void { moveCrosshairLine(xPosition: number, yPosition: number): void {
this.crosshair.style('display', null);
switch(this.options.crosshair.orientation) { switch(this.options.crosshair.orientation) {
case CrosshairOrientation.VERTICAL: case CrosshairOrientation.VERTICAL:
this.crosshair.select('#crosshair-line-x') this.crosshair.select('#crosshair-line-x')
@ -323,7 +242,7 @@ export class LinePod extends ChartwerkPod<LineTimeSerie, LineOptions> {
default: default:
throw new Error(`Unknown type of crosshair orientaion: ${this.options.crosshair.orientation}`); throw new Error(`Unknown type of crosshair orientaion: ${this.options.crosshair.orientation}`);
} }
// TODO: d3.bisect is not the best way. Use binary search // TODO: d3.bisect is not the best way. Use binary search
const bisectIndex = d3.bisector((d: [number, number]) => d[columnIdx]).left; const bisectIndex = d3.bisector((d: [number, number]) => d[columnIdx]).left;
let closestIdx = bisectIndex(datapoints, value); let closestIdx = bisectIndex(datapoints, value);
@ -350,7 +269,7 @@ export class LinePod extends ChartwerkPod<LineTimeSerie, LineOptions> {
// columnIdx: 1 for y, 0 for x // columnIdx: 1 for y, 0 for x
// inverval: x/y value interval between data points // inverval: x/y value interval between data points
// TODO: move it to base/state instead of timeInterval // TODO: move it to base/state instead of timeInterval
const intervals = _.map(this.series, serie => { const intervals = _.map(this.series.visibleSeries, serie => {
if(serie.datapoints.length < 2) { if(serie.datapoints.length < 2) {
return undefined; return undefined;
} }
@ -363,26 +282,17 @@ export class LinePod extends ChartwerkPod<LineTimeSerie, LineOptions> {
return _.max(intervals); return _.max(intervals);
} }
onMouseMove(): void { getMouseObj(): MouseObj {
const eventX = d3.mouse(this.chartContainer.node())[0]; const eventX = d3.mouse(this.chartContainer.node())[0];
const eventY = d3.mouse(this.chartContainer.node())[1]; const eventY = d3.mouse(this.chartContainer.node())[1];
const xValue = this.xScale.invert(eventX); // mouse x position in xScale const xValue = this.state.xScale.invert(eventX); // mouse x position in xScale
const yValue = this.yScale.invert(eventY); const yValue = this.state.yScale.invert(eventY);
// TODO: isOutOfChart is a hack, use clip path correctly
if(this.isOutOfChart() === true) {
this.crosshair.style('display', 'none');
return;
}
this.moveCrosshairLine(eventX, eventY);
const datapoints = this.findAndHighlightDatapoints(xValue, yValue); const datapoints = this.findAndHighlightDatapoints(xValue, yValue);
if(this.options.eventsCallbacks === undefined || this.options.eventsCallbacks.mouseMove === undefined) { // TODO: is shift key pressed
return;
}
// TDOO: is shift key pressed
// TODO: need to refactor this object // TODO: need to refactor this object
this.options.eventsCallbacks.mouseMove({ return {
x: d3.event.pageX, x: d3.event.pageX,
y: d3.event.pageY, y: d3.event.pageY,
xVal: xValue, xVal: xValue,
@ -390,102 +300,115 @@ export class LinePod extends ChartwerkPod<LineTimeSerie, LineOptions> {
series: datapoints, series: datapoints,
chartX: eventX, chartX: eventX,
chartWidth: this.width chartWidth: this.width
};
}
override onMouseMove(): void {
const obj = this.getMouseObj();
const eventX = d3.mouse(this.chartContainer.node())[0];
const eventY = d3.mouse(this.chartContainer.node())[1];
this.moveCrosshairLine(eventX, eventY);
// TODO: is shift key pressed
// TODO: need to refactor this object
this.options.callbackMouseMove(obj);
}
public renderSharedCrosshair(values: { x?: number, y?: number }): void {
this.showCrosshair();
this.moveCrosshairLine(
values.x ? this.state.xScale(values.x) : 0,
values.y ? this.state.yScale(values.y) : 0
);
const datapoints = this.findAndHighlightDatapoints(values.x, values.y);
this.options.callbackSharedCrosshairMove({
datapoints: datapoints,
eventX: values.x ? this.state.xScale(values.x) : 0,
eventY: values.y ? this.state.yScale(values.y) : 0
}); });
} }
override onMouseClick(): void {
this.options.callbackMouseClick(this.getMouseObj());
}
findAndHighlightDatapoints(xValue: number, yValue: number): { value: [number, number], color: string, label: string }[] { findAndHighlightDatapoints(xValue: number, yValue: number): { value: [number, number], color: string, label: string }[] {
if(this.series === undefined || this.series.length === 0) { if(!this.series.isSeriesAvailable) {
return []; return [];
} }
let points = []; // datapoints in each metric that is closest to xValue/yValue position let points = []; // datapoints in each metric that is closest to xValue/yValue position
this.series.forEach((serie: LineTimeSerie, serieIdx: number) => { this.series.visibleSeries.forEach((serie: LineTimeSerie) => {
if(
serie.visible === false ||
_.includes(this.seriesTargetsWithBounds, serie.target)
) {
this.hideCrosshairCircle(serieIdx);
return;
}
const closestDatapoint = this.getClosestDatapoint(serie, xValue, yValue); const closestDatapoint = this.getClosestDatapoint(serie, xValue, yValue);
if(closestDatapoint === undefined || this.isOutOfRange(closestDatapoint, xValue, yValue, serie.useOutOfRange)) { if(_.isNil(closestDatapoint) || _.isNil(closestDatapoint[0])) {
this.hideCrosshairCircle(serieIdx); this.hideCrosshairCircle(serie.idx);
return; } else {
const xPosition = this.state.xScale(closestDatapoint[0]);
let yPosition;
if(serie.yOrientation === yAxisOrientation.RIGHT) {
yPosition = this.state.y1Scale(closestDatapoint[1]);
} else {
yPosition = this.state.yScale(closestDatapoint[1]);
}
this.moveCrosshairCircle(xPosition, yPosition, serie.idx);
} }
const xPosition = this.xScale(closestDatapoint[0]);
const yPosition = this.yScale(closestDatapoint[1]);
this.moveCrosshairCircle(xPosition, yPosition, serieIdx);
points.push({ points.push({
value: closestDatapoint, value: closestDatapoint,
color: this.getSerieColor(serieIdx), color: serie.color,
label: serie.alias || serie.target label: serie.alias || serie.target
}); });
}); });
return points; return points;
} }
isOutOfRange(closestDatapoint: [number, number], xValue: number, yValue: number, useOutOfRange = true): boolean { showCrosshair() {
// find is mouse position more than xRange/yRange from closest point
// TODO: refactor getValueInterval to remove this!
if(useOutOfRange === false) {
return false;
}
let columnIdx; // 1 for y value, 0 for x value
let value; // xValue ot y Value
switch(this.options.crosshair.orientation) {
case CrosshairOrientation.VERTICAL:
columnIdx = 0;
value = xValue;
break;
case CrosshairOrientation.HORIZONTAL:
columnIdx = 1;
value = yValue;
break;
case CrosshairOrientation.BOTH:
// TODO: maybe use voronoi
columnIdx = 1;
value = yValue;
default:
throw new Error(`Unknown type of crosshair orientaion: ${this.options.crosshair.orientation}`);
}
const range = Math.abs(closestDatapoint[columnIdx] - value);
const interval = this.getValueInterval(columnIdx); // interval between points
// do not move crosshair circles, it mouse to far from closest point
return interval === undefined || range > interval / 2;
}
onMouseOver(): void {
this.crosshair.style('display', null); this.crosshair.style('display', null);
this.crosshair.selectAll('.crosshair-circle') this.crosshair.selectAll('.crosshair-circle')
.style('display', null); .style('display', null);
} }
onMouseOut(): void { hideCrosshair() {
if(this.options.eventsCallbacks !== undefined && this.options.eventsCallbacks.mouseOut !== undefined) {
this.options.eventsCallbacks.mouseOut();
}
this.crosshair.style('display', 'none'); this.crosshair.style('display', 'none');
this.crosshair.selectAll('.crosshair-circle')
.style('display', 'none');
}
override onMouseOver(): void {
this.showCrosshair();
this.onMouseMove();
}
override onMouseOut(): void {
this.hideCrosshair();
this.options.callbackMouseOut();
} }
isDoubleClickActive(): boolean { isDoubleClickActive(): boolean {
if(this.options.zoomEvents.mouse.doubleClick === undefined) { return this.options.doubleClickEvent.isActive;
return false; }
}
return this.options.zoomEvents.mouse.doubleClick.isActive; protected onBrushEnd(): void {
super.onBrushEnd();
this.onMouseOver();
} }
// methods below rewrite cores, (move more methods here) updateMarkers(markersConf: MarkersConf): void {
this._markersConf = markersConf;
}
updateSegments(segments: SegmentSerie[]): void {
this._segmentSeries = segments;
}
// methods below rewrite s, (move more methods here)
protected zoomOut(): void { protected zoomOut(): void {
// TODO: test to remove, seems its depricated
if(this.isOutOfChart() === true) {
return;
}
if(d3.event.type === 'dblclick' && !this.isDoubleClickActive()) { if(d3.event.type === 'dblclick' && !this.isDoubleClickActive()) {
return; return;
} }
// TODO: its not clear, why we use this orientation here. Mb its better to use separate option // TODO: its not clear, why we use this orientation here. Mb its better to use separate option
const orientation: BrushOrientation = this.options.zoomEvents.mouse.zoom.orientation; const orientation: BrushOrientation = this.options.mouseZoomEvent.orientation;
const xInterval = this.state.xValueRange[1] - this.state.xValueRange[0]; const xInterval = this.state.xValueRange[1] - this.state.xValueRange[0];
const yInterval = this.state.yValueRange[1] - this.state.yValueRange[0]; const yInterval = this.state.yValueRange[1] - this.state.yValueRange[0];
switch(orientation) { switch(orientation) {
@ -513,19 +436,28 @@ export class LinePod extends ChartwerkPod<LineTimeSerie, LineOptions> {
this.renderYAxis(); this.renderYAxis();
this.renderGrid(); this.renderGrid();
this.onMouseOver(); this.onMouseOver();
if(this.options.eventsCallbacks !== undefined && this.options.eventsCallbacks.zoomOut !== undefined) { let xAxisMiddleValue: number = this.state.xScale.invert(this.width / 2);
let xAxisMiddleValue: number = this.xScale.invert(this.width / 2); let yAxisMiddleValue: number = this.state.yScale.invert(this.height / 2);
let yAxisMiddleValue: number = this.yScale.invert(this.height / 2); const centers = {
const centers = { x: xAxisMiddleValue,
x: xAxisMiddleValue, y: yAxisMiddleValue
y: yAxisMiddleValue }
}
this.options.eventsCallbacks.zoomOut(centers); // TODO: refactor core not to take _options explicitly
if(
this.options._options.events !== undefined &&
this.options._options.events.zoomOut !== undefined
) {
this.options._options.events.zoomOut(
centers,
[this.state.xValueRange, this.state.yValueRange]
);
} }
} }
} }
// TODO: it should be moved to VUE folder
// it is used with Vue.component, e.g.: Vue.component('chartwerk-line-pod', VueChartwerkLinePod) // it is used with Vue.component, e.g.: Vue.component('chartwerk-line-pod', VueChartwerkLinePod)
export const VueChartwerkLinePod = { export const VueChartwerkLinePod = {
// alternative to `template: '<div class="chartwerk-line-pod" :id="id" />'` // alternative to `template: '<div class="chartwerk-line-pod" :id="id" />'`
@ -557,4 +489,4 @@ export const VueChartwerkLinePod = {
} }
}; };
export { LineTimeSerie, LineOptions, Mode, TickOrientation, TimeFormat }; export { LineTimeSerie, LineOptions, TimeFormat, LinePod };

21
src/models/line_series.ts

@ -0,0 +1,21 @@
import { CoreSeries, yAxisOrientation } from '@chartwerk/core';
import { LineTimeSerie } from '../types';
import * as _ from 'lodash';
const LINE_SERIE_DEFAULTS = {
maxLength: undefined,
renderDots: false,
renderLines: true,
dashArray: '0',
class: '',
renderArea: false,
yOrientation: yAxisOrientation.LEFT,
};
export class LineSeries extends CoreSeries<LineTimeSerie> {
constructor(series: LineTimeSerie[]) {
super(series, _.clone(LINE_SERIE_DEFAULTS));
}
}

15
src/models/marker.ts

@ -0,0 +1,15 @@
export type MarkerElem = [number, any?];
export type MarkerSerie = {
color: string;
// TODO: make one-dimensional array with only x
data: MarkerElem[] // [x, payload] payload is any data for tooltip
}
export type MarkersConf = {
series: MarkerSerie[],
events?: {
onMouseMove?: (el: MarkerElem) => void;
onMouseOut?: () => void;
}
}

11
src/models/segment.ts

@ -0,0 +1,11 @@
export type SegmentElement = [number, number, any?];
export type SegmentSerie = {
color: string;
data: SegmentElement[] // [from, to, payload?] payload is any data for tooltip,
select?: boolean,
opacity?: number,
opacitySelect?: number,
onSelect?: (SegmentElement) => void
onUnselect?: (SegmentElement) => void
}

30
src/types.ts

@ -1,19 +1,31 @@
import { TimeSerie, Options } from '@chartwerk/core'; import { Serie, Options } from '@chartwerk/core';
import { AxisRange } from '@chartwerk/core/dist/types';
type LineTimeSerieParams = { type LineTimeSerieParams = {
confidence: number,
mode: Mode,
maxLength: number, maxLength: number,
renderDots: boolean, renderDots: boolean,
renderLines: boolean, // TODO: refactor same as scatter-pod renderLines: boolean, // TODO: refactor same as scatter-pod
useOutOfRange: boolean, // It's temporary hack. Need to refactor getValueInterval() method
dashArray: string; // dasharray attr, only for lines dashArray: string; // dasharray attr, only for lines
class: string; // option to add custom class to each serie element class: string; // option to add custom class to each serie element
renderArea: boolean; // TODO: move to render type renderArea: boolean; // TODO: move to render type
} }
export enum Mode {
STANDARD = 'Standard', export type LineTimeSerie = Serie & Partial<LineTimeSerieParams>;
CHARGE = 'Charge' export type LineOptions = Options & {
events? : {
zoomOut?: (centers: {
x: number;
y: number;
}, range: AxisRange[]) => void;
}
} }
export type LineTimeSerie = TimeSerie & Partial<LineTimeSerieParams>;
export type LineOptions = Options; export type MouseObj = {
x: number,
y: number,
xVal: number,
yVal: number,
series: { value: [number, number], color: string, label: string }[],
chartX: number,
chartWidth: number
}

5
tsconfig.json

@ -17,6 +17,7 @@
"noImplicitUseStrict": false, "noImplicitUseStrict": false,
"noImplicitAny": false, "noImplicitAny": false,
"noUnusedLocals": false, "noUnusedLocals": false,
"baseUrl": "./src" "baseUrl": "./src",
} },
"include": ["src/**/*"]
} }

4878
yarn.lock

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save