2021년 11월 26일 금요일

[자바학원교육/JAVA학원교육]자바학원에서 안배웠어요! 자바구글API, Google Report API를 이용한 사이트 통계정보 가져오기, 성별, 페이지제목별, 연령구간대별,브라우저별,OS별,모바일디바이스 브랜드별로 페이지뷰, 접속사용자수, 접속세션수

[자바학원교육/JAVA학원교육]자바학원에서 안배웠어요! 자바구글API, Google Report API를 이용한 사이트 통계정보 가져오기, 성별, 페이지제목별, 연령구간대별,브라우저별,OS별,모바일디바이스 브랜드별로 페이지뷰, 접속사용자수, 접속세션수 


https://www.youtube.com/watch?v=5nu21jnBQuI&list=PLxU-iZCqT52BjreHUm5L7pknSzK4Kurv0&index=4 



http://ojc.asia/bbs/board.php?bo_table=LecJava&wr_id=774 


[자바]Google Report API를 이용한 사이트 통계정보 가져오기, 성별, 페이지제목별, 연령구간대별,브

[자바]Google Report API를 이용한 사이트 통계정보 가져오기, 성별, 페이지제목별, 연령구간대별,브라우저별,OS별,모바일디바이스 브랜드별로 페이지뷰, 접속사용자수, 접속세션수[1단계]구글 통계

ojc.asia

https://www.youtube.com/watch?v=67noSrdGLM0&list=PLxU-iZCqT52BjreHUm5L7pknSzK4Kurv0&index=5 

[1단계]

 

구글 통계 정보를 조회해서 API를 이용해 데이터를 자지고 오려면 

 

1. https://console.cloud.google.com/identity/serviceaccounts?folder=&organizationId=&project=top-creek-300922&supportedpurview=project

에서 사용자 추가, 아래 이메일이 그렇게 해서 만듦. 그리고 키파일 생성함

키파일은 자바 프로그램에서 사용하니 잘 보관해야 함

 

 

 

2. 위 사용자에 대해 구글 애널리틱스의 관리자에서 조회권한을 줘야 함

 

 

첫화면 좌하단 관리자 클릭

보기 à 보기 사용자관리 여기서 위에서 생성한 계정의 이메일을 통해 사용자 조회 권한을 부여함

 

 

 

[2단계, 자바코드 작성하기]

 

1. 마리아DB 테이블

 

CREATE TABLE T_GA_STATS(

start_ymd VARCHAR(8) ,

             end_ymd VARCHAR(8) ,

dimension VARCHAR(50),

             dim_val   VARCHAR(900),

page_views INT ,

             users INT ,

             sessions INT, 

reg_dt DATETIME DEFAULT CURRENT_TIMESTAMP,

mdfcn_dt DATETIME DEFAULT CURRENT_TIMESTAMP,

    PRIMARY KEY(start_ymd, end_ymd, dimension, dim_val)

)ENGINE = InnoDB  CHARACTER SET utf8;

 

 

2. ga.properties

 

#VIEW ID, 구글 애놀리틱스 화면에서 좌상 단 "전체웹사이트 데이터" 선택 후 팝업화면에서 오른쪽 전체웹사이트 데이터아래 숫자 입니다.

view.id=22931XXXX

 

#Key File Location, 파일명은 사용자 마다 다릅니다. 위 1에서 사용자 등록하고 키파일 만든 바로 그것 입니다.

key.file.location=c:\\/top-creek-3030922-ca1xbabc22484.json

 

#----------------------------------- Metric(측정항목) Start

#페이지뷰 Expression

metric1.expression=ga:pageviews

 

#페이지뷰 alias

metric1.alias=pageviews

 

#사용자수 Expression

metric2.expression=ga:users

 

#사용자수 alias

metric2.alias=users

 

#세션수 Expression

metric3.expression=ga:sessions

 

#세션수 alias

metric3.alias=sessions

#----------------------------------- Metric(측정항목) End

 

 

#----------------------------------- Dimension(측정기준) Start

#성별

dim1=ga:userGender

 

#페이지 제목별

dim2=ga:pageTitle

 

#연령 구간대별

dim3=ga:userAgeBracket

 

#브라우저별

dim4=ga:browser

 

#OS별

dim5=ga:operatingSystem

 

#모바일 디바이스 브랜드별

dim6=ga:mobileDeviceBranding

 

#기기별

dim7=ga:deviceCategory

#----------------------------------- Dimension(측정기준) End

 

3. mybatis-config.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD config 3.0//EN" 

"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

 

<!-- DB설정 파일 로딩 -->

<properties resource="db.properties" />

 

<typeAliases>

<typeAlias alias="GaStatsVO" type="jmx.vo.GaStatsVO" /> 

</typeAliases>

 

<!-- db1 : 마리아DB 연결 -->

<environments default="development">

<environment id="development">

<transactionManager type="JDBC" />

<dataSource type="POOLED">

<property name="driver" value="${db1.driver}" />

<property name="url" value="${db1.url}" />

<property name="username" value="${db1.id}" />

<property name="password" value="${db1.password}" />

<property name="poolPingQuery" value="select 1"/> <!-- 핑쿼리 추가 -->

<property name="poolPingEnabled" value="true"/> <!-- 핑쿼리 추가 -->

<property name="poolPingConnectionsNotUsedFor" value="7200000"/> <!-- 2시간마다 -->

<property name="poolMaximumActiveConnections" value="200"/> <!-- 주어진 시간에 존재할수 있는 활성화된 커넥션 수 -->

<property name="poolMaximumIdleConnections" value="200"/> <!-- 주어진 시간에 존재할 수 있는 유휴 커넥션의 수 -->

<property name="poolTimeToWait" value="20000"/> <!-- 풀이 로그 상태를 출력하고 비정상적으로 긴경우 커넥션을 다시얻을려고 시도하는 로우 레벨 셋팅 --> 

</dataSource>

</environment>

</environments>

 

<mappers>

<mapper resource="mapper/gastats.xml" />

</mappers>

 

</configuration>

 

4. GaStatsVO.java

 

package jmx.vo;

 

import lombok.Data;

 

/* ----------------------------------------------------------------------------

 *  File Name : GaStatsVO.java

 *  Desc : 구글 통계정보를 저장하기 위한 VO

 * ----------------------------------------------------------------------------

 */

@Data

public class GaStatsVO {

// 통계 시작년월일

private String start_ymd;


// 통계 종료년월일

private String end_ymd;


// 측정기준(성별, 브라우저별, OS별...... 접속현황, 이때 성별 이런것이 Dimension)

private String dimension;


// 측정기준값(male, female)

private String dim_val;


// 페이지뷰

private String page_views;


// 접속사용자수

private String users;


// 접속 세션수

private String sessions;


// 등록일시

private String reg_date;


// 수정일시

private String mdfcn_dt;

}

 

5. 매퍼 인터페이스(GaStatsMapper.java)

 

package jmx.db.mapper;

 

import jmx.vo.GaStatsVO;

 

/* ----------------------------------------------------------------------------

 *  File Name : GaStatsMapper.java

 *  Desc : 구글 통계정보 저장(T_GA_STATS)용 매퍼 인터페이스 

 * ----------------------------------------------------------------------------

 */

public interface GaStatsMapper {

// T_GA_STATS 입력(구글 통계정보)

public void insertGaStats(GaStatsVO gaStatsVO);


// T_GA_STATS 삭제(구글 통계정보)

public void deleteGaStats(GaStatsVO gaStatsVO);

}



6. DAO(GaStatsDAO.java)

 

package jmx.db.dao;

 

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.log4j.LogManager;

import org.apache.log4j.Logger;

 

import jmx.db.mapper.GaStatsMapper;

import jmx.vo.GaStatsVO;

 

/* ----------------------------------------------------------------------------

 *  File Name : GaStatsDAO.java

 *  Desc : 구글 통계정보를 DB에 저장

 * ----------------------------------------------------------------------------

 */

public class GaStatsDAO {

 

final static Logger logger = LogManager.getLogger(GaStatsDAO.class);

private SqlSessionFactory sqlSessionFactory = null;

private SqlSession session = null;

 

public void getSession() {

sqlSessionFactory = DBConnectionFactory.getSqlSessionFactory();

session = sqlSessionFactory.openSession();

}


/**

* 구글 통계정보 저장

* @param gaStatsVO

* @throws Exception 

*/

public void insertGaStats(GaStatsVO gaStatsVO) throws Exception {

 

getSession();

GaStatsMapper mapper = session.getMapper(GaStatsMapper.class);


try {

mapper.insertGaStats(gaStatsVO);

catch(Exception e) {

session.rollback();

logger.info(">>>> insertGaStats Insert ERROR ::: " + e);

throw new Exception("insertGaStats Insert ERROR" + e.getMessage());

}

finally {

session.commit();

session.close();

session = null;

}

}


/**

* 구글 통계정보 삭제

* @param gaStatsVO

* @throws Exception 

*/

public void deleteGaStats(GaStatsVO gaStatsVO) throws Exception {

 

getSession();

GaStatsMapper mapper = session.getMapper(GaStatsMapper.class);


try {

mapper.deleteGaStats(gaStatsVO);

catch(Exception e) {

session.rollback();

logger.info(">>>> deleteGaStats Delete ERROR ::: " + e);

throw new Exception("deleteGaStats Delete ERROR" + e.getMessage());

}

finally {

session.commit();

session.close();

session = null;

}

}

 

}



7. resource 아래 mapper 아래 매퍼 XML(gastats.xml)



<?xml version='1.0' encoding='UTF-8' ?>

<!DOCTYPE mapper PUBLIC '-//mybatis.org//DTD Mapper 3.0//EN'

  'http://mybatis.org/dtd/mybatis-3-mapper.dtd'>

 

<mapper namespace='jmx.db.mapper.GaStatsMapper'>

 

<!-- 구글 통계정보 저장 -->

<insert id='insertGaStats' parameterType='GaStatsVO'>

INSERT INTO T_GA_STATS (

START_YMD

,END_YMD

,DIMENSION

,DIM_VAL

,PAGE_VIEWS

,USERS

,SESSIONS

)

VALUES(

#{start_ymd}

,#{end_ymd}

,REPLACE(#{dimension},'ga:', '')

,#{dim_val}

,CAST(#{page_views} AS UNSIGNED)

,CAST(#{users} AS UNSIGNED)

,CAST(#{sessions} AS UNSIGNED)

)

</insert>


<!-- 구글 통계정보 삭제 -->

<delete id='deleteGaStats' parameterType='GaStatsVO'>

DELETE FROM T_GA_STATS 

WHERE  START_YMD = #{start_ymd}

AND    END_YMD   = #{end_ymd}

</delete>

</mapper>



7. 구글 통계정보 읽기

 

package batch;

 

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.security.GeneralSecurityException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.util.List;

import java.util.Properties;

 

import org.apache.log4j.LogManager;

import org.apache.log4j.Logger;

 

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;

import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;

import com.google.api.client.http.HttpTransport;

import com.google.api.client.json.JsonFactory;

import com.google.api.client.json.gson.GsonFactory;

import com.google.api.services.analyticsreporting.v4.AnalyticsReporting;

import com.google.api.services.analyticsreporting.v4.AnalyticsReportingScopes;

import com.google.api.services.analyticsreporting.v4.model.ColumnHeader;

import com.google.api.services.analyticsreporting.v4.model.DateRange;

import com.google.api.services.analyticsreporting.v4.model.DateRangeValues;

import com.google.api.services.analyticsreporting.v4.model.Dimension;

import com.google.api.services.analyticsreporting.v4.model.GetReportsRequest;

import com.google.api.services.analyticsreporting.v4.model.GetReportsResponse;

import com.google.api.services.analyticsreporting.v4.model.Metric;

import com.google.api.services.analyticsreporting.v4.model.MetricHeaderEntry;

import com.google.api.services.analyticsreporting.v4.model.Report;

import com.google.api.services.analyticsreporting.v4.model.ReportRequest;

import com.google.api.services.analyticsreporting.v4.model.ReportRow;

 

import jmx.db.dao.GaStatsDAO;

import jmx.vo.GaStatsVO;

 

/* ----------------------------------------------------------------------------

 *  File Name : GAStatistics.java

 *  Desc : 구글 통계정보 가지고 와서 DB 저장

 * ----------------------------------------------------------------------------

 */

public class GaStatistics {


// 화면 ID(22931XXXX로 고정)

private String viewId = null;


// 키파일 위치

private String keyFileLocation = null;


//----------------------------------- Metric(측정항목) Start

//측정항목1

private String metric1Expression = null;

private String metric1Alias = null;


//측정항목2

private String metric2Expression = null;

private String metric2Alias = null;


//측정항목3

private String metric3Expression = null;

private String metric3Alias = null;


//----------------------------------- Dimension Start

private String dim1 = null;

private String dim2 = null;

private String dim3 = null;

private String dim4 = null;

private String dim5 = null;

private String dim6 = null;

private String dim7 = null;

 

// GAStatistics 클래스가 new 될때 호출되는 초기화 블럭

{

// 구글 통뎨정보 가지고 오기 위한 설정 파일

loadProperties();

}

 

private String APPLICATION_NAME = "서구 Analytics Reporting";

private JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();

 

final static Logger logger = LogManager.getLogger(GaStatistics.class);


public static void main(String[] args) {


if (args.length < 2) {

System.out.println("기간(일별)을 입력하세요. java batch.GaStatistics 2014-01-01 2020-12-31");

System.exit(1);

}

 

GaStatistics ga = new GaStatistics();


//기간을 확인

logger.info("::::: " + args[0] + " 부터 " + args[1] + " 까지 구글 통계정보를 가지고 옵니다");


try {

ga.getData(args[0], args[1]);

logger.info("::::: " + args[0] + " 부터 " + args[1] + " 까지 구글 통계정보를 가지고 옵니다");

}

catch(Exception e) {

e.printStackTrace();

}

}

 

/**

* Get Google Analytics Statistics Data 

* @throws Exception

*/

public void getData(String startDate, String endDate) throws Exception {

try {

AnalyticsReporting service = initializeAnalyticsReporting();

 

// 입력 기간 데이터 삭제

delete(startDate, endDate);


GetReportsResponse response1 = getReport1(service, startDate, endDate);

saveResponse(response1, startDate, endDate);


GetReportsResponse response2 = getReport2(service, startDate, endDate);

saveResponse(response2, startDate, endDate);


} catch (Exception e) {

logger.error(">>>>> 구글 애널리틱스 데이터 수신 오류! ::: " + e.getMessage());

throw new Exception(e);

}

}

 

/**

* Initializes an Analytics Reporting API V4 service object.

*

* @return An authorized Analytics Reporting API V4 service object.

* @throws IOException

* @throws GeneralSecurityException

*/

private AnalyticsReporting initializeAnalyticsReporting() throws GeneralSecurityException, IOException {

 

HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(keyFileLocation))

.createScoped(AnalyticsReportingScopes.all());

 

// Construct the Analytics Reporting service object.

return new AnalyticsReporting.Builder(httpTransport, JSON_FACTORY, credential)

.setApplicationName(APPLICATION_NAME).build();

}

 

/**

* Queries the Analytics Reporting API V4.

* 주의 : Request의 MAX는 5개, 즉 통계정보를 받기 위한 요청은 최대5개

*      그래서 getReport1(5개), getReport2(2개) 만듦

*

* @param service An authorized Analytics Reporting API V4 service object.

* @return GetReportResponse The Analytics Reporting API V4 response.

* @throws IOException

*/

private GetReportsResponse getReport1(AnalyticsReporting service, String startDate, String endDate) throws IOException {

// Create the DateRange object.

DateRange dateRange = new DateRange();

dateRange.setStartDate(startDate);

dateRange.setEndDate(endDate);

 

// Create the Metrics object.

Metric metric1 = new Metric().setExpression(metric1Expression).setAlias(metric1Alias); // 페이지뷰

Metric metric2 = new Metric().setExpression(metric2Expression).setAlias(metric2Alias); // 사용자수

Metric metric3 = new Metric().setExpression(metric3Expression).setAlias(metric3Alias); // 세션수

Metric metrics[] = new Metric[3];

metrics[0] = metric1;

metrics[1] = metric2;

metrics[2] = metric3;


Dimension dimension1 = new Dimension().setName(dim1); // 성별

Dimension dimension2 = new Dimension().setName(dim2); // 페이지제목별

Dimension dimension3 = new Dimension().setName(dim3); // 연령 구간대별

Dimension dimension4 = new Dimension().setName(dim4); // 브라우저별

Dimension dimension5 = new Dimension().setName(dim5); // OS별

//Dimension dimension6 = new Dimension().setName(dim6); // 모바일디바이스 브랜드별

//Dimension dimension7 = new Dimension().setName(dim7); // 기기(Device)별

 

// Create the ReportRequest object.

ReportRequest request1 = new ReportRequest().setViewId(viewId).setDateRanges(Arrays.asList(dateRange))

.setMetrics(Arrays.asList(metrics)).setDimensions(Arrays.asList(dimension1));

ReportRequest request2 = new ReportRequest().setViewId(viewId).setDateRanges(Arrays.asList(dateRange))

.setMetrics(Arrays.asList(metrics)).setDimensions(Arrays.asList(dimension2));

ReportRequest request3 = new ReportRequest().setViewId(viewId).setDateRanges(Arrays.asList(dateRange))

.setMetrics(Arrays.asList(metrics)).setDimensions(Arrays.asList(dimension3));

ReportRequest request4 = new ReportRequest().setViewId(viewId).setDateRanges(Arrays.asList(dateRange))

.setMetrics(Arrays.asList(metrics)).setDimensions(Arrays.asList(dimension4));

ReportRequest request5 = new ReportRequest().setViewId(viewId).setDateRanges(Arrays.asList(dateRange))

.setMetrics(Arrays.asList(metrics)).setDimensions(Arrays.asList(dimension5));

//ReportRequest request6 = new ReportRequest().setViewId(viewId).setDateRanges(Arrays.asList(dateRange))

// .setMetrics(Arrays.asList(metrics)).setDimensions(Arrays.asList(dimension6));

//ReportRequest request7 = new ReportRequest().setViewId(viewId).setDateRanges(Arrays.asList(dateRange))

// .setMetrics(Arrays.asList(metrics)).setDimensions(Arrays.asList(dimension7));

 

ArrayList<ReportRequest> requests = new ArrayList<ReportRequest>();

requests.add(request1);

requests.add(request2);

requests.add(request3);

requests.add(request4);

requests.add(request5);

//requests.add(request6);

//requests.add(request7);

 

// Create the GetReportsRequest object.

GetReportsRequest getReport = new GetReportsRequest().setReportRequests(requests);

 

// Call the batchGet method.

GetReportsResponse response = service.reports().batchGet(getReport).execute();

 

// Return the response.

return response;

}


/**

* Queries the Analytics Reporting API V4.

* 주의 : Request의 MAX는 5개, 즉 통계정보를 받기 위한 요청은 최대5개

*      그래서 getReport1(5개), getReport2(2개) 만듦

*

* @param service An authorized Analytics Reporting API V4 service object.

* @return GetReportResponse The Analytics Reporting API V4 response.

* @throws IOException

*/

private GetReportsResponse getReport2(AnalyticsReporting service, String startDate, String endDate) throws IOException {

// Create the DateRange object.

DateRange dateRange = new DateRange();

dateRange.setStartDate(startDate);

dateRange.setEndDate(endDate);

 

// Create the Metrics object.

Metric metric1 = new Metric().setExpression(metric1Expression).setAlias(metric1Alias); // 페이지뷰

Metric metric2 = new Metric().setExpression(metric2Expression).setAlias(metric2Alias); // 사용자수

Metric metric3 = new Metric().setExpression(metric3Expression).setAlias(metric3Alias); // 세션수

Metric metrics[] = new Metric[3];

metrics[0] = metric1;

metrics[1] = metric2;

metrics[2] = metric3;


Dimension dimension6 = new Dimension().setName(dim6); // 모바일디바이스 브랜드별

Dimension dimension7 = new Dimension().setName(dim7); // 기기(Device)별

 

// Create the ReportRequest object.

ReportRequest request6 = new ReportRequest().setViewId(viewId).setDateRanges(Arrays.asList(dateRange))

.setMetrics(Arrays.asList(metrics)).setDimensions(Arrays.asList(dimension6));

ReportRequest request7 = new ReportRequest().setViewId(viewId).setDateRanges(Arrays.asList(dateRange))

.setMetrics(Arrays.asList(metrics)).setDimensions(Arrays.asList(dimension7));

 

ArrayList<ReportRequest> requests = new ArrayList<ReportRequest>();

requests.add(request6);

requests.add(request7);

 

// Create the GetReportsRequest object.

GetReportsRequest getReport = new GetReportsRequest().setReportRequests(requests);

 

// Call the batchGet method.

GetReportsResponse response = service.reports().batchGet(getReport).execute();

 

// Return the response.

return response;

}


// 기간별 삭제

private void delete(String startDate, String endDate) throws Exception {


GaStatsVO vo = new GaStatsVO();

vo.setStart_ymd(startDate.replace("-", ""));

vo.setEnd_ymd(endDate.replace("-", ""));


// DB에 Mybatis를 통해 값을 저장하기 위한 DAO

GaStatsDAO dao = new GaStatsDAO(); 


// 혹시 있담녀 그날분 삭제 

dao.deleteGaStats(vo);

}

 

/**

* Parses and prints the Analytics Reporting API V4 response.

*

* @param response An Analytics Reporting API V4 response.

* @throws Exception 

*/

private void saveResponse(GetReportsResponse response, String startDate, String endDate) throws Exception {

 

GaStatsVO vo = new GaStatsVO();

vo.setStart_ymd(startDate.replace("-", ""));

vo.setEnd_ymd(endDate.replace("-", ""));


// DB에 Mybatis를 통해 값을 저장하기 위한 DAO

GaStatsDAO dao = new GaStatsDAO(); 


for (Report report : response.getReports()) {

ColumnHeader header = report.getColumnHeader();

List<String> dimensionHeaders = header.getDimensions();

List<MetricHeaderEntry> metricHeaders = header.getMetricHeader().getMetricHeaderEntries();

List<ReportRow> rows = report.getData().getRows();

 

if (rows == null) {

logger.info("No data found for " + viewId);

return;

}


 

for (ReportRow row : rows) {

List<String> dimensions = row.getDimensions();

List<DateRangeValues> metrics = row.getMetrics();

 

for (int i = 0; i < dimensionHeaders.size() && i < dimensions.size(); i++) {

vo.setDimension(dimensionHeaders.get(i));


if (dimensions.get(i).contains("(상세보기)")) {

vo.setDim_val(dimensions.get(i).substring(0, dimensions.get(i).indexOf("(상세보기)")-1));

    }

else {

vo.setDim_val(dimensions.get(i));

}


if(dimensions.get(i).length() > 900) {

vo.setDim_val(dimensions.get(i).substring(0, 900));

}

}

 

for (int j = 0; j < metrics.size(); j++) {

DateRangeValues values = metrics.get(j);

for (int k = 0; k < values.getValues().size() && k < metricHeaders.size(); k++) {

if ("pageviews".equals(metricHeaders.get(k).getName())) {

vo.setPage_views(values.getValues().get(k));

}

else if ("users".equals(metricHeaders.get(k).getName())) {

vo.setUsers(values.getValues().get(k));

}

else if ("sessions".equals(metricHeaders.get(k).getName())) {

vo.setSessions(values.getValues().get(k));

}

}

}


try {

//VO에 있는 값을 저장

dao.insertGaStats(vo);

}

catch(Exception e) {

logger.error(">>>>> T_GA_STATS 저장 오류 ::: " + e);

e.printStackTrace();

throw new Exception("T_GA_STATS 저장 오류 ::: " + e);

}

}

}

}

 

/**

* 구글 통계정보를 가지고 오기 위한 설정파일 로딩

*/

    public void loadProperties() {

InputStream input = null;

try {

input = this.getClass().getClassLoader().getResourceAsStream("ga.properties");

Properties prop = new Properties();


// load a properties file

prop.load(input);


// get the property value

viewId = prop.getProperty("view.id");

keyFileLocation = prop.getProperty("key.file.location");


metric1Expression = prop.getProperty("metric1.expression");

metric1Alias = prop.getProperty("metric1.alias");

metric2Expression = prop.getProperty("metric2.expression");

metric2Alias = prop.getProperty("metric2.alias");

metric3Expression = prop.getProperty("metric3.expression");

metric3Alias = prop.getProperty("metric3.alias");


dim1 = prop.getProperty("dim1");

dim2 = prop.getProperty("dim2");

dim3 = prop.getProperty("dim3");

dim4 = prop.getProperty("dim4");

dim5 = prop.getProperty("dim5");

dim6 = prop.getProperty("dim6");

dim7 = prop.getProperty("dim7");


} catch (Exception ex) {

ex.printStackTrace();

} finally {

if (input != null) {

try {

input.close();

input = null;

} catch (Exception e) {

}

}

}

}

}

 

 #자바학원#자바교육#JAVA교육#JAVA학원 #자바학원교육#자바구글통계정보#구글통계정보#구글레포트#자바구글API#GoogleReport API#자바구글교육#자바사이트통계정보#자바접속통계#자바통계#자바구글API#자바동영상#JAVA동영상


자바학원자바교육JAVA교육JAVA학원 자바학원교육자바구글통계정보구글통계정보구글레포트자바구글APIGoogleReport API자바구글교육


2021년 11월 21일 일요일

 

WPF 텍스트블록(TextBlock)

https://www.youtube.com/watch?v=FJbQJxtMnVU&list=PLxU-iZCqT52Cmj47aKB1T-SxI33YL7rYS&index=9&t=1s 

http://ojc.asia/bbs/board.php?bo_table=WPF&wr_id=182 


WPF 텍스트블록(TextBlock)

WPF 텍스트블록(TextBlock)TextBlock은 텍스트 출력시 좀 더 포커스를 줘서 보여줄 수 있는 컨트롤이다. 각 텍스트를 다른 색상, 폰트, 굵기 등으로 표시할 수 있으며 사용자가 텍스트를 선택할 수는 없

ojc.asia

  • TextBlock은 텍스트 출력시 좀 더 포커스를 줘서 보여줄 수 있는 컨트롤이다. 각 텍스트를 다른 색상, 폰트, 굵기 등으로 표시할 수 있으며 사용자가 텍스트를 선택할 수는 없다.
  • Name 속성은 이름을, Width 및 Height 속성은 TextBlock의 폭과 높이를 나타내고 Text 속성은 TextBlock의 내용을 나타낸다. Foreground 속성은 내용의 전경색을 하며 Background 속성은 없다.


<TextBlock Name="TextBlock1" Height="30" Width="200"  

 Text="Hello! I am a TextBlock." Foreground="Red">  

</TextBlock> 


  • 기본적으로 TextBlock은 페이지의 중앙에 있는데 컨트롤의 여백, 수직 맞춤 및 수평 맞춤을 설정하는 Margin, VerticalAlignment 및 HorizontalAlignment 속성을 사용하여 원하는 위치에 TextBlock 컨트롤을 배치 할 수 있다.
  • 아래 예문에서는 페이지의 왼쪽 상단 모서리에 TextBlock 컨트롤의 위치를 ​​설정한다.


<TextBlock Name="TextBlock1" Height="30" Width="200"  

    Text="Hello! I am a TextBlock."  

    Margin="10,10,0,0" VerticalAlignment="Top"  

    HorizontalAlignment="Left">  

</TextBlock>


  • TextBlock 역시 동적으로 C# 코드를 통해 생성할 수 있다.

privatevoid CreateATextBlock()   

{  

    TextBlock txtBlock = newTextBlock();  

    txtBlock.Height = 50;  

    txtBlock.Width = 200;  

    txtBlock.Text = "Text Box content";  

    txtBlock.Foreground = newSolidColorBrush(Colors.Red);  

    LayoutRoot.Children.Add(txtBlock);  

}


  • TextWrapping 속성은 텍스트의 줄 바꿈을 설정한다.( TextWrapping="Wrap")
  • Inline 프로퍼티는 TextBlock 컨트롤 내의 인라인 텍스트 컬렉션을 나타내며, TextBlock 내의 텍스트 섹션으로 하나의 TextBlock에 여러 개의 Inline을 추가하고 각각을 다르게 포맷을 줄 수 있다.
  • Run 개체는 인라인 텍스트를 나타내며 자체 텍스트 컨트롤로 취급 할 수 있으며 전경 및 글꼴 관련 속성을 가진다. 아래 예문은 TextBlock의 Inlines 속성을 설정하고 다른 글꼴과 전경색을 설정한다.

<TextBlock.Inlines>  

    <Run FontWeight="Bold" FontSize="14" Text="Hi! I am a TextBlock. " />  

    <Run FontStyle="Italic" Foreground="Red" Text="This is red text. " />  

    <Run FontStyle="Italic" FontSize="18" Text="Here is some linear gradient text. ">  

        <Run.Foreground>  

            <LinearGradientBrush>  

                <GradientStop Color="Green" Offset="0.0" />  

                <GradientStop Color="Purple" Offset="0.25" />  

                <GradientStop Color="Orange" Offset="0.5" />  

                <GradientStop Color="Blue" Offset="0.75" />  

            </LinearGradientBrush>  

        </Run.Foreground>  

    </Run>  

    <Run FontStyle="Italic" Foreground="Green" Text="How about adding some green? " />  

</TextBlock.Inlines>


  • TextDecoration을 이용하여 시각적으로 장식을 해서 텍스트를 출력할 수 있다. TextDecorationCollection은 TextDecoration의 모임이며 Baseline , UnderLine, OverLine, StrikeThrough 등이 있는데 WPF는 Underline만 지원한다.



<TextBlock Name="TextBlock1"  

    Margin="10,10,0,0" VerticalAlignment="Top"  

    HorizontalAlignment="Left"  

    FontSize="12" FontFamily="Verdana"  

    TextWrapping="Wrap" TextAlignment="Left" Padding="2"  

 TextDecorations="Underline">


  

#WPF교육, #텍스트블록, #TextBlock, #WPF동영상, #WPF학원 

WPF교육, 텍스트블록, TextBlock, WPF동영상, WPF학원 

WPF DataBinding, IValueConverter를 이용한 데이터바인딩, WPf동영상, WPF교육, WPF학원

 

WPF DataBinding, IValueConverter를 이용한 데이터바인딩, WPf동영상, WPF교육, WPF학원

http://ojc.asia/bbs/board.php?bo_table=WPF&wr_id=181 


WPF DataBinding, IValueConverter를 이용한 데이터바인딩

WPF DataBinding, IValueConverter를 이용한 데이터바인딩데이터바인딩시 소스와 타겟의 데이터 타입이 다른 경우가 있다. 예를 들면 체크박스의 체크여부에 따라 “예”, “아니오” 등을 표시하는 경

ojc.asia


https://www.youtube.com/watch?v=YaiFRTypMhM&list=PLxU-iZCqT52Cmj47aKB1T-SxI33YL7rYS&index=19&t=1s 

https://www.youtube.com/watch?v=GAw8eSawrvE&list=PLxU-iZCqT52Cmj47aKB1T-SxI33YL7rYS&index=20&t=11s 

  • 데이터바인딩시 소스와 타겟의 데이터 타입이 다른 경우가 있다. 예를 들면 체크박스의 체크여부에 따라 “예”, “아니오” 등을 표시하는 경우 또는 파일 크기가 바이트이지만 파일의 크기에 따라 바이트, 킬로바이트, 메가 바이트 또는 기가 바이트로 표시하려고 하는 경우가 그렇다.
  • 데이터바인딩시 타입의 변경을 위해 IValueConverter가 존재하며 IValueConverter 인터페이스를 구현하는 클래스가 소스와 대상 사이의 값을 변환한다.
  • WPF 값 변환기는 IValueConverter 인터페이스 또는 IMultiValueConverter 인터페이스 를 구현해야 하며 두 인터페이스 모두 Convert() 및 ConvertBack()의 ​​두 가지 메서드 만 구현하면 된다.
  • 문자열을 입력으로 불리언(Boolean) 값을 반환하는 간단한 변환기를 구현해 보자.
  • [실행화면]


  • [YesNoToBoolConverter.cs]


using System;

using System.Windows.Data;

namespace WpfApp12

{

    class YesNoToBoolConverter : IValueConverter

    {

        // 소스값이 타겟에 바인딩 되는 경우 호출

        // TextBox >> TextBlock, TextBox >> CheckBox

        // TextBox의 Text속성의 값 "YES", "NO"에 따리 true, false를 리턴

        public object Convert(object value, Type targetType, object param, System.Globalization.CultureInfo culture)

        {

            switch (value.ToString().ToUpper())

            {


                case "YES"return true;

                case "NO"return false;

            }

            return false;

        }


        // 타겟값이 역으로 소스에 바인딩 될때 호출

        // CheckBox --> TextBox

        // CheckBox의 값 true, false에 따라 "YES", "NO" 문자열을 리턴

        public object ConvertBack(object value, Type targetType, object param, System.Globalization.CultureInfo culture)

        {

            if (value is bool)

            {

                if ((bool)value == truereturn "YES";

                else return "NO";

            }

            return "NO";

        }

    }


}


  • [MainWindow.xaml]


<Window x:Class="ConverterTest.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:ConverterTest"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Window.Resources>

        <local:YesNoToBoolConverter x:Key="converter"/>

    </Window.Resources>

    <StackPanel>

        <TextBox Name="txtValue" Text="YES"/>

        <WrapPanel>

            <TextBlock Text="Current Value : "/>

            <TextBlock Text="{Binding ElementName=txtValue, Path=Text, Converter={StaticResource converter}}"/>

        </WrapPanel>

        <CheckBox Content="Yes" IsChecked="{Binding ElementName=txtValue, Path=Text, Converter={StaticResource converter}}" />

    </StackPanel>

</Window>


#WPFDataBinding, #DataBinding, #IValueConverter, #데이터바인딩, #WPF교육, #WPF강좌, #WPF강의 

WPFDataBinding, DataBinding, IValueConverter, 데이터바인딩, WPF교육, WPF강좌, WPF강의 

(C#교육동영상)C# ADO.NET 실습 ODP.NET/ODAC 설치 오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원

  (C#교육동영상)C# ADO.NET 실습  ODP.NET/ODAC 설치  오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원 https://www.youtube.com/watch?v=qIPU85yAlzc&list=PLxU-i...