Skip to content

Commit

Permalink
fixing charts
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes committed Nov 24, 2020
1 parent 423888c commit c069d57
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,33 @@
*/

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 { 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';
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 @@ -38,8 +45,9 @@ interface Props {
export function TransactionBreakdownGraph({ fetchStatus, timeseries }: Props) {
const history = useHistory();
const chartRef = React.createRef<Chart>();
const { event, setEvent } = useChartsSync2();
const { event, setEvent, annotations } = useChartsSync();
const { urlParams } = useUrlParams();
const theme = useTheme();
const { start, end } = urlParams;

useEffect(() => {
Expand All @@ -53,6 +61,8 @@ export function TransactionBreakdownGraph({ fetchStatus, timeseries }: Props) {

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

const annotationColor = theme.eui.euiColorSecondary;

return (
<ChartContainer
height={XY_HEIGHT}
Expand Down Expand Up @@ -87,7 +97,22 @@ export function TransactionBreakdownGraph({ fetchStatus, timeseries }: Props) {
tickFormat={(y: number) => asPercent(y ?? 0, 1)}
/>

<Annotations />
<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

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,
SettingsSpec,
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 { 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,11 +63,14 @@ export function TimeseriesChart({
yLabelFormat,
yTickFormat,
showAnnotations = true,
yDomain,
}: Props) {
const history = useHistory();
const chartRef = React.createRef<Chart>();
const { event, setEvent } = useChartsSync();
const { event, setEvent, annotations } = useChartsSync();
const { urlParams } = useUrlParams();
const theme = useTheme();

const { start, end } = urlParams;

useEffect(() => {
Expand Down Expand Up @@ -89,6 +99,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 @@ -118,6 +130,7 @@ export function TimeseriesChart({
tickFormat={xFormatter}
/>
<Axis
domain={yDomain}
id="y-axis"
ticks={3}
position={Position.Left}
Expand All @@ -126,7 +139,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}
/>
)}

{timeseries.map((serie) => {
const Series = serie.type === 'area' ? AreaSeries : LineSeries;
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();

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

0 comments on commit c069d57

Please sign in to comment.