BemsWebApplication.LocEnergyConsumption = function (params, viewInfo) { "use strict"; var BUILDING_DEPTH = 1, FLOOR_DEPTH = 2, ZONE_DEPTH = 3; var CODE = $Code, FuelType = CODE.FuelType, FT = CODE.FacilityType, FC = CODE.FacilityCode, F = CODE.Formula, TIT = CODE.TimeIntervalType; var visiblePopup = ko.observable(false); var initialized = false, shouldReload = false, dataSource; var pointHistoryGet = new BWA.Chart.PointHistoryGet(BWA.UserInfo.SiteId()), timeBoxForSearch = BWA.SearchView.createDateTimeBox(), formulaGet = new BWA.Chart.FormulaGet(BWA.UserInfo.SiteId()); var UnionfuelType = 4, UnionfuelName = "혼합"; var buildingDataSource = BemsWebApplication.db.createDataSource('CmBuilding', true), floorDataSource = BemsWebApplication.db.createDataSource('CmFloor'), zoneDataSource = BemsWebApplication.db.createDataSource('CmZone'), fuelTypesDataSource = BemsWebApplication.db.createDataSource('BemsFuelType'), BemsFactorToeDataSource = BemsWebApplication.db.createDataSource('BemsFactorToe'), buildings = ko.observableArray(), timeTypesForSearch = ko.observableArray(), timeTypeIdForSearch = ko.observable(0), floorsForSearch = ko.observableArray(), zonesForSearch = ko.observableArray(), fuelTypesForSearch = ko.observableArray(), FactorToe = ko.observableArray(), buildingIdForSearch = ko.observable(), buildingNameForSearch = ko.observable(), floorIdForSearch = ko.observable(), floorNameForSearch = ko.observable(), zoneIdForSearch = ko.observable(), zoneNameForSearch = ko.observable(), fuelTypeIdForSearch = ko.observable(), deferredForSearch = new $.Deferred(); var deferredForLoadData = new $.Deferred(); var chart = BWA.Chart.Instance('chartLEC', 'dxChart'); var locationEnableForSearch = ko.observable(true); var chartDataArray = ko.observableArray(); // 2017.09.08 : 그리드 도표 작업 : 팝업창 추가 // 팝업창뷰 보이기 속성 제어용 변수 선언 var DataGridPopup_Visible = ko.observable(false); // 그래프 출력 이후 엑셀출력을 위한 팝업창을 위해 임시 저장용 배열 선언 var dataArrayresult = ko.observableArray(); // 2017.09.08 : 그리드 도표 작업 : 팝업창 추가 chartDataArray([ { DateTime: new Date(2014, 11, 16, 0), OutdoorTemperature: -3.4, OutdoorHumidity: 40, Value: 30 }, { DateTime: new Date(2014, 11, 16, 0, 15), OutdoorTemperature: -1.1, OutdoorHumidity: 44, Value: 32 }, { DateTime: new Date(2014, 11, 16, 0, 30), OutdoorTemperature: 3, OutdoorHumidity: 48, Value: 33 }, { DateTime: new Date(2014, 11, 16, 0, 45), OutdoorTemperature: 4, OutdoorHumidity: 55, Value: 34 }, { DateTime: new Date(2014, 11, 16, 1), OutdoorTemperature: 8, OutdoorHumidity: 54, Value: 50 }, { DateTime: new Date(2014, 11, 16, 2), OutdoorTemperature: 10, OutdoorHumidity: 53, Value: 60 }, { DateTime: new Date(2014, 11, 16, 3), OutdoorTemperature: 11, Value: 80 }, { DateTime: new Date(2014, 11, 16, 4), OutdoorTemperature: 12, OutdoorHumidity: 48, Value: 100 }, { DateTime: new Date(2014, 11, 16, 5), OutdoorTemperature: 12, OutdoorHumidity: 40 }, { DateTime: new Date(2014, 11, 16, 6), Value: 120 } ]); var goalUpperBound = ko.observable(40); function handleClickTreeItem(element, data) { var eq = BWA.DataUtil.constructEqualFilter; var depth = data.depth; switch (depth) { case BUILDING_DEPTH: // building data = data.data; buildingIdForSearch(data.BuildingId()); buildingNameForSearch(data.Name()); floorIdForSearch(null); floorNameForSearch(''); zoneIdForSearch(null); zoneNameForSearch(''); break; case FLOOR_DEPTH: // floor buildingNameForSearch(data.parentData.data.Name()); data = data.data; buildingIdForSearch(data.BuildingId()); floorIdForSearch(data.FloorId()); floorNameForSearch(data.Name()); zoneIdForSearch(null); zoneNameForSearch(''); break; case ZONE_DEPTH: // zone floorNameForSearch(data.parentData.data.Name()); buildingNameForSearch(data.parentData.parentData.data.Name()); data = data.data; buildingIdForSearch(data.BuildingId()); floorIdForSearch(data.FloorId()); zoneIdForSearch(data.ZoneId()); zoneNameForSearch(data.Name()); break; } } function handleSearchViewInitializeUpdate() { $('#locationTreeViewInSearchViewTEMP').cwTreeView({ width: '40%', height: '480px', onClickTreeItem: handleClickTreeItem, delegateDataSource: BWA.DataUtil.getLocationDataSourceDelegateForTreeView( buildingDataSource, floorDataSource, zoneDataSource ) }); } function handleViewShown() { if (initialized === false) { chart = chart(BWA.ChartOptions.LocEnergyConsumption.getChartOptions(timeBoxForSearch.type(), goalUpperBound)); floorNameForSearch(''); zoneNameForSearch(''); initialized = true; } if (shouldReload) { } } function handleViewHidden() { visiblePopup(false); $SearchView.visibleObservable(false); // hcLee 2015 03 23 } function handleViewShowing() { $.when(fuelTypesDataSource.load(), buildingDataSource.load(), BemsFactorToeDataSource.load()).done(function (fuelTypes, dbBuildings, dbFactorToe) { var varFuelTypeId = ko.observable(false); var varName = ko.observable(false); varFuelTypeId(UnionfuelType); varName(UnionfuelName); fuelTypes = fuelTypes[0]; dbFactorToe = dbFactorToe[0]; FactorToe = dbFactorToe; //fuelTypes.push({ //혼합 // FuelTypeId: varFuelTypeId, // Name: varName //}); dbBuildings = dbBuildings[0]; fuelTypesForSearch(_.rest(fuelTypes)); deferredForSearch.resolve(); if (_.isEmpty(dbBuildings) === false) { buildingIdForSearch(dbBuildings[0].BuildingId()); buildingNameForSearch(dbBuildings[0].Name()); handleSearchInSearchView(); } }); } function refreshList() { } function SetPointer(values1, values2) { var len = 0; var tmpArray = new Array(); var values1MinMax = 0; var values2MinMax = 0; if (values1.length > values2.length) { len = values2.length; } else { len = values1.length; } if (len <= 1) { utils.toast.show("지표를 연산 할 데이터 범위가 적어도 하나 이상 존재해야 합니다."); $('#pointerText').text("상관성 지표 : 연산 데이터가 부족합니다."); } else { var values1Min = 9999; var values1Max = -9999; for (var i = 0; i < values1.length; i++) { if (values1[i].Value <= values1Min) values1Min = values1[i].Value; if (values1[i].Value >= values1Max) values1Max = values1[i].Value; } values1MinMax = values1Max - values1Min; var values2Min = 9999; var values2Max = -9999; for (var i = 0; i < values2.length; i++) { if (values2[i].Value <= values2Min) values2Min = values2[i].Value; if (values2[i].Value >= values2Max) values2Max = values2[i].Value; } values2MinMax = values2Max - values2Min; for (var i = 0; i < len - 1; i++) { tmpArray.push((((values1[i].Value - values1[i + 1].Value) / values1MinMax * 100) / ((values2[i].Value - values2[i + 1].Value) / values2MinMax * 100)).toFixed(2)); } var sum = 0; var cnt = 0; for (var j = 0; j < tmpArray.length; j++) { if (isFinite(tmpArray[j]) && !isNaN(tmpArray[j])) { sum = sum + Math.abs(parseFloat(tmpArray[j].toString())); cnt++; } } if (isFinite((sum / cnt).toFixed(2)) && !isNaN((sum / cnt).toFixed(2))) { $('#pointerText').text("상관성 지표 : " + (sum / cnt).toFixed(2).toString() + "%"); } else { $('#pointerText').text("상관성 지표 : 0%"); } } } var searchViewOptions = { searchViewItems: [ { id: 'FuelTypeId', ignoreValue: 0, defaultValue: $Code.FuelType.ELECTRICITY, value: fuelTypeIdForSearch, dataSource: fuelTypesForSearch } ], promiseDataInSearchView: deferredForSearch.promise() }; function handleSearchInSearchView() { visiblePopup(true); $("#large-indicator").dxLoadIndicator({ height: 60, width: 60 }); var date = timeBoxForSearch.getDate(), timeType = timeBoxForSearch.type(); var commonParameters = { TimeIntervalType: timeBoxForSearch.type(), StartDate: date.startDate, EndDate: date.endDate }; var pointHistoryParameters = { BuildingId: buildingIdForSearch(), FloorId: floorIdForSearch(), ZoneId: zoneIdForSearch(), FuelTypeId: fuelTypeIdForSearch() }; var temperatureParameters = { FacilityTypeId: FT.OUTDOOR, FacilityCode: FC.OUTDOOR, FormulaId: F[FT.OUTDOOR].TEMPERATURE, }; var humidityParameters = _.extend({}, temperatureParameters, { FormulaId: F[FT.OUTDOOR].HUMIDITY, }); if (pointHistoryParameters.FuelTypeId == 4) { var pointHistoryParametersELECTRICITY = { BuildingId: buildingIdForSearch(), FloorId: floorIdForSearch(), ZoneId: zoneIdForSearch(), FuelTypeId: $Code.FuelType.ELECTRICITY }; var pointHistoryParametersGAS = { BuildingId: buildingIdForSearch(), FloorId: floorIdForSearch(), ZoneId: zoneIdForSearch(), FuelTypeId: $Code.FuelType.GAS }; $.when( pointHistoryGet.apiGet(_.extend(pointHistoryParametersELECTRICITY, commonParameters)), pointHistoryGet.apiGet(_.extend(pointHistoryParametersGAS, commonParameters)), formulaGet.apiGet(_.extend(temperatureParameters, commonParameters))) .done(function (elevalues, gasvalues, temperatures) { var unionvalues, unionlength; var maxValue = 0; var dataArray = []; for (var i = 0; i < elevalues[0].length; i++) { elevalues[0][i].Value *= FactorToe[0].kcal(); } for (var j = 0; j < gasvalues[0].length; j++) { gasvalues[0][j].Value *= FactorToe[1].kcal(); } for (var i = 0; i < elevalues[0].length; i++) { for (var j = 0; j < gasvalues[0].length; j++) { if (elevalues[0][i].DateTime == gasvalues[0][j].DateTime) { elevalues[0][i].Value += gasvalues[0][j].Value; break; } } } SetPointer(elevalues[0], temperatures[0]); _.each(elevalues[0], function (x) { dataArray.push({ DateTime: BWA.Chart.getCustermTimeArgumentAxisString(timeBoxForSearch.type(), moment(x.DateTime).toDate()), Value: x.Value }); if (maxValue < x.Value) maxValue = x.Value; }); _.each(temperatures[0], function (x) { dataArray.push({ DateTime: BWA.Chart.getCustermTimeArgumentAxisString(timeBoxForSearch.type(), moment(x.DateTime).toDate()), Temperature: x.Value }); }); // 2017.09.08 : 그리드 도표 작업 : 팝업창 추가 // 출력용 maxValue = 0; var dataArray_Temp = []; var dataArray_Temp1 = []; var dataArray_Temp2 = []; _.each(elevalues[0], function (x) { dataArray_Temp1.push({ DateTime: BWA.Chart.getCustermTimeArgumentAxisString(timeBoxForSearch.type(), moment(x.DateTime).toDate()), Value: x.Value }); if (maxValue < x.Value) maxValue = x.Value; }); _.each(temperatures[0], function (x) { dataArray_Temp2.push({ DateTime: BWA.Chart.getCustermTimeArgumentAxisString(timeBoxForSearch.type(), moment(x.DateTime).toDate()), Temperature: x.Value }); }); // 배열을 하나로 합치자 var array_length_size = Math.max(dataArray_Temp1.length, dataArray_Temp2.length); // 크기가 동일한 경우에만 허용함 (예외 방어용) if (dataArray_Temp1.length == dataArray_Temp2.length) { for (var i = 0; i < array_length_size; i++) { dataArray_Temp.push( { DateTime: dataArray_Temp1[i].DateTime, Value: dataArray_Temp1[i].Value, Temperature: dataArray_Temp2[i].Temperature }); } } dataArrayresult = dataArray_Temp; // 2017.09.08 : 그리드 도표 작업 : 팝업창 추가 var options = BWA.ChartOptions.LocEnergyConsumption.getSeriesOptions(timeBoxForSearch.type(), UnionfuelType, goalUpperBound()); options.valueAxis[0].tickInterval = Math.round(maxValue / 10); options.dataSource = dataArray; chart().option(options); $('#st').text('' + "" + buildingNameForSearch() + " - " + floorNameForSearch() + " - " + zoneNameForSearch()); visiblePopup(false); }) .fail(function (error) { visiblePopup(false); }); } else { $.when( pointHistoryGet.apiGet(_.extend(pointHistoryParameters, commonParameters)), formulaGet.apiGet(_.extend(temperatureParameters, commonParameters))) .done(function (values, temperatures) { var maxValue = 0; var dataArray = []; SetPointer(values[0], temperatures[0]); _.each(values[0], function (x) { dataArray.push({ DateTime: BWA.Chart.getCustermTimeArgumentAxisString(timeBoxForSearch.type(), moment(x.DateTime).toDate()), Value: x.Value }); if (maxValue < x.Value) maxValue = x.Value; }); _.each(temperatures[0], function (x) { dataArray.push({ DateTime: BWA.Chart.getCustermTimeArgumentAxisString(timeBoxForSearch.type(), moment(x.DateTime).toDate()), Temperature: x.Value }); }); // 2017.09.08 : 그리드 도표 작업 : 팝업창 추가 // 출력용 maxValue = 0; var dataArray_Temp = []; var dataArray_Temp1 = []; var dataArray_Temp2 = []; _.each(values[0], function (x) { dataArray_Temp1.push({ DateTime: BWA.Chart.getCustermTimeArgumentAxisString(timeBoxForSearch.type(), moment(x.DateTime).toDate()), Value: x.Value }); if (maxValue < x.Value) maxValue = x.Value; }); _.each(temperatures[0], function (x) { dataArray_Temp2.push({ DateTime: BWA.Chart.getCustermTimeArgumentAxisString(timeBoxForSearch.type(), moment(x.DateTime).toDate()), Temperature: x.Value }); }); // 배열을 하나로 합치자 var array_length_size = Math.max(dataArray_Temp1.length, dataArray_Temp2.length); // 크기가 동일한 경우에만 허용함 (예외 방어용) if (dataArray_Temp1.length == dataArray_Temp2.length) { for (var i = 0; i < array_length_size; i++) { dataArray_Temp.push( { DateTime: dataArray_Temp1[i].DateTime, Value: dataArray_Temp1[i].Value, Temperature: dataArray_Temp2[i].Temperature }); } // 만약 일렬로 병합을 원한다면 아래코드를 사용 //dataArray_Temp = dataArray_Temp1.concat(dataArray_Temp2); } dataArrayresult = dataArray_Temp; // 2017.09.08 : 그리드 도표 작업 : 팝업창 추가 var options = BWA.ChartOptions.LocEnergyConsumption.getSeriesOptions(timeBoxForSearch.type(), fuelTypeIdForSearch(), goalUpperBound()); options.valueAxis[0].tickInterval = Math.round(maxValue / 10); options.dataSource = dataArray; chart().option(options); $('#st').text('' + "" + buildingNameForSearch() + " - " + floorNameForSearch() + " - " + zoneNameForSearch()); visiblePopup(false); }) .fail(function (error) { visiblePopup(false); }); } } function handleMenuItemClick(e) { var chartIntance = chart(); var chartOptions = chartIntance._options; var series = chartIntance.getAllSeries(); var table = []; var columns = [chartOptions.argumentAxis.title]; var values = {}; _.each(chartOptions.valueAxis, function (a) { columns.push(a.title.text); }); _.each(series, function (s, i) { _.each(s.pointsByArgument, function (point, argument) { var array = values[argument]; if (_.isArray(array) === false) { array = values[argument] = []; } array[i] = point.value; }); }); var csvRows = [columns.join(',')]; _.each(values, function (values, argument) { csvRows.push([$G.dateTime(moment(Number(argument))), values.join(',')].join(',')); }); var csvContent = csvRows.join("\r\n"); var agent = navigator.userAgent.toLowerCase(); if (window.navigator.msSaveOrOpenBlob) { var fileData = ['\uFEFF' + csvContent]; var blobObject = new Blob(fileData); window.navigator.msSaveOrOpenBlob(blobObject, 'export.csv'); } else { var csvString = 'data:text/csv;utf-8,\uFEFF' + encodeURI(csvContent); var a = $('#csvExporter'); a.attr({ 'href': csvString, 'target': '_blank', 'download': 'export.csv' }); // a.trigger('click'); var aa = document.getElementById('csvExporter'); aa.click(); } } // 2017.09.08 : 그리드 도표 작업 : 팝업창 추가 function DataGridPopup_ButtonClick() { DataGridPopup_Visible(true); } function handlePopupShown() { var ColumnCaptionName = ""; switch (fuelTypeIdForSearch()) { case $Code.FuelType.ELECTRICITY: ColumnCaptionName = "전기"; break; case $Code.FuelType.GAS: ColumnCaptionName = "가스"; break; case $Code.FuelType.WATER: ColumnCaptionName = "수도"; break; case UnionfuelType: ColumnCaptionName = UnionfuelName; break; } viewModel.LocEnergyConsumptionDataGridOptions.columns[1].caption = ColumnCaptionName; // 데이타소스 업데이트 $("#LocEnergyConsumptionDataGrid_Container").dxDataGrid("instance").option("dataSource", dataArrayresult); // 팝업창 갱신 DataGridPopup_Visible(true); // 2017.09.08 : 그리드 도표 작업 : 팝업창 추가 } var viewModel = $.extend(BWA.CommonView.create( params, viewInfo, searchViewOptions, ko.observable(null), handleViewShown, null, handleSearchInSearchView, handleSearchViewInitializeUpdate), { refreshList: refreshList, viewShowing: handleViewShowing, viewHidden: handleViewHidden, // hcLee 2015 03 23 locationEnableForSearch: locationEnableForSearch, handleMenuItemClick: handleMenuItemClick, menuItems: [{ text: '엑셀(csv) 내보내기' }] // 2017.09.08 : 그리드 도표 작업 : 팝업창 추가 , LocEnergyConsumptionDataGridPopup_ButtonClicked: DataGridPopup_ButtonClick // 2017.09.08 : 그리드 도표 작업 : 팝업창 추가 }); viewModel.timeBoxForSearch = timeBoxForSearch; // 2017.09.08 : 그리드 도표 작업 : 팝업창 추가 // 그래프 도표 팝업창 viewModel.LocEnergyConsumptionDataGridPopupOptions = { width: '480px', height: '750px', // cyim 2017.05.30 : 테스트 결과 Devextreme 버그임. px 로 지정하면 초기 출력시 정확하게 가운데에 있지만, auto 설정시 화면아래에 위치하게 됨 contentTemplate: "content", showTitle: true, title: "도표", visible: DataGridPopup_Visible, dragEnabled: true, closeOnOutsideClick: true, shownAction: handlePopupShown, animation: window.utils.popup.createAnimation() }; // 2017.09.08 : 그리드 도표 작업 : 팝업창 추가 // 에너지사용량 조회중 팝업창 viewModel.popupOptions = { width: 300, height: 140, // cyim 2017.05.29 : 조회 팝업창 출력안되는 문제 수정 contentTemplate: "info", showTitle: false, title: "작업 중", visible: visiblePopup, dragEnabled: false, shading: false, closeOnOutsideClick: false }; // 2017.09.08 : 그리드 도표 작업 : 팝업창 추가 // 주석이 너무 없음. 나중에 변경 작업을 진행하는 누군가가 시간 소비를 하지 않기 위해...남김 // 시간 포맷은 그래프에 귀속해야 되므로 포맷은 변경하지 않는 것이 유리함 // 전기,가스,수도 와 같이 선택적으로 변경해야 된다면 아래와 같이 캡션은 고정하지 말것 viewModel.LocEnergyConsumptionDataGridOptions = { dataSource: dataArrayresult, paging: { pageSize: 17 }, columns: [ { dataField: 'DateTime', caption: "날짜", width: '33%', alignment: 'center', },// dataType: 'date', format: 'yyyy-MM-dd HH:mm' }, { dataField: 'Value', width: '33%', alignment: 'center', format: 'fixedPoint', precision: 2 }, { dataField: 'Temperature', caption: "외기온도", width: '33%', alignment: 'center', format: 'fixedPoint', precision: 2 } ], // 엑셀 출력 "export": { enabled: true, fileName: '에너지 사용량(외기온도)' } }; /// 2017.09.08 : 그리드 도표 작업 : 팝업창 추가 return viewModel; };