-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-component): adding drag and delete to TC
- Loading branch information
Showing
14 changed files
with
325 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/react-components/src/components/chart/Chart.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react'; | ||
|
||
import { mockTimeSeriesDataQuery } from '@iot-app-kit/testing-util'; | ||
import { DataStream } from '@iot-app-kit/core'; | ||
import Chart from './index'; | ||
import { render } from '@testing-library/react'; | ||
|
||
const VIEWPORT = { duration: '5m' }; | ||
|
||
const DATA_STREAM: DataStream = { id: 'abc-1', data: [], resolution: 0, name: 'my-name' }; | ||
jest.mock('echarts', () => ({ | ||
use: jest.fn(), | ||
init: jest.fn(), | ||
getInstanceByDom: jest.fn(), | ||
registerTheme: jest.fn(), | ||
})); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('Chart Component Testing', () => { | ||
it('Chart renders', async () => { | ||
const query = mockTimeSeriesDataQuery([ | ||
{ | ||
dataStreams: [DATA_STREAM], | ||
viewport: VIEWPORT, | ||
thresholds: [], | ||
}, | ||
]); | ||
|
||
const element = render(<Chart queries={[query]} viewport={VIEWPORT} size={{ width: 500, height: 500 }} />); | ||
expect(element).not.toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 23 additions & 17 deletions
40
packages/react-components/src/components/chart/useTrendCursors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,51 @@ | ||
import React, { Dispatch, SetStateAction, useEffect } from 'react'; | ||
import { EChartsType, getInstanceByDom, GraphicComponentOption } from 'echarts'; | ||
import { EChartsType, getInstanceByDom } from 'echarts'; | ||
import { addNewTrendCursor } from './utils/getInfo'; | ||
import { Viewport } from '@iot-app-kit/core'; | ||
import { SizeConfig } from './types'; | ||
import { InternalGraphicComponentGroupOption, SizeConfig } from './types'; | ||
import { MAX_TREND_CURSORS } from './eChartsConstants'; | ||
|
||
const useTrendCursors = ( | ||
ref: React.RefObject<HTMLDivElement>, | ||
graphic: GraphicComponentOption[], | ||
graphic: InternalGraphicComponentGroupOption[], | ||
size: SizeConfig, | ||
isInCursorAddMode: boolean, | ||
setGraphic: Dispatch<SetStateAction<GraphicComponentOption[]>>, | ||
viewport?: Viewport | ||
setGraphic: Dispatch<SetStateAction<InternalGraphicComponentGroupOption[]>>, | ||
viewport?: Viewport, | ||
theme?: string | ||
) => { | ||
const mouseoverHandler = (isInCursorAddMode: boolean, chart?: EChartsType) => { | ||
if (isInCursorAddMode) { | ||
chart?.getZr().setCursorStyle('crosshair'); | ||
} else { | ||
chart?.getZr().setCursorStyle('auto'); | ||
} | ||
}; | ||
useEffect(() => { | ||
let chart: EChartsType | undefined; | ||
if (ref.current !== null) { | ||
chart = getInstanceByDom(ref.current); | ||
|
||
chart?.getZr().on('click', (e) => { | ||
if (isInCursorAddMode) { | ||
setGraphic([...graphic, addNewTrendCursor(e, size, viewport)]); | ||
if (isInCursorAddMode && graphic.length < MAX_TREND_CURSORS) { | ||
setGraphic(addNewTrendCursor(e, size, graphic.length, graphic, setGraphic, viewport, chart)); | ||
} | ||
}); | ||
|
||
chart?.getZr().on('mousemove', () => { | ||
if (isInCursorAddMode) { | ||
chart?.getZr().setCursorStyle('crosshair'); | ||
} else { | ||
chart?.getZr().setCursorStyle('auto'); | ||
} | ||
}); | ||
chart?.getZr().on('mousemove', () => mouseoverHandler(isInCursorAddMode, chart)); | ||
} | ||
|
||
return () => { | ||
if (ref.current !== null) { | ||
chart?.getZr().off('click'); | ||
// intentionally not removing the "moverover" event as shown below, this will remove the default show tooltip behaviour | ||
// chart?.getZr().off('mouseover'); | ||
chart?.getZr().off('mouseover', () => mouseoverHandler(isInCursorAddMode, chart)); | ||
} | ||
}; | ||
}, [ref, graphic, size, isInCursorAddMode]); | ||
}, [ref, graphic, size, isInCursorAddMode, setGraphic, viewport, theme]); | ||
|
||
useEffect(() => { | ||
console.log(graphic); | ||
}, [graphic]); | ||
}; | ||
|
||
export default useTrendCursors; |
62 changes: 62 additions & 0 deletions
62
packages/react-components/src/components/chart/utils/getInfo.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { describe, expect } from '@jest/globals'; | ||
import { addNewTrendCursor, setXWithBounds } from './getInfo'; | ||
import { ElementEvent } from 'echarts'; | ||
|
||
describe('Testing Charts getInfo', () => { | ||
const mockSize = { width: 500, height: 500 }; | ||
|
||
describe('addNewTrendCursor', () => { | ||
it('should add a new TC', () => { | ||
const mockEvent = {} as ElementEvent; | ||
const mockSetGraphic = () => jest.fn(); | ||
const mockViewport = { duration: '5m' }; | ||
const newTrendCursor = addNewTrendCursor(mockEvent, mockSize, 0, [], mockSetGraphic, mockViewport); | ||
|
||
expect(newTrendCursor).not.toBeNull(); | ||
expect(newTrendCursor[0].children.length).toBe(3); | ||
}); | ||
|
||
it('on drag should should the TC x co-ordinate', () => { | ||
const mockEvent = {} as ElementEvent; | ||
const mockSetGraphic = () => jest.fn(); | ||
const mockViewport = { duration: '5m' }; | ||
const newTrendCursor = addNewTrendCursor(mockEvent, mockSize, 0, [], mockSetGraphic, mockViewport); | ||
|
||
if (newTrendCursor[0]!.children[0]!.ondrag) { | ||
newTrendCursor[0]!.children[0]!.ondrag({ | ||
target: { id: newTrendCursor[0].children[0].id }, | ||
offsetX: 100, | ||
} as never); | ||
expect(newTrendCursor[0].children[1].x).toBe(100); | ||
} | ||
}); | ||
|
||
it('on delete should should the TC x co-ordinate', () => { | ||
const mockEvent = {} as ElementEvent; | ||
const mockSetGraphic = () => jest.fn(); | ||
const mockViewport = { duration: '5m' }; | ||
const newTrendCursor = addNewTrendCursor(mockEvent, mockSize, 0, [], mockSetGraphic, mockViewport); | ||
|
||
if (newTrendCursor[0]!.children[2]!.onmousedown) { | ||
newTrendCursor[0]!.children[2]!.onmousedown({ | ||
target: { id: newTrendCursor[0].children[2].id }, | ||
} as never); | ||
expect(newTrendCursor.length).toBe(0); | ||
} | ||
}); | ||
}); | ||
|
||
describe('setXWithBounds', () => { | ||
it('should return max of width minus margin', () => { | ||
const maxX = setXWithBounds(mockSize, 475); | ||
|
||
expect(maxX).toBe(450); | ||
}); | ||
|
||
it('should return min of margin', () => { | ||
const maxX = setXWithBounds(mockSize, 20); | ||
|
||
expect(maxX).toBe(50); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.