-
#4 Am Chart 사용Java & Spring 2019. 6. 14. 01:14
Am Chart 사용
처음 사용해보는 차트여서 그런지 몰라도 적용시키는게 조금 헷갈렸지만
열심히 구글링하면서 해결!
// Controller
1234567891011121314151617181920212223// 실내 모니터링에서 차트@ResponseBody@RequestMapping(value = "/insideChart", method = RequestMethod.GET)public JSONArray insideChart(Model model, HttpServletRequest request, HttpSession session) {String member_id = ((MemberVO) request.getSession().getAttribute("user")).getMember_id();String date = request.getParameter("date");ArrayList<MeasureDataVO> list = manageService.insideChart(member_id, date);model.addAttribute("list" + list);JSONArray jArray = new JSONArray();for (int i = 0; i < list.size(); i++) {JSONObject json = new JSONObject();json.put("measure", list.get(i).getMeasure()); // 측정 값json.put("TODAY", list.get(i).getTODAY()); // 현재 날짜json.put("measuretime", list.get(i).getMeasuretime()); // 측정 시간json.put("CODE", list.get(i).getCODE()); // 측정 코드json.put("iotID", list.get(i).getIotID()); // 기기 IDjArray.add(json);}return jArray;}cs // jsp
1234567891011121314151617181920212223242526272829<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@include file="../include/header.jsp"%><link rel="stylesheet" href="/resources/css/manage/monitoringIn.css"><div id="inside"><%-- AmChart 시작 --%><div id="chartdiv"></div><%-- AmChart 끝 --%><%-- 달력 선택 시작 --%><input type="date" id="measureDate" onchange="measureDate(this)" /><%-- 달력 선택 끝 --%><div id="chartList"><h3>측정 상세 정보</h3><table class="table"><th class="table__heading">Today</th><th class="table__heading">측정 값<sub>(㎍/㎥)</sub></th><th class="table__heading">기기 ID</th><th class="table__heading">측정 코드</th><tbody class="chartList-list"></tbody></table></div></div><script src="resources/js/core.js"></script><script src="resources/js/charts.js"></script><script src="https://www.amcharts.com/lib/4/themes/spiritedaway.js"></script><script src="resources/js/manage/monitoringIn.js"></script><%@include file="../include/footer.jsp"%>cs // js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129var ip = sessionStorage.getItem('IP_ADDRESS');var user = JSON.parse(sessionStorage.getItem("user"));var member_id = user.member_id;/*날짜 선택하기 시작*/function measureDate(object) {console.log(object.value); // 20xx-xx-xx 형식으로 리턴됨var date = object.value;chartDetail(date);}/* 날짜 선택하기 끝 *//* 현재 날짜 세팅하기 시작 */document.getElementById('measureDate').value = new Date().toISOString().substring(0, 10);var date = document.getElementById('measureDate').value;chartDetail(date);/* 현재 날짜 세팅하기 끝 *//* 차트 불러오는거 시작 */function chartDetail(date) {$.ajax({crossDomain: true,type : "GET",contentType: "application/json; charset=utf-8",url : ip+"/m.insideChart?date="+date+"&member_id="+member_id,headers: { "Access-Control-Allow-Origin": "*" },dataType : "json",async : false,success : function(data) {console.log(data);am4core.ready(function() {// Themes beginam4core.useTheme(am4themes_spiritedaway);// Themes end// Create chart instancevar chart = am4core.create("chartdiv", am4charts.XYChart);// Add datachart.data = data;console.log(chart.data);// Set input format for the dateschart.dateFormatter.inputDateFormat = "yyyy MMMM dd hh:mm a";// Create axesvar dateAxis = chart.xAxes.push(new am4charts.DateAxis());var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());valueAxis.tooltip.disabled = true;valueAxis.title.text = "미세먼지 측정 값(㎍/㎥)";dateAxis.baseInterval = {"timeUnit" : "second","count" : 1};dateAxis.tooltipDateFormat = "yyyy MMMM dd hh:mm a";dateAxis.dateFormats.setKey("second", "hh:mm:ss a");// Create seriesvar series = chart.series.push(new am4charts.LineSeries());series.dataFields.valueY = "measure";series.dataFields.dateX = "measuretime";series.tooltipText = "{measure}"series.strokeWidth = 2;series.minBulletDistance = 15;// Drop-shaped tooltipsseries.tooltip.background.cornerRadius = 20;series.tooltip.background.strokeOpacity = 0;series.tooltip.pointerOrientation = "vertical";series.tooltip.label.minWidth = 40;series.tooltip.label.minHeight = 40;series.tooltip.label.textAlign = "middle";series.tooltip.label.textValign = "middle";// Make bullets grow on hovervar bullet = series.bullets.push(new am4charts.CircleBullet());bullet.circle.strokeWidth = 2;bullet.circle.radius = 4;bullet.circle.fill = am4core.color("#fff");var bullethover = bullet.states.create("hover");bullethover.properties.scale = 1.3;// Make a panning cursorchart.cursor = new am4charts.XYCursor();chart.cursor.behavior = "none";chart.cursor.xAxis = dateAxis;chart.cursor.snapToSeries = series;// Create vertical scrollbar and place it before the value axis// chart.scrollbarY = new am4core.Scrollbar();// chart.scrollbarY.parent = chart.leftAxesContainer;// chart.scrollbarY.toBack();// Create a horizontal scrollbar with previe and place it// underneath the// date axis// chart.scrollbarX = new am4charts.XYChartScrollbar();// chart.scrollbarX.series.push(series);// chart.scrollbarX.parent = chart.bottomAxesContainer;chart.events.on("ready", function() {// dateAxis.zoom({start:0.79, end:1});}); // end am4core.ready()}); // 차트 끝// appendvar list = "";for (var i = 0; i < data.length; i++) {list += '<tr class="table__row">'+ '<td class="table__content">' + data[i].measuretime + '</td>'+ '<td class="table__content">' + data[i].measure + '<sub>(㎍/㎥)</sub></td>'+ '<td class="table__content">' + data[i].iotID + '</td>'+ '<td class="table__content">' + data[i].CODE + '</td>'+ '</tr>';}$(".chartList-list td").remove();$(".chartList-list").append(list);}, // SUCCESSerror : function() {console.log("연결 실패");}});}cs // css
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273@charset "UTF-8";:root {--yellow: #ffc842;}* {margin: 0;padding: 0;box-sizing: border-box;}body {font-weight: 400;font-family: system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue",sans-serif;}td,th {padding: 0.5rem;}th {text-align: left;font-weight: 300;font-size: 1.25rem;}.table {width: 100%;padding: 1rem;}h3{text-align: center;}.table__heading {border-bottom: 2px solid var(--yellow);}@media (max-width: 32rem) {.table__heading {display: none;}.table__content {display: block;padding: 0.5rem 0;}.table__row {margin: 0.25rem 1rem;padding: 0.5rem 0;display: block;border-bottom: 2px solid var(--yellow);}.table__content:before {content: attr(data-heading);display: inline-block;width: 5rem;margin-right: 0.5rem;color: #999;font-size: 0.75rem;font-weight: 700;font-family: inherit;text-transform: uppercase;letter-spacing: 2px;}}cs // 실행 화면'Java & Spring' 카테고리의 다른 글
#14 Static 키워드 (0) 2019.12.09 #11 ArrayList ? (0) 2019.07.05 #10 getter/setter 사용하기 (0) 2019.07.01 #9 HashMap 응용 - 게시판 만들기 (0) 2019.06.27 #8 HashMap 사용하기 (0) 2019.06.27