@nova-ui/dashboards
Version:
Nova Dashboards is a framework designed to provide feature developers with a common solution for presenting data coming from various sources within a single view, as well as a set of predefined widget visualizations that are 100% configuration-driven and
182 lines • 7.76 kB
JavaScript
;
// © 2022 SolarWinds Worldwide, LLC. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimeseriesScalesService = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@angular/core");
const moment_1 = require("moment/moment");
const bits_1 = require("@nova-ui/bits");
const charts_1 = require("@nova-ui/charts");
const types_1 = require("./types");
const public_api_1 = require("../../common/pipes/public-api");
const round_array_values_1 = require("../../functions/round-array-values");
const timeseries_datetime_formatter_1 = require("../../functions/timeseries-datetime-formatter");
/**
* This service handles scale creation and configuration for the timeseries widget
*/
let TimeseriesScalesService = class TimeseriesScalesService {
constructor(unitConversionService) {
this.unitConversionService = unitConversionService;
this.unitConversionPipe = new public_api_1.DashboardUnitConversionPipe(this.unitConversionService);
}
/**
* Creates a scale based on given configuration
*
* @param scaleConfig
*/
getScale(scaleConfig, units, widgetConfig) {
let scale;
switch (scaleConfig.type) {
case types_1.TimeseriesScaleType.Time: {
scale = new charts_1.TimeScale();
scale.isTimeseriesScale = true;
scale.formatters.title = timeseries_datetime_formatter_1.timeSeriesDatetimeFormatter;
break;
}
case types_1.TimeseriesScaleType.Linear: {
scale = new charts_1.LinearScale();
scale.isTimeseriesScale = true;
scale.formatters.tick = (value, isLabelFormatter) => this.unitConversionPipe.transform(value, scaleConfig.properties?.axisUnits ?? units, bits_1.UnitBase.Standard);
break;
}
case types_1.TimeseriesScaleType.TimeInterval: {
scale = new charts_1.TimeIntervalScale((0, moment_1.duration)(1, "hour"));
break;
}
}
this.updateConfiguration(scale, scaleConfig, widgetConfig);
return scale;
}
/**
* Currently only TimeIntervalScale has configuration
*
* @param scale
* @param scaleConfig
*/
updateConfiguration(scale, scaleConfig, widgetConfig) {
switch (scaleConfig.type) {
case types_1.TimeseriesScaleType.Time: {
const interval = scaleConfig.properties?.timeInterval;
if (interval?.startDatetime && interval?.endDatetime) {
scale.fixDomain([
interval.startDatetime,
interval.endDatetime,
]);
}
break;
}
case types_1.TimeseriesScaleType.TimeInterval: {
const interval = scaleConfig.properties?.interval;
if (typeof interval === "number") {
if (interval <= 0) {
throw new Error("Interval value must be greater than zero.");
}
if (scale instanceof charts_1.TimeIntervalScale) {
scale.interval((0, moment_1.duration)(interval, "seconds"));
}
}
break;
}
case types_1.TimeseriesScaleType.Linear: {
if (scaleConfig.properties?.axisUnits) {
scale.scaleUnits = scaleConfig.properties.axisUnits;
}
if (widgetConfig?.preset === types_1.TimeseriesChartPreset.StatusBar) {
return;
}
if (scaleConfig.properties?.axisUnits === "percent" &&
scale.setFixDomainValues) {
scale.setFixDomainValues([0, 25, 50, 75, 100]);
}
if (scaleConfig.properties?.axisUnits !== "percent" &&
scaleConfig.properties?.domain &&
scale.setFixDomainValues) {
let domainAdjusted;
switch (widgetConfig?.preset) {
case types_1.TimeseriesChartPreset.StackedBar:
domainAdjusted = this.getStackedBarScaleDomain(scaleConfig.properties.domain);
break;
case types_1.TimeseriesChartPreset.StackedArea:
domainAdjusted = this.getStackedAreaScaleDomain(scaleConfig.properties.domain.max);
break;
default:
domainAdjusted = this.getLineScaleDomain(scaleConfig.properties.domain);
}
scale.setFixDomainValues(domainAdjusted);
}
break;
}
}
}
getStackedBarScaleDomain({ min, max, }) {
if (max === 0 || max % 4 > 0) {
max = max + 4 - (max % 4);
}
const increment = Math.floor((max - min) / 4);
return [
min,
min + increment,
min + 2 * increment,
max - increment,
max,
];
}
getLineScaleDomain({ min, max, }) {
if (min > max) {
const tmp = min;
min = max;
max = tmp;
}
const extentRange = Math.abs((max - min) / ((max + min) / 2));
// for small domain ranges increase domain so that spikes are not exaggerated
if (extentRange < 0.5) {
min = min - Math.abs(min) * 0.5;
max = max + Math.abs(max) * 0.5;
}
// handles zero case
if (min === 0 && max === 0) {
min = -1;
max = 1;
}
// special case for real small number since not using si prefix anymore
if (Math.abs(max - min) < 0.04) {
max += 0.04;
}
const point = (max - min) / 4;
return (0, round_array_values_1.roundToOptimalDecimals)([
min,
min + point,
min + 2 * point,
max - point,
max,
]);
}
getStackedAreaScaleDomain(max) {
const point = max / 4;
return [0, point, point * 2, max - point, max];
}
};
exports.TimeseriesScalesService = TimeseriesScalesService;
exports.TimeseriesScalesService = TimeseriesScalesService = tslib_1.__decorate([
(0, core_1.Injectable)(),
tslib_1.__metadata("design:paramtypes", [bits_1.UnitConversionService])
], TimeseriesScalesService);
//# sourceMappingURL=timeseries-scales.service.js.map