Note

express + fetch + chart.js

zdiv 2023. 8. 1. 00:54

index.js

const express = require('express')
const app = express()

app.use(express.static('static'))

app.get('/data', (req,res) => {
    res.json({
            labels: ["HTML", "CSS", "JAVASCRIPT", "CHART.JS", "JQUERY", "BOOTSTRP"],
            datasets: [{
               label: "online tutorial subjects",
               data: [20, 40, 30, 35, 30, 20],
               backgroundColor: ['yellow', 'aqua', 'pink', 'lightgreen', 'lightblue', 'gold'],
               borderColor: ['black'],
               borderWidth: 2,
               pointRadius: 5,
            },{
               label: "online tutorial subjects",
               data: [10, 50, 20, 25, 40, 30],
               borderColor: ['red'],
               borderWidth: 2,
               pointRadius: 5,
            }],
         })
})

app.listen(3080, () => {
    console.log("서버가 실행됩니다.")
})

 

chart_js_ajax.html

<!DOCTYPE>
<html>
<head>
   <meta charset- "UTF-8" />
   <!--<meta name="viewport" content="width=device-width, initial-scale=1" />-->
   <title>chart.js</title>
   <script src="js/chart-3.1.1.js"></script>
</head>
<body>
   <canvas id="chartId" aria-label="chart" heigth="350" width="580"></canvas>
   <script>
     fetch(window.location.origin + "/data", {
        method: "get", 
     })
     .then((response) => response.json())
     .then((data) => {
        console.log(data)
          const chrt = document.getElementById("chartId").getContext("2d");
          const chartId = new Chart(chrt, {
            type: 'line',
            data: data,
            options: {
              responsive: false,
            },
          });
     }) 
   </script>
</body>
</html>

 

Local File

<!DOCTYPE>
<html>
<head>
   <meta charset- "UTF-8" />
   <meta name="viewport" content="width=device-width, initial-scale=1" />
   <title>chart.js</title>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/chart.min.js"></script>
   <!--<script src="js/chart-3.1.1.js"></script>-->
</head>
<body>
   <canvas id="chartId" aria-label="chart" heigth="350" width="580"></canvas>
   <script>
      var chrt = document.getElementById("chartId").getContext("2d");
      var chartId = new Chart(chrt, {
         type: 'line',
         data: {
            labels: ["HTML", "CSS", "JAVASCRIPT", "CHART.JS", "JQUERY", "BOOTSTRP"],
            datasets: [{
               label: "online tutorial subjects",
               data: [20, 40, 30, 35, 30, 20],
               backgroundColor: ['yellow', 'aqua', 'pink', 'lightgreen', 'lightblue', 'gold'],
               borderColor: ['black'],
               borderWidth: 2,
               pointRadius: 5,
            },{
               label: "online tutorial subjects",
               data: [10, 50, 20, 25, 40, 30],
               //backgroundColor: ['yellow', 'aqua', 'pink', 'lightgreen', 'lightblue', 'gold'],
               borderColor: ['red'],
               borderWidth: 2,
               pointRadius: 5,
            }],
         },
         options: {
            responsive: false,
         },
      });
   </script>
</body>
</html>

'Note' 카테고리의 다른 글

express + webpack + chart.js + jquery  (0) 2023.08.06
html에서 react 사용하기  (0) 2023.08.05
express로 nodejs 웹서버 만들기  (0) 2023.07.30
Bootstrap Collapse with Indicator  (0) 2023.07.23
Bootstrap Circle Badge  (0) 2023.07.23