Check Out the Chart.js Course: Chart.js, The Missing Manual Course
Categories
Uncategorized

How to draw a dashed line on a scatter plot in Chart JS 4

To do this we will need to use showLine to force a line being drawn and add a borderDash with an array value. Where we specific how many pixels are solid and how many pixels are transparent.

  1. showLine: true:
    • This property is used to determine whether to draw a line between data points or not.
    • When set to true, a line will be drawn connecting the data points in the chart.
    • In your context, since you’re using a scatter plot with the type: 'line', setting showLine: true makes sure that a line is drawn connecting the data points, essentially converting your scatter plot into a line chart.
  2. borderDash: [6, 6]:
    • This property is used to create a dashed line effect in the border of the dataset.
    • It takes an array of values, where each pair of values represents the length of the dash and the length of the gap between dashes.
    • In your example, [6, 6] means that the line will consist of dashes with a length of 6 units, followed by gaps of 6 units. This creates a visually distinct dashed pattern in the line.

In summary, these properties are used to control the appearance of the line in your line chart.

The showLine: true ensures that a line is drawn, and borderDash: [6, 6] specifies that the line should be dashed with a dash length of 6 units and a gap length of 6 units.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Getting Started with Chart JS with www.chartjs3.com</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        font-family: sans-serif;
      }
      .chartMenu {
        width: 100vw;
        height: 40px;
        background: #1A1A1A;
        color: rgba(54, 162, 235, 1);
      }
      .chartMenu p {
        padding: 10px;
        font-size: 20px;
      }
      .chartCard {
        width: 100vw;
        height: calc(100vh - 40px);
        background: rgba(54, 162, 235, 0.2);
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .chartBox {
        width: 700px;
        padding: 20px;
        border-radius: 20px;
        border: solid 3px rgba(54, 162, 235, 1);
        background: white;
      }
    </style>
  </head>
  <body>
    <div class="chartMenu">
      <p>WWW.CHARTJS3.COM (Chart JS <span id="chartVersion"></span>)</p>
    </div>
    <div class="chartCard">
      <div class="chartBox">
        <canvas id="myChart"></canvas>
      </div>
    </div>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/chart.js/dist/chart.umd.min.js"></script>
    <script>
    // setup 
    const data = {
      labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      datasets: [{
        label: 'Weekly Sales',
        data: [18, 12, 6, 9, 12, 3, 9],
        backgroundColor: [
          'rgba(255, 26, 104, 0.2)',
          'rgba(54, 162, 235, 0.2)',
          'rgba(255, 206, 86, 0.2)',
          'rgba(75, 192, 192, 0.2)',
          'rgba(153, 102, 255, 0.2)',
          'rgba(255, 159, 64, 0.2)',
          'rgba(0, 0, 0, 0.2)'
        ],
        borderColor: [
          'rgba(255, 26, 104, 1)',
          'rgba(54, 162, 235, 1)',
          'rgba(255, 206, 86, 1)',
          'rgba(75, 192, 192, 1)',
          'rgba(153, 102, 255, 1)',
          'rgba(255, 159, 64, 1)',
          'rgba(0, 0, 0, 1)'
        ],
        borderWidth: 1,
        showLine: true,
        borderDash: [6, 6]
      }]
    };

    // config 
    const config = {
      type: 'line',
      data,
      options: {
        scales: {
          y: {
            beginAtZero: true
          }
        }
      }
    };

    // render init block
    const myChart = new Chart(
      document.getElementById('myChart'),
      config
    );

    // Instantly assign Chart.js version
    const chartVersion = document.getElementById('chartVersion');
    chartVersion.innerText = Chart.version;
    </script>

  </body>
</html>
en_USEnglish