billboard.js ์ ๋ํด ์์๋ณด์. (pie chart ์์)
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,
});
});