Skip to content

Commit 9ee67dc

Browse files
authored
fix(partition): elimination of zero values (#658)
Ignore not only negative values but also zero values. fix #642
1 parent 89eaf0d commit 9ee67dc

File tree

6 files changed

+107
-1
lines changed

6 files changed

+107
-1
lines changed
Loading
Loading
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License. */
18+
19+
import { getHierarchyOfArrays } from './hierarchy_of_arrays';
20+
21+
const rawFacts = [
22+
{ sitc1: '7', exportVal: 0 },
23+
{ sitc1: '3', exportVal: 3 },
24+
{ sitc1: 'G', exportVal: 1 },
25+
{ sitc1: '5', exportVal: -8 },
26+
];
27+
28+
const valueAccessor = (d: any) => d.exportVal;
29+
30+
const groupByRollupAccessors = [() => null, (d: any) => d.sitc1];
31+
32+
describe('Test', () => {
33+
test('getHierarchyOfArrays should omit zero and negative values', () => {
34+
const outerResult = getHierarchyOfArrays(rawFacts, valueAccessor, groupByRollupAccessors);
35+
expect(outerResult.length).toBe(1);
36+
37+
const results = outerResult[0];
38+
expect(results.length).toBe(2);
39+
expect(results[0]).toBeNull();
40+
41+
const result = results[1];
42+
const expectedLength = rawFacts.filter((d: any) => valueAccessor(d) > 0).length;
43+
expect(expectedLength).toBe(2);
44+
expect(result.children.length).toBe(expectedLength);
45+
});
46+
});

src/chart_types/partition_chart/layout/viewmodel/hierarchy_of_arrays.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function getHierarchyOfArrays(
3838

3939
const facts = rawFacts.filter((n) => {
4040
const value = valueAccessor(n);
41-
return Number.isFinite(value) && value >= 0;
41+
return Number.isFinite(value) && value > 0;
4242
});
4343

4444
// don't render anything if the total, the width or height is not positive

stories/treemap/9_zero_values.tsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License. */
18+
19+
import { Chart, Datum, Partition, PartitionLayout } from '../../src';
20+
import { mocks } from '../../src/mocks/hierarchical/index';
21+
import { config } from '../../src/chart_types/partition_chart/layout/config/config';
22+
import { arrayToLookup, hueInterpolator } from '../../src/chart_types/partition_chart/layout/utils/calcs';
23+
import { productDimension } from '../../src/mocks/hierarchical/dimension_codes';
24+
import { palettes } from '../../src/mocks/hierarchical/palettes';
25+
import React from 'react';
26+
27+
const productLookup = arrayToLookup((d: Datum) => d.sitc1, productDimension);
28+
29+
// style calcs
30+
const interpolatorCET2s = hueInterpolator(palettes.CET2s.map(([r, g, b]) => [r, g, b, 0.7]));
31+
32+
const defaultFillColor = (colorMaker: any) => (d: any, i: number, a: any[]) => colorMaker(i / (a.length + 1));
33+
34+
export const example = () => (
35+
<Chart className="story-chart">
36+
<Partition
37+
id="spec_1"
38+
data={mocks.pie.map((d: any, i: number) => (i ? d : { ...d, exportVal: 0 }))}
39+
valueAccessor={(d: Datum) => d.exportVal as number}
40+
valueFormatter={(d: number) => `$${config.fillLabel.valueFormatter(Math.round(d / 1000000000))}\xa0Bn`}
41+
layers={[
42+
{
43+
groupByRollup: (d: Datum) => d.sitc1,
44+
nodeLabel: (d: Datum) => productLookup[d].name,
45+
fillLabel: {
46+
textInvertible: true,
47+
valueFormatter: (d: number) => `${config.fillLabel.valueFormatter(Math.round(d / 1000000000))}\xa0Bn`,
48+
},
49+
shape: {
50+
fillColor: defaultFillColor(interpolatorCET2s),
51+
},
52+
},
53+
]}
54+
config={{
55+
partitionLayout: PartitionLayout.treemap,
56+
}}
57+
/>
58+
</Chart>
59+
);

stories/treemap/treemap.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ export { example as multiColor } from './5_multicolor';
3333
export { example as customStyle } from './6_custom_style';
3434
export { example as percentage } from './7_percentage';
3535
export { example as grooveText } from './8_groove_text';
36+
export { example as zeroValues } from './9_zero_values';

0 commit comments

Comments
 (0)