config.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import {
  2. FieldColorModeId,
  3. FieldConfigProperty,
  4. FieldType,
  5. identityOverrideProcessor,
  6. SetFieldConfigOptionsArgs,
  7. stringOverrideProcessor,
  8. } from '@grafana/data';
  9. import {
  10. BarAlignment,
  11. GraphDrawStyle,
  12. GraphFieldConfig,
  13. GraphGradientMode,
  14. LineInterpolation,
  15. LineStyle,
  16. VisibilityMode,
  17. StackingMode,
  18. GraphTresholdsStyleMode,
  19. GraphTransform,
  20. } from '@grafana/schema';
  21. import { graphFieldOptions, commonOptionsBuilder } from '@grafana/ui';
  22. import { FillBellowToEditor } from './FillBelowToEditor';
  23. import { LineStyleEditor } from './LineStyleEditor';
  24. import { SpanNullsEditor } from './SpanNullsEditor';
  25. import { ThresholdsStyleEditor } from './ThresholdsStyleEditor';
  26. export const defaultGraphConfig: GraphFieldConfig = {
  27. drawStyle: GraphDrawStyle.Line,
  28. lineInterpolation: LineInterpolation.Linear,
  29. lineWidth: 1,
  30. fillOpacity: 0,
  31. gradientMode: GraphGradientMode.None,
  32. barAlignment: BarAlignment.Center,
  33. stacking: {
  34. mode: StackingMode.None,
  35. group: 'A',
  36. },
  37. axisGridShow: true,
  38. };
  39. const categoryStyles = ['Graph styles'];
  40. export function getGraphFieldConfig(cfg: GraphFieldConfig): SetFieldConfigOptionsArgs<GraphFieldConfig> {
  41. return {
  42. standardOptions: {
  43. [FieldConfigProperty.Color]: {
  44. settings: {
  45. byValueSupport: true,
  46. bySeriesSupport: true,
  47. preferThresholdsMode: false,
  48. },
  49. defaultValue: {
  50. mode: FieldColorModeId.PaletteClassic,
  51. },
  52. },
  53. },
  54. useCustomConfig: (builder) => {
  55. builder
  56. .addRadio({
  57. path: 'drawStyle',
  58. name: 'Style',
  59. category: categoryStyles,
  60. defaultValue: cfg.drawStyle,
  61. settings: {
  62. options: graphFieldOptions.drawStyle,
  63. },
  64. })
  65. .addRadio({
  66. path: 'lineInterpolation',
  67. name: 'Line interpolation',
  68. category: categoryStyles,
  69. defaultValue: cfg.lineInterpolation,
  70. settings: {
  71. options: graphFieldOptions.lineInterpolation,
  72. },
  73. showIf: (c) => c.drawStyle === GraphDrawStyle.Line,
  74. })
  75. .addRadio({
  76. path: 'barAlignment',
  77. name: 'Bar alignment',
  78. category: categoryStyles,
  79. defaultValue: cfg.barAlignment,
  80. settings: {
  81. options: graphFieldOptions.barAlignment,
  82. },
  83. showIf: (c) => c.drawStyle === GraphDrawStyle.Bars,
  84. })
  85. .addSliderInput({
  86. path: 'lineWidth',
  87. name: 'Line width',
  88. category: categoryStyles,
  89. defaultValue: cfg.lineWidth,
  90. settings: {
  91. min: 0,
  92. max: 10,
  93. step: 1,
  94. ariaLabelForHandle: 'Line width',
  95. },
  96. showIf: (c) => c.drawStyle !== GraphDrawStyle.Points,
  97. })
  98. .addSliderInput({
  99. path: 'fillOpacity',
  100. name: 'Fill opacity',
  101. category: categoryStyles,
  102. defaultValue: cfg.fillOpacity,
  103. settings: {
  104. min: 0,
  105. max: 100,
  106. step: 1,
  107. ariaLabelForHandle: 'Fill opacity',
  108. },
  109. showIf: (c) => c.drawStyle !== GraphDrawStyle.Points,
  110. })
  111. .addRadio({
  112. path: 'gradientMode',
  113. name: 'Gradient mode',
  114. category: categoryStyles,
  115. defaultValue: graphFieldOptions.fillGradient[0].value,
  116. settings: {
  117. options: graphFieldOptions.fillGradient,
  118. },
  119. showIf: (c) => c.drawStyle !== GraphDrawStyle.Points,
  120. })
  121. .addCustomEditor({
  122. id: 'fillBelowTo',
  123. path: 'fillBelowTo',
  124. name: 'Fill below to',
  125. category: categoryStyles,
  126. editor: FillBellowToEditor,
  127. override: FillBellowToEditor,
  128. process: stringOverrideProcessor,
  129. hideFromDefaults: true,
  130. shouldApply: (f) => true,
  131. })
  132. .addCustomEditor<void, LineStyle>({
  133. id: 'lineStyle',
  134. path: 'lineStyle',
  135. name: 'Line style',
  136. category: categoryStyles,
  137. showIf: (c) => c.drawStyle === GraphDrawStyle.Line,
  138. editor: LineStyleEditor,
  139. override: LineStyleEditor,
  140. process: identityOverrideProcessor,
  141. shouldApply: (f) => f.type === FieldType.number,
  142. })
  143. .addCustomEditor<void, boolean>({
  144. id: 'spanNulls',
  145. path: 'spanNulls',
  146. name: 'Connect null values',
  147. category: categoryStyles,
  148. defaultValue: false,
  149. editor: SpanNullsEditor,
  150. override: SpanNullsEditor,
  151. showIf: (c) => c.drawStyle === GraphDrawStyle.Line,
  152. shouldApply: (f) => f.type !== FieldType.time,
  153. process: identityOverrideProcessor,
  154. })
  155. .addRadio({
  156. path: 'showPoints',
  157. name: 'Show points',
  158. category: categoryStyles,
  159. defaultValue: graphFieldOptions.showPoints[0].value,
  160. settings: {
  161. options: graphFieldOptions.showPoints,
  162. },
  163. showIf: (c) => c.drawStyle !== GraphDrawStyle.Points,
  164. })
  165. .addSliderInput({
  166. path: 'pointSize',
  167. name: 'Point size',
  168. category: categoryStyles,
  169. defaultValue: 5,
  170. settings: {
  171. min: 1,
  172. max: 40,
  173. step: 1,
  174. ariaLabelForHandle: 'Point size',
  175. },
  176. showIf: (c) => c.showPoints !== VisibilityMode.Never || c.drawStyle === GraphDrawStyle.Points,
  177. });
  178. commonOptionsBuilder.addStackingConfig(builder, cfg.stacking, categoryStyles);
  179. builder.addSelect({
  180. category: categoryStyles,
  181. name: 'Transform',
  182. path: 'transform',
  183. settings: {
  184. options: [
  185. {
  186. label: 'Constant',
  187. value: GraphTransform.Constant,
  188. description: 'The first value will be shown as a constant line',
  189. },
  190. {
  191. label: 'Negative Y',
  192. value: GraphTransform.NegativeY,
  193. description: 'Flip the results to negative values on the y axis',
  194. },
  195. ],
  196. isClearable: true,
  197. },
  198. hideFromDefaults: true,
  199. });
  200. commonOptionsBuilder.addAxisConfig(builder, cfg);
  201. commonOptionsBuilder.addHideFrom(builder);
  202. builder.addCustomEditor({
  203. id: 'thresholdsStyle',
  204. path: 'thresholdsStyle',
  205. name: 'Show thresholds',
  206. category: ['Thresholds'],
  207. defaultValue: { mode: GraphTresholdsStyleMode.Off },
  208. settings: {
  209. options: graphFieldOptions.thresholdsDisplayModes,
  210. },
  211. editor: ThresholdsStyleEditor,
  212. override: ThresholdsStyleEditor,
  213. process: identityOverrideProcessor,
  214. shouldApply: () => true,
  215. });
  216. },
  217. };
  218. }