StatMigrations.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { FieldColorModeId, FieldConfigSource, PanelModel } from '@grafana/data';
  2. import { sharedSingleStatPanelChangedHandler, BigValueGraphMode, BigValueColorMode } from '@grafana/ui';
  3. import { BigValueTextMode } from '@grafana/ui/src/components/BigValue/BigValue';
  4. import { StatPanelOptions } from './types';
  5. // This is called when the panel changes from another panel
  6. export const statPanelChangedHandler = (
  7. panel: PanelModel<Partial<StatPanelOptions>> | any,
  8. prevPluginId: string,
  9. prevOptions: any
  10. ) => {
  11. // This handles most config changes
  12. const options = sharedSingleStatPanelChangedHandler(panel, prevPluginId, prevOptions) as StatPanelOptions;
  13. // Changing from angular singlestat
  14. if (prevOptions.angular && (prevPluginId === 'singlestat' || prevPluginId === 'grafana-singlestat-panel')) {
  15. const oldOptions = prevOptions.angular;
  16. options.graphMode = BigValueGraphMode.None;
  17. if (oldOptions.sparkline && oldOptions.sparkline.show) {
  18. options.graphMode = BigValueGraphMode.Area;
  19. }
  20. if (oldOptions.colorBackground) {
  21. options.colorMode = BigValueColorMode.Background;
  22. } else if (oldOptions.colorValue) {
  23. options.colorMode = BigValueColorMode.Value;
  24. } else {
  25. options.colorMode = BigValueColorMode.None;
  26. if (oldOptions.sparkline?.lineColor && options.graphMode === BigValueGraphMode.Area) {
  27. const cfg: FieldConfigSource = panel.fieldConfig ?? {};
  28. cfg.defaults.color = {
  29. mode: FieldColorModeId.Fixed,
  30. fixedColor: oldOptions.sparkline.lineColor,
  31. };
  32. panel.fieldConfig = cfg;
  33. }
  34. }
  35. if (oldOptions.valueName === 'name') {
  36. options.textMode = BigValueTextMode.Name;
  37. }
  38. }
  39. return options;
  40. };