123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import {
- Field,
- FieldType,
- getFieldDisplayName,
- PanelOptionsEditorBuilder,
- PanelPlugin,
- SelectableValue,
- } from '@grafana/data';
- import { config } from '@grafana/runtime';
- import { GraphFieldConfig } from '@grafana/schema';
- import { commonOptionsBuilder } from '@grafana/ui';
- import { defaultGraphConfig, getGraphFieldConfig } from '../timeseries/config';
- import { CandlestickPanel } from './CandlestickPanel';
- import { CandlestickData, candlestickFieldsInfo, FieldPickerInfo, prepareCandlestickFields } from './fields';
- import {
- defaultColors,
- CandlestickOptions,
- VizDisplayMode,
- ColorStrategy,
- defaultPanelOptions,
- CandleStyle,
- } from './models.gen';
- import { CandlestickSuggestionsSupplier } from './suggestions';
- const modeOptions = [
- { label: 'Candles', value: VizDisplayMode.Candles },
- { label: 'Volume', value: VizDisplayMode.Volume },
- { label: 'Both', value: VizDisplayMode.CandlesVolume },
- ] as Array<SelectableValue<VizDisplayMode>>;
- const candleStyles = [
- { label: 'Candles', value: CandleStyle.Candles },
- { label: 'OHLC Bars', value: CandleStyle.OHLCBars },
- ] as Array<SelectableValue<CandleStyle>>;
- const colorStrategies = [
- { label: 'Since Open', value: ColorStrategy.OpenClose },
- { label: 'Since Prior Close', value: ColorStrategy.CloseClose },
- ] as Array<SelectableValue<ColorStrategy>>;
- const numericFieldFilter = (f: Field) => f.type === FieldType.number;
- function addFieldPicker(
- builder: PanelOptionsEditorBuilder<CandlestickOptions>,
- info: FieldPickerInfo,
- data: CandlestickData | null
- ) {
- let placeholderText = 'Auto ';
- if (data) {
- const current = data[info.key] as Field;
- if (current?.config) {
- placeholderText += '= ' + getFieldDisplayName(current);
- if (current === data?.open && info.key !== 'open') {
- placeholderText += ` (${info.defaults.join(',')})`;
- }
- } else {
- placeholderText += `(${info.defaults.join(',')})`;
- }
- }
- builder.addFieldNamePicker({
- path: `fields.${info.key}`,
- name: info.name,
- description: info.description,
- settings: {
- filter: numericFieldFilter,
- placeholderText,
- },
- });
- }
- export const plugin = new PanelPlugin<CandlestickOptions, GraphFieldConfig>(CandlestickPanel)
- .useFieldConfig(getGraphFieldConfig(defaultGraphConfig))
- .setPanelOptions((builder, context) => {
- const opts = context.options ?? defaultPanelOptions;
- const info = prepareCandlestickFields(context.data, opts, config.theme2);
- builder
- .addRadio({
- path: 'mode',
- name: 'Mode',
- description: '',
- defaultValue: defaultPanelOptions.mode,
- settings: {
- options: modeOptions,
- },
- })
- .addRadio({
- path: 'candleStyle',
- name: 'Candle style',
- description: '',
- defaultValue: defaultPanelOptions.candleStyle,
- settings: {
- options: candleStyles,
- },
- showIf: (opts) => opts.mode !== VizDisplayMode.Volume,
- })
- .addRadio({
- path: 'colorStrategy',
- name: 'Color strategy',
- description: '',
- defaultValue: defaultPanelOptions.colorStrategy,
- settings: {
- options: colorStrategies,
- },
- })
- .addColorPicker({
- path: 'colors.up',
- name: 'Up color',
- defaultValue: defaultColors.up,
- })
- .addColorPicker({
- path: 'colors.down',
- name: 'Down color',
- defaultValue: defaultColors.down,
- });
- addFieldPicker(builder, candlestickFieldsInfo.open, info);
- if (opts.mode !== VizDisplayMode.Volume) {
- addFieldPicker(builder, candlestickFieldsInfo.high, info);
- addFieldPicker(builder, candlestickFieldsInfo.low, info);
- }
- addFieldPicker(builder, candlestickFieldsInfo.close, info);
- if (opts.mode !== VizDisplayMode.Candles) {
- addFieldPicker(builder, candlestickFieldsInfo.volume, info);
- }
- builder.addRadio({
- path: 'includeAllFields',
- name: 'Additional fields',
- description: 'Use standard timeseries options to configure any fields not mapped above',
- defaultValue: defaultPanelOptions.includeAllFields,
- settings: {
- options: [
- { label: 'Ignore', value: false },
- { label: 'Include', value: true },
- ],
- },
- });
- // commonOptionsBuilder.addTooltipOptions(builder);
- commonOptionsBuilder.addLegendOptions(builder);
- })
- .setDataSupport({ annotations: true, alertStates: true })
- .setSuggestionsSupplier(new CandlestickSuggestionsSupplier());
|