I'm new to JavaScript and for an assignment I need to use 2D Colormaps Javascript Plugin (https://dominikjaeckle.com/projects/color2d/) for visualization. I know this should be easy, but somehow I encountered the error ERROR in src\ColorMap.js Line 48:29: 'Color2D' is not defined no-undef
. I suppose it had something to do with the way I integrated the color2D.js file, so I looked up online and followed the instructions on this post Adding script tag to React/JSX. However, the error still existed and I had no idea why.
The framework I use is React and my folder structure is like:
├── node_modules
├── public
├── src
│ ├── data # colormaps pngs (downloaded from the above website)
│ ├── App.css
│ ├── App.js # main app
│ ├── ColorMap.js # colormap component
│ ├── color2d.js # colormap code (downloaded from the above website)
# other files
In ColorMap.js, part of my code is as below.
import React, { useRef, useEffect } from "react";
import * as d3 from "d3"
function ColorMap() {
const svgRef = useRef();
const wrapperRef = useRef();
useEffect(() => {
// code for points and scale are omitted
const scriptTag = document.createElement('script');
scriptTag.src = "color2D.js";
scriptTag.async = false;
document.head.appendChild(scriptTag);
const svg = d3.select(svgRef.current);
result_mapped = svg... // some operations
function colorPoints() {
result_mapped.style("fill", function(d) {
const rgb = Color2D.getColor(d[0], d[1]);
return "rgb(" + rgb + ")";
})
};
Color2D.setColormap(Color2D.colormaps.BREMM, () => colorPoints());
return () => document.head.removeChild(scriptTag);
});
return (
<div ref={wrapperRef}>
<svg ref={svgRef}></svg>
</div>
);
}
export default ColorMap;
The implementation basically follows the source code of the 2D Colormaps Javascript Plugin website. Can anyone help me with it? Thanks.