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

Sentiment chart #265

Merged
merged 22 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6aa36d6
wip(line-charts): first try with nivo line charts
cannarocks Jul 27, 2023
93d2bff
🐛 fix(_types.ts): change value type in ChartTooltipFunction to accept…
marcbon Jul 28, 2023
cc4640a
🐛 fix(line/index.tsx): change max value of yScale from 5 to 6
marcbon Jul 31, 2023
3dda49a
🐛 fix(line/_data.ts): add missing custom_data property to data object
marcbon Jul 31, 2023
5375e3a
🔥 refactor(line/index.tsx): remove unused import statement for 'get' …
marcbon Jul 31, 2023
c48f809
🎨 style(globalStyle.ts): add height property to ::-webkit-scrollbar
marcbon Aug 30, 2023
8b3cf62
🐛 fix(_data.ts): change data variable to be of type Data
marcbon Aug 31, 2023
711a0b9
🐛 fix(line/_data.ts): change id value from "sentiment-chart" to "Sent…
marcbon Aug 31, 2023
f50e029
🔥 refactor(line/index.tsx): remove unused imports and code
marcbon Aug 31, 2023
221a0a4
🐛 fix(_data.ts): update custom_data values in data array
marcbon Aug 31, 2023
e42631e
🐛 fix(line/_data.ts): fix typo in custom_data value
marcbon Aug 31, 2023
099aeaf
🐛 fix(line/_types.ts): rename LineData type to SentimentData
marcbon Sep 1, 2023
4e35dc4
🐛 fix(line): remove unused colors prop in index.stories.tsx
marcbon Sep 1, 2023
b9dc41a
refactor(sentiment): rename files
marcbon Sep 1, 2023
9918a76
✨ feat(index.tsx): add sentiment chart export
marcbon Sep 1, 2023
6fab9a3
🔥 refactor(sentiment/index.tsx): remove unused import CHARTS_COLOR_SC…
marcbon Sep 1, 2023
145cff4
🔀 refactor(sentiment/_types.ts): remove unused props and update prop …
marcbon Sep 1, 2023
69b2fa6
🐛 fix(index.stories.tsx): remove duplicate code and pass width, heigh…
marcbon Sep 1, 2023
7edd9c6
🔀 refactor(_types.ts): update ChartTooltipFunction type to accept Rea…
marcbon Sep 1, 2023
9cb64de
🔀 refactor(_types.ts): remove unused import of LegendType
marcbon Sep 1, 2023
c5f879b
🐛 fix(sentiment/index.tsx): add i18n prop to formatSentiment function
marcbon Sep 1, 2023
39a8e46
🔨 refactor(index.stories.tsx): remove unused imports and formatPoint …
marcbon Sep 1, 2023
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 src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export * from "./stories/charts/pie";
export * from "./stories/charts/halfPie";
export * from "./stories/charts/sunburst";
export * from "./stories/charts/waffle";
export * from "./stories/charts/sentiment";

// --- Close ---
export * from "./stories/close";
Expand Down
2 changes: 1 addition & 1 deletion src/stories/charts/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type ChartTooltipFunction = ({
label,
data,
}: {
value: number;
value: string | number;
label: string | number;
data?: Record<string, string | number | undefined>;
}) => React.ReactNode;
69 changes: 69 additions & 0 deletions src/stories/charts/sentiment/_data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
export type Data = {
id: string;
data: {
x: string;
y: number;
custom_data?: string;
}[];
};

export const data: Data = {
id: "Sentiment",
data: [
{
"x": "Homepage",
"y": 2,
"custom_data": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla euismod, nisl quis tincidunt ultricies, nunc nisl lacinia nunc, quis lacinia nisl nun"
},
{
"x": "Catalogo",
"y": 4,
"custom_data": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla euismod, nisl quis tincidunt ultricies, nunc nisl lacinia nunc, quis lacinia nisl nun"
},
{
"x": "Scheda Prodotto",
"y": 3,
"custom_data": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla euismod, nisl quis tincidunt ultricies, nunc nisl lacinia nunc, quis lacinia nisl nunc"
},
{
"x": "Check Out",
"y": 3,
"custom_data": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla euismod, nisl quis tincidunt ultricies, nunc nisl lacinia nunc, quis lacinia nisl nun"
},
{
"x": "Conferma Ordine",
"y": 1,
"custom_data": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla euismod, nisl quis tincidunt ultricies, nunc nisl lacinia nunc, quis lacinia nisl nun"
},
{
"x": "Carrello",
"y": 5,
"custom_data": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla euismod, nisl quis tincidunt ultricies, nunc nisl lacinia nunc, quis lacinia nisl nun"
},
{
"x": "Catalogo 2",
"y": 4,
"custom_data": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla euismod, nisl quis tincidunt ultricies, nunc nisl lacinia nunc, quis lacinia nisl nun"
},
{
"x": "Scheda Prodotto 2",
"y": 3,
"custom_data": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla euismod, nisl quis tincidunt ultricies, nunc nisl lacinia nunc, quis lacinia nisl nun"
},
{
"x": "Check Out 2",
"y": 3,
"custom_data": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla euismod, nisl quis tincidunt ultricies, nunc nisl lacinia nunc, quis lacinia nisl nun"
},
{
"x": "Conferma Ordine 2",
"y": 1,
"custom_data": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla euismod, nisl quis tincidunt ultricies, nunc nisl lacinia nunc, quis lacinia nisl nun"
},
{
"x": "Carrello 2",
"y": 5,
"custom_data": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla euismod, nisl quis tincidunt ultricies, nunc nisl lacinia nunc, quis lacinia nisl nun"
},
]
};
29 changes: 29 additions & 0 deletions src/stories/charts/sentiment/_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ChartTooltipFunction } from "../_types";
import { LegendType } from "../Legend";

type SentimentData = {
id: string;
data: {
x: string;
y?: number;
custom_data?: string;
}[];
};

export interface SentimentChartProps {
data: SentimentData;
width?: string;
cannarocks marked this conversation as resolved.
Show resolved Hide resolved
height?: string;
padding?: number;
colors?: string[];
margin?: {
top?: number;
right?: number;
bottom?: number;
left?: number;
};
axisLeftLabel?: string;
axisBottomLabel?: string;
tooltip?: ChartTooltipFunction;
legend?: LegendType | true;
}
11 changes: 11 additions & 0 deletions src/stories/charts/sentiment/assets/sentiment_1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions src/stories/charts/sentiment/assets/sentiment_2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/stories/charts/sentiment/assets/sentiment_3.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/stories/charts/sentiment/assets/sentiment_4.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/stories/charts/sentiment/assets/sentiment_5.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
134 changes: 134 additions & 0 deletions src/stories/charts/sentiment/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { Meta, StoryFn } from "@storybook/react";
import { SentimentChart } from ".";
import { SentimentChartProps } from "./_types";
import { data } from "./_data";
import { theme } from "../../theme";
import styled from "styled-components";
import { ContainerCard } from "../../cards";
import { DatumValue } from "@nivo/core";
import { ReactComponent as S1 } from "./assets/sentiment_1.svg";
import { ReactComponent as S2 } from "./assets/sentiment_2.svg";
import { ReactComponent as S3 } from "./assets/sentiment_3.svg";
import { ReactComponent as S4 } from "./assets/sentiment_4.svg";
import { ReactComponent as S5 } from "./assets/sentiment_5.svg";
import { LG, MD } from "../../typography/typescale";
import { Grid } from "../../grid/grid";
import { Row } from "../../grid/row";
import { Col } from "../../grid/col";
import { Span } from "../../typography/span";

const Label = styled.p`
color: ${({ theme }) => theme.palette.blue[600]};
font-size: ${({ theme }) => theme.fontSizes.md};
padding: ${({ theme }) => theme.space.sm};
`;

const VerticalLabel = styled(Label)`
writing-mode: vertical-rl;
text-align: center;
margin: auto;
`;

const HorizontalLabel = styled(Label)`
text-align: center;
`;

const Tooltip = styled.div`
background-color: white;
border: 1px solid ${({ theme }) => theme.palette.grey[300]};
border-radius: ${({ theme }) => theme.borderRadii.md};
padding: ${({ theme }) => theme.space.sm};
box-shadow: ${theme.shadows.boxShadow(theme)};
max-width: 270px;
white-space: normal;
`;

const formatPoint = (value: DatumValue) => {
switch (value as number) {
case 1:
return <S1 style={{ width: "16px" }} />;
case 2:
return <S2 style={{ width: "16px" }} />;
case 3:
return <S3 style={{ width: "16px" }} />;
case 4:
return <S4 style={{ width: "16px" }} />;
case 5:
return <S5 style={{ width: "16px" }} />;
default:
return "";
}
};

const Template: StoryFn<SentimentChartProps> = (args) => (
<>
<ContainerCard style={{ padding: theme.space.md }}>
<LG isBold>Sentiment Chart</LG>
<br />
<p>
This is the sentiment chart. It is a wrapper around the{" "}
<a href="https://nivo.rocks/line/">Nivo Line Chart</a> component.
</p>
<p>
The Nivo Line Chart is a wrapper around the{" "}
<a href="https://d3js.org/">D3</a> library.
</p>
<br />
<Grid>
<Row>
<Col xs="1" style={{ display: "flex", alignItems: "center", margin: 0 }}>
<VerticalLabel>Vertical Label</VerticalLabel>
</Col>
<Col xs="11" style={{ margin: 0 }}>
<SentimentChart
width={`${data.data.length * 150}px`}
height="350px"
margin={{ top: 50, right: 0, bottom: 50, left: 0 }}
{...args}
/>
</Col>
</Row>
<Row>
<Col xs="1" style={{ margin: 0 }}></Col>
<Col xs="11" style={{ margin: 0 }}>
<div style={{ width: data.data.length * 150, maxWidth: "100%", marginTop: theme.space.md }}>
<HorizontalLabel>Horizontal Label</HorizontalLabel>
</div>
</Col>
</Row>
</Grid>
</ContainerCard>
</>
);

export const Default = Template.bind({});
Default.args = {
data: data,
};

export const WithCustomTooltip = Template.bind({});
WithCustomTooltip.args = {
data: data,
tooltip: (node) => {
const { data, label: cluster } = node;
const sentimentValue = parseInt(data?.yValue as string);

return (
<Tooltip>
<div style={{ display: "flex", alignItems: "center" }}>
{formatPoint(sentimentValue)}<Span isBold style={{ marginLeft: theme.space.xs, color: theme.palette.grey[600] }}>{cluster}</Span>
</div>
{data?.customData && (
<MD style={{ marginTop: theme.space.xs }}>
{data?.customData}
</MD>
)}
</Tooltip>
)
}
};

export default {
title: "Atoms/Charts/Sentiment",
component: SentimentChart,
} as Meta<typeof SentimentChart>;
Loading
Loading