module.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import {
  2. Field,
  3. FieldType,
  4. getFieldDisplayName,
  5. PanelOptionsEditorBuilder,
  6. PanelPlugin,
  7. SelectableValue,
  8. } from '@grafana/data';
  9. import { config } from '@grafana/runtime';
  10. import { GraphFieldConfig } from '@grafana/schema';
  11. import { commonOptionsBuilder } from '@grafana/ui';
  12. import { defaultGraphConfig, getGraphFieldConfig } from '../timeseries/config';
  13. import { CandlestickPanel } from './CandlestickPanel';
  14. import { CandlestickData, candlestickFieldsInfo, FieldPickerInfo, prepareCandlestickFields } from './fields';
  15. import {
  16. defaultColors,
  17. CandlestickOptions,
  18. VizDisplayMode,
  19. ColorStrategy,
  20. defaultPanelOptions,
  21. CandleStyle,
  22. } from './models.gen';
  23. import { CandlestickSuggestionsSupplier } from './suggestions';
  24. const modeOptions = [
  25. { label: 'Candles', value: VizDisplayMode.Candles },
  26. { label: 'Volume', value: VizDisplayMode.Volume },
  27. { label: 'Both', value: VizDisplayMode.CandlesVolume },
  28. ] as Array<SelectableValue<VizDisplayMode>>;
  29. const candleStyles = [
  30. { label: 'Candles', value: CandleStyle.Candles },
  31. { label: 'OHLC Bars', value: CandleStyle.OHLCBars },
  32. ] as Array<SelectableValue<CandleStyle>>;
  33. const colorStrategies = [
  34. { label: 'Since Open', value: ColorStrategy.OpenClose },
  35. { label: 'Since Prior Close', value: ColorStrategy.CloseClose },
  36. ] as Array<SelectableValue<ColorStrategy>>;
  37. const numericFieldFilter = (f: Field) => f.type === FieldType.number;
  38. function addFieldPicker(
  39. builder: PanelOptionsEditorBuilder<CandlestickOptions>,
  40. info: FieldPickerInfo,
  41. data: CandlestickData | null
  42. ) {
  43. let placeholderText = 'Auto ';
  44. if (data) {
  45. const current = data[info.key] as Field;
  46. if (current?.config) {
  47. placeholderText += '= ' + getFieldDisplayName(current);
  48. if (current === data?.open && info.key !== 'open') {
  49. placeholderText += ` (${info.defaults.join(',')})`;
  50. }
  51. } else {
  52. placeholderText += `(${info.defaults.join(',')})`;
  53. }
  54. }
  55. builder.addFieldNamePicker({
  56. path: `fields.${info.key}`,
  57. name: info.name,
  58. description: info.description,
  59. settings: {
  60. filter: numericFieldFilter,
  61. placeholderText,
  62. },
  63. });
  64. }
  65. export const plugin = new PanelPlugin<CandlestickOptions, GraphFieldConfig>(CandlestickPanel)
  66. .useFieldConfig(getGraphFieldConfig(defaultGraphConfig))
  67. .setPanelOptions((builder, context) => {
  68. const opts = context.options ?? defaultPanelOptions;
  69. const info = prepareCandlestickFields(context.data, opts, config.theme2);
  70. builder
  71. .addRadio({
  72. path: 'mode',
  73. name: 'Mode',
  74. description: '',
  75. defaultValue: defaultPanelOptions.mode,
  76. settings: {
  77. options: modeOptions,
  78. },
  79. })
  80. .addRadio({
  81. path: 'candleStyle',
  82. name: 'Candle style',
  83. description: '',
  84. defaultValue: defaultPanelOptions.candleStyle,
  85. settings: {
  86. options: candleStyles,
  87. },
  88. showIf: (opts) => opts.mode !== VizDisplayMode.Volume,
  89. })
  90. .addRadio({
  91. path: 'colorStrategy',
  92. name: 'Color strategy',
  93. description: '',
  94. defaultValue: defaultPanelOptions.colorStrategy,
  95. settings: {
  96. options: colorStrategies,
  97. },
  98. })
  99. .addColorPicker({
  100. path: 'colors.up',
  101. name: 'Up color',
  102. defaultValue: defaultColors.up,
  103. })
  104. .addColorPicker({
  105. path: 'colors.down',
  106. name: 'Down color',
  107. defaultValue: defaultColors.down,
  108. });
  109. addFieldPicker(builder, candlestickFieldsInfo.open, info);
  110. if (opts.mode !== VizDisplayMode.Volume) {
  111. addFieldPicker(builder, candlestickFieldsInfo.high, info);
  112. addFieldPicker(builder, candlestickFieldsInfo.low, info);
  113. }
  114. addFieldPicker(builder, candlestickFieldsInfo.close, info);
  115. if (opts.mode !== VizDisplayMode.Candles) {
  116. addFieldPicker(builder, candlestickFieldsInfo.volume, info);
  117. }
  118. builder.addRadio({
  119. path: 'includeAllFields',
  120. name: 'Additional fields',
  121. description: 'Use standard timeseries options to configure any fields not mapped above',
  122. defaultValue: defaultPanelOptions.includeAllFields,
  123. settings: {
  124. options: [
  125. { label: 'Ignore', value: false },
  126. { label: 'Include', value: true },
  127. ],
  128. },
  129. });
  130. // commonOptionsBuilder.addTooltipOptions(builder);
  131. commonOptionsBuilder.addLegendOptions(builder);
  132. })
  133. .setDataSupport({ annotations: true, alertStates: true })
  134. .setSuggestionsSupplier(new CandlestickSuggestionsSupplier());