From 574d72dfcd9f27d434e284667f63b37f5ae2b596 Mon Sep 17 00:00:00 2001 From: Shruthi Rai Date: Thu, 13 Jan 2022 17:47:07 +0100 Subject: [PATCH] fix: modifying continuous legend code --- .../src/component/Legend/ContinuousLegend.tsx | 15 ++-- .../src/component/Utils/continousLegend.ts | 26 ++---- .../src/component/WelllayerTemplateTypes.ts | 15 ---- .../src/component/welllayer_template.json | 89 ------------------- react-app/src/index.ts | 1 - .../ContinousLgends/MD.stories.jsx | 4 +- .../ContinousLgends/NTG.stories.jsx | 4 +- .../ContinousLgends/PermTot.stories.jsx | 4 +- .../ContinousLgends/Permeability.stories.jsx | 4 +- .../ContinousLgends/PoroTot.stories.jsx | 4 +- .../ContinousLgends/Porosity.stories.jsx | 4 +- .../ContinousLgends/SW.stories.jsx | 4 +- .../DiscreteLegends/Facies.stories.jsx | 1 - .../DiscreteLegends/RDE.stories.jsx | 1 - 14 files changed, 29 insertions(+), 147 deletions(-) delete mode 100644 react-app/src/component/WelllayerTemplateTypes.ts delete mode 100644 react-app/src/component/welllayer_template.json diff --git a/react-app/src/component/Legend/ContinuousLegend.tsx b/react-app/src/component/Legend/ContinuousLegend.tsx index 3d20b636..c97e78a8 100644 --- a/react-app/src/component/Legend/ContinuousLegend.tsx +++ b/react-app/src/component/Legend/ContinuousLegend.tsx @@ -1,7 +1,6 @@ import * as React from "react"; import { RGBToHex, colorsArray } from "../Utils/continousLegend"; import { select, scaleLinear, scaleSequential, axisBottom } from "d3"; -import { templateArray } from "../WelllayerTemplateTypes"; import { colorTablesArray } from "../ColorTableTypes"; declare type legendProps = { @@ -9,8 +8,7 @@ declare type legendProps = { max: number; dataObjectName: string; position: number[]; - name: string; - template: templateArray; + colorName: string; colorTables: colorTablesArray; horizontal: boolean; } @@ -25,19 +23,18 @@ export const ContinuousLegend: React.FC = ({ max, dataObjectName, position, - name, - template, + colorName, colorTables, horizontal, }: legendProps) => { React.useEffect(() => { continuousLegend("#legend"); - }, [min, max, template, colorTables, horizontal]); + }, [min, max, colorName, colorTables, horizontal]); function continuousLegend(selected_id: string) { const itemColor: ItemColor[] = []; // Return the matched colors array from color.tables.json file - const colorTableColors = colorsArray(name, template, colorTables); + const colorTableColors = colorsArray(colorName, colorTables); colorTableColors.forEach((value: [number, number, number, number]) => { // return the color and offset needed to draw the legend itemColor.push({ @@ -72,10 +69,10 @@ export const ContinuousLegend: React.FC = ({ .data(itemColor) .enter() .append("stop") - .attr("offset", function (data: any) { + .attr("offset", function (data) { return data.offset + "%"; }) - .attr("stop-color", function (data: any) { + .attr("stop-color", function (data) { return data.color; }); diff --git a/react-app/src/component/Utils/continousLegend.ts b/react-app/src/component/Utils/continousLegend.ts index 28328c1c..923266fc 100644 --- a/react-app/src/component/Utils/continousLegend.ts +++ b/react-app/src/component/Utils/continousLegend.ts @@ -4,37 +4,26 @@ import { colorTablesArray, colorTablesObj, } from "../ColorTableTypes"; -import { - templateArray, - propertiesObj, -} from "../WelllayerTemplateTypes"; // Based on objectName return the colors array from color.tables.json file export function colorsArray( - objectName: string, - template: templateArray, + colorName: string, colorTables: colorTablesArray ): [number, number, number, number][] { - const properties = template[0]["properties"]; - const propertiesData = properties.filter( - (value: propertiesObj) => value.objectName == objectName - ); const colorTableData = colorTables.filter( (value: colorTablesObj) => - value.name.toLowerCase() == - propertiesData[0].colorTable.toLowerCase() + value.name.toLowerCase() == colorName.toLowerCase() ); - return colorTableData[0].colors; + return colorTableData.length > 0 ? colorTableData[0].colors : []; } // return the colors based on the point export function rgbValues( - objectName: string, point: number, - template: templateArray, + colorName: string, colorTables: colorTablesArray ): number[] | { r: number; g: number; b: number; opacity: number } | undefined { - const colorTableColors = colorsArray(objectName, template, colorTables); + const colorTableColors = colorsArray(colorName, colorTables); // compare the point and first value from colorTableColors const colorArrays = colorTableColors.find( (value: [number, number, number, number]) => { @@ -57,10 +46,13 @@ export function rgbValues( const secondColorArray = colorTableColors[index]; if ((firstColorArray || secondColorArray) != undefined) { + const t0 = firstColorArray[0]; + const t1 = secondColorArray[0]; + const t = (point - t0) / (t1 - t0); // t = 0.0 gives first color, t = 1.0 gives second color. const interpolatedValues = interpolateRgb( RGBToHex(firstColorArray).color, RGBToHex(secondColorArray).color - )(point); + )(t); return color(interpolatedValues)?.rgb(); } return undefined; diff --git a/react-app/src/component/WelllayerTemplateTypes.ts b/react-app/src/component/WelllayerTemplateTypes.ts deleted file mode 100644 index 45c75f3a..00000000 --- a/react-app/src/component/WelllayerTemplateTypes.ts +++ /dev/null @@ -1,15 +0,0 @@ -export declare type propertiesObj = { - objectName: string; - colorTable: string; - context: string; - colorInterpolation: string; -} - -type propertiesArr = Array; - -declare type template = { - name: string; - properties: propertiesArr; -} - -export type templateArray = Array