ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

 

 

 

 

Today I Learned

 

 

๐Ÿ“Œ ์ฐจํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

 

์ง€๋‚œ ์ฃผ์— ์žˆ์—ˆ๋˜ ํ”„๋กœ์ ํŠธ ์ค‘๊ฐ„๋ฐœํ‘œ ํ›„ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ,

๊ธฐ์กด์— ์žˆ์—ˆ๋˜ ๋ฐˆ ์‚ฌ์ „ ํŽ˜์ด์ง€๋ฅผ ๊ฐ•ํ™”ํ•˜๋Š” ์ชฝ์œผ๋กœ ๊ฒฐ์ •์ด ๋ผ์„œ ๋‚ด๊ฐ€ ์ˆ˜์น˜ ๊ด€๋ จ ํƒญ ํ™”๋ฉด์„ ๋‹ด๋‹นํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

 

์‚ฌ์‹ค ์ฐจํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•ด๋ณด๊ณ  ์‹ถ์–ด์„œ ๋‚ด๊ฐ€ ํ•˜๊ณ  ์‹ถ๋‹ค๊ณ  ๋จผ์ € ์–˜๊ธฐํ–ˆ๋‹ค. ใ…Žใ…Ž

 

๋ฆฌ์•กํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ฐจํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์•Œ์•„๋ณด๋˜ ์ค‘ nivo๋ฅผ ์•Œ๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ

์ฒ˜์Œ์—๋Š” nivo๊ฐ€ ์ปค์Šคํ…€์˜ ๋ํŒ์™•์ด๋ผ๋Š” ํ›„๊ธฐ๊ฐ€ ๋งŽ์ด ๋ณด์—ฌ์„œ ์‚ฌ์šฉํ•ด๋ณด๋ ค ํ–ˆ๋‹ค.

 

 

https://nivo.rocks/

 

Home | nivo

 

nivo.rocks

 

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜๊ณ  ์ ์šฉํ•ด๋ณด๋Š”๋ฐ,

์˜คํžˆ๋ ค ์ปค์Šคํ…€ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ”์œ„๊ฐ€ ๋„ˆ๋ฌด ๋ฐฉ๋Œ€ํ•˜๋‹ค๋ณด๋‹ˆ ์ดˆ์‹ฌ์ž์˜ ์ž…์žฅ์—์„œ๋Š” ์ด ์žฅ์ ์„ ์ž˜ ํ™œ์šฉํ•˜๊ธฐ ์–ด๋ ค์› ๋‹ค.

 

ํ•˜๋‚˜๋ถ€ํ„ฐ ์—ด๊นŒ์ง€ ๋ชจ๋‘ ์ปค์Šคํ…€ํ•˜๋ ค๋ฉด ๊ทธ ํ•˜๋‚˜๋ถ€ํ„ฐ ์—ด๊นŒ์ง€์˜ ์†์„ฑ๋“ค์„ ์ž˜ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•˜๋Š”๋ฐ

๋‚˜์—๊ฒŒ๋Š” ์•„์ง ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ์†์„ฑ๋“ค์ด ๋งŽ์•„์„œ ๋ฌด์—‡์„, ์–ผ๋งˆ๋‚˜ ์ปค์Šคํ…€ํ•˜๋ฉด ์ข‹์„์ง€ ์‰ฝ์‚ฌ๋ฆฌ ์ •ํ•˜๊ธฐ ์–ด๋ ค์› ๋‹ค.

 

๊ทธ๋ž˜์„œ UI๊ฐ€ ๊น”๋”ํ•˜๋ฉด์„œ๋„ ์‚ฌ์šฉ๋ฒ•์ด ๊ฐ„๋‹จํ•˜๊ณ , ์ ๋‹นํžˆ ์ปค์Šคํ…€ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค๋ฅธ ์ฐจํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ฐพ๋˜ ์ค‘

recharts๋ฅผ ๋ฐœ๊ฒฌํ•˜๊ณ  ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

 

https://recharts.org/en-US/

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” chart.js๊ฐ€ ์œ ๋ช…ํ•˜๊ธด ํ•˜์ง€๋งŒ ๋ฆฌ์•กํŠธ์—์„œ๋Š” react-chartjs๊ฐ€ ๋”ฐ๋กœ ์žˆ์–ด์„œ

npm ํŠธ๋ Œ๋“œ์—์„œ react-chartjs์™€ recharts๋ฅผ ๋น„๊ตํ•ด๋ณด์•˜๋‹ค.

 

 

recharts ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ•œ ์ด์œ ๋Š”,

 

1) ์šฐ์„  Stars ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋งŽ์•„์„œ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ฐพ๊ธฐ ์‰ฌ์šธ ๊ฒƒ ๊ฐ™์•˜๊ณ ,

2) ์—…๋ฐ์ดํŠธ ์ผ์ž๊ฐ€ ๊ฐ€์žฅ ์ตœ๊ทผ์ด๋ฉฐ,

3) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ƒ๊ธด ์‹œ๊ธฐ๋Š” ๋น„์Šทํ•˜์ง€๋งŒ ์ด์Šˆ ์ˆ˜๊ฐ€ ๋งŽ์€ ์ ์ด ๋งˆ์Œ์— ๋“ค์—ˆ๋‹ค!

 

๊ทธ๋ฆฌ๊ณ  ๋ฌด์—‡๋ณด๋‹ค ๊ณต์‹ ๋ฌธ์„œ์— Examples๋‚˜ API ์ •๋ฆฌ๊ฐ€ ๊ต‰์žฅํžˆ ์ž˜ ๋˜์–ด ์žˆ์–ด์„œ

์ดˆ์‹ฌ์ž๊ฐ€ ์ปค์Šคํ…€ํ•˜๊ธฐ์—๋„ ํฐ ์–ด๋ ค์›€์ด ์—†์„ ๊ฒƒ ๊ฐ™์•˜๊ณ  ์‹ค์ œ๋กœ๋„ ๊ทธ๋žฌ๋‹ค. 

 

 

 

๋‚˜๋Š” ์ตœ๋Œ€ํ•œ ์‹ฌํ”Œํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  ์‹ถ์–ด์„œ ์†์„ฑ๋“ค์„ ๋งŽ์ด ๋œ์–ด๋‚ด๊ณ  ์‚ฌ์šฉํ–ˆ๋‹ค.

 

๋ผ์ธ ์ฐจํŠธ ์ปดํฌ๋„ŒํŠธ
/* DictChart.js */

/* ์‚ฌ์šฉํ•  ์†์„ฑ๋“ค์„ import ํ•œ๋‹ค. */
import { ResponsiveContainer, LineChart, Line, XAxis, CartesianGrid, Tooltip } from 'recharts'

const DictChart = ({ chartData }) => {
  return (
    <>
      <ResponsiveContainer maxWidth={400} maxHeight={240}>
        <LineChart
          /* ์ฐจํŠธ๋กœ ๊ทธ๋ฆด ๋ฐ์ดํ„ฐ ์ฃผ์ž… */
          data={data}
          margin={{
            top: 10,
            right: 0,
            left: 0,
            bottom: 0,
          }}
        >
          <CartesianGrid stroke="#e5e5e5" strokeDasharray="3 3" />
          <XAxis dataKey="name" fontSize="12px" fontWeight="500" padding={{ left: 10, right: 10 }} />
          <Tooltip content={<CustomTooltip />} />
          <Line type="linear" isAnimationActive={true} animationDuration={1500} dataKey="cnt" stroke="#6698FC" strokeWidth={2} activeDot={{ r: 8 }} />
        </LineChart>
      </ResponsiveContainer>
    </>
  )
}

export default DictChart

 

์ฐจํŠธ์— ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ
/* ๋ผ์ธ ์ฐจํŠธ์— ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ */
/* ์ถ”ํ›„ ์„œ๋ฒ„์—์„œ ๋ฐ›์•„์˜ค๋Š” ๋ฐ์ดํ„ฐ๋กœ ์ˆ˜์ •ํ•  ์˜ˆ์ •! */

const data = [
  {
    name: 'ํ† ',
    cnt: 10,
  },
  {
    name: '์ผ',
    cnt: 19,
  },
  {
    name: '์›”',
    cnt: 10,
  },
  {
    name: 'ํ™”',
    cnt: 34,
  },
  {
    name: '์ˆ˜',
    cnt: 10,
  },
  {
    name: '๋ชฉ',
    cnt: 8,
  },
  {
    name: '์˜ค๋Š˜',
    cnt: 22,
  },
]

 

ํˆดํŒ ์ปค์Šคํ…€ํ•˜๊ธฐ
/* customedTooltip */

const CustomTooltip = ({ active, payload, label }) => {
  if (active && payload && payload.length) {
    return (
      <Container>
        <p className="label">{`${label} : ${payload[0].value}`}</p>
      </Container>
    )
  }

  return null
}

/* styled-components */

const Container = styled.div`
  width: fit-content;
  height: 36px;
  padding: 8px;
  background-color: #fff;
  opacity: 0.85;
  border: 1px solid ${({ theme }) => theme.colors.line};
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  .label {
    font-size: ${({ theme }) => theme.fontSizes.base};
  }
`

 

Line์— ์ ์šฉํ•œ ์†์„ฑ
type : ๋ผ์ธ ๋””์ž์ธ, String / ์ง์„ ์€ "linear", ๊ณก์„ ์€ "monotones"
isAnimationAcitve : ์• ๋‹ˆ๋ฉ”์ด์…˜ ํ™œ์„ฑํ™”, Boolean / default false
animationDuration : ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ง„ํ–‰ ์‹œ๊ฐ„, Number / ๋‹จ์œ„๋Š” ๋ฐ€๋ฆฌ์„ธ์ปจ๋“œ
stroke : ๋ผ์ธ ์ปฌ๋Ÿฌ, String
strokeWidth : ๋ผ์ธ ๊ตต๊ธฐ, Number
activeDot : ๋งˆ์šฐ์Šค ์˜ค๋ฒ„ ์‹œ ํ™•๋Œ€ํ•  Dot ํฌ๊ธฐ

 

 

LineChart์—๋Š” maxWidth, maxHeight ์†์„ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ณ  width, height ๋„ % ๋‹จ์œ„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

๋ฌด์กฐ๊ฑด ๊ณ ์ •๋œ pixel ๋‹จ์œ„์ด๋‹ค.

 

์ฒ˜์Œ์—๋Š” ResponsiveContainer๋ฅผ ์ œ๊ณตํ•˜๋Š” ์ค„ ๋ชจ๋ฅด๊ณ  ๋ฐ˜์‘ํ˜•์œผ๋กœ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค์ง€ ๊ณ ๋ฏผํ–ˆ๋Š”๋ฐ

๊ตฌ๊ธ€๋ง ํ•ด๋ณด๋‹ˆ ๋ฐ˜์‘ํ˜• ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋”ฐ๋กœ ์ง€์›ํ•˜๊ณ  ์žˆ์–ด์„œ import ํ›„

ResponsiveContainer ํƒœ๊ทธ์— maxWidth์™€ maxHeight๋ฅผ ์„ค์ •ํ–ˆ๋”๋‹ˆ ํ™”๋ฉด์ด ์ค„์–ด๋“ค์—ˆ์„ ๋•Œ ๊ฐ™์ด ์ž˜ ์ค„์–ด๋“ ๋‹ค!

 

๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณต๋˜๋Š” ํˆดํŒ๋„ ๋งˆ์Œ์— ์•ˆ ๋“ค์—ˆ๋Š”๋ฐ 

๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐพ์•„๋ณด๋‹ˆ ์ปค์Šคํ…€ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์ž˜ ๋‚˜์™€ ์žˆ์–ด์„œ ์ฐพ์•„๋ณด๊ณ  ์ ์šฉํ–ˆ๋‹ค.

 

์—ญ์‹œ ๋ญ”๊ฐ€ ์ž˜ ์•ˆ ๋  ๋•Œ๋Š” ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐพ์•„๋ณด๋Š” ๊ฒŒ ๋‹ต์ธ ๊ฒƒ ๊ฐ™๋‹ค!

 

 

 

๋งํฌ
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Total
Today
Yesterday