From 5f7b63e76eb9f9c0b255009a02dfee6189839f7e Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Thu, 9 Apr 2020 14:02:10 +0200 Subject: [PATCH] improve types --- .../helpers/point_series/_get_aspects.ts | 13 ++- .../vislib/helpers/point_series/_get_point.ts | 12 +-- .../helpers/point_series/_get_series.ts | 94 ++++++++++--------- .../helpers/point_series/_init_x_axis.ts | 16 ++-- .../point_series/_ordered_date_axis.ts | 12 +-- .../helpers/point_series/point_series.ts | 3 +- 6 files changed, 74 insertions(+), 76 deletions(-) diff --git a/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_get_aspects.ts b/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_get_aspects.ts index 3bd60410809e2..29134409ddd5f 100644 --- a/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_get_aspects.ts +++ b/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_get_aspects.ts @@ -18,7 +18,7 @@ */ import { makeFakeXAspect } from './_fake_x_aspect'; -import { Dimensions, Dimension, Aspects } from './point_series'; +import { Dimensions, Aspects } from './point_series'; import { Table } from '../../types'; /** @@ -29,12 +29,11 @@ import { Table } from '../../types'; * may be undefined or an array of aspects. */ export function getAspects(table: Table, dimensions: Dimensions) { - const aspects = {} as Aspects; + const aspects: Partial = {}; (Object.keys(dimensions) as Array).forEach(name => { - const dimension = Array.isArray(dimensions[name]) - ? (dimensions[name] as Dimension[]) - : [dimensions[name] as Dimension]; - dimension.forEach(d => { + const dimension = dimensions[name]; + const dimensionList = Array.isArray(dimension) ? dimension : [dimension]; + dimensionList.forEach(d => { if (!d) { return; } @@ -59,5 +58,5 @@ export function getAspects(table: Table, dimensions: Dimensions) { aspects.x = [makeFakeXAspect()]; } - return aspects; + return aspects as Aspects; } diff --git a/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_get_point.ts b/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_get_point.ts index bb5a5fa8fe56d..3fc13eb0c04b5 100644 --- a/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_get_point.ts +++ b/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_get_point.ts @@ -21,12 +21,12 @@ import { getFormatService } from '../../../services'; import { Aspect } from './point_series'; import { Table, Row } from '../../types'; -type RowValue = number | 'NaN'; +type RowValue = number | string | object | 'NaN'; interface Raw { table: Table; - column: number; - row: number; - value: RowValue; + column: number | undefined; + row: number | undefined; + value?: RowValue; } export interface Point { x: RowValue | '_all'; @@ -61,7 +61,7 @@ export function getPoint( const yRow = row[y.accessor]; const zRow = z && row[z.accessor]; - const point = { + const point: Point = { x: xRow, y: yRow, z: zRow, @@ -98,7 +98,7 @@ export function getPoint( title: table.$parent.name, }, parent: series ? series[0] : null, - } as Point; + }; if (point.y === 'NaN') { // filter out NaN from stats diff --git a/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_get_series.ts b/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_get_series.ts index a410faac3ec36..edde5b69af022 100644 --- a/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_get_series.ts +++ b/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_get_series.ts @@ -17,7 +17,7 @@ * under the License. */ -import _ from 'lodash'; +import { partial } from 'lodash'; import { getPoint } from './_get_point'; import { addToSiri, Serie } from './_add_to_siri'; import { Chart } from './point_series'; @@ -30,57 +30,59 @@ export function getSeries(table: Table, chart: Chart) { const zAspect = aspects.z && aspects.z[0]; const multiY = Array.isArray(aspects.y) && aspects.y.length > 1; - const partGetPoint = _.partial(getPoint, table, xAspect, aspects.series); - return _(table.rows) - .transform((seriesMap: any, row, rowIndex) => { - if (!multiY) { - const point = partGetPoint(row, rowIndex, yAspect, zAspect); - if (point) { - const id = `${point.series}-${yAspect.accessor}`; - point.seriesId = id; - addToSiri( - seriesMap as Map, - point, - id, - point.series, - yAspect.format, - zAspect && zAspect.format, - zAspect && zAspect.title - ); - } - return; - } - - aspects.y.forEach(function(y) { - const point = partGetPoint(row, rowIndex, y, zAspect); - if (!point) { - return; - } - - // use the point's y-axis as it's series by default, - // but augment that with series aspect if it's actually - // available - let seriesId = y.accessor; - let seriesLabel = y.title; + const partGetPoint = partial(getPoint, table, xAspect, aspects.series); - if (aspects.series) { - const prefix = point.series ? point.series + ': ' : ''; - seriesId = prefix + seriesId; - seriesLabel = prefix + seriesLabel; - } + const seriesMap = new Map(); - point.seriesId = seriesId; + table.rows.forEach((row, rowIndex) => { + if (!multiY) { + const point = partGetPoint(row, rowIndex, yAspect, zAspect); + if (point) { + const id = `${point.series}-${yAspect.accessor}`; + point.seriesId = id; addToSiri( seriesMap, point, - seriesId as string, - seriesLabel, - y.format, + id, + point.series, + yAspect.format, zAspect && zAspect.format, zAspect && zAspect.title ); - }); - }, new Map() as any) - .thru(s => [...s.values()]) - .value() as Serie[]; + } + return; + } + + aspects.y.forEach(function(y) { + const point = partGetPoint(row, rowIndex, y, zAspect); + if (!point) { + return; + } + + // use the point's y-axis as it's series by default, + // but augment that with series aspect if it's actually + // available + let seriesId = y.accessor; + let seriesLabel = y.title; + + if (aspects.series) { + const prefix = point.series ? point.series + ': ' : ''; + seriesId = prefix + seriesId; + seriesLabel = prefix + seriesLabel; + } + + point.seriesId = seriesId; + addToSiri( + seriesMap, + point, + seriesId as string, + seriesLabel, + y.format, + zAspect && zAspect.format, + zAspect && zAspect.title + ); + }); + }); + + return [...seriesMap.values()]; } diff --git a/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_init_x_axis.ts b/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_init_x_axis.ts index 145c2a0480a5b..9d16c4857be00 100644 --- a/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_init_x_axis.ts +++ b/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_init_x_axis.ts @@ -19,23 +19,23 @@ import { uniq } from 'lodash'; import moment from 'moment'; -import { Chart, FakeParams, DateHistogramParams, HistogramParams } from './point_series'; +import { Chart } from './point_series'; import { Table } from '../../types'; export function initXAxis(chart: Chart, table: Table) { const { format, title, params, accessor } = chart.aspects.x[0]; chart.xAxisOrderedValues = - accessor === -1 - ? [(params as FakeParams).defaultValue] + accessor === -1 && 'defaultValue' in params + ? [params.defaultValue] : uniq(table.rows.map(r => r[accessor])); chart.xAxisFormat = format; chart.xAxisLabel = title; - const { interval, date } = params as DateHistogramParams; - if (interval) { - if (date) { - const { intervalESUnit, intervalESValue } = params as DateHistogramParams; + if ('interval' in params) { + const { interval } = params; + if ('date' in params) { + const { intervalESUnit, intervalESValue } = params; chart.ordered = { interval: moment.duration(interval), intervalESUnit, @@ -43,7 +43,7 @@ export function initXAxis(chart: Chart, table: Table) { }; } else { chart.ordered = { - interval: (params as HistogramParams).interval, + interval: params.interval, }; } } diff --git a/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_ordered_date_axis.ts b/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_ordered_date_axis.ts index eace3228ee7f0..193b10a563563 100644 --- a/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_ordered_date_axis.ts +++ b/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/_ordered_date_axis.ts @@ -17,21 +17,17 @@ * under the License. */ -import { DateHistogramParams, OrderedChart } from './point_series'; +import { OrderedChart } from './point_series'; export function orderedDateAxis(chart: OrderedChart) { const x = chart.aspects.x[0]; - const { bounds } = x.params as DateHistogramParams; + const bounds = 'bounds' in x.params ? x.params.bounds : undefined; chart.ordered.date = true; if (bounds) { - chart.ordered.min = isNaN(bounds.min as number) - ? Date.parse(bounds.min as string) - : (bounds.min as number); - chart.ordered.max = isNaN(bounds.max as number) - ? Date.parse(bounds.max as string) - : (bounds.max as number); + chart.ordered.min = typeof bounds.min === 'string' ? Date.parse(bounds.min) : bounds.min; + chart.ordered.max = typeof bounds.max === 'string' ? Date.parse(bounds.max) : bounds.max; } else { chart.ordered.endzones = false; } diff --git a/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/point_series.ts b/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/point_series.ts index 6b4b19fca0eae..a1681e0d71bd3 100644 --- a/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/point_series.ts +++ b/src/legacy/core_plugins/vis_type_vislib/public/vislib/helpers/point_series/point_series.ts @@ -106,7 +106,8 @@ export const buildPointSeriesData = (table: Table, dimensions: Dimensions) => { initXAxis(chart, table); initYAxis(chart); - if ((chart.aspects.x[0].params as DateHistogramParams).date) { + if ('date' in chart.aspects.x[0].params) { + // initXAxis will turn `chart` into an `OrderedChart if it is a date axis` orderedDateAxis(chart as OrderedChart); }