Line Chart

    config setup

    1. const data = {
    2. labels: labels,
    3. datasets: [{
    4. label: 'My First Dataset',
    5. data: [65, 59, 80, 81, 56, 55, 40],
    6. fill: false,
    7. borderColor: 'rgb(75, 192, 192)',
    8. tension: 0.1
    9. }]
    10. };

    Namespaces:

    • data.datasets[index] - options for this dataset only
    • options.datasets.line - options for all line datasets
    • options.elements.point - options for all point elements
    • options - 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 element
    • p1: second point element
    • p0DataIndex: index of first point in the data array
    • p1DataIndex: index of second point in the data array
    • datasetIndex: 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.

    1. const stackedLine = new Chart(ctx, {
    2. type: 'line',
    3. data: data,
    4. options: {
    5. scales: {
    6. y: {
    7. stacked: true
    8. }
    9. }
    10. }
    11. });

    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.

    Line Chart - 图6

    config setup

    1. const labels = Utils.months({count: 7});
    2. labels: labels,
    3. datasets: [{
    4. axis: 'y',
    5. label: 'My First Dataset',
    6. data: [65, 59, 80, 81, 56, 55, 40],
    7. fill: false,
    8. backgroundColor: [
    9. 'rgba(255, 99, 132, 0.2)',
    10. 'rgba(255, 159, 64, 0.2)',
    11. 'rgba(255, 205, 86, 0.2)',
    12. 'rgba(75, 192, 192, 0.2)',
    13. 'rgba(54, 162, 235, 0.2)',
    14. 'rgba(153, 102, 255, 0.2)',
    15. 'rgba(201, 203, 207, 0.2)'
    16. ],
    17. borderColor: [
    18. 'rgb(255, 99, 132)',
    19. 'rgb(255, 159, 64)',
    20. 'rgb(255, 205, 86)',
    21. 'rgb(75, 192, 192)',
    22. 'rgb(54, 162, 235)',
    23. 'rgb(153, 102, 255)',
    24. 'rgb(201, 203, 207)'
    25. ],
    26. borderWidth: 1
    27. }]
    28. };

    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.

    Internal data format