GraphLegendEditor.tsx 3.1 KB

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