-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create StackedBarChart component
- Loading branch information
Showing
15 changed files
with
328 additions
and
82 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,31 @@ | ||
import { ChartDataItem } from 'src/components/charts/chartData'; | ||
import { Color } from 'src/styles/Color'; | ||
|
||
export const PLACEHOLDER_BAR_CHART_ITEM = { | ||
label: 'None', | ||
value: 0, | ||
percentage: 100, | ||
color: Color.Grey, | ||
}; | ||
|
||
export function StackedBarChart({ | ||
data, | ||
}: { | ||
data: Array<ChartDataItem & { percentage?: number; color: string }>; | ||
}) { | ||
return ( | ||
<div className="flex border border-taupe-300 p-px"> | ||
{data.map((item, index) => ( | ||
<div | ||
key={index} | ||
style={{ | ||
width: `${item.percentage || 0}%`, | ||
backgroundColor: item.color, | ||
}} | ||
className="tooltip h-2" | ||
data-tip={item.label} | ||
></div> | ||
))} | ||
</div> | ||
); | ||
} |
This file contains 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,23 @@ | ||
import { CHART_COLORS } from 'src/styles/Color'; | ||
import { sum } from 'src/utils/math'; | ||
|
||
export interface ChartDataItem { | ||
label: string; | ||
value: number; | ||
percentage?: number; | ||
} | ||
|
||
export function sortAndCombineChartData(data: Array<ChartDataItem>) { | ||
const maxNumItems = CHART_COLORS.length - 1; | ||
let sortedData = data.sort((a, b) => b.value - a.value); | ||
if (sortedData.length > maxNumItems) { | ||
const topData = sortedData.slice(0, maxNumItems); | ||
const combinedData = { | ||
label: 'Others', | ||
value: sum(sortedData.slice(maxNumItems).map((d) => d.value)), | ||
percentage: sum(sortedData.slice(maxNumItems).map((d) => d.percentage || 0)), | ||
}; | ||
sortedData = [...topData, combinedData]; | ||
} | ||
return sortedData.map((d, i) => ({ ...d, color: CHART_COLORS[i] })); | ||
} |
This file contains 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 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 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
Oops, something went wrong.