Chart.js Line Chart
A line chart plots data arranged in columns or rows of a worksheet into a line chart.
A line chart can display continuous data that changes over time (based on common scale settings), so it is very suitable for showing trends in data at equal time intervals.
Line chart type The attribute is line, and type describes the chart type.
const config = {
type: 'line',
data: data,
};
Next, let's create a simple line chart:
Example
const
ctx
=
document.
getElementById
(
'myChart'
)
;
const
labels
=
[
'January'
,
'February'
,
'March'
,
'April'
,
'May'
,
'June'
,
'July'
]
;
// Set the corresponding labels on the X axis
const
data
=
{
labels
:
labels
,
datasets
:
[
{
label
:
'My First Line Chart'
,
data
:
[
65
,
59
,
80
,
81
,
56
,
55
,
40
]
,
fill
:
false
,
borderColor
:
'rgb(75, 192, 192)'
,
// Set the line color
tension
:
0.1
}
]
}
;
const
config
=
{
type
:
'line'
,
// Set the chart type
data
:
data
,
}
;
const
myChart
=
new
Chart
(
ctx
,
config
)
;
Try it »
The output of the above example is:
Next, let's enrich the line chart by adding options as follows:
Example
const
ctx
=
document.
getElementById
(
'myChart'
)
;
const
labels
=
[
'January'
,
'February'
,
'March'
,
'April'
,
'May'
,
'June'
,
'July'
]
;
// Set the corresponding labels on the X axis
const
data
=
{
labels
:
labels
,
datasets
:
[
{
label
:
'My First Line Chart'
,
data
:
[
65
,
59
,
80
,
81
,
56
,
55
,
40
]
,
fill
:
false
,
borderColor
:
'rgb(75, 192, 192)'
,
// Set the line color
backgroundColor
:
[
'rgba(179, 0, 33, 0.5)'
]
,
// Set the point fill color
pointStyle
:
'circle'
,
// Set the point type to circle
pointRadius
:
6
,
// Set the circle radius
pointHoverRadius
:
10
,
// Set the circle radius on mouse hover
tension
:
0.1
}
]
}
;
const
config
=
{
type
:
'line'
,
// Set the chart type
data
:
data
,
options
:
{
responsive
:
true
,
// Make the chart responsive
interaction
:
{
// Set interaction for each point
intersect
:
false
,
}
,
scales
:
{
// Set the X axis and Y axis
x
:
{
display
:
true
,
title
:
{
display
:
true
,
text
:
'Date'
}
}
,
y
:
{
display
:
true
,
title
:
{
display
:
true
,
text
:
'Votes'
}
}
}
}
}
;
const
myChart
=
new
Chart
(
ctx
,
config
)
;
Try it »
The output of the above example is:
Vertical Line Chart
A vertical line chart is a variant of the horizontal line chart.
To create a vertical line chart, set the indexAxis property in the options object to y; the default value of indexAxis is x.
Example
const
ctx
=
document.
getElementById
(
'myChart'
)
;
const
labels
=
[
'January'
,
'February'
,
'March'
,
'April'
,
'May'
,
'June'
,
'July'
]
;
// Set the corresponding labels on the X axis
const
data
=
{
labels
:
labels
,
datasets
:
[
{
label
:
'My First Line Chart'
,
data
:
[
65
,
59
,
80
,
81
,
56
,
55
,
40
]
,
fill
:
false
,
borderColor
:
'rgb(75, 192, 192)'
,
// Set the line color
backgroundColor
:
[
'rgba(179, 0, 33, 0.5)'
]
,
// Set the point fill color
pointStyle
:
'circle'
,
// Set the point type to circle
pointRadius
:
6
,
// Set the circle radius
pointHoverRadius
:
10
,
// Set the circle radius on mouse hover
tension
:
0.1
}
]
}
;
const
config
=
{
type
:
'line'
,
// Set the chart type
data
:
data
,
options
:
{
indexAxis
:
'y'
,
// Set a vertical line chart
responsive
:
true
,
// Make the chart responsive
interaction
:
{
// Set interaction for each point
intersect
:
false
,
}
,
scales
:
{
// Set the X axis and Y axis
x
:
{
beginAtZero
:
true
,
// Set the X axis to start from 0
display
:
true
,
title
:
{
display
:
true
,
text
:
'Date'
}
}
,
y
:
{
display
:
true
,
title
:
{
display
:
true
,
text
:
'Votes'
}
}
}
}
}
;
const
myChart
=
new
Chart
(
ctx
,
config
)
;
Try it »
The output of the above example is: