import $ from 'jquery'; import { escapeHTML } from '../utils/html'; import { Metric } from '../types/types'; import { GraphProps, GraphSeries } from './Graph'; export const formatValue = (y: number | null): string => { if (y === null) { return 'null'; } const absY = Math.abs(y); if (absY >= 1e24) { return (y / 1e24).toFixed(2) + 'Y'; } else if (absY >= 1e21) { return (y / 1e21).toFixed(2) + 'Z'; } else if (absY >= 1e18) { return (y / 1e18).toFixed(2) + 'E'; } else if (absY >= 1e15) { return (y / 1e15).toFixed(2) + 'P'; } else if (absY >= 1e12) { return (y / 1e12).toFixed(2) + 'T'; } else if (absY >= 1e9) { return (y / 1e9).toFixed(2) + 'G'; } else if (absY >= 1e6) { return (y / 1e6).toFixed(2) + 'M'; } else if (absY >= 1e3) { return (y / 1e3).toFixed(2) + 'k'; } else if (absY >= 1) { return y.toFixed(2); } else if (absY === 0) { return y.toFixed(2); } else if (absY < 1e-23) { return (y / 1e-24).toFixed(2) + 'y'; } else if (absY < 1e-20) { return (y / 1e-21).toFixed(2) + 'z'; } else if (absY < 1e-17) { return (y / 1e-18).toFixed(2) + 'a'; } else if (absY < 1e-14) { return (y / 1e-15).toFixed(2) + 'f'; } else if (absY < 1e-11) { return (y / 1e-12).toFixed(2) + 'p'; } else if (absY < 1e-8) { return (y / 1e-9).toFixed(2) + 'n'; } else if (absY < 1e-5) { return (y / 1e-6).toFixed(2) + 'ยต'; } else if (absY < 1e-2) { return (y / 1e-3).toFixed(2) + 'm'; } else if (absY <= 1) { return y.toFixed(2); } throw Error("couldn't format a value, this is a bug"); }; export const getHoverColor = (color: string, opacity: number, stacked: boolean) => { const { r, g, b } = $.color.parse(color); if (!stacked) { return `rgba(${r}, ${g}, ${b}, ${opacity})`; } /* Unfortunately flot doesn't take into consideration the alpha value when adjusting the color on the stacked series. TODO: find better way to set the opacity. */ const base = (1 - opacity) * 255; return `rgb(${Math.round(base + opacity * r)},${Math.round(base + opacity * g)},${Math.round(base + opacity * b)})`; }; export const toHoverColor = (index: number, stacked: boolean) => (series: GraphSeries, i: number) => ({ ...series, color: getHoverColor(series.color, i !== index ? 0.3 : 1, stacked), }); export const getOptions = (stacked: boolean): jquery.flot.plotOptions => { return { grid: { hoverable: true, clickable: true, autoHighlight: true, mouseActiveRadius: 100, }, legend: { show: false, }, xaxis: { mode: 'time', showTicks: true, showMinorTicks: true, timeBase: 'milliseconds', }, yaxis: { tickFormatter: formatValue, }, crosshair: { mode: 'xy', color: '#bbb', }, tooltip: { show: true, cssClass: 'graph-tooltip', content: (_, xval, yval, { series }): string => { const { labels, color } = series; return `