How to Change the Line Width of A Chart.js Line Chart

The default line width of a Chart.js line chasrt is 3 which is quite thick to my eyes. Here is how we can change the line width both indivisually or gloabally.

The line width property is called borderWidth, which can found in both the dataset and the element namespace.

Changing the borderWidth property in the dataset changes the line width for a single line, while chaging the borderWidth in the element changes all lines' width. We can find borderWidth for all lines in the options/elemets/line namespace.

Changing Indivisual Line's Width

Black Line Width:
Red Line Width:

Sample Code

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<canvas id="myChart"></canvas>
<script>
const labels = [
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
  ];

  const data = {
    labels: labels,
    datasets: [{
      label: 'My First dataset',
      borderColor: 'black',
      data: [0, 10, 5, 2, 20, 30, 45],
      borderWidth:3
    },
          {
      label: 'My Second dataset',
      borderColor: 'rgb(255, 99, 132)',
      data: [1, 20, 3, 4, 30, 20, 5],
      borderWidth:3
    }]
  };

  const config = {
    type: 'line',
    data: data,
    options: {}
  };
  
  const myChart = new Chart(
    document.getElementById('myChart'),
    config
  );
</script>

Change All Lines Width

Lines' Width:

Sample Code

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<canvas id="myChart"></canvas>
<script>
const labels = [
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
  ];

  const data = {
    labels: labels,
    datasets: [{
      label: 'My First dataset',
      borderColor: 'black',
      data: [0, 10, 5, 2, 20, 30, 45]
    },
          {
      label: 'My Second dataset',
      borderColor: 'rgb(255, 99, 132)',
      data: [1, 20, 3, 4, 30, 20, 5]
    }]
  };

  const config = {
    type: 'line',
    data: data,
    options: {
    	line:{
            	borderWidth:3
            }
    }
  };
  
  const myChart = new Chart(
    document.getElementById('myChart'),
    config
  );
</script>

What About the Size of Circles Represeting Data Points in the Lines

As we can see, there are dots on the line that represt data point and when the lines are thin, the dots will look relatively big. Here is how we chanage the dot size. We change the pointRadius property.

Change Indivisual Dot Size

Black Line Dot Size:
Red Line Dot Size:

Sample Code

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<canvas id="myChart"></canvas>
<script>
const labels = [
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
  ];

  const data = {
    labels: labels,
    datasets: [{
      label: 'My First dataset',
      borderColor: 'black',
      data: [0, 10, 5, 2, 20, 30, 45],
      pointRadius:1
    },
          {
      label: 'My Second dataset',
      borderColor: 'rgb(255, 99, 132)',
      data: [1, 20, 3, 4, 30, 20, 5],
      pointRadius:1
    }]
  };

  const config = {
    type: 'line',
    data: data,
    options: {}
  };
  
  const myChart = new Chart(
    document.getElementById('myChart'),
    config
  );
</script>

Change All Dots' Size

Dot Size:

Sample Code

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<canvas id="myChart"></canvas>
<script>
const labels = [
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
  ];

  const data = {
    labels: labels,
    datasets: [{
      label: 'My First dataset',
      borderColor: 'black',
      data: [0, 10, 5, 2, 20, 30, 45]
    },
          {
      label: 'My Second dataset',
      borderColor: 'rgb(255, 99, 132)',
      data: [1, 20, 3, 4, 30, 20, 5]
    }]
  };

  const config = {
    type: 'line',
    data: data,
    options: {
    	elements:{
        	point:{
            	pointRadius:1
            }
        }
    }
  };
  
  const myChart = new Chart(
    document.getElementById('myChart'),
    config
  );
</script>

Comments

Popular posts from this blog

How to Make A Reusable Image Slideshow HTML Component With Vanilla JavaScript

HTML Tags and Inline CSS that Work In Plotly.js Title

How to Type Spaces In HTML Input And Display In the Browser