exploreGraphStyleUtils.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import produce from 'immer';
  2. import { FieldConfigSource } from '@grafana/data';
  3. import { GraphDrawStyle, GraphFieldConfig, StackingMode } from '@grafana/schema';
  4. import { ExploreGraphStyle } from '../../types';
  5. export type FieldConfig = FieldConfigSource<GraphFieldConfig>;
  6. export function applyGraphStyle(config: FieldConfig, style: ExploreGraphStyle): FieldConfig {
  7. return produce(config, (draft) => {
  8. if (draft.defaults.custom === undefined) {
  9. draft.defaults.custom = {};
  10. }
  11. const { custom } = draft.defaults;
  12. if (custom.stacking === undefined) {
  13. custom.stacking = { group: 'A' };
  14. }
  15. switch (style) {
  16. case 'lines':
  17. custom.drawStyle = GraphDrawStyle.Line;
  18. custom.stacking.mode = StackingMode.None;
  19. custom.fillOpacity = 0;
  20. break;
  21. case 'bars':
  22. custom.drawStyle = GraphDrawStyle.Bars;
  23. custom.stacking.mode = StackingMode.None;
  24. custom.fillOpacity = 100;
  25. break;
  26. case 'points':
  27. custom.drawStyle = GraphDrawStyle.Points;
  28. custom.stacking.mode = StackingMode.None;
  29. custom.fillOpacity = 0;
  30. break;
  31. case 'stacked_lines':
  32. custom.drawStyle = GraphDrawStyle.Line;
  33. custom.stacking.mode = StackingMode.Normal;
  34. custom.fillOpacity = 100;
  35. break;
  36. case 'stacked_bars':
  37. custom.drawStyle = GraphDrawStyle.Bars;
  38. custom.stacking.mode = StackingMode.Normal;
  39. custom.fillOpacity = 100;
  40. break;
  41. default: {
  42. // should never happen
  43. // NOTE: casting to `never` will cause typescript
  44. // to verify that the switch statement checks every possible
  45. // enum-value
  46. const invalidValue: never = style;
  47. throw new Error(`Invalid graph-style: ${invalidValue}`);
  48. }
  49. }
  50. });
  51. }