Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Design graph component (CO2 Captured and Canopy Cover) #1984

Open
wants to merge 30 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1213804
add graph
prachigarg19 Feb 20, 2024
9b7385e
modify graph
prachigarg19 Feb 20, 2024
dd0567a
change stories for graph
prachigarg19 Feb 20, 2024
48a56fb
add style for tooltips
prachigarg19 Feb 20, 2024
0fba756
changes made:
prachigarg19 Feb 20, 2024
1d5fc82
add annotation line
prachigarg19 Feb 20, 2024
112cbfb
add title and subtitle
prachigarg19 Feb 20, 2024
0773520
make component common
prachigarg19 Feb 20, 2024
0b580e9
add interface
prachigarg19 Feb 20, 2024
127bf0e
remove unused imports
prachigarg19 Feb 20, 2024
4bfc2ed
resolve ts errors
prachigarg19 Feb 21, 2024
b877681
add chromatic workflow
prachigarg19 Feb 21, 2024
905bef8
add gradient in graph
prachigarg19 Feb 21, 2024
0c17009
add property to make tooltip follow cursor
prachigarg19 Feb 21, 2024
e26e9fb
add styles for annotation
prachigarg19 Feb 21, 2024
af864b8
add translations
prachigarg19 Feb 21, 2024
9081299
add useEffect and useState
prachigarg19 Feb 22, 2024
ee59f93
fix heading style
prachigarg19 Feb 27, 2024
fa4dd15
remove hard coded width
prachigarg19 Feb 27, 2024
e88986a
Merge branch 'feature/redesign-explore-btn' into feature/design-captu…
prachigarg19 Mar 5, 2024
5b48d1f
replace hardcoded colors and fonts
prachigarg19 Mar 5, 2024
32dd91a
fix: fix heading
prachigarg19 Mar 14, 2024
466bbf8
refactor: merge base branch,resolve merge conflict
sunilsabatp Jul 7, 2024
338075b
fix: translation issue, code clean up, remove hardcode value
sunilsabatp Jul 9, 2024
ae8b71a
feat: story for canopy cover, minor code cleanup
sunilsabatp Jul 12, 2024
4029516
refactor: increase the font-size
sunilsabatp Jul 12, 2024
b6227d7
refactor: make text bold
sunilsabatp Jul 12, 2024
c7de0b1
feat: dot notation for project launch verticle line
sunilsabatp Jul 12, 2024
09f4a45
feat: include month in the graph tootip
sunilsabatp Jul 12, 2024
314a4e2
feat: reduce stroke dash array
sunilsabatp Jul 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/chromatic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
- feature/storybook-update
- develop
- feature/redesign-explore-btn
- feature/design-capture-graph
# List of jobs
jobs:
chromatic-deployment:
Expand Down
9 changes: 8 additions & 1 deletion public/static/locales/en/projectDetails.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{
"satelliteAnalysis": "Satellite Analysis"
"satelliteAnalysis": "Satellite Analysis",
"co2CapturePerHa": "CO₂ Captured <p>(per ha.)</p>",
"comparedToRegionalAverage": "Compared to regional average",
"co2Removed": "CO₂ removed",
"biomass": "Biomass",
"yoy": "YoY",
"projectLaunch": "Project Launch",
"canopyCover": "Canopy Cover"
}
76 changes: 76 additions & 0 deletions src/temp/CarbonCapture/Graph.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
@import '../../theme/theme';

.tooltipContainer {
min-width: 100px;
.header {
padding: 10px 10px 8px 10px;
.title {
color: #333; //to be replaced
font-size: $fontSmall;
font-weight: 500;
margin-bottom: 5px;
}
.subtitle {
font-size: $fontXXXSmallNew;
font-weight: 400;
}
}
.body {
background-color: rgba(var(--primary-color-new), 0.10);
padding: 4px 10px;
display: flex;
justify-content: space-between;
align-items: center;
.yoyValue {
color: $secondaryColorNew;
font-weight: 700;
font-size: $fontXXSmallNew;
}
.date {
font-size: 6px; //to be replaced
font-weight: 500;
color: $dark;
opacity: 0.5;
}
}
}

.container {
width: 100%;
max-width: 338px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
.graphHeading {
font-size: $fontXXSmall;
color: #2F3336; //to be replaced
display: flex;
align-items: center;
padding:0 15px;
gap: 2px;
font-weight: 700;
p {
font-weight: 400;
}

}
.graphSubheading {
color: #BDBDBD; //to be replaced
font-size: $fontXXXSmallNew;
text-transform: lowercase;
padding:0 15px;
}
}

.titleContainer {
width: 100%;
max-width: 300px;
}
.newInfoIcon {
margin-left: auto;
margin-right: 6px;
display: flex;
align-items: center;
cursor: pointer;
}
236 changes: 236 additions & 0 deletions src/temp/CarbonCapture/Graph.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
import React, { useEffect, useState } from 'react';
import ReactApexChart from 'react-apexcharts';
import styles from './Graph.module.scss';
import ReactDOMServer from 'react-dom/server';
import NewInfoIcon from '../icons/NewInfoIcon';
import { ApexOptions } from 'apexcharts';
import { Trans, useTranslation } from 'next-i18next';

interface TooltipProps {
headerTitle: string;
subTitle: string;
yoyValue: string;
date: string;
}

export const Tooltip = ({
headerTitle,
subTitle,
yoyValue,
date,
}: TooltipProps) => {
const { t } = useTranslation('projectDetails');
return (
<div className={styles.tooltipContainer}>
<div className={styles.header}>
<p className={styles.title}>{headerTitle}</p>
<p className={styles.subtitle}>{subTitle}</p>
</div>
<div className={styles.body}>
<p className={styles.yoyValue}>
{yoyValue} {t('yoy')}
</p>
<p className={styles.date}>{date}</p>
</div>
</div>
);
};

interface GraphProps {
title: string;
subtitle: string;
years: number[];
series1Values: number[];
series2Values: number[];
tooltip: {
heading: string;
unit: string;
subheading: string;
};
}

interface CustomTooltipProps {
dataPointIndex: number;
w: { config: ApexOptions; globals: any };
}

const Graph = ({
title,
subtitle,
years,
series1Values,
series2Values,
tooltip,
}: GraphProps) => {
const { t } = useTranslation('projectDetails');
const [xaxisOptions, setXaxisOptions] = useState<
(number | (string | number)[])[]
>([]);

useEffect(() => {
const newOptions = years.map((year, index) => {
if (index === 1) {
return [2020, ` ${t('projectLaunch')}`];
} else {
return year;
}
});
setXaxisOptions(newOptions);
}, []);
Comment on lines +70 to +79
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Avoid hardcoding values and update dependency array

In the useEffect hook, the use of a hardcoded year (2020) when setting x-axis options can lead to incorrect labeling if the actual years differ. Additionally, the dependency array is empty, which may cause issues if years or t (translations) change.

Apply this diff to use dynamic year values and update the dependency array:

useEffect(() => {
  const newOptions = years.map((year, index) => {
    if (index === 1) {
-     return [2020, ` ${t('projectLaunch')}`];
+     return [year, ` ${t('projectLaunch')}`];
    } else {
      return year;
    }
  });
  setXaxisOptions(newOptions);
-}, []);
+}, [years, t]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
useEffect(() => {
const newOptions = years.map((year, index) => {
if (index === 1) {
return [2020, ` ${t('projectLaunch')}`];
} else {
return year;
}
});
setXaxisOptions(newOptions);
}, []);
useEffect(() => {
const newOptions = years.map((year, index) => {
if (index === 1) {
return [year, ` ${t('projectLaunch')}`];
} else {
return year;
}
});
setXaxisOptions(newOptions);
}, [years, t]);


const options = {
fill: {
type: 'gradient',
gradient: {
shadeIntensity: 1,
opacityFrom: 0.58,
opacityTo: 0,
},
},
chart: {
type: 'area',
width: 300,
toolbar: {
show: false,
},
},
Comment on lines +90 to +96
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove hardcoded chart width for responsiveness

The chart's width is hardcoded to 300, which may not render well on different screen sizes. Allowing the chart to adapt to its container improves responsiveness.

Remove the hardcoded width:

chart: {
  type: 'area',
- width: 300,
  toolbar: {
    show: false,
  },
},
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
chart: {
type: 'area',
width: 300,
toolbar: {
show: false,
},
},
chart: {
type: 'area',
toolbar: {
show: false,
},
},

tooltip: {
custom: function ({ dataPointIndex, w }: CustomTooltipProps) {
const getToolTip = () => {
const dataPoint = xaxisOptions[dataPointIndex];
const year = Array.isArray(dataPoint) ? dataPoint[0] : dataPoint;
return (
<Tooltip
headerTitle={`${w.globals.series[0][dataPointIndex]}${
tooltip.unit
} ${t(`${tooltip.heading}`)}`}
subTitle={
tooltip.subheading
? `${w.globals.series[1][dataPointIndex]}${tooltip.unit} ${t(
`${tooltip.subheading}`
)}`
: ''
}
yoyValue={'+4%'}
date={year.toString()}
/>
);
};

return ReactDOMServer.renderToString(getToolTip());
},
followCursor: true,
},
markers: {
size: 0,
colors: ['#fff', 'transparent'],
strokeColors: ['#219653', 'transparent'],
strokeOpacity: [1, 1],
strokeWidth: 2.2,
hover: {
size: 6,
},
},

dataLabels: {
enabled: false,
},
stroke: {
curve: 'smooth',
width: 2.2,
},
xaxis: {
type: 'year',
labels: {
formatter: function (index: number) {
if (index === 2) {
return xaxisOptions[1];
} else if (index == xaxisOptions.length) {
return xaxisOptions[index - 1];
} else {
return '';
}
},
Comment on lines +151 to +159
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Correct x-axis label formatter function

The x-axis label formatter function uses index, but ApexCharts provides value to the formatter. This may result in labels not displaying correctly.

Modify the formatter function to use value:

labels: {
- formatter: function (index: number) {
-   if (index === 2) {
-     return xaxisOptions[1];
-   } else if (index == xaxisOptions.length) {
-     return xaxisOptions[index - 1];
+ formatter: function (value: number, timestamp?: number, opts?: any) {
+   const dataPointIndex = opts?.dataPoints?.findIndex(
+     (point: any) => point.x === value
+   );
+   if (dataPointIndex === 1) {
+     return xaxisOptions[1][1];
+   } else if (dataPointIndex === xaxisOptions.length - 1) {
+     return xaxisOptions[dataPointIndex];
    } else {
      return '';
    }
  },

This ensures labels are displayed correctly based on the x-axis values.

Committable suggestion was skipped due to low confidence.

minHeight: 35,
style: {
colors: '#4F4F4F',
fontSize: 10,
},
},
show: false,
axisTicks: {
show: false,
},
tooltip: {
enabled: false,
},
axisBorder: {
show: false,
},
},
yaxis: {
show: false,
},
grid: {
show: false,
},
legend: {
show: false,
},
annotations: {
xaxis: [
{
x: xaxisOptions[1],
strokeDashArray: 0,
borderColor: '#219653',
},
],
},
};
const series = [
{
name: 'series1',
data: series1Values,
color: '#219653',
zIndex: 2,
},
{
name: 'series2',
data: series2Values,
color: '#BDBDBD',
zIndex: 1,
},
];
return (
<div className={styles.container}>
<div className={styles.titleContainer}>
<h5 className={styles.graphHeading}>
{title === 'co2CapturePerHa' ? (
<Trans i18nKey="co2CapturePerHa">
CO₂ Captured<p>(per ha.)</p>
</Trans>
) : (
t(`${title}`)
)}
<div className={styles.newInfoIcon}>
<NewInfoIcon height={17} width={17} color={'#BDBDBD'} />
</div>
</h5>
<p className={styles.graphSubheading}> {t(`${subtitle}`)}</p>
</div>
<div id="chart">
<ReactApexChart
options={options}
series={series}
type="area"
height={153}
width={'100%'}
style={{ display: 'flex', justifyContent: 'center' }}
/>
</div>
<div id="html-dist"></div>
</div>
);
};

export default Graph;
26 changes: 26 additions & 0 deletions src/temp/stories/Graph.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Meta, StoryObj } from '@storybook/react';
import Graph from '../CarbonCapture/Graph';

const meta: Meta<typeof Graph> = {
component: Graph,
};

export default meta;
type Story = StoryObj<typeof Graph>;

const years = [2019, 2020, 2021, 2022, 2023];

export const Preview: Story = {
args: {
years: years,
title: 'co2CapturePerHa',
subtitle: 'comparedToRegionalAverage',
series1Values: [21.4, 21.27, 20.78, 21.7, 21.78],
series2Values: [22.54, 22.65, 21.8, 21.85, 22.03],
tooltip: {
heading: 'co2Removed',
unit: 't',
subheading: 'biomass',
},
},
};
1 change: 1 addition & 0 deletions src/theme/_colors.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ $primaryLightColor: var(--primary-light-color);
$topProjectBackgroundColor: var(--top-project-background-color);
$nonDonatableProjectBackgroundColor: var(--non-donatable-project-background-color);
$primaryColorNew: var(--primary-color-new);
$secondaryColorNew: var(--secondary-color-new)
1 change: 1 addition & 0 deletions src/theme/_fonts.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ $fontXXLarge: var(--font-xx-large);
$fontXXXLarge: var(--font-xxx-large);
$secondaryFontFamily: var(--secondary-font-family);
$fontXXXSmallNew: var(--font-xxx-extra-small-new);
$fontXXSmallNew: var(--font-xx-extra-small-new);
3 changes: 3 additions & 0 deletions src/theme/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
topProjectBackgroundColor,
nonDonatableProjectBackgroundColor,
primaryColorNew,
secondaryColorNew,
} = theme;

const globalStyles = css.global`
Expand All @@ -23,6 +24,7 @@ const globalStyles = css.global`
--secondary-font-family: ${config.font.secondaryFontFamily ||
defaultFontFamily};
--font-xxx-extra-small-new: ${fontSizes.fontXXXSmallNew};
--font-xx-extra-small-new: ${fontSizes.fontXXSmallNew};
--font-xx-extra-small: ${fontSizes.fontXXSmall};
--font-x-extra-small: ${fontSizes.fontXSmall};
--font-small: ${fontSizes.fontSmall};
Expand All @@ -38,6 +40,7 @@ const globalStyles = css.global`
--top-project-background-color: ${topProjectBackgroundColor};
--non-donatable-project-background-color: ${nonDonatableProjectBackgroundColor};
--primary-color-new: ${primaryColorNew};
--secondary-color-new: ${secondaryColorNew};
}
.theme-light {
--primary-font-color: ${light.primaryFontColor};
Expand Down
Loading