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

[APM] Elastic chart issues #84238

Merged
merged 6 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,34 @@
*/

import {
AnnotationDomainTypes,
AreaSeries,
Axis,
Chart,
CurveType,
LegendItemListener,
LineAnnotation,
LineSeries,
niceTimeFormatter,
Placement,
Position,
ScaleType,
Settings,
YDomainRange,
} from '@elastic/charts';
import { EuiIcon } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import moment from 'moment';
import React, { useEffect } from 'react';
import { useHistory } from 'react-router-dom';
import { useChartTheme } from '../../../../../observability/public';
import { asAbsoluteDateTime } from '../../../../common/utils/formatters';
import { TimeSeries } from '../../../../typings/timeseries';
import { FETCH_STATUS } from '../../../hooks/useFetcher';
import { useTheme } from '../../../hooks/useTheme';
import { useUrlParams } from '../../../hooks/useUrlParams';
import { useChartsSync } from '../../../hooks/use_charts_sync';
import { unit } from '../../../style/variables';
import { Annotations } from './annotations';
import { ChartContainer } from './chart_container';
import { onBrushEnd } from './helper/helper';

Expand All @@ -45,6 +51,7 @@ interface Props {
*/
yTickFormat?: (y: number) => string;
showAnnotations?: boolean;
yDomain?: YDomainRange;
}

export function TimeseriesChart({
Expand All @@ -56,12 +63,15 @@ export function TimeseriesChart({
yLabelFormat,
yTickFormat,
showAnnotations = true,
yDomain,
}: Props) {
const history = useHistory();
const chartRef = React.createRef<Chart>();
const { event, setEvent, annotations } = useChartsSync();
const chartTheme = useChartTheme();
const { event, setEvent } = useChartsSync();
const { urlParams } = useUrlParams();
const theme = useTheme();

const { start, end } = urlParams;

useEffect(() => {
Expand All @@ -83,6 +93,8 @@ export function TimeseriesChart({
y === null || y === undefined
);

const annotationColor = theme.eui.euiColorSecondary;

return (
<ChartContainer hasData={!isEmpty} height={height} status={fetchStatus}>
<Chart ref={chartRef} id={id}>
Expand Down Expand Up @@ -112,6 +124,7 @@ export function TimeseriesChart({
tickFormat={xFormatter}
/>
<Axis
domain={yDomain}
id="y-axis"
ticks={3}
position={Position.Left}
Expand All @@ -120,7 +133,24 @@ export function TimeseriesChart({
showGridLines
/>

{showAnnotations && <Annotations />}
{showAnnotations && (
<LineAnnotation
id="annotations"
domainType={AnnotationDomainTypes.XDomain}
dataValues={annotations.map((annotation) => ({
dataValue: annotation['@timestamp'],
header: asAbsoluteDateTime(annotation['@timestamp']),
details: `${i18n.translate('xpack.apm.chart.annotation.version', {
defaultMessage: 'Version',
})} ${annotation.text}`,
}))}
style={{
line: { strokeWidth: 1, stroke: annotationColor, opacity: 1 },
}}
marker={<EuiIcon type="dot" color={annotationColor} />}
markerPosition={Position.Top}
/>
Copy link
Member

@sorenlouv sorenlouv Nov 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for this duplication? Wasn't it better to have it in a separate <Annotations /> component?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't it better to have it in a separate component?

Having it as a separate component is the reason the chart disappears after the annotations is rendered. It is a workaround until this issue gets fixed elastic/elastic-charts#914

)}

{timeseries.map((serie) => {
const Series = serie.type === 'area' ? AreaSeries : LineSeries;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,34 @@
*/

import {
AnnotationDomainTypes,
AreaSeries,
Axis,
Chart,
CurveType,
LineAnnotation,
niceTimeFormatter,
Placement,
Position,
ScaleType,
Settings,
} from '@elastic/charts';
import { EuiIcon } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import moment from 'moment';
import React, { useEffect } from 'react';
import { useHistory } from 'react-router-dom';
import { useChartTheme } from '../../../../../../observability/public';
import { asPercent } from '../../../../../common/utils/formatters';
import {
asAbsoluteDateTime,
asPercent,
} from '../../../../../common/utils/formatters';
import { TimeSeries } from '../../../../../typings/timeseries';
import { FETCH_STATUS } from '../../../../hooks/useFetcher';
import { useTheme } from '../../../../hooks/useTheme';
import { useUrlParams } from '../../../../hooks/useUrlParams';
import { useChartsSync as useChartsSync2 } from '../../../../hooks/use_charts_sync';
sorenlouv marked this conversation as resolved.
Show resolved Hide resolved
import { useChartsSync } from '../../../../hooks/use_charts_sync';
import { unit } from '../../../../style/variables';
import { Annotations } from '../../charts/annotations';
import { ChartContainer } from '../../charts/chart_container';
import { onBrushEnd } from '../../charts/helper/helper';

Expand All @@ -44,9 +51,10 @@ export function TransactionBreakdownChartContents({
}: Props) {
const history = useHistory();
const chartRef = React.createRef<Chart>();
const { event, setEvent, annotations } = useChartsSync();
const chartTheme = useChartTheme();
const { event, setEvent } = useChartsSync2();
const { urlParams } = useUrlParams();
const theme = useTheme();
const { start, end } = urlParams;

useEffect(() => {
Expand All @@ -60,6 +68,8 @@ export function TransactionBreakdownChartContents({

const xFormatter = niceTimeFormatter([min, max]);

const annotationColor = theme.eui.euiColorSecondary;

return (
<ChartContainer height={height} hasData={!!timeseries} status={fetchStatus}>
<Chart ref={chartRef} id="timeSpentBySpan">
Expand Down Expand Up @@ -91,7 +101,24 @@ export function TransactionBreakdownChartContents({
tickFormat={(y: number) => asPercent(y ?? 0, 1)}
/>

{showAnnotations && <Annotations />}
{showAnnotations && (
<LineAnnotation
id="annotations"
domainType={AnnotationDomainTypes.XDomain}
dataValues={annotations.map((annotation) => ({
dataValue: annotation['@timestamp'],
header: asAbsoluteDateTime(annotation['@timestamp']),
details: `${i18n.translate('xpack.apm.chart.annotation.version', {
defaultMessage: 'Version',
})} ${annotation.text}`,
}))}
style={{
line: { strokeWidth: 1, stroke: annotationColor, opacity: 1 },
}}
marker={<EuiIcon type="dot" color={annotationColor} />}
markerPosition={Position.Top}
/>
)}

{timeseries?.length ? (
timeseries.map((serie) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export function TransactionErrorRateChart({
]}
yLabelFormat={yLabelFormat}
yTickFormat={yTickFormat}
yDomain={{ min: 0, max: 1 }}
/>
</EuiPanel>
);
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/apm/public/context/charts_sync_context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import React, {
SetStateAction,
useState,
} from 'react';
import { Annotation } from '../../common/annotations';
import { useAnnotations } from '../hooks/use_annotations';

export const ChartsSyncContext = createContext<{
event: any;
setEvent: Dispatch<SetStateAction<{}>>;
annotations: Annotation[];
} | null>(null);

export function ChartsSyncContextProvider({
Expand All @@ -23,10 +26,11 @@ export function ChartsSyncContextProvider({
children: ReactNode;
}) {
const [event, setEvent] = useState({});
const { annotations } = useAnnotations();
cauemarcondes marked this conversation as resolved.
Show resolved Hide resolved

return (
<ChartsSyncContext.Provider
value={{ event, setEvent }}
value={{ event, setEvent, annotations }}
children={children}
/>
);
Expand Down