/** (C) 2023 - ntop.org */ import Dygraph from 'dygraphs'; import colorsInterpolation from "../colors/colors-interpolation"; import formatterUtils from "../formatter-utils"; /* ***************************************** */ /* This function return the default config for dygraph charts */ function getDefaultConfig() { return { labelsSeparateLines: true, legend: "follow", connectSeparatedPoints: false, includeZero: true, drawPoints: true, highlightSeriesBackgroundAlpha: 0.7, highlightSeriesOpts: { strokeWidth: 2, pointSize: 3, highlightCircleSize: 6, }, axisLabelFontSize: 12, axes: { x: { axisLabelWidth: 90 } }, }; } /* ***************************************** */ /* This function put the correct formatters in the configuration */ function changeFormatters(config, options) { if (options?.formatters?.length > 1) { /* Multiple formatters */ /* NOTE: at most 2 formatters can be used */ config.axes.y1 = getAxisConfiguration(formatterUtils.getFormatter(options.formatters[0])); config.axes.y2 = getAxisConfiguration(formatterUtils.getFormatter(options.formatters[1])); } else if (options?.formatters?.length == 1) { /* Single formatter */ config.axes.y = getAxisConfiguration(formatterUtils.getFormatter(options.formatters[0])); } } /* ***************************************** */ /* This function return the color of the serie when highlighted */ function getHighlightColor() { const is_dark_mode = document.getElementsByClassName('body dark').length > 0; let highlight_color = 'rgb(255, 255, 255)'; if (is_dark_mode) { highlight_color = 'rgb(13, 17, 23)'; } return highlight_color; } /* ***************************************** */ /* This function is used to format the value on the legend */ function getAxisConfiguration(formatter) { return { axisLabelFormatter: formatter, valueFormatter: function (num_or_millis, opts, seriesName, dygraph, row, col) { const serie_point = dygraph?.rawData_?.[row][col]; let data = ''; if (typeof (serie_point) == "object") { /* This is the case for the serie with bounds */ serie_point.forEach((el) => { data = `${data} / ${formatter(el || 0)}`; }) data = data.substring(3); /* Remove the first three characters ' / ' */ } else { /* This is the standard case */ data = formatter(num_or_millis); } return (data); }, axisLabelWidth: 80, } } /* ***************************************** */ /* This function is used to format the value on the legend */ function getDefaultLegendFormatter(options) { return function (data) { if (!data.x) return ""; // no hover const timeBadge = `