Skip to content

Commit d5071a5

Browse files
fix(TCDICORE-475): fix manual colors for csv
1 parent ed5c6a5 commit d5071a5

4 files changed

Lines changed: 37 additions & 13 deletions

File tree

.changeset/odd-pants-admire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@devgateway/dvz-wp-commons": patch
3+
---
4+
5+
Fix manual CSV colors

packages/commons/src/ChartColors.jsx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export const ChartColors = (props) => {
157157

158158
useEffect(() => {
159159
let nextUseColors = useColors;
160-
if (app != "csv") {
160+
if (app !== "csv") {
161161
// All conditions for coloring by measures
162162
if ((dimension2 == "none" && colorBy === "index" && swap) ||
163163
(dimension1 == "none" && dimension2 == "none")) { //Multi measure colored by first dimension but swapped (Colored by Measure) or dimensionless bar charts
@@ -198,13 +198,26 @@ export const ChartColors = (props) => {
198198

199199
const updateColor = (value, color, colorByMode = null) => {
200200

201-
const newColors = Object.assign({}, manualColors)
201+
const newColors = {
202+
...manualColors,
203+
[app]: {
204+
...manualColors[app],
205+
}
206+
}
202207
if (colorByMode) {
203-
// For CSV colors, nest by colorBy mode
204-
if (!newColors[app][colorByMode]) {
205-
newColors[app][colorByMode] = {}
208+
// For CSV colors, nest by colorBy mode.
209+
// If the key exists as a flat (legacy) entry, remove it to avoid duplicates.
210+
const appColors = newColors[app];
211+
const baseApp = typeof appColors[value] === "string"
212+
? (({ [value]: _removed, ...rest }) => rest)(appColors)
213+
: appColors;
214+
newColors[app] = {
215+
...baseApp,
216+
[colorByMode]: {
217+
...(baseApp[colorByMode] || {}),
218+
[value]: color
219+
}
206220
}
207-
newColors[app][colorByMode][value] = color
208221
} else {
209222
// For non-CSV colors, use flat structure
210223
newColors[app][value] = color
@@ -399,7 +412,9 @@ export const ChartColors = (props) => {
399412
return null
400413
}
401414

402-
const csvColors = () => {
415+
const csvColors = (colorByParams) => {
416+
console.log("color by params...", colorByParams)
417+
console.log("csv colors...", manualColors, colorBy)
403418
const data = Papa.parse(csv, {header: true, dynamicTyping: true});
404419
const values = [];
405420

@@ -416,11 +431,12 @@ export const ChartColors = (props) => {
416431

417432
if (manualColors[app] && values) {
418433
return values.map(v => {
419-
// Get the current colorBy mode's color storage, creating it if needed
434+
// Get the current colorBy mode's color storage, falling back to flat structure for legacy data
420435
const colorByColors = manualColors[app][colorBy] || {};
436+
const color = colorByColors[v] !== undefined ? colorByColors[v] : manualColors[app][v];
421437
return <PanelColorSettings
422438
colorSettings={[{
423-
value: colorByColors[v],
439+
value: color,
424440

425441
onChange: (color) => {
426442
if (color) {

plugins/wp-react-blocks-plugin/blocks/charts/Bar.jsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ const BarOptions = (props) => {
5555
enableGridY,
5656
sort,
5757
sortReverse,
58+
manualColors
5859
}
5960
} = props;
60-
61+
6162
const getSeries = () => {
6263
if (allCategories) {
6364

@@ -127,10 +128,12 @@ const BarOptions = (props) => {
127128
}
128129
const series = app == 'csv' ? getCSVSeries() : getSeries();
129130

131+
132+
130133
return [
131134
<PanelBody initialOpen={false} title={__("Bar Options")}>
132135
<PanelBody initialOpen={false} title={__("Colors")}>
133-
<ChartColors {...props}></ChartColors>
136+
<ChartColors {...props} manualColors={manualColors}></ChartColors>
134137
</PanelBody>
135138
<PanelBody initialOpen={false} title={"Layout"}>
136139
<PanelRow>

plugins/wp-react-blocks-plugin/blocks/format-bar/text-color.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { RichTextToolbarButton, ColorPalette } from '@wordpress/block-editor';
44
import { Popover, Panel, PanelBody, Button } from '@wordpress/components';
55
import { useState } from '@wordpress/element';
66
import { tag as tagIcon } from '@wordpress/icons';
7-
import { BLOCK_NS } from '@devgateway/dvz-wp-commons'
7+
import { BLOCKS_NS } from '@devgateway/dvz-wp-commons'
88

9-
const name = BLOCK_NS + '/text-color';
9+
const name = BLOCKS_NS + '/text-color';
1010

1111
function Edit(props) {
1212
const { isActive, value, onChange, contentRef } = props;

0 commit comments

Comments
 (0)