mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Canvas: Support dashed connection lines (#84496)
* feat(canvas): add connection types
This commit is contained in:
parent
aa9dc9c821
commit
3c97476390
@ -49,6 +49,7 @@ export interface CanvasConnection {
|
|||||||
path: ConnectionPath;
|
path: ConnectionPath;
|
||||||
color?: ColorDimensionConfig;
|
color?: ColorDimensionConfig;
|
||||||
size?: ScaleDimensionConfig;
|
size?: ScaleDimensionConfig;
|
||||||
|
lineStyle?: string;
|
||||||
vertices?: ConnectionCoordinates[];
|
vertices?: ConnectionCoordinates[];
|
||||||
// See https://github.com/anseki/leader-line#options for more examples of more properties
|
// See https://github.com/anseki/leader-line#options for more examples of more properties
|
||||||
}
|
}
|
||||||
|
@ -134,7 +134,7 @@ export const ConnectionSVG = ({
|
|||||||
const { x1, y1, x2, y2 } = calculateCoordinates(sourceRect, parentRect, info, target, transformScale);
|
const { x1, y1, x2, y2 } = calculateCoordinates(sourceRect, parentRect, info, target, transformScale);
|
||||||
const midpoint = calculateMidpoint(x1, y1, x2, y2);
|
const midpoint = calculateMidpoint(x1, y1, x2, y2);
|
||||||
|
|
||||||
const { strokeColor, strokeWidth } = getConnectionStyles(info, scene, defaultArrowSize);
|
const { strokeColor, strokeWidth, lineStyle } = getConnectionStyles(info, scene, defaultArrowSize);
|
||||||
|
|
||||||
const isSelected = selectedConnection === v && scene.panel.context.instanceState.selectedConnection;
|
const isSelected = selectedConnection === v && scene.panel.context.instanceState.selectedConnection;
|
||||||
|
|
||||||
@ -199,6 +199,7 @@ export const ConnectionSVG = ({
|
|||||||
d={pathString}
|
d={pathString}
|
||||||
stroke={strokeColor}
|
stroke={strokeColor}
|
||||||
strokeWidth={strokeWidth}
|
strokeWidth={strokeWidth}
|
||||||
|
strokeDasharray={lineStyle}
|
||||||
fill={'none'}
|
fill={'none'}
|
||||||
markerEnd={`url(#${CONNECTION_HEAD_ID})`}
|
markerEnd={`url(#${CONNECTION_HEAD_ID})`}
|
||||||
/>
|
/>
|
||||||
@ -261,6 +262,7 @@ export const ConnectionSVG = ({
|
|||||||
stroke={strokeColor}
|
stroke={strokeColor}
|
||||||
pointerEvents="auto"
|
pointerEvents="auto"
|
||||||
strokeWidth={strokeWidth}
|
strokeWidth={strokeWidth}
|
||||||
|
strokeDasharray={lineStyle}
|
||||||
markerEnd={`url(#${CONNECTION_HEAD_ID})`}
|
markerEnd={`url(#${CONNECTION_HEAD_ID})`}
|
||||||
x1={x1}
|
x1={x1}
|
||||||
y1={y1}
|
y1={y1}
|
||||||
|
24
public/app/plugins/panel/canvas/editor/LineStyleEditor.tsx
Normal file
24
public/app/plugins/panel/canvas/editor/LineStyleEditor.tsx
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import React, { useCallback } from 'react';
|
||||||
|
|
||||||
|
import { SelectableValue, StandardEditorProps } from '@grafana/data';
|
||||||
|
import { RadioButtonGroup } from '@grafana/ui/src';
|
||||||
|
|
||||||
|
import { LineStyle } from '../types';
|
||||||
|
|
||||||
|
const options: Array<SelectableValue<LineStyle>> = [
|
||||||
|
{ value: LineStyle.Solid, label: 'Solid' },
|
||||||
|
{ value: LineStyle.Dashed, label: 'Dashed' },
|
||||||
|
];
|
||||||
|
|
||||||
|
export const LineStyleEditor = ({ value, onChange }: StandardEditorProps<string, undefined, undefined>) => {
|
||||||
|
const lineStyle = value ?? LineStyle.Solid;
|
||||||
|
|
||||||
|
const onLineStyleChange = useCallback(
|
||||||
|
(lineStyle: string) => {
|
||||||
|
onChange(lineStyle);
|
||||||
|
},
|
||||||
|
[onChange]
|
||||||
|
);
|
||||||
|
|
||||||
|
return <RadioButtonGroup value={lineStyle} options={options} onChange={onLineStyleChange} fullWidth />;
|
||||||
|
};
|
@ -36,6 +36,7 @@ export function getConnectionEditor(opts: CanvasConnectionEditorOptions): Nested
|
|||||||
const ctx = { ...context, options: opts.connection.info };
|
const ctx = { ...context, options: opts.connection.info };
|
||||||
optionBuilder.addColor(builder, ctx);
|
optionBuilder.addColor(builder, ctx);
|
||||||
optionBuilder.addSize(builder, ctx);
|
optionBuilder.addSize(builder, ctx);
|
||||||
|
optionBuilder.addLineStyle(builder, ctx);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,16 @@ import { CanvasConnection, CanvasElementOptions } from 'app/features/canvas';
|
|||||||
import { ColorDimensionEditor, ResourceDimensionEditor, ScaleDimensionEditor } from 'app/features/dimensions/editors';
|
import { ColorDimensionEditor, ResourceDimensionEditor, ScaleDimensionEditor } from 'app/features/dimensions/editors';
|
||||||
import { BackgroundSizeEditor } from 'app/features/dimensions/editors/BackgroundSizeEditor';
|
import { BackgroundSizeEditor } from 'app/features/dimensions/editors/BackgroundSizeEditor';
|
||||||
|
|
||||||
|
import { LineStyle } from '../types';
|
||||||
|
|
||||||
|
import { LineStyleEditor } from './LineStyleEditor';
|
||||||
|
|
||||||
interface OptionSuppliers {
|
interface OptionSuppliers {
|
||||||
addBackground: PanelOptionsSupplier<CanvasElementOptions>;
|
addBackground: PanelOptionsSupplier<CanvasElementOptions>;
|
||||||
addBorder: PanelOptionsSupplier<CanvasElementOptions>;
|
addBorder: PanelOptionsSupplier<CanvasElementOptions>;
|
||||||
addColor: PanelOptionsSupplier<CanvasConnection>;
|
addColor: PanelOptionsSupplier<CanvasConnection>;
|
||||||
addSize: PanelOptionsSupplier<CanvasConnection>;
|
addSize: PanelOptionsSupplier<CanvasConnection>;
|
||||||
|
addLineStyle: PanelOptionsSupplier<CanvasConnection>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const getCategoryName = (str: string, type: string | undefined) => {
|
const getCategoryName = (str: string, type: string | undefined) => {
|
||||||
@ -120,4 +125,17 @@ export const optionBuilder: OptionSuppliers = {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
addLineStyle: (builder, context) => {
|
||||||
|
const category = ['Line style'];
|
||||||
|
builder.addCustomEditor({
|
||||||
|
category,
|
||||||
|
id: 'lineStyle',
|
||||||
|
path: 'lineStyle',
|
||||||
|
name: 'Line style',
|
||||||
|
editor: LineStyleEditor,
|
||||||
|
settings: {},
|
||||||
|
defaultValue: { value: LineStyle.Solid, label: 'Solid' },
|
||||||
|
});
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
@ -43,3 +43,13 @@ export interface ConnectionState {
|
|||||||
info: CanvasConnection;
|
info: CanvasConnection;
|
||||||
vertices?: ConnectionCoordinates[];
|
vertices?: ConnectionCoordinates[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum LineStyle {
|
||||||
|
Solid = 'solid',
|
||||||
|
Dashed = 'dashed',
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum StrokeDasharray {
|
||||||
|
Solid = '0',
|
||||||
|
Dashed = '8 8',
|
||||||
|
}
|
||||||
|
@ -17,7 +17,7 @@ import { FrameState } from 'app/features/canvas/runtime/frame';
|
|||||||
import { Scene, SelectionParams } from 'app/features/canvas/runtime/scene';
|
import { Scene, SelectionParams } from 'app/features/canvas/runtime/scene';
|
||||||
import { DimensionContext } from 'app/features/dimensions';
|
import { DimensionContext } from 'app/features/dimensions';
|
||||||
|
|
||||||
import { AnchorPoint, ConnectionState } from './types';
|
import { AnchorPoint, ConnectionState, LineStyle, StrokeDasharray } from './types';
|
||||||
|
|
||||||
export function doSelect(scene: Scene, element: ElementState | FrameState) {
|
export function doSelect(scene: Scene, element: ElementState | FrameState) {
|
||||||
try {
|
try {
|
||||||
@ -379,7 +379,8 @@ export const getConnectionStyles = (info: CanvasConnection, scene: Scene, defaul
|
|||||||
const lastRowIndex = getRowIndex(info.size?.field, scene);
|
const lastRowIndex = getRowIndex(info.size?.field, scene);
|
||||||
const strokeColor = info.color ? scene.context.getColor(info.color).value() : defaultArrowColor;
|
const strokeColor = info.color ? scene.context.getColor(info.color).value() : defaultArrowColor;
|
||||||
const strokeWidth = info.size ? scene.context.getScale(info.size).get(lastRowIndex) : defaultArrowSize;
|
const strokeWidth = info.size ? scene.context.getScale(info.size).get(lastRowIndex) : defaultArrowSize;
|
||||||
return { strokeColor, strokeWidth };
|
const lineStyle = info.lineStyle === LineStyle.Dashed ? StrokeDasharray.Dashed : StrokeDasharray.Solid;
|
||||||
|
return { strokeColor, strokeWidth, lineStyle };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getParentBoundingClientRect = (scene: Scene) => {
|
export const getParentBoundingClientRect = (scene: Scene) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user