관리자 매출현황에 차트를 넣어보려고 합니다.

관리자 매출현황에 차트를 넣어보려고 합니다.

QA

관리자 매출현황에 차트를 넣어보려고 합니다.

본문

매출현황을 차트로 만들어보고 있습니다.

ajax로 연도별 매출데이터들을 json 으로 가져오고 싶습니다.

 

지금 막연하게 생각하고 차트구현시켜놓긴 했는데 ajax가 아닌 

스크립트로 테이블에 데이터들을 불러와 배열에 넣고 그 데이터로 차트를 그렸습니다.

var tr = document.querySelectorAll('tbody tr');
var yearArr = [];
var operFitArr = [];
var netFitArr = [];
var cardArr = [];
var accountArr = [];
for(var i = 0; i < tr.length; i++){
    yearArr.push(tr[i].children[0].children[0].innerText);
    operFitArr.push(tr[i].children[2].innerText.replace(/,/g, ''));
    netFitArr.push(tr[i].children[4].innerText.replace(/,/g, ''));
    cardArr.push(tr[i].children[8].innerText.replace(/,/g, ''));
    accountArr.push(tr[i].children[5].innerText.replace(/,/g, ''));
}
var ctx = document.getElementById('saleChart').getContext('2d');
var chart = new Chart(ctx, {
    data: {
        labels: yearArr,
        datasets: [
            /*{
                type: 'line',
                label: '카드사용',
                data: cardArr,
                backgroundColor: 'red',
                borderColor: 'red',
                fill: false
            },
            {
                type: 'line',
                label: '무통장',
                data: accountArr,
                backgroundColor: 'yellow',
                borderColor: 'yellow',
                fill: false
            },*/
            {
                type: 'bar',
                label: '주문합계',
                data: operFitArr,
                backgroundColor: '#00796B'
            },
            {
                type: 'bar',
                label: '실매출합계',
                data: netFitArr,
                backgroundColor: '#FFA000'
            },
        ]
    },
    options: {
        animation: {
            duration: 2000,
        }
    }
});

 

저렇게 하는게 심플하긴 한데, 제가 해보고싶은건 ajax로 데이터를 직접 가져와보고싶어서입니다.

ajax를 잘 못다루고 개발자는 아니어서 sql, php 또한 가져다 쓰는 것만 할 줄 알고 잘 못다룹니다;;ㅠㅠㅠ

예전에 게시판 리스트페이지에서 ajax로 게시글 데이터를 가져와서 뷰페이지를 모달로 띄웠었는데 그때 처음으로 ajax를 써봤는데 그 때 해본 것을 경험삼아 매출데이터도 ajax로 가져오려고 합니다. https://sir.kr/g5_skin/9410  <- ajax로 뷰페이지 모달 띄울때 참고했던 스킨

sale1year.php 파일에서 sql로 연도별 데이터를 취합하는 부분을 그대로 가져와서 사용해볼까해서

ajax.salesStatus.php 파일을 만들어서 데이터를 취합하려는데 잘 안돼서 질문드립니다.


<?php
include_once('./_common.php');
header('Content-type:application/json');
include_once(G5_LIB_PATH.'/json.lib.php');
$fr_year = isset($_REQUEST['fr_year']) ? strip_tags($_REQUEST['fr_year']) : '';
$to_year = isset($_REQUEST['to_year']) ? strip_tags($_REQUEST['to_year']) : '';
$data = array();
$sql = " select od_id,
                SUBSTRING(od_time,1,4) as od_date,
                od_send_cost,
                od_settle_case,
                od_receipt_price,
                od_receipt_point,
                od_cart_price,
                od_cancel_price,
                od_misu,
                (od_cart_price + od_send_cost + od_send_cost2) as orderprice,
                (od_cart_coupon + od_coupon + od_send_coupon) as couponprice
           from {$g5['g5_shop_order_table']}
          where SUBSTRING(od_time,1,4) between '$fr_year' and '$to_year' ";
$chartData = sql_query($sql);
for($i=0;$row=sql_fetch_array($chartData);$i++){
    if( $i == 0 )
        $save['od_date'] = $row['od_date'];
    if( $save['od_date'] != $row['od_date']) {
        $data['year'] = $save['od_date'];
        $data['order'] = $save['orderprice'];
        $data['real'] = $save['receiptbank'] + $save['receiptvbank'] + $save['receiptiche'] +$save['receiptcard'] + $save['receipthp'];
        $data['card'] = $save['receiptcard'];
        unset($save);
        $save['od_date'] = $row['od_date'];
    }
    $save['orderprice'] += $row['orderprice'];
    if($row['od_settle_case'] == '무통장')
        $save['receiptbank']   += $row['od_receipt_price'];
    if($row['od_settle_case'] == '가상계좌')
        $save['receiptvbank']   += $row['od_receipt_price'];
    if($row['od_settle_case'] == '계좌이체')
        $save['receiptiche']   += $row['od_receipt_price'];
    if($row['od_settle_case'] == '휴대폰')
        $save['receipthp']   += $row['od_receipt_price'];
    if($row['od_settle_case'] == '신용카드')
        $save['receiptcard']   += $row['od_receipt_price'];
}
if( $i == 0 ) {
    $data['year'] = $save['od_date'];
    $data['order'] = $save['orderprice'];
    $data['real'] = $save['receiptbank'] + $save['receiptvbank'] + $save['receiptiche'] +$save['receiptcard'] + $save['receipthp'];
    $data['card'] = $save['receiptcard'];
}

$data['error'] = '';
// print_r2($data);
die(json_encode($data));

이렇게 했는데 2021년도 데이터만 불러와집니다.


 
{
    "year": "2021",
    "order": 2021년도 주문합계 데이터,
    "real": 2021년도 실제 매출합계 데이터,
    "card": 2021년도 카드결제금액 데이터,
    "error": ""
}

 

어느 부분을 수정하면 연도별로 데이터를 불러올 수 있을까요??

이 질문에 댓글 쓰기 :

답변 1

$sql = " select year( od_date) od_year,  -- 추가
               sum( od_send_cost) od_send_cost,
               sum( ...
                 ...
               group by od_year
...

 

for($i=0;$row=sql_fetch_array($chartData);$i++){
    if( !isset( $dave[ $row['od_year']]) $save[ $row['od_year']]= array();
    $save['od_date']= $row['od_send_cost'];

...

}

이런 식으로 되어야 할 겁니다.

감사합니다!


for($i=0;$row=sql_fetch_array($chartData);$i++){
    if( !isset($data[$row['od_date']]) )
        $data[$row['od_date']] = array();

    $data[$row['od_date']]['order'] = $row['orderprice'];
    $data[$row['od_date']]['net'] = $row['tot_receip_price'];
}


sql 알려주신 방법으로 수정하고 데이터도 위와 같이 불러오니 잘 됩니다!!

답변을 작성하시기 전에 로그인 해주세요.
전체 166
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT