JavaScript

[JavaScript] ํ•˜์ด์ฐจํŠธ(HighCharts) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ ๊ทธ๋ž˜ํ”„ ๋งŒ๋“ค๊ธฐ

carsumin 2023. 5. 24. 16:11

 

ํ•˜์ด์ฐจํŠธ (HighCharts)

์ˆœ์ˆ˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ์ž‘์„ฑ๋œ ์ฐจํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

 

 

<!DOCTYPE html>
<html>
<head>
    <title>Document</title>
    <meta charset="utf-8">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://code.highcharts.com/highcharts.js"></script>
    <script src="dataset.js"></script>

</head>
<body>
    <div id="container_scatter" style="min-width: 310px; height: 400px; max-width: 1900px; margin: 0 auto;"></div>
    <div id="container_line" style="height: 400; width: 1900px; margin: 0 auto;"></div>
</body>
</html>

 

  • ํ•˜์ด์ฐจํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ script ์ถ”๊ฐ€ ํ•„์š” 
  • div ํƒœ๊ทธ์— id๋ฅผ ํ• ๋‹นํ•ด์„œ ์ฐจํŠธ๊ฐ€ ๋“ค์–ด๊ฐˆ ์˜์—ญ์„ ๋งŒ๋“ค์–ด์คŒ

 

 

$(document).ready(function() {

//์ž„์˜์˜ dataset ๊ฐ์ฒด ์ƒ์„ฑ (A-Z)
const dataObj = {};

for(let i=65; i<=90; i++){
    let alphabet = String.fromCharCode(i);
    let dataArr = [];

    for(let j=0; j<=100; j++){
        let x = parseFloat((Math.random() * 50).toFixed(1));
        dataArr.push([x]);
    }

    dataObj['DATA_' + alphabet] = dataArr;
}

//HighCharts์˜ series
let seriesData = [];

for(let key in dataObj){
    if(dataObj.hasOwnProperty(key)){

        let series = {
          name: key,
          data: dataObj[key]
        };
  
        seriesData.push(series);
  
    }
}

//HighCharts์˜ categories
let categories = Object;

//scatter chart
Highcharts.chart('container_scatter', {
    chart: {
      type: 'scatter',
      zoomType: 'xy'
    },
    title: {
      text: 'Sample Scatter Chart'
    },
    xAxis: {
      title: {
        enabled: true,
        text: 'x data',
        align: 'low',
        margin: 50
      },
      startOnTick: true,
      endOnTick: true,
      showLastLabel: true
    },
    yAxis: {
      title: {
        enabled: true,
        text: 'y data',
        align: 'low'
        
      }
    },
    legend: {
      layout: 'horizontal',
      align: 'right',
      verticalAlign: 'bottom',
      x: 0,
      y: 7,
      floating: true,
      itemStyle: {
        fontWeight: 'normal'
      }
    },
    navigation: {
      buttonOptions: {
        theme: {
          states: {
            hover: {
              fill: '#fff'
            },
            select: {
              fill: '#f7f8fa'
            }
          }
        }
      },
      menuItemStyle: {
        fontWeight: 'normal',
        background: 'none'
      },
      menuItemHoverStyle: {
        fontWeight: 'bold',
        background: '#1784B0',
        color: '#fff'
      }
    }, 
    plotOptions: {
      scatter: {
        marker: {
          radius: 3,
          states: {
            hover: {
              enabled: true,
            }
          }
        },
        states: {
          hover: {
            marker: {
              enabled: false
            }
          }
        },
        tooltip: {
          headerFormat: '{series.name}<br>',
          pointFormat: '{point.x}'
        }
      }
    },
    series: seriesData
  });  

  //line chart
  let chart = new Highcharts.Chart({
    chart: {
      renderTo: 'container_line',
      marginBottom: 120
    },
    title: {
      text: 'Sample Line Chart'
    },
    xAxis: {
      categories: categories,
      labels: {
        rotation: 90
      }
    },

    series: seriesData
  });

});

 

  • ์ฐจํŠธ์— ๋“ค์–ด๊ฐˆ ์ž„์˜์˜ ๋ฐ์ดํ„ฐ์…‹ ๋งŒ๋“ค์–ด์คŒ (์†Œ์ˆ˜์  ์ฒซ์งธ์ž๋ฆฌ์˜ ์‹ค์ˆ˜ํ˜• ๋ฐ์ดํ„ฐ)
  • ๋ถ„์‚ฐํ˜•(scatter), ๊บพ์€์„ ํ˜•(line) ๋‘ ๊ฐ€์ง€ ์œ ํ˜•์˜ ์ฐจํŠธ
  • ๋ฐ์ดํ„ฐ์…‹์˜ ๊ฐ’๋“ค์„ ํ•˜์ด์ฐจํŠธ์˜ series ์˜ต์…˜์— ํ• ๋‹น