top of page

Part 2 JS Charts : Line Chart

In this tutorial, we will be looking at JS Line Chart.


Below is the code used in the JS Line Chart.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>JS Line Chart</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.slim.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
  <link rel="stylesheet" type="text/css" href="customtable2.css">
  <!--
    
  -->

  <div class="container-fluid p-3 mt-3 bg-dark bg-gradient text-white">
    <h1>JS Charts</h1>
  </div>
  
</head>

<body>

<div class="row">

  <div class="container col-6 mt-3">
  <canvas id="mychart3"></canvas>
        
        <script>
          
        var xValues = [2019,2020,2021,2022,2023];
        var yValues = [1000,1100,900,1200,1000,800];

        new Chart("mychart3", {
        type: "line",
        data: {
            labels: xValues,
            datasets: [{

            //pointStyle : 'crossRot',
            //pointStyle : 'circle',
            pointStyle : 'triangle',

            backgroundColor: "rgba(0,0,255,0.1)",
            borderColor : "rgba(0,0,255,0.3)",

            pointBorderColor : "rgba(0,0,255)",
            pointHoverBorderColor : "rgba(255,255,0)",
            
            pointBackgroundColor : "rgba(0,0,100)",
            //pointBorderWidth : "5",
            pointHoverBorderWidth : "2",

            pointHoverRadius : "6",
            pointRadius : "4",
            

            data: yValues
            }]
        },
        options: {
            legend: {display: false},
            title: {
            display: true,
            text: "World Wine Production 2018"
            }
        }

        });
        </script>    
  
    </div>

  <div class="container col-6 mt-3">
      <canvas id="mychart4"></canvas>

            <script>
              
            var xValues = [2019,2020,2021,2022,2023];
            var yValues = [800,1100,900,1200,800,700];
            var zValues = [1100,900,1100,800,900];

            new Chart("mychart4", {
            type: "line",
            data: {
                labels: xValues,
                datasets: [{

                //pointStyle : 'crossRot',
                //pointStyle : 'circle',
                pointStyle : 'triangle',

                backgroundColor: "rgba(0,0,255,0.1)",
                borderColor : "rgba(0,0,255,0.3)",

                pointBorderColor : "rgba(0,0,255)",
                pointHoverBorderColor : "rgba(255,255,0)",
                
                pointBackgroundColor : "rgba(0,0,100)",
                //pointBorderWidth : "5",
                pointHoverBorderWidth : "2",

                pointHoverRadius : "6",
                pointRadius : "4",
                

                data: yValues},
                {
                  data: zValues,
                  borderColor: "green",
                  backgroundColor: "rgba(0,255,0,0.1)", 
                },
                
                {
                  data: [750,750,800,1000,1100],
                  borderColor: "red",
                  backgroundColor: "rgba(255,0,0,0.1)",
                },
              ]
            },
            options: {
                legend: {display: false},
                title: {
                display: true,
                text: "World Wine Production 2018"
                }
            }

            });
            </script> 
  </div>

</div>

</body>
</html>

For more detailed instruction, feel free to check out my YouTube video


Comments


bottom of page