GraphLegendEditor.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import React from 'react';
  2. import { LegendOptions, PanelOptionsGroup, Switch, Input, StatsPicker } from '@grafana/ui';
  3. export interface GraphLegendEditorLegendOptions extends LegendOptions {
  4. stats?: string[];
  5. decimals?: number;
  6. sortBy?: string;
  7. sortDesc?: boolean;
  8. }
  9. interface GraphLegendEditorProps {
  10. options: GraphLegendEditorLegendOptions;
  11. onChange: (options: GraphLegendEditorLegendOptions) => void;
  12. }
  13. export const GraphLegendEditor: React.FunctionComponent<GraphLegendEditorProps> = props => {
  14. const { options, onChange } = props;
  15. const onStatsChanged = (stats: string[]) => {
  16. onChange({
  17. ...options,
  18. stats,
  19. });
  20. };
  21. const onOptionToggle = (option: keyof LegendOptions) => (event?: React.SyntheticEvent<HTMLInputElement>) => {
  22. const newOption = {};
  23. if (!event) {
  24. return;
  25. }
  26. // TODO: fix the ignores
  27. // @ts-ignore
  28. newOption[option] = event.target.checked;
  29. if (option === 'placement') {
  30. // @ts-ignore
  31. newOption[option] = event.target.checked ? 'right' : 'under';
  32. }
  33. onChange({
  34. ...options,
  35. ...newOption,
  36. });
  37. };
  38. const labelWidth = 8;
  39. return (
  40. <PanelOptionsGroup title="Legend">
  41. <div className="section gf-form-group">
  42. <h4>Options</h4>
  43. <Switch
  44. label="Show legend"
  45. labelClass={`width-${labelWidth}`}
  46. checked={options.isVisible}
  47. onChange={onOptionToggle('isVisible')}
  48. />
  49. <Switch
  50. label="Display as table"
  51. labelClass={`width-${labelWidth}`}
  52. checked={options.asTable}
  53. onChange={onOptionToggle('asTable')}
  54. />
  55. <Switch
  56. label="To the right"
  57. labelClass={`width-${labelWidth}`}
  58. checked={options.placement === 'right'}
  59. onChange={onOptionToggle('placement')}
  60. />
  61. </div>
  62. <div className="section gf-form-group">
  63. <h4>Show</h4>
  64. <div className="gf-form">
  65. <StatsPicker
  66. allowMultiple={true}
  67. stats={options.stats ? options.stats : []}
  68. onChange={onStatsChanged}
  69. placeholder={'Pick Values'}
  70. />
  71. </div>
  72. <div className="gf-form">
  73. <div className="gf-form-label">Decimals</div>
  74. <Input
  75. className="gf-form-input width-5"
  76. type="number"
  77. value={options.decimals}
  78. placeholder="Auto"
  79. onChange={event => {
  80. onChange({
  81. ...options,
  82. decimals: parseInt(event.target.value, 10),
  83. });
  84. }}
  85. />
  86. </div>
  87. </div>
  88. <div className="section gf-form-group">
  89. <h4>Hidden series</h4>
  90. {/* <Switch label="With only nulls" checked={!!options.hideEmpty} onChange={onOptionToggle('hideEmpty')} /> */}
  91. <Switch label="With only zeros" checked={!!options.hideZero} onChange={onOptionToggle('hideZero')} />
  92. </div>
  93. </PanelOptionsGroup>
  94. );
  95. };