@grafana/data#sortThresholds TypeScript Examples

The following examples show how to use @grafana/data#sortThresholds. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SingleStatBaseOptions.ts    From grafana-chinese with Apache License 2.0 6 votes vote down vote up
export function migrateOldThresholds(thresholds?: any[]): Threshold[] | undefined {
  if (!thresholds || !thresholds.length) {
    return undefined;
  }
  const copy = thresholds.map(t => {
    return {
      // Drops 'index'
      value: t.value === null ? -Infinity : t.value,
      color: t.color,
    };
  });
  sortThresholds(copy);
  copy[0].value = -Infinity;
  return copy;
}
Example #2
Source File: ThresholdsEditor.tsx    From grafana-chinese with Apache License 2.0 6 votes vote down vote up
onAddThresholdAfter = (threshold: ThresholdWithKey) => {
    const { steps } = this.state;

    const maxValue = 100;
    const minValue = 0;

    let prev: ThresholdWithKey | undefined = undefined;
    let next: ThresholdWithKey | undefined = undefined;
    for (const t of steps) {
      if (prev && prev.key === threshold.key) {
        next = t;
        break;
      }
      prev = t;
    }

    const prevValue = prev && isFinite(prev.value) ? prev.value : minValue;
    const nextValue = next && isFinite(next.value) ? next.value : maxValue;

    const color = colors.filter(c => !steps.some(t => t.color === c))[1];
    const add = {
      value: prevValue + (nextValue - prevValue) / 2.0,
      color: color,
      key: counter++,
    };
    const newThresholds = [...steps, add];
    sortThresholds(newThresholds);

    this.setState(
      {
        steps: newThresholds,
      },
      () => this.onChange()
    );
  };
Example #3
Source File: ThresholdsEditor.tsx    From grafana-chinese with Apache License 2.0 6 votes vote down vote up
onBlur = () => {
    const steps = [...this.state.steps];
    sortThresholds(steps);
    this.setState(
      {
        steps,
      },
      () => this.onChange()
    );
  };
Example #4
Source File: ThresholdsEditor.tsx    From grafana-chinese with Apache License 2.0 6 votes vote down vote up
onAddThreshold = () => {
    const { steps } = this.state;

    let nextValue = 0;

    if (steps.length > 1) {
      nextValue = steps[steps.length - 1].value + 10;
    }

    const color = colors.filter(c => !steps.some(t => t.color === c))[1];

    const add = {
      value: nextValue,
      color: color,
      key: counter++,
    };

    const newThresholds = [...steps, add];
    sortThresholds(newThresholds);

    this.setState({ steps: newThresholds }, this.onChange);
  };
Example #5
Source File: ThresholdsEditor.tsx    From grafana-chinese with Apache License 2.0 6 votes vote down vote up
onChangeThresholdValue = (event: ChangeEvent<HTMLInputElement>, threshold: ThresholdWithKey) => {
    const cleanValue = event.target.value.replace(/,/g, '.');
    const parsedValue = parseFloat(cleanValue);
    const value = isNaN(parsedValue) ? '' : parsedValue;

    const steps = this.state.steps.map(t => {
      if (t.key === threshold.key) {
        t = { ...t, value: value as number };
      }
      return t;
    });

    if (steps.length) {
      steps[0].value = -Infinity;
    }

    sortThresholds(steps);
    this.setState({ steps });
  };
Example #6
Source File: ThresholdsEditor.tsx    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
onBlur = () => {
    const steps = [...this.state.steps];
    sortThresholds(steps);
    this.setState({ steps }, this.onChange);
  };