Line Chart
config setup
const data = {
labels: labels,
datasets: [{
label: 'My First Dataset',
data: [65, 59, 80, 81, 56, 55, 40],
fill: false,
borderColor: 'rgb(75, 192, 192)',
tension: 0.1
}]
};
Namespaces:
data.datasets[index]
- options for this dataset onlyoptions.datasets.line
- options for all line datasetsoptions.elements.point
- options for all point elementsoptions
- options for the whole chart
The line chart allows a number of properties to be specified for each dataset. These are used to set display properties for a specific dataset. For example, the colour of a line is generally set this way.
All these values, if undefined
, fallback to the scopes described in
Point Styling
The style of each point can be controlled with the following properties:
All these values, if undefined
, fallback first to the dataset options then to the associated options.
The style of the line can be controlled with the following properties:
If the value is undefined
, showLine
and spanGaps
fallback to the associated chart configuration options. The rest of the values fallback to the associated options.
Interactions
The following interpolation modes are supported.
'default'
'monotone'
The 'default'
algorithm uses a custom weighted cubic interpolation, which produces pleasant curves for all types of datasets.
The 'monotone'
algorithm is more suited to datasets: it preserves monotonicity (or piecewise monotonicity) of the dataset being interpolated, and ensures local extremums (if any) stay at input data points.
If left untouched (undefined
), the global options.elements.line.cubicInterpolationMode
property is used.
Segment
Line segment styles can be overridden by scriptable options in the segment
object. Currently all of the border*
and backgroundColor
options are supported. The segment styles are resolved for each section of the line between each point. undefined
fallbacks to main line styles.
TIP
To be able to style gaps, you need the spanGaps option enabled.
Context for the scriptable segment contains the following properties:
type
:'segment'
p0
: first point elementp1
: second point elementp0DataIndex
: index of first point in the data arrayp1DataIndex
: index of second point in the data arraydatasetIndex
: dataset index
false
: No Step Interpolation (default)true
: Step-before Interpolation (eq.'before'
)'before'
: Step-before Interpolation'after'
: Step-after Interpolation'middle'
: Step-middle Interpolation
If the stepped
value is set to anything other than false, tension
will be ignored.
Default Options
It is common to want to apply a configuration setting to all created line charts. The global line chart settings are stored in Chart.overrides.line
. Changing the global options only affects charts created after the change. Existing charts are not changed.
For example, to configure all line charts with spanGaps = true
you would do:
All of the supported can be used with line charts.
Stacked Area Chart
Line charts can be configured into stacked area charts by changing the settings on the y-axis to enable stacking. Stacked area charts can be used to show how one data trend is made up of a number of smaller pieces.
const stackedLine = new Chart(ctx, {
type: 'line',
data: data,
options: {
scales: {
y: {
stacked: true
}
}
}
});
A vertical line chart is a variation on the horizontal line chart. To achieve this you will have to set the indexAxis
property in the options object to 'y'
. The default for this property is 'x'
and thus will show horizontal lines.
config setup
const labels = Utils.months({count: 7});
labels: labels,
datasets: [{
axis: 'y',
label: 'My First Dataset',
data: [65, 59, 80, 81, 56, 55, 40],
fill: false,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(255, 159, 64, 0.2)',
'rgba(255, 205, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(201, 203, 207, 0.2)'
],
borderColor: [
'rgb(255, 99, 132)',
'rgb(255, 159, 64)',
'rgb(255, 205, 86)',
'rgb(75, 192, 192)',
'rgb(54, 162, 235)',
'rgb(153, 102, 255)',
'rgb(201, 203, 207)'
],
borderWidth: 1
}]
};
Config Options
The configuration options for the vertical line chart are the same as for the line chart. However, any options specified on the x-axis in a line chart, are applied to the y-axis in a vertical line chart.