How to render custom svg in IconLayer of Deck.gl - reactjs

i am trying to render a custom svg in iconLayer when a particular condition is met, from this example, https://deck.gl/gallery/icon-layer my getIcon option is
getIcon: (d) => ({
url: './glow.svg',
width: 128,
height: 128
}),
I think the url is meant to be the path to the image i want to render
However, nothing is been shown on the map.
the full component is
import { IconLayer } from "#deck.gl/layers";
import icons from "../assets/images/monsoon_icons.png";
const COLORS = {
...colors
};
const ICON_MAPPING = {
circle: { x: 0, y: 0, width: 70, height: 70, mask: true },
rectangle: { x: 70, y: 0, width: 70, height: 70, mask: true },
triangle: { x: 140, y: 0, width: 70, height: 70, mask: true },
star: { x: 210, y: 0, width: 70, height: 70, mask: true },
};
export const RenderLayers = (props) => {
let { data } = props;
if(props.isSaved) {
return [
new IconLayer({
...other icon props
// iconMapping: ICON_MAPPING,
getIcon: (d) => ({
url: './glow.svg',
width: 128,
height: 128
}),
...other icon props,
}),
new IconLayer({
...other icon props
// iconMapping: ICON_MAPPING,
getIcon: (d) => ({
url: './glow.svg',
width: 128,
height: 128
}),
...other icon props,
}),
];
};
if (!data?.length) {
console.log("NO DATA TO MAP");
return;
};
return [
new IconLayer({
...normal icon layer props
})
];
};

You would need to convert your svg to data URLs.
Following the official deck.gl example:
import yourSvg from 'whatever/path';
function svgToDataURL(svg) {
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`;
}
new IconLayer({
getIcon: () => ({
url: svgToDataURL(yourSvg),
width: 24,
height: 24
}),
})
Also, you can check a full working example here.
Cheers!

Related

How can I use a custom layer for each bar using a nivochart

I would like to be able to provide a custom layer on each of my bars in a Nivo responsive bar chart. I have been using this guide as a reference. I am creating a warning level and would like to have a line representing that level set on each bar. The issue I have is that the level is moving around when resizing the page.
The expected response upon screen resizing is for the warning level line to retain its expected position on the chart. What I am getting instead is the warning line moving from its desired position.
"next": "~12.2.4",
"#nivo/bar": "~0.79.1",
"#nivo/core": "~0.78.0",
My chart component using mock data -
import CardComponent from '#components/CardComponent';
import NivoWrapper from '#components/NivoWrapper';
import {
ResponsiveBar,
} from '#nivo/bar';
import React from 'react';
const data = [
{
x: '0',
name: 'weight5',
current: 200,
warningLevel: 100,
},
{
x: '1',
name: 'weight2',
current: 300,
warningLevel: 150,
},
{
x: '2',
name: 'weight3',
current: 400,
warningLevel: 200,
},
{
x: '3',
name: 'weight4',
current: 500,
warningLevel: 250,
},
{
x: '4',
name: 'weight5',
current: 600,
warningLevel: 300,
},
];
const MarginTop = 0;
const Marginright = 24;
const Marginbottom = 24;
const Marginleft = 64;
function Line({ bars, xScale }) {
return (
{bars.map((bar: any) => {
return (
<line
key={bar.key}
y1={bar.absY}
y2={bar.absY + bar.height}
x1={xScale(bar.width - bar.data.data.warningLevel - bar.data.data.warningLevel)}
x2={xScale(bar.width - bar.data.data.warningLevel - bar.data.data.warningLevel)}
stroke="black"
/>
);
})}
);
}
function PackageStockLevelChart() {
return (
<NivoWrapper>
<ResponsiveBar
data={data}
indexBy="name"
keys={['current']}
layout="horizontal"
enableLabel={false}
axisLeft={{
tickValues: 7,
}}
margin={{
top: MarginTop, right: Marginright, bottom: Marginbottom, left: Marginleft,
}}
enableGridY={false}
colors="green"
legendLabel="legend label"
legends={[{
dataFrom: 'keys',
anchor: 'bottom-right',
direction: 'column',
itemWidth: 80,
itemHeight: 16,
translateY: -16,
}]}
layers={['axes', 'bars', Line, 'legends']}
/>
</NivoWrapper>
);
}
export default PackageStockLevelChart;
View before resizing -
View before resizing
During / After resizing
View during after reszing

Customize Images on Amcharts 5 force directed graph

I am trying to make a force directed graph with amcharts 5 where the nodes are images.
I was able to make images as nodes but was not really able to customize it. I want it to be rounded and has an onClick handler, which returns the node which is being clicked.
import React, { useLayoutEffect } from "react";
import "./App.css";
import * as am5 from "#amcharts/amcharts5";
import * as am5hierarchy from "#amcharts/amcharts5/hierarchy";
import am5themes_Animated from "#amcharts/amcharts5/themes/Animated";
function App(props) {
useLayoutEffect(() => {
let root = am5.Root.new("chartdiv");
root.setThemes([am5themes_Animated.new(root)]);
let chart = root.container.children.push(
am5.Container.new(root, {
width: am5.percent(100),
height: am5.percent(100),
layout: root.verticalLayout,
})
);
let series = chart.children.push(
am5hierarchy.ForceDirected.new(root, {
downDepth: 1,
initialDepth: 1,
topDepth: 0,
valueField: "value",
categoryField: "name",
childDataField: "children",
xField: "x",
yField: "y",
minRadius: 30,
manyBodyStrength: -40,
})
);
// series.circles.template.set("forceHidden", true);
// series.outerCircles.template.set("forceHidden", true);
series.circles.template.events.on("click", function (ev) {
console.log(ev);
console.log("Clicked on");
});
// Use template.setup function to prep up node with an image
series.nodes.template.setup = function (target) {
target.events.on("dataitemchanged", function (ev) {
target.children.push(
am5.Picture.new(root, {
width: 90,
height: 90,
centerX: am5.percent(50),
centerY: am5.percent(50),
src: ev.target.dataItem.dataContext.image,
})
);
});
};
series.bullets.push(function (root) {
return am5.Bullet.new(root, {
sprite: am5.Picture.new(root, {
radius: 4,
fill: series.get("fill"),
}),
});
});
series.data.setAll([
{
name: "Chrome",
value: 1,
image: "https://picsum.photos/202",
children: [
{ name: "Google", value: 1, image: "https://picsum.photos/203" },
{
name: "Firefox",
value: 1,
image: "https://picsum.photos/204",
},
{
name: "IE",
value: 1,
image: "https://picsum.photos/203",
},
{
name: "Safari",
value: 1,
image: "https://picsum.photos/205",
},
{
name: "Opera",
value: 1,
image: "https://picsum.photos/206",
},
],
},
]);
series.set("selectedDataItem", series.dataItems[0]);
return () => {
root.dispose();
};
}, []);
return <div id="chartdiv" style={{ width: "100%", height: "500px" }}></div>;
}
export default App;
I was able to find some workaround using pinBullets in amhcarts 4 but I'm trying to get it working on amcharts 5.

Struggling to correctly update and re-render chart on react-chartjs-2

I am building an app for gym-goers to record and track their progress. For each exercise they input, it should render a chart showing their previous track record. This is working fine.
Users can then add another entry to this track record, but it does not update the chart unless you refresh the page. I can't work out why or how to fix it.
There are a number of different components involved - a parent Exercise.js one, then an ExerciseFooter.js one, which contains the buttons to adjust the target or add a new entry to the exercise, and then AddHistory.js and SetTarget.js components which contain modals and the logic to update the exercise via Redux and MongoDB.
A minimal version of the Exercise.js page is here (I've collapsed the stuff that's mainly styling into single lines as much as possible):
import React, { useState, useEffect } from "react";
import { ExerciseFooter } from "./ExerciseFooter";
import { Line } from "react-chartjs-2";
import { useLocation } from "react-router-dom";
import { useSelector } from "react-redux";
export const Exercise = (props) => {
const location = useLocation();
const users = useSelector((state) => state.auth);
const localUser = JSON.parse(localStorage.getItem("profile"));
const [user, setUser] = useState("");
const [exerciseProp, setExerciseProp] = useState({
history: [""],
target: 0,
});
useEffect(() => {
localUser &&
localUser?.result &&
users.length > 0 &&
setUser(
users.filter(
(filteredUser) => filteredUser._id == props.match.params.userId
)[0]
);
if (!localUser) setUser("");
setExerciseProp(
user?.exercises?.filter(
(exercise) => exercise._id == props.match.params.exerciseId
)[0]
);
}, [users, location]);
//styling for chart
const [barData, setBarData] = useState({
labels: [""],
datasets: [
{ label: "Target", fill: false, radius: 0, data: [""], borderColor: ["rgba(35, 53, 89)"], borderWidth: [3], },
{ label: "You did", data: [""], tension: 0.3, borderColor: ["white"], backgroundColor: ["white"], borderWidth: 3, },
],
});
//updating chart data
var weightArr = [];
var dateArr = [];
var targetArr = [];
if (exerciseProp) {
exerciseProp.history.map((hist) =>
weightArr.push(parseInt(hist.weight) || 0)
);
exerciseProp.history.map((hist) => dateArr.push(hist.date));
for (let i = 0; i < exerciseProp.history.length; i++) {
targetArr.push(exerciseProp.target);
}
}
useEffect(() => {
if (exerciseProp) {
setBarData({
labels: dateArr,
datasets: [
{
label: "Target",
fill: false,
radius: 0,
data: targetArr,
borderColor: ["rgba(35, 53, 89)"], borderWidth: [3],
},
{
label: "Weight",
data: weightArr,
tension: 0.3, borderColor: ["white"], backgroundColor: ["white"], borderWidth: 3,
},
],
});
}
}, [users]);
//render chart ones exerciseProp is populated
if (exerciseProp) {
return (
<div style={{ marginTop: "200px" }}>
<Line
data={barData}
options={{ plugins: { title: { display: false, }, legend: { display: false, }, },
scales: { x: { grid: { color: "white", font: { family: "Dongle", size: 20, }, }, ticks: { color: "white", font: { family: "Dongle", size: 20, }, }, }, y: { grid: { color: "white", }, ticks: { color: "white", font: { family: "Dongle", size: 20, }, }, }, }, }}
/>
{exerciseProp && <ExerciseFooter user={user} exercise={exerciseProp} />}
</div>
);
} else {
return <>Loading...</>;
}
};
I've tried doing a few different things but nothing has worked. I tried adding an 'update' state variable which was updated by a function passed down to the the various dispatches, and then added it to the dependencies of the useEffects, but that didn't seem to make any difference.
Any help much appreciated! As I say, if I just force a refresh then it works fine but know that's bad practice so trying to work out why it isn't re-rendering correctly.
Thanks!
You just have to enable redraw prop
like this
<Line
redraw={true}
data={barData}
options={{ plugins: { title: { display: false, }, legend: { display: false, }, },
scales: { x: { grid: { color: "white", font: { family: "Dongle", size: 20, }, }, ticks: { color: "white", font: { family: "Dongle", size: 20, }, }, }, y: { grid: { color: "white", }, ticks: { color: "white", font: { family: "Dongle", size: 20, }, }, }, }, }}/>
this all you have to do
redraw={true}

How to animate prop transform for svg with React-Native and Reanimated 2

import React, { FC } from "react";
import { G } from "react-native-svg";
import Animated, { useAnimatedProps, useDerivedValue, withSpring } from "react-native-reanimated";
import { CanvasControlledValuesProps } from "./helpers/types";
import { Candle } from "./Candle";
import { CANDLE_WIDTH } from "../../constants/sizes";
const AnimatedGroup = Animated.createAnimatedComponent(G);
export const Canvas: FC<CanvasControlledValuesProps> = ({
scaleY,
scaleX,
translateX,
offsetByY,
data,
initialDomain,
}) => {
const props = useAnimatedProps(() => {
return {
x: withSpring(translateX.value, {
damping: 20,
stiffness: 90,
}),
y: withSpring(offsetByY.value, {
damping: 20,
stiffness: 90,
}),
transform: { scale: [scaleX.value, scaleY.value] },
};
});
return (
<AnimatedGroup animatedProps={props}>
{data.map((candle, index) => (
<Candle key={index} width={CANDLE_WIDTH} {...{ candle, index }} domain={initialDomain} />
))}
</AnimatedGroup>
);
};
Good day! I need to increase or decrease the content of the AnimatedGroup, so I decided to use G but there was a problem: the scale is not applied for the AnimatedGroup, why it's like that? I have not used Aniamted.View since the quality of the Svg content, inside which the AnimatedGroup is located, will be lost.
const style = useAnimatedStyle(() => {
return {
transform: [
{
translateX: offsetByX.value,
},
{
translateX: withSpring(translateX.value, springConfig),
},
{
translateY: withSpring(adaptation.value.offsetByY, springConfig),
},
{
scaleX: scaleX.value,
},
{
scaleY: withSpring(adaptation.value.scaleY, springConfig),
},
],
};
});
return (
<AnimatedGroup style={style}>
{data.map((candle, index) => (
<Candle key={index} width={CANDLE_WIDTH} {...{ candle, index }} domain={initialDomain} />
))}
</AnimatedGroup>
);
The solution is to add animated styles for the Animated Group. Or you can use the matrix property like this:
const props = useAnimatedProps(() => {
return {
x: withSpring(translateX.value, {
damping: 20,
stiffness: 90,
}),
y: withSpring(offsetByY.value, {
damping: 20,
stiffness: 90,
}),
matrix: [scaleX.value, 0, 0, scaleY.value, 0, 0],
};
});
You can read about the work of matrix here https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform

React, ApexCharts, Radial Bar series value from data

I'm new to React and trying to learn. My personal project is homebrewing display site.
I want to show some of the details in radial bar. I followed tutorial and made array data file, and index file where details show. Now I'm stuck getting single values to display in radial bar.
Also values need to be calculated from percent to numeric. I got that working. I tried some push functions but did not get it to work way I wanted. My coding skills are beginner level.
display example
data.js
import product1 from '../../images/product-1.png';
import product2 from '../../images/product-1.png';
export const productData = [
{
id: 1,
img: product1,
alt: 'Beer',
name: 'Lager',
desc: 'Saaz',
ibu: '35',
ebc: '40',
abv: '8.5',
},
{
id: 2,
img: product2,
alt: 'Beer',
name: 'IPA',
desc: 'Mango, Citrus',
ibu: '85',
ebc: '25',
abv: '5.5',
},
];
index.js
import React, { Component } from 'react';
import Chart from 'react-apexcharts';
import {
ProductsContainer,
ProductWrapper,
ProductsHeading,
ProductTitle,
ProductCard,
ProductImg,
ProductInfo,
ProductDesc,
ProductIbu,
ProductEbc,
ProductAbv,
} from './ProductsElements';
const max = 119;
function valueToPercent(value) {
return (value * 100) / max;
}
class IBU extends Component {
constructor(props) {
super(props);
this.state = {
series: [valueToPercent(15)],
options: {
plotOptions: {
radialBar: {
startAngle: -90,
endAngle: 90,
hollow: {
margin: 10,
size: '70%',
background: '#222222',
image: undefined,
imageOffsetX: 0,
imageOffsetY: 0,
position: 'front',
dropShadow: {
enabled: true,
top: 5,
left: 0,
blur: 4,
opacity: 0.9,
},
},
track: {
background: '#d42d2d',
strokeWidth: '67%',
margin: 0, // margin is in pixels
dropShadow: {
enabled: true,
top: 0,
left: 0,
blur: 4,
color: '#000',
opacity: 0.6,
},
},
dataLabels: {
show: true,
name: {
offsetY: -10,
show: true,
color: '#fff',
fontSize: '17px',
},
value: {
formatter: function (value) {
return (parseFloat(value) * max) / 100;
},
color: '#dadada',
fontSize: '36px',
show: true,
},
},
},
},
fill: {
colors: [
function ({ value, seriesIndex, w }) {
if (value < 55) {
return '#7E36AF';
} else if (value >= 55 && value < 80) {
return '#164666';
} else {
return '#D9534F';
}
},
],
},
stroke: {
lineCap: 'round',
},
labels: ['IBU'],
},
};
}
render() {
return <Chart options={this.state.options} series={this.state.series} type="radialBar" height={250} />;
}
}
const Products = ({ data }) => {
return (
<ProductsContainer>
<ProductsHeading>heading</ProductsHeading>
<ProductWrapper>
{data.map((product, index) => {
return (
<ProductCard key={index}>
<ProductImg src={product.img} alt={product.alt} />
<ProductInfo>
<ProductTitle>{product.name}</ProductTitle>
<ProductDesc>{product.desc}</ProductDesc>
<ProductIbu>{product.ibu}</ProductIbu>
<IBU></IBU>
<ProductEbc>{product.ebc}</ProductEbc>
<ProductAbv>{product.abv}</ProductAbv>
</ProductInfo>
</ProductCard>
);
})}
</ProductWrapper>
</ProductsContainer>
);
};
export default Products;

Resources