Skip to content

Commit 8669f3b

Browse files
Merge pull request #1038 from neo4j-labs/fix/1037-line-rule-based-styling
Fix/1037 line rule based styling
2 parents 8f560ef + b89c22d commit 8669f3b

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

src/chart/line/LineChart.tsx

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ import React, { useEffect } from 'react';
33
import { NoDrawableDataErrorMessage } from '../../component/editor/CodeViewerComponent';
44
import { evaluateRulesOnDict, useStyleRules } from '../../extensions/styling/StyleRuleEvaluator';
55
import { ChartProps } from '../Chart';
6-
import {
7-
convertRecordObjectToString,
8-
mutateName,
9-
processHierarchyFromRecords,
10-
recordToNative,
11-
toNumber,
12-
} from '../ChartUtils';
6+
import { recordToNative, toNumber } from '../ChartUtils';
137
import { themeNivo } from '../Utils';
148
import { extensionEnabled } from '../../utils/ReportUtils';
159

@@ -77,17 +71,18 @@ const NeoLineChart = (props: ChartProps) => {
7771
// For line charts, the line color is overridden if at least one value meets the criteria.
7872
const getLineColors = (line) => {
7973
const xFieldName = props.selection && props.selection.x;
80-
const yFieldName = line.id && line.id.split('(')[1] && line.id.split('(')[1].split(')')[0];
74+
const yFieldName = line.id;
8175
let color = 'black';
82-
line.data.forEach((entry) => {
76+
for (const entry of line.data) {
8377
const data = {};
84-
data[xFieldName] = entry[selection.x];
85-
data[yFieldName] = entry[selection.value];
78+
data[xFieldName] = entry.x;
79+
data[yFieldName] = entry.y;
8680
const validRuleIndex = evaluateRulesOnDict(data, styleRules, ['line color']);
8781
if (validRuleIndex !== -1) {
8882
color = styleRules[validRuleIndex].customizationValue;
83+
break;
8984
}
90-
});
85+
}
9186
return color;
9287
};
9388

src/chart/table/TableChart.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ export const NeoTableChart = (props: ChartProps) => {
245245
ColumnSortedDescendingIcon: () => <></>,
246246
ColumnSortedAscendingIcon: () => <></>,
247247
},
248-
// TODO: if mixing and matching row and cell styling, row rules MUST be set first or will not populate correctly
249248
getRowClassName: (params) => {
250249
return ['row color', 'row text color']
251250
.map((e) => {
@@ -256,21 +255,28 @@ export const NeoTableChart = (props: ChartProps) => {
256255
getCellClassName: (params) => {
257256
return ['cell color', 'cell text color']
258257
.map((e) => {
259-
let trueRulesList = [''];
260-
let trueRule;
258+
let validRuleClass = '';
261259
for (const [index, rule] of styleRules.entries()) {
260+
let ruleClass = '';
261+
// If the rule target is not the current cell
262262
if (rule.targetField) {
263263
if (rule.targetField === params.field) {
264-
trueRule = `rule${evaluateSingleRuleOnDict({ [rule.field]: params.row[rule.field] }, rule, index, [
264+
ruleClass = `rule${evaluateSingleRuleOnDict({ [rule.field]: params.row[rule.field] }, rule, index, [
265265
e,
266266
])}`;
267267
}
268-
} else {
269-
trueRule = `rule${evaluateSingleRuleOnDict({ [params.field]: params.value }, rule, index, [e])}`;
270268
}
271-
trueRulesList.push(trueRule);
269+
// If the rule target is the current cell
270+
else {
271+
ruleClass = `rule${evaluateSingleRuleOnDict({ [params.field]: params.value }, rule, index, [e])}`;
272+
}
273+
// If rule class is valid (rule-1 means rule check has failed)
274+
if (ruleClass && ruleClass !== 'rule-1') {
275+
validRuleClass = ruleClass;
276+
break;
277+
}
272278
}
273-
return trueRulesList.join(' ');
279+
return validRuleClass;
274280
})
275281
.join(' ');
276282
},

src/extensions/styling/StyleRuleEvaluator.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ export const evaluateRulesOnDict = (dict, rules, customizations) => {
6363
}
6464
for (const [index, rule] of rules.entries()) {
6565
// Only check customizations that are specified
66-
return evaluateSingleRuleOnDict(dict, rule, index, customizations);
66+
const evaluationResult = evaluateSingleRuleOnDict(dict, rule, index, customizations);
67+
if (evaluationResult !== -1) {
68+
return evaluationResult;
69+
}
6770
}
6871
// If no rules are met, return not found (index=-1)
6972
return -1;

0 commit comments

Comments
 (0)