-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(view): add cloc linechart (#105)
* feat: add cloc linechart cloc 차트를 구현하였습니다. 차후 수정해야할 부분을 주석의 TODO로 정리 해두었습니다. * fix: data가 없을 경우 early return
- Loading branch information
1 parent
d07704b
commit d4a98a5
Showing
2 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
72 changes: 70 additions & 2 deletions
72
packages/view/src/components/TemporalFilter/ClocLineChart/ClocLineChart.tsx
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 |
---|---|---|
@@ -1,8 +1,76 @@ | ||
import { | ||
axisBottom, | ||
axisLeft, | ||
extent, | ||
scaleBand, | ||
scaleLinear, | ||
scaleTime, | ||
select, | ||
ticks, | ||
timeTicks, | ||
} from "d3"; | ||
import { useEffect, useRef } from "react"; | ||
|
||
import type { CommitNode } from "../TemporalFilter.type"; | ||
import { getCloc, timeFormatter } from "../TemporalFilter.util"; | ||
|
||
// TODO margin 추가하기 | ||
|
||
const ClocLineChart = ({ data }: { data: CommitNode[] }) => { | ||
console.log(data); | ||
return <>ClocLineChart</>; | ||
const wrapperRef = useRef<HTMLDivElement>(null); | ||
const ref = useRef<SVGSVGElement>(null); | ||
|
||
useEffect(() => { | ||
if (!wrapperRef.current || !ref.current || !data) return; | ||
|
||
const { width, height } = wrapperRef.current.getBoundingClientRect(); | ||
const svg = select(ref.current).attr("width", width).attr("height", height); | ||
|
||
// TODO cleanup으로 옮기기 | ||
svg.selectAll("*").remove(); | ||
|
||
const xMin = data[0].commit.commitDate; | ||
const xMax = data[data.length - 1].commit.commitDate; | ||
|
||
const [yMin, yMax] = extent(data, (d) => getCloc(d)) as [number, number]; | ||
|
||
const xScale = scaleTime() | ||
.domain([new Date(xMin), new Date(xMax)]) | ||
.range([0, width]); | ||
|
||
const xScaleBand = scaleBand<Date>() | ||
.domain(data.map((commitNode) => commitNode.commit.commitDate)) | ||
.range([0, width]); | ||
|
||
const xAxis = axisBottom<Date>(xScale) | ||
.tickValues(timeTicks(new Date(xMin), new Date(xMax), 10)) | ||
.tickFormat((d) => timeFormatter(new Date(d))); | ||
|
||
const yScale = scaleLinear().domain([yMin, yMax]).range([height, 0]); | ||
|
||
const yAxis = axisLeft(yScale).tickValues(ticks(yMin, yMax, 10)); | ||
|
||
svg.append("g").call(xAxis).attr("transform", `translate(0,${height})`); | ||
|
||
svg.append("g").call(yAxis).attr("transform", `translate(${width},0)`); | ||
|
||
svg | ||
.selectAll(".cloc") | ||
.data(data) | ||
.join("rect") | ||
.classed("cloc", true) | ||
.attr("x", (d) => xScale(new Date(d.commit.commitDate))) | ||
.attr("y", (d) => yScale(getCloc(d))) | ||
.attr("height", (d) => height - yScale(getCloc(d))) | ||
.attr("width", xScaleBand.bandwidth()) | ||
.attr("fill", "black"); | ||
}, [data]); | ||
|
||
return ( | ||
<div ref={wrapperRef}> | ||
<svg ref={ref} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ClocLineChart; |
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