@grafana/data#createDimension TypeScript Examples
The following examples show how to use
@grafana/data#createDimension.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Graph.tsx From grafana-chinese with Apache License 2.0 | 5 votes |
renderTooltip = () => {
const { children, series } = this.props;
const { pos, activeItem, isTooltipVisible } = this.state;
let tooltipElement: React.ReactElement<TooltipProps> | null = null;
if (!isTooltipVisible || !pos || series.length === 0) {
return null;
}
// Find children that indicate tooltip to be rendered
React.Children.forEach(children, c => {
// We have already found tooltip
if (tooltipElement) {
return;
}
// @ts-ignore
const childType = c && c.type && (c.type.displayName || c.type.name);
if (childType === Tooltip.displayName) {
tooltipElement = c as React.ReactElement<TooltipProps>;
}
});
// If no tooltip provided, skip rendering
if (!tooltipElement) {
return null;
}
const tooltipElementProps = (tooltipElement as React.ReactElement<TooltipProps>).props;
const tooltipMode = tooltipElementProps.mode || 'single';
// If mode is single series and user is not hovering over item, skip rendering
if (!activeItem && tooltipMode === 'single') {
return null;
}
// Check if tooltip needs to be rendered with custom tooltip component, otherwise default to GraphTooltip
const tooltipContentRenderer = tooltipElementProps.tooltipComponent || GraphTooltip;
// Indicates column(field) index in y-axis dimension
const seriesIndex = activeItem ? activeItem.series.seriesIndex : 0;
// Indicates row index in active field values
const rowIndex = activeItem ? activeItem.dataIndex : undefined;
const activeDimensions: ActiveDimensions<GraphDimensions> = {
// Described x-axis active item
// When hovering over an item - let's take it's dataIndex, otherwise undefined
// Tooltip itself needs to figure out correct datapoint display information based on pos passed to it
xAxis: [seriesIndex, rowIndex],
// Describes y-axis active item
yAxis: activeItem ? [activeItem.series.seriesIndex, activeItem.dataIndex] : null,
};
const tooltipContentProps: TooltipContentProps<GraphDimensions> = {
dimensions: {
// time/value dimension columns are index-aligned - see getGraphSeriesModel
xAxis: createDimension(
'xAxis',
series.map(s => s.timeField)
),
yAxis: createDimension(
'yAxis',
series.map(s => s.valueField)
),
},
activeDimensions,
pos,
mode: tooltipElementProps.mode || 'single',
};
const tooltipContent = React.createElement(tooltipContentRenderer, { ...tooltipContentProps });
return React.cloneElement<TooltipProps>(tooltipElement as React.ReactElement<TooltipProps>, {
content: tooltipContent,
position: { x: pos.pageX, y: pos.pageY },
offset: { x: 10, y: 10 },
});
};
Example #2
Source File: Graph.tsx From grafana-chinese with Apache License 2.0 | 5 votes |
renderContextMenu = () => {
const { series } = this.props;
const { contextPos, contextItem, isContextVisible } = this.state;
if (!isContextVisible || !contextPos || !contextItem || series.length === 0) {
return null;
}
// Indicates column(field) index in y-axis dimension
const seriesIndex = contextItem ? contextItem.series.seriesIndex : 0;
// Indicates row index in context field values
const rowIndex = contextItem ? contextItem.dataIndex : undefined;
const contextDimensions: ContextDimensions<GraphDimensions> = {
// Described x-axis context item
xAxis: [seriesIndex, rowIndex],
// Describes y-axis context item
yAxis: contextItem ? [contextItem.series.seriesIndex, contextItem.dataIndex] : null,
};
const dimensions: GraphDimensions = {
// time/value dimension columns are index-aligned - see getGraphSeriesModel
xAxis: createDimension(
'xAxis',
series.map(s => s.timeField)
),
yAxis: createDimension(
'yAxis',
series.map(s => s.valueField)
),
};
const formatDate = (date: DateTimeInput, format?: string) => {
return dateTime(date)?.format(format);
};
const closeContext = () => this.setState({ isContextVisible: false });
const getContextMenuSource = () => {
return {
datapoint: contextItem.datapoint,
dataIndex: contextItem.dataIndex,
series: contextItem.series,
seriesIndex: contextItem.series.seriesIndex,
pageX: contextPos.pageX,
pageY: contextPos.pageY,
};
};
const contextContentProps: GraphContextMenuProps = {
x: contextPos.pageX,
y: contextPos.pageY,
onClose: closeContext,
getContextMenuSource: getContextMenuSource,
formatSourceDate: formatDate,
dimensions,
contextDimensions,
};
return <GraphContextMenu {...contextContentProps} />;
};
Example #3
Source File: MultiModeGraphTooltip.test.tsx From grafana-chinese with Apache License 2.0 | 4 votes |
describe('MultiModeGraphTooltip', () => {
describe('when shown when hovering over a datapoint', () => {
beforeEach(() => {
dimensions = {
xAxis: createDimension('xAxis', [
{
config: {},
values: new ArrayVector([0, 100, 200]),
name: 'A-series time',
type: FieldType.time,
},
{
config: {},
values: new ArrayVector([0, 100, 200]),
name: 'B-series time',
type: FieldType.time,
},
]),
yAxis: createDimension('yAxis', [
{
config: {},
values: new ArrayVector([10, 20, 10]),
name: 'A-series values',
type: FieldType.number,
},
{
config: {},
values: new ArrayVector([20, 30, 40]),
name: 'B-series values',
type: FieldType.number,
},
]),
};
});
it('highlights series of the datapoint', () => {
// We are simulating hover over A-series, middle point
const activeDimensions: ActiveDimensions<GraphDimensions> = {
xAxis: [0, 1], // column, row
yAxis: [0, 1], // column, row
};
const container = mount(
<MultiModeGraphTooltip
dimensions={dimensions}
activeDimensions={activeDimensions}
// pos is not relevant in this test
pos={{ x: 0, y: 0, pageX: 0, pageY: 0, x1: 0, y1: 0 }}
/>
);
// We rendered two series rows
const rows = container.find('SeriesTableRow');
// We expect A-series(1st row) to be higlighted
expect(rows.get(0).props.isActive).toBeTruthy();
// We expect B-series(2nd row) not to be higlighted
expect(rows.get(1).props.isActive).toBeFalsy();
});
it("doesn't highlight series when not hovering over datapoint", () => {
// We are simulating hover over graph, but not datapoint
const activeDimensions: ActiveDimensions<GraphDimensions> = {
xAxis: [0, undefined], // no active point in time
yAxis: null, // no active series
};
const container = mount(
<MultiModeGraphTooltip
dimensions={dimensions}
activeDimensions={activeDimensions}
// pos is not relevant in this test
pos={{ x: 0, y: 0, pageX: 0, pageY: 0, x1: 0, y1: 0 }}
/>
);
// We rendered two series rows
const rows = container.find('SeriesTableRow');
// We expect A-series(1st row) not to be higlighted
expect(rows.get(0).props.isActive).toBeFalsy();
// We expect B-series(2nd row) not to be higlighted
expect(rows.get(1).props.isActive).toBeFalsy();
});
});
});