From 04d0aa356741245ce290ee7ff05896dad57ed3f0 Mon Sep 17 00:00:00 2001 From: Marco Vettorello Date: Thu, 17 Oct 2019 15:15:02 +0200 Subject: [PATCH] fix: align series names on splitted series configuration This commit align the series name when using a splitted series accessor. The names of the series now uses the value coming from the splitted series accessor, independentely on how many series are computed fix #420 --- .playground/index.html | 6 +-- .playground/playgroud.tsx | 39 ++++++++++++++++--- .../xy_chart/legend/legend.test.ts | 23 +++++++++++ src/chart_types/xy_chart/legend/legend.ts | 8 +++- 4 files changed, 64 insertions(+), 12 deletions(-) diff --git a/.playground/index.html b/.playground/index.html index 7bdda72199..6f31981cf3 100644 --- a/.playground/index.html +++ b/.playground/index.html @@ -18,14 +18,12 @@ position: absolute; top: 10px; left: 10px; - bottom: 10px; - right: 10px; } .chart { background: white; position: relative; - width: 800px; - height: 450px; + width: 600px; + height: 350px; margin: 10px; } diff --git a/.playground/playgroud.tsx b/.playground/playgroud.tsx index f993a990b4..b819393c17 100644 --- a/.playground/playgroud.tsx +++ b/.playground/playgroud.tsx @@ -1,13 +1,41 @@ import React, { Fragment } from 'react'; -import { Axis, Chart, getAxisId, getSpecId, Position, ScaleType, BarSeries } from '../src'; +import { Axis, Chart, getAxisId, getSpecId, Position, ScaleType, BarSeries, Settings } from '../src'; -export class Playground extends React.Component { +export class Playground extends React.Component<{}, { dataLimit: boolean }> { + state = { + dataLimit: false, + }; + changeData = () => { + this.setState((prevState) => { + return { + dataLimit: !prevState.dataLimit, + }; + }); + }; render() { - const data = [{ x: 0, y: -4 }, { x: 1, y: -3 }, { x: 2, y: 2 }, { x: 3, y: 1 }]; + const data = [ + { + g: null, + i: 'aa', + x: 1571212800000, + y: 16, + y1: 2, + }, + // { + // x: 1571290200000, + // y: 1, + // y1: 5, + // // g: 'authentication_success', + // }, + ]; return ( +
+ +
+
diff --git a/src/chart_types/xy_chart/legend/legend.test.ts b/src/chart_types/xy_chart/legend/legend.test.ts index 0ae9985af0..d8369f8d87 100644 --- a/src/chart_types/xy_chart/legend/legend.test.ts +++ b/src/chart_types/xy_chart/legend/legend.test.ts @@ -263,4 +263,27 @@ describe('Legends', () => { label = getSeriesColorLabel([0], false, spec1); expect(label).toBe('0'); }); + it('use the splitted value as label if has a single series and splitSeries is used', () => { + const specWithSplit: BasicSeriesSpec = { + ...spec1, + splitSeriesAccessors: ['g'], + }; + let label = getSeriesColorLabel([], true, specWithSplit); + expect(label).toBe('Spec 1 title'); + + label = getSeriesColorLabel(['a'], true, specWithSplit); + expect(label).toBe('a'); + + // happens when we have multiple values in splitSeriesAccessor + // or we have also multiple yAccessors + label = getSeriesColorLabel(['a', 'b'], true, specWithSplit); + expect(label).toBe('a - b'); + + // happens when the value of a splitSeriesAccessor is null + label = getSeriesColorLabel([null], true, specWithSplit); + expect(label).toBe('Spec 1 title'); + + label = getSeriesColorLabel([], false, specWithSplit); + expect(label).toBe('Spec 1 title'); + }); }); diff --git a/src/chart_types/xy_chart/legend/legend.ts b/src/chart_types/xy_chart/legend/legend.ts index b9a00adff7..d1153ff89b 100644 --- a/src/chart_types/xy_chart/legend/legend.ts +++ b/src/chart_types/xy_chart/legend/legend.ts @@ -95,7 +95,7 @@ export function computeLegend( } export function getSeriesColorLabel( - colorValues: any[], + colorValues: Array, hasSingleSeries: boolean, spec?: BasicSeriesSpec, ): string | undefined { @@ -104,7 +104,11 @@ export function getSeriesColorLabel( if (!spec) { return; } - label = spec.name || `${spec.id}`; + if (spec.splitSeriesAccessors && colorValues.length > 0 && colorValues[0] !== null) { + label = colorValues.join(' - '); + } else { + label = spec.name || `${spec.id}`; + } } else { label = colorValues.join(' - '); }