Notice
Recent Posts
Recent Comments
Link
ยซ   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
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

billboard.js ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž. (pie chart ์˜ˆ์‹œ) ๋ณธ๋ฌธ

๐Ÿง‘‍๐Ÿ’ป Web/JavaScript

billboard.js ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž. (pie chart ์˜ˆ์‹œ)

Lennon 2022. 3. 24. 16:43
728x90
๋ฐ˜์‘ํ˜•

billboard.js ๋ž€?

C3.js๋ฅผ ๋„ค์ด๋ฒ„์—์„œ ๋ณ€ํ˜•ํ•˜์—ฌ ๋ถˆํŽธํ•œ ๋ถ€๋ถ„์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์„œ ์˜คํ”ˆ์†Œ์Šค๋กœ ๋ฐฐํฌํ•œ ๊ฒƒ์ด๋‹ค.

์ฐจํŠธ๋ฅผ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ณ , ๋‚ด๋ถ€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์‰ฝ๊ฒŒ ์ฐจํŠธ๋ฅผ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

https://naver.github.io/billboard.js/

 

billboard.js

billboard.js, the "chart" library The name "billboard" comes from the famous "billboard chart" which everybody knows. billboard.js provides the easiest way to create a 'chart' instantly. Quick Start Guide Step 1. Load billboard.js and D3.js Step 2. Setup y

naver.github.io

 

์‚ฌ์šฉ๋ฒ•์€ ๊ฐ„๋‹จํ•˜๋‹ค. ์œ„ ํŽ˜์ด์ง€์— ๋“ค์–ด๊ฐ€์„œ ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด billboard.js๋ฅผ install ํ•ด์ฃผ๋„๋ก ํ•˜์ž.

npm install billboard.js
npm install billboard.js@next

๊ทธ๋ฆฌ๊ณ  ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋กœ ์—ฐ๊ฒฐํ•ด๋ณด์ž.

 

 

์ฒซ ๋ฒˆ์งธ

for production์—

billboard.min.js์™€ billboard.min.css ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ๋ณธ์ธ ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋„ฃ์–ด์ฃผ๊ณ 

jsํŒŒ์ผ, cssํŒŒ์ผ๊ณผ ๊ฐ™์ด <script> <link> ํŒŒ์ผ๋กœ ์—ฐ๊ฒฐํ•ด์ฃผ๊ฑฐ๋‚˜

 

(๐Ÿ‘๐Ÿผ) ๋‘ ๋ฒˆ์งธ

๊ทธ๋ƒฅ ๋งํฌ๋กœ ๊ฐ€์ ธ์™€์„œ ์—ฐ๊ฒฐํ•ด์ฃผ์ž!

  <link rel="stylesheet" href="https://naver.github.io/billboard.js/release/latest/dist/theme/datalab.min.css">
  <script src="https://naver.github.io/billboard.js/release/latest/dist/billboard.pkgd.min.js"></script>

 

๊ทธ๋ฆฌ๊ณ  ํ”„๋กœ์ ํŠธ์— ๋“ค์–ด๊ฐ€ ํ‘œ์‹œํ•˜๊ณ  ์‹ถ์€ ๋ถ€๋ถ„์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์ž. (pie chart ์˜ˆ์‹œ)

https://naver.github.io/billboard.js/demo/ ๋” ๋งŽ์€ ์ฐจํŠธ ์˜ˆ์‹œ๋Š” ํ•ด๋‹น url๋ฅผ ์ฐธ๊ณ ํ•˜์ž.

<div id="pieChart"></div>

๊ทธ๋ฆฌ๊ณ  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ๋งŒ๋“ค๊ฑฐ๋‚˜, 

html์•„๋ž˜ script๋กœ ๋ฌถ์–ด ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ด๋ณด์ž.

var chart = bb.generate({
  data: {
    columns: [
	["data1", 30],
	["data2", 120]
    ],
    type: "pie", // for ESM specify as: pie()
    onclick: function(d, i) {
	console.log("onclick", d, i);
   },
    onover: function(d, i) {
	console.log("onover", d, i);
   },
    onout: function(d, i) {
	console.log("onout", d, i);
   }
  },
  bindto: "#pieChart"
});

setTimeout(function() {
	chart.load({
		columns: [
			["setosa", 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.2, 0.1, 0.1, 0.2, 0.4, 0.4, 0.3, 0.3, 0.3, 0.2, 0.4, 0.2, 0.5, 0.2, 0.2, 0.4, 0.2, 0.2, 0.2, 0.2, 0.4, 0.1, 0.2, 0.2, 0.2, 0.2, 0.1, 0.2, 0.2, 0.3, 0.3, 0.2, 0.6, 0.4, 0.3, 0.2, 0.2, 0.2, 0.2],
			["versicolor", 1.4, 1.5, 1.5, 1.3, 1.5, 1.3, 1.6, 1.0, 1.3, 1.4, 1.0, 1.5, 1.0, 1.4, 1.3, 1.4, 1.5, 1.0, 1.5, 1.1, 1.8, 1.3, 1.5, 1.2, 1.3, 1.4, 1.4, 1.7, 1.5, 1.0, 1.1, 1.0, 1.2, 1.6, 1.5, 1.6, 1.5, 1.3, 1.3, 1.3, 1.2, 1.4, 1.2, 1.0, 1.3, 1.2, 1.3, 1.3, 1.1, 1.3],
			["virginica", 2.5, 1.9, 2.1, 1.8, 2.2, 2.1, 1.7, 1.8, 1.8, 2.5, 2.0, 1.9, 2.1, 2.0, 2.4, 2.3, 1.8, 2.2, 2.3, 1.5, 2.3, 2.0, 2.0, 1.8, 2.1, 1.8, 1.8, 1.8, 2.1, 1.6, 1.9, 2.0, 2.2, 1.5, 1.4, 2.3, 2.4, 1.8, 1.8, 2.1, 2.4, 2.3, 1.9, 2.3, 2.5, 2.3, 1.9, 2.0, 2.3, 1.8],
		]
	});
}, 1500);

setTimeout(function() {
	chart.unload({ ids: "data1" });
	chart.unload({ ids: "data2" });
}, 2500);

๊ทธ๋Ÿผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฐจํŠธ๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

๊ทธ๋Ÿฌ๋‚˜ ์ฐจํŠธ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹ฌ๋ ฅ์ฒ˜๋Ÿผ ์“ฐ๋ ค๋ฉด % ํ‘œ์‹œ, legend ๋“ฑ ์—†์• ๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

 

๊ทธ๋Ÿด ๋• ์•„๋ž˜ url์—์„œ ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ์ฐพ์•„ ์ ์šฉ์‹œํ‚ค๋ฉด ๋œ๋‹ค. 

https://naver.github.io/billboard.js/release/latest/doc/

 

์•„๋ž˜๋Š” ์œ„ ๋‹ฌ๋ ฅ๊ณผ ๊ฐ™์ด % ์™€ legend ์•ˆ ๋ณด์—ฌ์ฃผ๊ณ  ๊ฐ ์ƒ‰์ƒ๊ณผ ํฌ๊ธฐ๋ฅผ ์ƒˆ๋กœ ์ง€์ •ํ•œ pie chart ์˜ˆ์‹œ์ด๋‹ค.

์ฃผ์„์„ ํ†ตํ•ด ์ดํ•ดํ•ด๋ณด๋„๋ก ํ•˜์ž.

์•„๋ฌด๊ฒƒ๋„ ์•ˆ ๋ณด์ด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ ๋งˆ์šฐ์Šค ์˜ฌ๋ฆฌ๋ฉด ๋ฌด์—‡์ด ๋ช‡ % ์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

  const study = document.querySelector('#study').innerHTML;
  const reading = document.querySelector('#reading').innerHTML;
  const exercise = document.querySelector('#exercise').innerHTML;
  const walk = document.querySelector('#walk').innerHTML;
  const love = document.querySelector('#love').innerHTML;
  const other = document.querySelector('#other').innerHTML;
 
 const chart = bb.generate({
      data: {
        columns: [  // ๊ฐ ๋ฐ์ดํ„ฐ ์ง€์ • [ํ‚ค, ๊ฐ’]
          ['๊ณต๋ถ€', study],
          ['๋…์„œ', reading],
          ['์šด๋™', exercise],
          ['์‚ฐ์ฑ…', walk],
          ['๋ฐ์ดํŠธ', love],
          ['๊ธฐํƒ€', other],
        ], 
        type: 'pie', // pie chart ์‚ฌ์šฉ
      },
      pie: {  // pie chart ๋ผ๋ฒจ(%) ์‚ฌ์šฉ ์•ˆํ•˜๊ธฐ
        label: {
          show: false,
        },
      },

      bindto: `#pieChart`, // document ์…€๋ ‰ํ„ฐ๋ฅผ ์ฐพ์•„ ๊ฑฐ๊ธฐ์— chart๋ฅผ ๋„ฃ์–ด์คŒ
    });

    chart.legend.hide(); // legend ์ˆจ๊น€(๋ฐ‘์— ํ‘œ์‹œ๋˜๋Š” ๊ณต๋ถ€, ๋…์„œ, ์šด๋™ ๋“ฑ๋“ฑ)

    chart.data.colors({
      ๊ณต๋ถ€: '#3e95cd',
      ๋…์„œ: '#8e5ea2',
      ์šด๋™: '#3cba9f',
      ์‚ฐ์ฑ…: '#e8c3b9',
      ๋ฐ์ดํŠธ: '#c45850',
      ๊ธฐํƒ€: '#000000',
    }); // ์ปฌ๋Ÿฌ ์ง€์ •

    chart.resize({ // chartํฌ๊ธฐ ์ง€์ •
      width: 85,
      height: 100,
    });
  });

 

728x90
๋ฐ˜์‘ํ˜•
Comments