Commit 5324bb4f by Ryan McKinley Committed by Torkel Ödegaard

Refactor: Rename TimeSeriesVM to GraphSeriesXY (#16216)

* Rename TimeSeriesVM to Trace

* remove TimeSeriesVM from types

* rename to GraphSeriesVM

* rename again

* now GraphSeriesXY
parent 04b3afcd
......@@ -3,11 +3,11 @@ import $ from 'jquery';
import React, { PureComponent } from 'react';
// Types
import { TimeRange, TimeSeriesVMs } from '../../types';
import { TimeRange, GraphSeriesXY } from '../../types';
interface GraphProps {
timeSeries: TimeSeriesVMs;
timeRange: TimeRange;
series: GraphSeriesXY[];
timeRange: TimeRange; // NOTE: we should aim to make `time` a property of the axis, not force it for all graphs
showLines?: boolean;
showPoints?: boolean;
showBars?: boolean;
......@@ -37,7 +37,7 @@ export class Graph extends PureComponent<GraphProps> {
return;
}
const { width, timeSeries, timeRange, showLines, showBars, showPoints } = this.props;
const { width, series, timeRange, showLines, showBars, showPoints } = this.props;
if (!width) {
return;
......@@ -95,9 +95,9 @@ export class Graph extends PureComponent<GraphProps> {
try {
console.log('Graph render');
$.plot(this.element, timeSeries, flotOptions);
$.plot(this.element, series, flotOptions);
} catch (err) {
console.log('Graph rendering error', err, flotOptions, timeSeries);
console.log('Graph rendering error', err, flotOptions, series);
throw new Error('Error rendering panel');
}
}
......
......@@ -53,24 +53,12 @@ export interface TimeSeries {
unit?: string;
}
/** View model projection of a time series */
export interface TimeSeriesVM {
label: string;
color: string;
data: TimeSeriesValue[][];
allIsNull: boolean;
allIsZero: boolean;
}
export enum NullValueMode {
Null = 'null',
Ignore = 'connected',
AsZero = 'null as zero',
}
/** View model projection of many time series */
export type TimeSeriesVMs = TimeSeriesVM[];
export interface AnnotationEvent {
annotation?: any;
dashboardId?: number;
......
import { DisplayValue } from './displayValue';
export type GraphSeriesValue = number | null;
/** View model projection of a series */
export interface GraphSeriesXY {
label: string;
color: string;
data: GraphSeriesValue[][]; // [x,y][]
info?: DisplayValue[]; // Legend info
}
......@@ -4,6 +4,7 @@ export * from './panel';
export * from './plugin';
export * from './datasource';
export * from './theme';
export * from './graph';
export * from './threshold';
export * from './input';
export * from './displayValue';
import { getFlotPairs } from './flotPairs';
describe('getFlotPairs', () => {
const table = {
const series = {
fields: [],
rows: [[1, 100, 'a'], [2, 200, 'b'], [3, 300, 'c']],
};
it('should get X and y', () => {
const pairs = getFlotPairs({ rows: table.rows, xIndex: 0, yIndex: 1 });
const pairs = getFlotPairs({ series, xIndex: 0, yIndex: 1 });
expect(pairs.length).toEqual(3);
expect(pairs[0].length).toEqual(2);
......@@ -14,7 +15,7 @@ describe('getFlotPairs', () => {
});
it('should work with strings', () => {
const pairs = getFlotPairs({ rows: table.rows, xIndex: 0, yIndex: 2 });
const pairs = getFlotPairs({ series, xIndex: 0, yIndex: 2 });
expect(pairs.length).toEqual(3);
expect(pairs[0].length).toEqual(2);
......
// Types
import { NullValueMode } from '../types/index';
import { NullValueMode, GraphSeriesValue, SeriesData } from '../types/index';
export interface FloatPairsOptions {
rows: any[][];
export interface FlotPairsOptions {
series: SeriesData;
xIndex: number;
yIndex: number;
nullValueMode?: NullValueMode;
}
export function getFlotPairs({ rows, xIndex, yIndex, nullValueMode }: FloatPairsOptions): any[][] {
export function getFlotPairs({ series, xIndex, yIndex, nullValueMode }: FlotPairsOptions): GraphSeriesValue[][] {
const rows = series.rows;
const ignoreNulls = nullValueMode === NullValueMode.Ignore;
const nullAsZero = nullValueMode === NullValueMode.AsZero;
......
......@@ -2,7 +2,7 @@
import _ from 'lodash';
import React, { PureComponent } from 'react';
import { Graph, PanelProps, NullValueMode, colors, TimeSeriesVMs, FieldType, getFirstTimeField } from '@grafana/ui';
import { Graph, PanelProps, NullValueMode, colors, GraphSeriesXY, FieldType, getFirstTimeField } from '@grafana/ui';
import { Options } from './types';
import { getFlotPairs } from '@grafana/ui/src/utils/flotPairs';
......@@ -13,42 +13,40 @@ export class GraphPanel extends PureComponent<Props> {
const { data, timeRange, width, height } = this.props;
const { showLines, showBars, showPoints } = this.props.options;
const vmSeries: TimeSeriesVMs = [];
for (const table of data) {
const timeColumn = getFirstTimeField(table);
const graphs: GraphSeriesXY[] = [];
for (const series of data) {
const timeColumn = getFirstTimeField(series);
if (timeColumn < 0) {
continue;
}
for (let i = 0; i < table.fields.length; i++) {
const column = table.fields[i];
for (let i = 0; i < series.fields.length; i++) {
const field = series.fields[i];
// Show all numeric columns
if (column.type === FieldType.number) {
if (field.type === FieldType.number) {
// Use external calculator just to make sure it works :)
const points = getFlotPairs({
rows: table.rows,
series,
xIndex: timeColumn,
yIndex: i,
nullValueMode: NullValueMode.Null,
});
vmSeries.push({
label: column.name,
data: points,
color: colors[vmSeries.length % colors.length],
// TODO (calculate somewhere)
allIsNull: false,
allIsZero: false,
});
if (points.length > 0) {
graphs.push({
label: field.name,
data: points,
color: colors[graphs.length % colors.length],
});
}
}
}
}
return (
<Graph
timeSeries={vmSeries}
series={graphs}
timeRange={timeRange}
showLines={showLines}
showPoints={showPoints}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment