채택완료

chart.js 배열 질문드립니다.

2022-09-05 (월) 13:49:10 3,265

[/code]

$sql = "
select
    group_concat(wr_12 separator '||') wr_12,
    group_concat(wr_21 separator '||') wr_21,
    group_concat(wr_26 separator '||') wr_26
  from {$write_table}
     where wr_29 between '{$stx1}' and '{$stx2}'
";
$result = sql_query($sql, true);
while ($row = sql_fetch_array($result)) {
    $list_product = explode('||',$row['wr_12']);
    $list_volume = explode('||',$row['wr_21']);
    $list_money = explode('||',$row['wr_26']);
    
    $result_array = array();
    foreach ($list_product as $key=>$val)
    {
        if (isset($result_array[$list_product[$key]]) == false) {
            $result_array[$list_product[$key]] = array(0, 0);
        }
        $result_array[$list_product[$key]][0] += $list_volume[$key];
        $result_array[$list_product[$key]][1] += $list_money[$key];
    }
    arsort($result_array);
        foreach ($result_array as $key=>$val) { ?>
        <tr>
            <td><?php echo $key; ?></td>
            <td class="right"><?php echo $val[0]; ?></td>
            <td class="right"><?php echo number_format($val[1]); ?></td>
        </tr>
<?php
    }
 }  
?>
[/code]

이걸 차트로 만들려고 합니다.

$key에는 딸기 바나나 오렌지 등 제품명이 들어가 있고
$val[0]에는 개수
$val[1]에는 금액이 들어가 있습니다.


chart.js를 이용해서 차트를 만들어 보고 싶은데

labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange']
data: [12, 19, 3, 5, 2, 3]
labels에는 제품명
data에는 수량 또는 금액 이 위와 같이 들어가야 하는데

어떻게 수정을 해야 할지 잘 모르겠습니다.

알려주시면 감사하겠습니다.

답변 1개 / 댓글 1개

채택된 답변
+20 포인트

아래는 예시로 해본거니.. 잘 활용해보세요.

Copy
<?php

$sql = "

select

    group_concat(wr_12 separator '||') wr_12,

    group_concat(wr_21 separator '||') wr_21,

    group_concat(wr_26 separator '||') wr_26

  from {$write_table}

     where wr_29 between '{$stx1}' and '{$stx2}'

";

$result = sql_query($sql, true);

while ($row = sql_fetch_array($result)) {

    $list_product = explode('||',$row['wr_12']);

    $list_volume = explode('||',$row['wr_21']);

    $list_money = explode('||',$row['wr_26']);

   

    $result_array = array();

    foreach ($list_product as $key=>$val)

    {

        if (isset($result_array[$list_product[$key]]) == false) {

            $result_array[$list_product[$key]] = array(0, 0);

        }

        $result_array[$list_product[$key]][0] += $list_volume[$key];

        $result_array[$list_product[$key]][1] += $list_money[$key];

    }

    arsort($result_array);

    foreach ($result_array as $key=>$val) {

        if($labels) $labels .= ", ";

        $labels .= "'{$key}'";

        if($data1) $data1 .= ", ";

        $data1 .= "'{$val[0]}'";

        if($data2) $data2 .= ", ";

        $data2 .= "'{$val[0]}'";

?>

    labels: [<?php echo $labels; ?>],

    data: [<?php echo $data1; ?>],

<?php

    }

}

답변에 대한 댓글 1개

감사합니다.

답변을 작성하려면 로그인이 필요합니다.