2019. 11. 12. 13:29 프로그래밍/java script
billboard.js를 사용한 실시간 라인 Chart 그려주기
나는 서버에서 특정 api에 요청을 하여 받아온 api 결과 값들을 서버에 존재하는 queue에 관리하도록 하고
view 단에서는 서버에 존재하는 queue의 데이터를 받아와 그래프로 그려주는 형식으로 만들었다.
<script> 영역
line_net();
function line_net() {
getNetChartData().then(result => {
let data_Net = getDrawNetChartData(result);
let dataXColum = [];
let dataInputNet = [];
let dataOutputNet = [];
data_Net.forEach((dataItem) => {
dataXColum.push(dataItem[0]);
dataInputNet.push(dataItem[1]);
dataOutputNet.push(dataItem[2]);
});
var chart = bb.generate({
data: {
x : "Time",
columns: [
dataXColum,
dataInputNet,
dataOutputNet
]
},
point: {
show: false
},
axis: {
x : {
type: "timeseries",
localtime: false,
tick : {
format : "%H:%M",
count : 5,
},
}
},
size: {
height: 150,
width: 450
},
legend: {
show: false
},
bindto: "#lineChart"
}); // chart가 끝나는 부분
function netInit() {
netInterval = window.setInterval(function () { draw();
},30000);
};
function draw() {
getNetChartData().then(result => {
let data_Net = getDrawNetChartData(result);
let dataXColum = [];
let dataInputNet = [];
let dataOutputNet = [];
data_Net.forEach((dataItem) => {
dataXColum.push(dataItem[0]);
dataInputNet.push(dataItem[1]);
dataOutputNet.push(dataItem[2]);
});
chart.load({
columns: [
dataXColum,
dataInputNet,
dataOutputNet
]
});
});
clearInterval(netInterval);
netInit();
};
netInit();
});
}
function getNetChartData(){
return new Promise((resolve, reject) => {
$.ajax({
type: "GET",
url: "/chart/networkInOut",
success: function (result) {
resolve(result);
}
});
});
}
function getDrawNetChartData(apiResultData){
let NetCharData = [
['Time', 'InputNet','outputNet']
];
apiResultData.forEach((chartItem) => {
let date = chartItem.currentTime;
let NetIn = (chartItem.networkInValue)/1000;
let NetOut = (chartItem.networkOutValue)/1000;
if(date != 0 && NetIn != 0 && NetOut != 0){
NetCharData.push([date, NetIn, NetOut]);
} else if(date !=0 && NetIn !=0){
NetOut=0;
NetCharData.push([date, NetIn, NetOut]);
} else if(date !=0 && NetOut !=0){
NetIn=0;
NetCharData.push([date, NetIn, NetOut]);
} else if(date !=0){
NetIn=0;
NetOut=0;
NetCharData.push([date, NetIn, NetOut]);
}
});
return NetCharData;
};
서버는 이거랑 같이 정리 좀 하고 올려야겠군
'프로그래밍 > java script' 카테고리의 다른 글
billnoard.js를 이용한 실시간 Donut Chart (0) | 2019.11.12 |
---|---|
파일 크기 변환(convertSize) (0) | 2019.11.12 |
BootStrap Modal-element를 한 번만 생성하여 재활용하기 (0) | 2019.11.12 |
javascript에서 배열을 json화 하기 (0) | 2019.11.07 |
디비 값 출력하기 (0) | 2018.08.01 |