Chart.js/docs/02-Line-Chart.md

156 lines
5.8 KiB
Markdown
Raw Normal View History

2014-06-29 19:35:49 +02:00
---
title: Line Chart
anchor: line-chart
---
###Introduction
A line chart is a way of plotting data points on a line.
Often, it is used to show trend data, and the comparison of two data sets.
<div class="canvas-holder">
<canvas width="250" height="125"></canvas>
</div>
###Example usage
```javascript
2015-06-23 01:05:50 +02:00
var myLineChart = new Chart(ctx, {
type: 'line',
data: data,
2015-06-23 01:05:50 +02:00
options: options
});
```
Alternatively a line chart can be created using syntax similar to the v1.0 syntax
```javascript
var myLineChart = Chart.Line(ctx, {
data: data,
2015-06-04 02:33:50 +02:00
options: options
});
2014-06-29 19:35:49 +02:00
```
###Data structure
```javascript
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
2015-06-04 02:46:35 +02:00
// Boolean - if true fill the area under the line
fill: false,
2015-06-04 02:46:35 +02:00
// String - the color to fill the area under the line with if fill is true
backgroundColor: "rgba(220,220,220,0.2)",
2015-06-04 02:46:35 +02:00
// The properties below allow an array to be specified to change the value of the item at the given index
2015-06-04 02:46:35 +02:00
// String or array - Line color
borderColor: "rgba(220,220,220,1)",
2015-06-23 01:05:50 +02:00
// String - cap style of the line. See https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineCap
borderCapStyle: 'butt',
// Array - Length and spacing of dashes. See https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash
borderDash: [],
// Number - Offset for line dashes. See https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset
borderDashOffset: 0.0,
// String - line join style. See https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineJoin
borderJoinStyle: 'miter',
2015-06-04 02:46:35 +02:00
// String or array - Point stroke color
pointBorderColor: "rgba(220,220,220,1)",
2015-06-04 02:46:35 +02:00
// String or array - Point fill color
pointBackgroundColor: "#fff",
2015-06-04 02:46:35 +02:00
// Number or array - Stroke width of point border
pointBorderWidth: 1,
// Number or array - Radius of point when hovered
2015-06-04 02:49:22 +02:00
pointHoverRadius: 5,
2015-06-04 02:46:35 +02:00
// String or array - point background color when hovered
2015-06-04 02:49:22 +02:00
pointHoverBackgroundColor: "rgba(220,220,220,1)",
2015-06-04 02:46:35 +02:00
// Point border color when hovered
2015-06-04 02:49:22 +02:00
pointHoverBorderColor: "rgba(220,220,220,1)",
2015-06-04 02:46:35 +02:00
// Number or array - border width of point when hovered
pointHoverBorderWidth: 2,
2015-06-04 02:46:35 +02:00
// Tension - bezier curve tension of the line. Set to 0 to draw straight Wlines connecting points
2015-12-22 10:50:11 +01:00
tension: 0.1,
2015-06-04 02:46:35 +02:00
// The actual data
data: [65, 59, 80, 81, 56, 55, 40],
// String - If specified, binds the dataset to a certain y-axis. If not specified, the first y-axis is used.
yAxisID: "y-axis-1",
2014-06-29 19:35:49 +02:00
},
{
label: "My Second dataset",
2015-06-04 02:46:35 +02:00
fill: false,
backgroundColor: "rgba(220,220,220,0.2)",
borderColor: "rgba(220,220,220,1)",
pointBorderColor: "rgba(220,220,220,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(220,220,220,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
2014-06-29 19:35:49 +02:00
data: [28, 48, 40, 19, 86, 27, 90]
}
]
};
```
2016-02-08 01:55:32 +01:00
The line chart requires an array of labels. This labels are shown on the X axis. There must be one label for each data point. More labels than datapoints are allowed, in which case the line ends at the last data point.
2014-06-29 19:35:49 +02:00
The data for line charts is broken up into an array of datasets. Each dataset has a colour for the fill, a colour for the line and colours for the points and strokes of the points. These colours are strings just like CSS. You can use RGBA, RGB, HEX or HSL notation.
The label key on each dataset is optional, and can be used when generating a scale for the chart.
### Chart options
These are the customisation options specific to Line charts. These options are merged with the [global chart configuration options](#getting-started-global-chart-configuration), and form the options of the chart.
The default options for line chart are defined in `Chart.defaults.Line`.
Name | Type | Default | Description
--- | --- | --- | ---
2016-01-14 13:39:06 +01:00
showLines | Boolean | true | If false, the lines between points are not drawn
stacked | Boolean | false | If true, lines stack on top of each other along the y axis.
*hover*.mode | String | "label" | Label's hover mode. "label" is used since the x axis displays data by the index in the dataset.
scales | - | - | -
*scales*.xAxes | Array | `[{type:"category","id":"x-axis-1"}]` | Defines all of the x axes used in the chart. See the [scale documentation](#getting-started-scales) for details on the available options.
*Options for xAxes* | | |
type | String | "category" | As defined in ["Category"](#scales-category-scale).
id | String | "x-axis-1" | Id of the axis so that data can bind to it.
| | |
*scales*.yAxes | Array | `[{type:"linear","id":"y-axis-1"}]` | Defines all of the x axes used in the chart. See the [scale documentation](#getting-started-scales) for details on the available options.
*Options for yAxes* | | |
type | String | "linear" | As defined in ["Linear"](#scales-linear-scale).
id | String | "y-axis-1" | Id of the axis so that data can bind to it.
2014-06-29 19:35:49 +02:00
You can override these for your `Chart` instance by passing a member `options` into the `Line` method.
2014-06-29 19:35:49 +02:00
For example, we could have a line chart display without an x axis by doing the following. The config merge is smart enough to handle arrays so that you do not need to specify all axis settings to change one thing.
2014-06-29 19:35:49 +02:00
```javascript
2015-06-23 01:05:50 +02:00
new Chart(ctx, {
type: 'line',
data: data,
options: {
xAxes: [{
2015-11-25 01:21:09 +01:00
display: false
}]
}
2014-06-29 19:35:49 +02:00
});
// This will create a chart with all of the default options, merged from the global config,
// and the Line chart defaults, but this particular instance will have the x axis not displaying.
2014-06-29 19:35:49 +02:00
```
2015-12-22 10:50:11 +01:00
We can also change these defaults values for each Line type that is created, this object is available at `Chart.defaults.line`.