mirror of
https://github.com/grafana/grafana.git
synced 2025-02-20 11:48:34 -06:00
* graph: initial histogram support #600 * graph histogram mode: add Bars number option * graph histogram mode: fix X axis ticks calculation * graph histogram mode: change bar style (align and width) * refactor(graph): move histogram functions into separate module * graph histogram mode: rename series to "count" * graph histogram mode: fix errors if no data * refactor(graph and heatmap): move shared code into app/core * graph: add tests for histogram mode
49 lines
1.1 KiB
TypeScript
49 lines
1.1 KiB
TypeScript
import _ from 'lodash';
|
|
|
|
/**
|
|
* Convert series into array of series values.
|
|
* @param data Array of series
|
|
*/
|
|
export function getSeriesValues(data: any): number[] {
|
|
let values = [];
|
|
|
|
// Count histogam stats
|
|
for (let i = 0; i < data.length; i++) {
|
|
let series = data[i];
|
|
for (let j = 0; j < series.data.length; j++) {
|
|
if (series.data[j][1] !== null) {
|
|
values.push(series.data[j][1]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return values;
|
|
}
|
|
|
|
/**
|
|
* Convert array of values into timeseries-like histogram:
|
|
* [[val_1, count_1], [val_2, count_2], ..., [val_n, count_n]]
|
|
* @param values
|
|
* @param bucketSize
|
|
*/
|
|
export function convertValuesToHistogram(values: number[], bucketSize: number): any[] {
|
|
let histogram = {};
|
|
|
|
for (let i = 0; i < values.length; i++) {
|
|
let bound = getBucketBound(values[i], bucketSize);
|
|
if (histogram[bound]) {
|
|
histogram[bound] = histogram[bound] + 1;
|
|
} else {
|
|
histogram[bound] = 1;
|
|
}
|
|
}
|
|
|
|
return _.map(histogram, (count, bound) => {
|
|
return [Number(bound), count];
|
|
});
|
|
}
|
|
|
|
function getBucketBound(value: number, bucketSize: number): number {
|
|
return Math.floor(value / bucketSize) * bucketSize;
|
|
}
|