Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event handler on Timeseries #366

Merged
merged 5 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-components/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.8.47",
"version": "0.8.48",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion packages/react-components/src/ECharts/BaseChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useEffect, useState } from 'react';
import { StandardChartProps } from './types';

const BaseChart = (props: StandardChartProps) => {
const { className, options, chartHeightFunc, getRefFunc, debug } = props;
const { className, options, chartHeightFunc, getRefFunc, debug, onEvents } = props;
const [chartSize, setChartSize] = useState({ width: '100%', height: chartHeightFunc() });

useEffect(() => {
Expand Down Expand Up @@ -33,6 +33,7 @@ const BaseChart = (props: StandardChartProps) => {
}}
style={{ width: chartSize.width, height: chartSize.height }}
option={options}
onEvents={onEvents}
{...props}
/>
</div>
Expand Down
7 changes: 7 additions & 0 deletions packages/react-components/src/ECharts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,11 @@ export interface StandardChartProps {
* Display loading on data change
*/
showLoading?: boolean;
/**
* Trigger Event on ReactEChart
*/
onEvents?: {
click: (params: any) => void;
mousemove?: (params: any) => void;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { DateFilter } from '../../common/DateFilter/DateFilter';
import { DateProps } from '../../common/types';
import { useWindowSize } from '@react-hook/window-size';
import mikeColors from '../../ThemeProvider/mikeColors';
import copy from 'copy-to-clipboard';

const NAME_TEXT_STYLE = {
padding: 12,
Expand All @@ -23,12 +24,6 @@ const DATA_ZOOM = [
{
type: 'inside',
},
{
type: 'slider',
height: 40,
bottom: 10,
labelFormatter: (value) => (value ? format(value, 'dd MMM yyyy') : ''),
},
];

const GRID = {
Expand All @@ -52,10 +47,13 @@ const AXIS_LABEL = {

const X_AXIS = {
name: 'Time',
type: 'time',
nameLocation: 'center',
nameTextStyle: NAME_TEXT_STYLE,
min: 'dataMin',
max: 'dataMax',
axisLabel: AXIS_LABEL,
triggerEvent: true
};

const Y_AXIS = {
Expand All @@ -65,21 +63,9 @@ const Y_AXIS = {
axisLabel: AXIS_LABEL,
};

const LEGEND_STYLE = {
backgroundColor: 'rgba(255,255,255,1)',
padding: 8,
borderRadius: 5,
shadowColor: 'rgba(0, 0, 0, 0.25)',
shadowBlur: 5,
shadowOffsetX: 3,
shadowOffsetY: 3,
};

const TimeseriesExplorer = ({
dataSource,
title,
legendPosition = 'right',
legendPositionOffset,
startTimeUtc,
dateTimeFormat,
timeZone,
Expand All @@ -97,23 +83,33 @@ const TimeseriesExplorer = ({
const [ids, setIds] = useState<string[]>([]);

const [options, setOptions] = useState({
title: {
text: title,
left: 'center',
textStyle: TEXT_STYLE,
toolbox: {
feature: {
dataZoom: {
},
restore: {},
saveAsImage: {}
}
},
grid: GRID,
dataZoom: DATA_ZOOM,
legend: {
...LEGEND_STYLE,
top: legendPosition === 'left' ? 90 : 35,
left: legendPosition === 'left' ? legendPositionOffset ?? 150 : undefined,
right: legendPosition === 'right' ? legendPositionOffset ?? 60 : undefined,
align: legendPosition,
orient: 'vertical',
bottom: 0
},
xAxis: X_AXIS,
yAxis: Y_AXIS,
tooltip: {},
tooltip: {
trigger: 'axis',
formatter: function (params) {
let result = '';
params.forEach((param) => {
result += `Date: ${new Date(param.data.value[0]).toISOString()} <br/>`;
result += `Value: ${param.data.value[1]} <br/><br/>`;
});
return result;
}
},
series: [],
});

Expand Down Expand Up @@ -178,12 +174,19 @@ const TimeseriesExplorer = ({

fetchTimeseriesValues([dataSource], dataSource.token).subscribe((res) => {
const series = res
.map((item) => {
.map((item, index) => {
return {
name: item.id,
data: item.data.map((d) => [new Date(d[0]).getTime(), d[1]]),
data: item.data.map((d, idx) => {
return {
value: [new Date(d[0]).getTime(), d[1]],
name: `Point ${index * item.data.length + idx + 1}`
};
}),
symbolSize: 3,
type: 'line',
symbol: 'diamond',
triggerLineEvent: true,
};
})
.filter((item) => item);
Expand All @@ -194,7 +197,16 @@ const TimeseriesExplorer = ({
...options.xAxis,
axisLabel: {
...options.xAxis.axisLabel,
formatter: (value) => format(value, 'dd MMM yyyy'),
formatter: {
year: '{yyyy}',
month: '{MMM}',
day: '{d}',
hour: '{HH}:{mm}',
minute: '{HH}:{mm}',
second: '{HH}:{mm}:{ss}',
millisecond: '{hh}:{mm}:{ss} {SSS}',
none: '{yyyy}-{MM}-{dd} {hh}:{mm}:{ss} {SSS}'
},
},
},
yAxis: {
Expand Down Expand Up @@ -239,7 +251,20 @@ const TimeseriesExplorer = ({
onSetDate={(date) => setDate(date)}
onClearDateFilter={clearDateFilter}
>
<BaseChart className="standard_chat" chartHeightFunc={() => height - 100} options={options} notMerge />
<BaseChart
className="standard_chat"
chartHeightFunc={() => height - 100}
options={options}
onEvents={{
click: (params) => {
const { seriesName } = params
if (seriesName) {
copy(seriesName);
}
},
}}
notMerge
/>
</DateFilter>
) : (
<>
Expand Down