score:303
Solution for Version 1 (old charts version)
According to documentation on chartjs.org
you can set the 'bezierCurve' in options and pass it in when you create the chart.
bezierCurve: false
eg:
var options = {
//Boolean - Whether the line is curved between points
bezierCurve : false
};
var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(lineChartData, options);
Update for version 2
According to updated documentation for Line Configuration in global options
Name Type Default Description
tension Number 0.4 Default bezier curve tension. Set to 0 for no bezier curves.
eg:
var options = {
elements: {
line: {
tension: 0
}
}
};
And also directly in the Dataset Structure by setting lineTension
to 0 (zero).
Property Type Usage
lineTension Number Bezier curve tension of the line. Set to 0 to draw straightlines.
This option is ignored if monotone cubic interpolation is used.
Note This was renamed from 'tension' but the old name still works.
An example data object using these attributes is shown below.
var lineChartData = {
labels: labels,
datasets: [
{
label: "My First dataset",
lineTension: 0,
data: data,
}
]
};
score:2
I think it's been updated to lineTension
. Check the docs.
score:10
Just to complete version compatibility and to add something to this nice thread here:
Still the same with chart.js
v3.x.x
(which is not backwards compatible with v2.x.x -- however, lineTension
stays unchanged within
data: { datasets: [{ lineTension: ...
)
LineTension for chart.js
v3.x.x
Following, you can Run the snippet with 10 buttons to play with different lineTensions (0 to 1) right here:
// for now, let's assume sample data
let sample_data = {
"Labels" : [
"2021-08-02",
"2021-08-03",
"2021-08-04",
"2021-08-05",
"2021-08-06"
],
"Values": [
6,
4,
3,
8,
2
]
};
// Draw chart
const ctx = document.querySelector('canvas').getContext('2d');
const myLineChart = new Chart(ctx, {
type: 'line',
data: {
labels: sample_data.Labels,
datasets: [{
label: 'LineTension Sample',
data: sample_data.Values,
lineTension: 0,
borderColor: 'rgba(0,255,0,1)',
backgroundColor: 'rgba(0,255,0,0.3)',
fill: true
}]
}
});
function lineTension(event) {
// Redraw the chart with modified lineTension
// a bit of 'button-cosmetics' here
// enabling all buttons
document.querySelectorAll('button').forEach(element => element.disabled = false);
// disabling the pressed button
event.target.disabled = true;
// setting programmatically the 'lineTension' here
myLineChart.data.datasets[0].lineTension = parseFloat(event.target.dataset.linetension);
myLineChart.update();
};
button {
color: blue;
}
button:disabled {
color: black;
background-color: rgba(0,255,0,0.3);
}
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<!-- gets you the latest version of Chart.js, now at v3.5.0 -->
<button onclick="lineTension(event)" data-linetension="0" disabled>0</button>
<button onclick="lineTension(event)" data-linetension="0.1">0.1</button>
<button onclick="lineTension(event)" data-linetension="0.2">0.2</button>
<button onclick="lineTension(event)" data-linetension="0.3">0.3</button>
<button onclick="lineTension(event)" data-linetension="0.4">0.4</button>
<button onclick="lineTension(event)" data-linetension="0.5">0.5</button>
<button onclick="lineTension(event)" data-linetension="0.6">0.6</button>
<button onclick="lineTension(event)" data-linetension="0.7">0.7</button>
<button onclick="lineTension(event)" data-linetension="0.8">0.8</button>
<button onclick="lineTension(event)" data-linetension="0.9">0.9</button>
<button onclick="lineTension(event)" data-linetension="1">1</button>
<canvas width="320" height="240"></canvas>
score:13
I have used lineTension to set the smoothness of the curve.
From the docs: lineTension receives a number, Bezier curve tension of the line. Set to 0 to draw straight lines. This option is ignored if monotone cubic interpolation is used.
Just make sure to test with different values how smooth you want the line.
For example:
var data = {
labels: ["Jan", "Feb", "Mar"],
datasets: [{
label: "Label 1",
lineTension: 0.2
}, {
label: "Stock B",
lineTension: 0.2
}
]
};
score:72
You can use lineTension option to set the desired curve. Set 0 for straight lines. You can give a number between 0-1
data: {
datasets: [{
lineTension: 0
}]
}
Source: stackoverflow.com
Related Query
- Chart.js : straight lines instead of curves
- chart js draw line chart by points instead of by lines
- Chart.js : straight lines and curves lines in the same graph
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- How can I make two of my lines in Chart JS thicker
- Chart JS Fill Between two lines
- ChartJs bar chart - keep bars left instead of equally spread across the width
- How to display data labels outside in pie chart with lines in ionic
- charts.js straight lines - i cant find a solution
- ChartJS Radar Chart radar lines color?
- Find intersection between the chart lines in chartjs
- Chart.js bar chart mouse hovering highlights all the datasets instead of just the selected one
- Chart js logarithmic line chart showing NaN values instead of null
- Angular-chart / line chart with multiple horizontal lines (margins)
- ChartJS v2 - Keep tooltip open when user click on a point of a multiple lines chart
- Chart x axis displays in timestamp instead of dates Chart.js
- How to Remove axis Lines from chart in chart js
- Chart.js v2 - combined stacked bar chart and 2 unstacked lines
- change space between horizontal (grid) lines for line chart
- How to make lines in line charts from ng2-charts straight lines?
- Tooltips in Chart js are always black instead of the color of the corresponding dataset
- React JS Chart JS 2 is not hiding the grid lines in the background
- Chart JS, ng2-Charts - How to make the label to the right of pie chart instead on top?
- Chart.js: Reverse bar chart with regular bars (Bottom to top instead of top to bottom)
- chart.js-plugin-annotations multiple horizontal lines in one chart
- Connect the dot vertically instead of horizontally on Line Chart
- Chart JS tooltip appears differently when set from script instead of html
- Chart JS stacked labels instead of making them horizontal
- How to display date instead of year in morris line chart in js
- Draw points and lines inside a bar in bar chart
More Query from same tag
- Chart.js - line chart with two yAxis: "TypeError: yScale is undefined"
- Chartjs Counting Elements in a Chart
- Is it possible to have a canvas next to another canvas without space on the bottom?
- Chart.js V2 ticklines no grid
- Using associative PHP array in JS to later on use in chart generated via chart.js
- How do I create a chartjs bar chart?
- Chart: Two bipolar bars below each other
- How do you create rounded corners on the top and bottom of a bar chart and the between 2 part still draw like image?
- ChartJS - How to change color of some data points in graph
- Why is here a number?
- Chartjs working with large and small values
- How to change Y axis labels
- Chartjs isn't rendering bar chart, even though chart variable shows correctly when console logged
- How to set colors for Chart.js tooltip labels
- Javascript highcharts code similar to image
- Maximum call stack error when attempting to update Chart in Vue js
- Modifying the X-Axis Labels of a Scatterplot in Chart.js 2
- Parsing json in jQuery error
- Chartkick cumulative chart by date never descending
- Ng2-charts Time xAxes : unwanted high zoom on click
- Display Chartjs tooltip values outside the canvas - multi line chart
- Cannot read property 'length' of null error in Javascript
- Identify which chartJs chart was clicked on
- Chart.js x axis date-time formatting fails
- How to use a Data Array within a Chart JS dataset?
- Chart.js reset zoom button
- How to get the image from graph created using chartjs
- Print pie chart in chartjs
- ChartJS Horizontal bars barely visible with 50 records on the Y axis
- How can i do this chart in react?