From 515a0e1e7af1ca50a522e5a73d9ce0805601069f Mon Sep 17 00:00:00 2001 From: Oliver Roick Date: Thu, 19 Dec 2024 16:35:28 +1100 Subject: [PATCH] Add chart rendering --- src/App.jsx | 17 +++++++++-- src/atoms.js | 1 + src/components/BarChart.jsx | 12 ++++---- src/components/MessageOut/MessageTool.jsx | 35 +++++++++++++++-------- 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index a63848c..d708d06 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,8 +1,12 @@ import { Box, Grid, Text } from "@chakra-ui/react"; +import { useAtomValue } from "jotai"; import Providers from "./Providers"; import { ChatInput, ChatOutput, Map } from "./components"; +import { chartDataAtom } from "./atoms"; +import BarChart from "./components/BarChart"; function App() { + const chartData = useAtomValue(chartDataAtom); return ( - - - + + + + + {chartData && ( + + + + )} + diff --git a/src/atoms.js b/src/atoms.js index 49c0f26..3d86173 100644 --- a/src/atoms.js +++ b/src/atoms.js @@ -5,6 +5,7 @@ export const mapLayersAtom = atom([]); export const chatHistoryAtom = atom([]); export const sessionIdAtom = atom(uuidv4()); export const isLoadingAtom = atom(false); +export const chartDataAtom = atom(); function makeInputMessage(query) { return { diff --git a/src/components/BarChart.jsx b/src/components/BarChart.jsx index 7990043..b6b35b4 100644 --- a/src/components/BarChart.jsx +++ b/src/components/BarChart.jsx @@ -14,8 +14,8 @@ const BarChart = ({ data }) => { const tooltip = d3.select(tooltipRef.current); const width = containerRef.current?.offsetWidth || 250; - const height = 250; - const margin = { top: 20, right: 20, bottom: 20, left: 50 }; + const height = 230; + const margin = { top: 20, right: 20, bottom: 20, left: 60 }; const x = d3.scaleBand() .domain(data.map(d => d.category)) @@ -32,14 +32,14 @@ const BarChart = ({ data }) => { .attr("transform", `translate(0,${height - margin.bottom})`) .call(d3.axisBottom(x)) .selectAll("text") - .style("text-anchor", "end") - .attr("transform", "rotate(-45)") .style("font-size", "12px"); // Add y-axis svg.append("g") .attr("transform", `translate(${margin.left},0)`) - .call(d3.axisLeft(y)); + .call(d3.axisLeft(y)) + .selectAll("text") + .style("font-size", "12px"); // Add bars svg.append("g") @@ -71,7 +71,7 @@ const BarChart = ({ data }) => {
Using context layer {message}

); @@ -61,20 +59,33 @@ function DistAlertsTool({message, artifact}) { // artifact is geojson object to render to a map const setMapLayers = useSetAtom(mapLayersAtom); + const setChartData = useSetAtom(chartDataAtom); + + const numDisturbances = artifact ? artifact?.features.length : 0; + + if (numDisturbances === 0) { + return

No disturbances found in the region.

; + } const json = JSON.parse(message); const keys = Object.keys(json); const data = Object.entries(json[keys[0]]).map(([category, value]) => ({ category, value })); + return ( <> -

Dist alerts tool

-

Dist alerts

- -

Map

- - - - +

Found {numDisturbances} disturbances in the region.

+ ); }