-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[WEB-3329] dev: new chart components #6565
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export const LABEL_CLASSNAME = "uppercase text-custom-text-300/60 text-sm tracking-wide"; | ||
| export const AXIS_LINE_CLASSNAME = "text-custom-text-400/70"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| .next | ||
| .turbo | ||
| out/ | ||
| dist/ | ||
| build/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "printWidth": 120, | ||
| "tabWidth": 2, | ||
| "trailingComma": "es5" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
| "use client"; | ||
|
|
||
| import React, { useMemo } from "react"; | ||
| import { AreaChart as CoreAreaChart, Area, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts"; | ||
| // plane imports | ||
| import { AXIS_LINE_CLASSNAME, LABEL_CLASSNAME } from "@plane/constants"; | ||
| import { TAreaChartProps } from "@plane/types"; | ||
| // local components | ||
| import { CustomXAxisTick, CustomYAxisTick } from "../tick"; | ||
| import { CustomTooltip } from "../tooltip"; | ||
|
|
||
| export const AreaChart = React.memo(<K extends string, T extends string>(props: TAreaChartProps<K, T>) => { | ||
| const { | ||
| data, | ||
| areas, | ||
| xAxis, | ||
| yAxis, | ||
| className = "w-full h-96", | ||
| tickCount = { | ||
| x: undefined, | ||
| y: 10, | ||
| }, | ||
| showTooltip = true, | ||
| } = props; | ||
| // derived values | ||
| const itemKeys = useMemo(() => areas.map((area) => area.key), [areas]); | ||
| const itemDotClassNames = useMemo( | ||
| () => areas.reduce((acc, area) => ({ ...acc, [area.key]: area.dotClassName }), {}), | ||
| [areas] | ||
| ); | ||
|
|
||
| const renderAreas = useMemo( | ||
| () => | ||
| areas.map((area) => ( | ||
| <Area | ||
| key={area.key} | ||
| type="monotone" | ||
| dataKey={area.key} | ||
| stackId={area.stackId} | ||
| className={area.className} | ||
| stroke="inherit" | ||
| fill="inherit" | ||
| /> | ||
| )), | ||
| [areas] | ||
| ); | ||
|
|
||
| return ( | ||
| <div className={className}> | ||
| <ResponsiveContainer width="100%" height="100%"> | ||
| <CoreAreaChart | ||
| width={500} | ||
| height={300} | ||
| data={data} | ||
| margin={{ | ||
| top: 5, | ||
| right: 30, | ||
| left: 20, | ||
| bottom: 5, | ||
| }} | ||
| reverseStackOrder | ||
| > | ||
| <XAxis | ||
| dataKey={xAxis.key} | ||
| tick={(props) => <CustomXAxisTick {...props} />} | ||
| tickLine={{ | ||
| stroke: "currentColor", | ||
| className: AXIS_LINE_CLASSNAME, | ||
| }} | ||
| axisLine={{ | ||
| stroke: "currentColor", | ||
| className: AXIS_LINE_CLASSNAME, | ||
| }} | ||
| label={{ | ||
| value: xAxis.label, | ||
| dy: 28, | ||
| className: LABEL_CLASSNAME, | ||
| }} | ||
| tickCount={tickCount.x} | ||
| /> | ||
| <YAxis | ||
| domain={yAxis.domain} | ||
| tickLine={{ | ||
| stroke: "currentColor", | ||
| className: AXIS_LINE_CLASSNAME, | ||
| }} | ||
| axisLine={{ | ||
| stroke: "currentColor", | ||
| className: AXIS_LINE_CLASSNAME, | ||
| }} | ||
| label={{ | ||
| value: yAxis.label, | ||
| angle: -90, | ||
| position: "bottom", | ||
| offset: -24, | ||
| dx: -16, | ||
| className: LABEL_CLASSNAME, | ||
| }} | ||
| tick={(props) => <CustomYAxisTick {...props} />} | ||
| tickCount={tickCount.y} | ||
| allowDecimals={!!yAxis.allowDecimals} | ||
| /> | ||
| {showTooltip && ( | ||
| <Tooltip | ||
| cursor={{ fill: "currentColor", className: "text-custom-background-90/80 cursor-pointer" }} | ||
| content={({ active, label, payload }) => ( | ||
| <CustomTooltip | ||
| active={active} | ||
| label={label} | ||
| payload={payload} | ||
| itemKeys={itemKeys} | ||
| itemDotClassNames={itemDotClassNames} | ||
| /> | ||
| )} | ||
| /> | ||
| )} | ||
| {renderAreas} | ||
| </CoreAreaChart> | ||
| </ResponsiveContainer> | ||
| </div> | ||
| ); | ||
| }); | ||
| AreaChart.displayName = "AreaChart"; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
sriramveeraghanta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import React from "react"; | ||
| // plane imports | ||
| import { TChartData } from "@plane/types"; | ||
| import { cn } from "@plane/utils"; | ||
|
|
||
| // Helper to calculate percentage | ||
| const calculatePercentage = <K extends string, T extends string>( | ||
| data: TChartData<K, T>, | ||
| stackKeys: T[], | ||
| currentKey: T | ||
| ): number => { | ||
| const total = stackKeys.reduce((sum, key) => sum + data[key], 0); | ||
| return total === 0 ? 0 : Math.round((data[currentKey] / total) * 100); | ||
| }; | ||
|
|
||
| const MIN_BAR_HEIGHT_FOR_INTERNAL_TEXT = 14; // Minimum height needed to show text inside | ||
| const BAR_BORDER_RADIUS = 2; // Border radius for each bar | ||
|
|
||
| export const CustomBar = React.memo((props: any) => { | ||
| const { fill, x, y, width, height, dataKey, stackKeys, payload, textClassName, showPercentage } = props; | ||
| // Calculate text position | ||
| const TEXT_PADDING_Y = Math.min(6, Math.abs(MIN_BAR_HEIGHT_FOR_INTERNAL_TEXT - height / 2)); | ||
| const textY = y + height - TEXT_PADDING_Y; // Position inside bar if tall enough | ||
| // derived values | ||
| const currentBarPercentage = calculatePercentage(payload, stackKeys, dataKey); | ||
| const showText = | ||
| // from props | ||
| showPercentage && | ||
| // height of the bar is greater than or equal to the minimum height required to show the text | ||
| height >= MIN_BAR_HEIGHT_FOR_INTERNAL_TEXT && | ||
| // bar percentage text has some value | ||
| currentBarPercentage !== undefined && | ||
| // bar percentage is a number | ||
| !Number.isNaN(currentBarPercentage); | ||
|
|
||
| if (!height) return null; | ||
| return ( | ||
| <g> | ||
| <path | ||
| d={` | ||
| M${x + BAR_BORDER_RADIUS},${y + height} | ||
| L${x + BAR_BORDER_RADIUS},${y} | ||
| Q${x},${y} ${x},${y + BAR_BORDER_RADIUS} | ||
| L${x},${y + height - BAR_BORDER_RADIUS} | ||
| Q${x},${y + height} ${x + BAR_BORDER_RADIUS},${y + height} | ||
| L${x + width - BAR_BORDER_RADIUS},${y + height} | ||
| Q${x + width},${y + height} ${x + width},${y + height - BAR_BORDER_RADIUS} | ||
| L${x + width},${y + BAR_BORDER_RADIUS} | ||
| Q${x + width},${y} ${x + width - BAR_BORDER_RADIUS},${y} | ||
| L${x + BAR_BORDER_RADIUS},${y} | ||
| `} | ||
| className={cn("transition-colors duration-200", fill)} | ||
| fill="currentColor" | ||
| /> | ||
| {showText && ( | ||
| <text | ||
| x={x + width / 2} | ||
| y={textY} | ||
| textAnchor="middle" | ||
| className={cn("text-xs font-medium", textClassName)} | ||
| fill="currentColor" | ||
| > | ||
| {currentBarPercentage}% | ||
| </text> | ||
| )} | ||
| </g> | ||
| ); | ||
| }); | ||
| CustomBar.displayName = "CustomBar"; | ||
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
| "use client"; | ||
|
|
||
| import React, { useMemo } from "react"; | ||
| import { BarChart as CoreBarChart, Bar, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts"; | ||
| // plane imports | ||
| import { AXIS_LINE_CLASSNAME, LABEL_CLASSNAME } from "@plane/constants"; | ||
| import { TBarChartProps } from "@plane/types"; | ||
| // local components | ||
| import { CustomXAxisTick, CustomYAxisTick } from "../tick"; | ||
| import { CustomTooltip } from "../tooltip"; | ||
| import { CustomBar } from "./bar"; | ||
|
|
||
| export const BarChart = React.memo(<K extends string, T extends string>(props: TBarChartProps<K, T>) => { | ||
| const { | ||
| data, | ||
| bars, | ||
| xAxis, | ||
| yAxis, | ||
| barSize = 40, | ||
| className = "w-full h-96", | ||
| tickCount = { | ||
| x: undefined, | ||
| y: 10, | ||
| }, | ||
| showTooltip = true, | ||
| } = props; | ||
| // derived values | ||
| const stackKeys = useMemo(() => bars.map((bar) => bar.key), [bars]); | ||
| const stackDotClassNames = useMemo( | ||
| () => bars.reduce((acc, bar) => ({ ...acc, [bar.key]: bar.dotClassName }), {}), | ||
| [bars] | ||
| ); | ||
sriramveeraghanta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const renderBars = useMemo( | ||
| () => | ||
| bars.map((bar) => ( | ||
| <Bar | ||
| key={bar.key} | ||
| dataKey={bar.key} | ||
| stackId={bar.stackId} | ||
| fill={bar.fillClassName} | ||
| shape={(shapeProps: any) => ( | ||
sriramveeraghanta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <CustomBar | ||
| {...shapeProps} | ||
| stackKeys={stackKeys} | ||
| textClassName={bar.textClassName} | ||
| showPercentage={bar.showPercentage} | ||
| /> | ||
| )} | ||
| /> | ||
| )), | ||
| [stackKeys, bars] | ||
| ); | ||
|
|
||
| return ( | ||
| <div className={className}> | ||
| <ResponsiveContainer width="100%" height="100%"> | ||
| <CoreBarChart | ||
| data={data} | ||
| margin={{ top: 10, right: 10, left: 10, bottom: 40 }} | ||
| barSize={barSize} | ||
| className="recharts-wrapper" | ||
| > | ||
| <XAxis | ||
| dataKey={xAxis.key} | ||
| tick={(props) => <CustomXAxisTick {...props} />} | ||
| tickLine={{ | ||
| stroke: "currentColor", | ||
| className: AXIS_LINE_CLASSNAME, | ||
| }} | ||
| axisLine={{ | ||
| stroke: "currentColor", | ||
| className: AXIS_LINE_CLASSNAME, | ||
| }} | ||
| label={{ | ||
| value: xAxis.label, | ||
| dy: 28, | ||
| className: LABEL_CLASSNAME, | ||
| }} | ||
| tickCount={tickCount.x} | ||
| /> | ||
| <YAxis | ||
| domain={yAxis.domain} | ||
| tickLine={{ | ||
| stroke: "currentColor", | ||
| className: AXIS_LINE_CLASSNAME, | ||
| }} | ||
| axisLine={{ | ||
| stroke: "currentColor", | ||
| className: AXIS_LINE_CLASSNAME, | ||
| }} | ||
| label={{ | ||
| value: yAxis.label, | ||
| angle: -90, | ||
| position: "bottom", | ||
| offset: -24, | ||
| dx: -16, | ||
| className: LABEL_CLASSNAME, | ||
| }} | ||
| tick={(props) => <CustomYAxisTick {...props} />} | ||
| tickCount={tickCount.y} | ||
| allowDecimals={!!yAxis.allowDecimals} | ||
| /> | ||
| {showTooltip && ( | ||
| <Tooltip | ||
| cursor={{ fill: "currentColor", className: "text-custom-background-90/80 cursor-pointer" }} | ||
| content={({ active, label, payload }) => ( | ||
| <CustomTooltip | ||
| active={active} | ||
| label={label} | ||
| payload={payload} | ||
| itemKeys={stackKeys} | ||
| itemDotClassNames={stackDotClassNames} | ||
| /> | ||
| )} | ||
| /> | ||
| )} | ||
| {renderBars} | ||
| </CoreBarChart> | ||
| </ResponsiveContainer> | ||
| </div> | ||
| ); | ||
| }); | ||
| BarChart.displayName = "BarChart"; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from "./root"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.