JavaScript
[JavaScript] ํ์ด์ฐจํธ(HighCharts) ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด์ฉํด์ ๊ทธ๋ํ ๋ง๋ค๊ธฐ
carsumin
2023. 5. 24. 16:11
ํ์ด์ฐจํธ (HighCharts)
์์ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ์์ฑ๋ ์ฐจํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="dataset.js"></script>
</head>
<body>
<div id="container_scatter" style="min-width: 310px; height: 400px; max-width: 1900px; margin: 0 auto;"></div>
<div id="container_line" style="height: 400; width: 1900px; margin: 0 auto;"></div>
</body>
</html>
- ํ์ด์ฐจํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์ script ์ถ๊ฐ ํ์
- div ํ๊ทธ์ id๋ฅผ ํ ๋นํด์ ์ฐจํธ๊ฐ ๋ค์ด๊ฐ ์์ญ์ ๋ง๋ค์ด์ค
$(document).ready(function() {
//์์์ dataset ๊ฐ์ฒด ์์ฑ (A-Z)
const dataObj = {};
for(let i=65; i<=90; i++){
let alphabet = String.fromCharCode(i);
let dataArr = [];
for(let j=0; j<=100; j++){
let x = parseFloat((Math.random() * 50).toFixed(1));
dataArr.push([x]);
}
dataObj['DATA_' + alphabet] = dataArr;
}
//HighCharts์ series
let seriesData = [];
for(let key in dataObj){
if(dataObj.hasOwnProperty(key)){
let series = {
name: key,
data: dataObj[key]
};
seriesData.push(series);
}
}
//HighCharts์ categories
let categories = Object;
//scatter chart
Highcharts.chart('container_scatter', {
chart: {
type: 'scatter',
zoomType: 'xy'
},
title: {
text: 'Sample Scatter Chart'
},
xAxis: {
title: {
enabled: true,
text: 'x data',
align: 'low',
margin: 50
},
startOnTick: true,
endOnTick: true,
showLastLabel: true
},
yAxis: {
title: {
enabled: true,
text: 'y data',
align: 'low'
}
},
legend: {
layout: 'horizontal',
align: 'right',
verticalAlign: 'bottom',
x: 0,
y: 7,
floating: true,
itemStyle: {
fontWeight: 'normal'
}
},
navigation: {
buttonOptions: {
theme: {
states: {
hover: {
fill: '#fff'
},
select: {
fill: '#f7f8fa'
}
}
}
},
menuItemStyle: {
fontWeight: 'normal',
background: 'none'
},
menuItemHoverStyle: {
fontWeight: 'bold',
background: '#1784B0',
color: '#fff'
}
},
plotOptions: {
scatter: {
marker: {
radius: 3,
states: {
hover: {
enabled: true,
}
}
},
states: {
hover: {
marker: {
enabled: false
}
}
},
tooltip: {
headerFormat: '{series.name}<br>',
pointFormat: '{point.x}'
}
}
},
series: seriesData
});
//line chart
let chart = new Highcharts.Chart({
chart: {
renderTo: 'container_line',
marginBottom: 120
},
title: {
text: 'Sample Line Chart'
},
xAxis: {
categories: categories,
labels: {
rotation: 90
}
},
series: seriesData
});
});
- ์ฐจํธ์ ๋ค์ด๊ฐ ์์์ ๋ฐ์ดํฐ์ ๋ง๋ค์ด์ค (์์์ ์ฒซ์งธ์๋ฆฌ์ ์ค์ํ ๋ฐ์ดํฐ)
- ๋ถ์ฐํ(scatter), ๊บพ์์ ํ(line) ๋ ๊ฐ์ง ์ ํ์ ์ฐจํธ
- ๋ฐ์ดํฐ์ ์ ๊ฐ๋ค์ ํ์ด์ฐจํธ์ series ์ต์ ์ ํ ๋น