Bar

A bar chart provides a way of showing data values represented as vertical bars. It is sometimes used to show trend data, and the comparison of multiple data sets side by side.

Example Usage

var myBarChart = new Chart(ctx, {
    type: 'bar',
    data: data,
    options: options
});

Dataset Properties

The bar 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 color of the bars is generally set this way.

Name Type Scriptable Indexable Default
backgroundColor Color Yes Yes 'rgba(0,0,0,0.1)'
borderColor Color Yes Yes 'rgba(0,0,0,0.1)'
borderSkipped String Yes Yes 'bottom'
borderWidth Number Yes Yes 0
data Object[] - - required
hoverBackgroundColor Color - Yes undefined
hoverBorderColor Color - Yes undefined
hoverBorderWidth Number - Yes 1
label String - - ''
xAxisID String - - first x axis
yAxisID String - - first y axis

General

Name Description
label The label for the dataset which appears in the legend and tooltips.
xAxisID The ID of the x axis to plot this dataset on.
yAxisID The ID of the y axis to plot this dataset on.

Styling

The style of each bar can be controlled with the following properties:

Name Description
backgroundColor The bar background color.
borderColor The bar border color.
borderSkipped The edge to skip when drawing bar.
borderWidth The bar border width (in pixels).

All these values, if undefined, fallback to the associated elements.rectangle.* options.

borderSkipped

This setting is used to avoid drawing the bar stroke at the base of the fill. In general, this does not need to be changed except when creating chart types that derive from a bar chart.

Options are:

  • 'bottom'
  • 'left'
  • 'top'
  • 'right'

Interactions

The interaction with each bar can be controlled with the following properties:

Name Description
hoverBackgroundColor The bar background color when hovered.
hoverBorderColor The bar border color hovered.
hoverBorderWidth The bar border width when hovered (in pixels).

All these values, if undefined, fallback to the associated elements.point.* options.

Scale Configuration

The bar chart accepts the following configuration from the associated scale options:

Name Type Default Description
barPercentage Number 0.9 Percent (0-1) of the available width each bar should be within the category width. 1.0 will take the whole category width and put the bars right next to each other. more...
categoryPercentage Number 0.8 Percent (0-1) of the available width each category should be within the sample width. more...
barThickness Number/String Manually set width of each bar in pixels. If set to 'flex', it computes "optimal" sample widths that globally arrange bars side by side. If not set (default), bars are equally sized based on the smallest interval. more...
maxBarThickness Number Set this to ensure that bars are not sized thicker than this.
minBarLength Number Set this to ensure that bars have a minimum length in pixels.
gridLines.offsetGridLines Boolean true If true, the bars for a particular data point fall between the grid lines. The grid line will move to the left by one half of the tick interval. If false, the grid line will go right down the middle of the bars. more...

Example Usage

options = {
    scales: {
        xAxes: [{
            barPercentage: 0.5,
            barThickness: 6,
            maxBarThickness: 8,
            minBarLength: 2,
            gridLines: {
                offsetGridLines: true
            }
        }]
    }
}

barThickness

If this value is a number, it is applied to the width of each bar, in pixels. When this is enforced, barPercentage and categoryPercentage are ignored.

If set to 'flex', the base sample widths are calculated automatically based on the previous and following samples so that they take the full available widths without overlap. Then, bars are sized using barPercentage and categoryPercentage. There is no gap when the percentage options are 1. This mode generates bars with different widths when data are not evenly spaced.

If not set (default), the base sample widths are calculated using the smallest interval that prevents bar overlapping, and bars are sized using barPercentage and categoryPercentage. This mode always generates bars equally sized.

offsetGridLines

If true, the bars for a particular data point fall between the grid lines. The grid line will move to the left by one half of the tick interval, which is the space between the grid lines. If false, the grid line will go right down the middle of the bars. This is set to true for a category scale in a bar chart while false for other scales or chart types by default.

Default Options

It is common to want to apply a configuration setting to all created bar charts. The global bar chart settings are stored in Chart.defaults.bar. Changing the global options only affects charts created after the change. Existing charts are not changed.

barPercentage vs categoryPercentage

The following shows the relationship between the bar percentage option and the category percentage option.

// categoryPercentage: 1.0
// barPercentage: 1.0
Bar:        | 1.0 | 1.0 |
Category:   |    1.0    |
Sample:     |===========|

// categoryPercentage: 1.0
// barPercentage: 0.5
Bar:          |.5|  |.5|
Category:  |      1.0     |
Sample:    |==============|

// categoryPercentage: 0.5
// barPercentage: 1.0
Bar:            |1.||1.|
Category:       |  .5  |
Sample:     |==============|

Data Structure

The data property of a dataset for a bar chart is specified as a an array of numbers. Each point in the data array corresponds to the label at the same index on the x axis.

data: [20, 10]

You can also specify the dataset as x/y coordinates when using the time scale.

data: [{x:'2016-12-25', y:20}, {x:'2016-12-26', y:10}]

Stacked Bar Chart

Bar charts can be configured into stacked bar charts by changing the settings on the X and Y axes to enable stacking. Stacked bar charts can be used to show how one data series is made up of a number of smaller pieces.

var stackedBar = new Chart(ctx, {
    type: 'bar',
    data: data,
    options: {
        scales: {
            xAxes: [{
                stacked: true
            }],
            yAxes: [{
                stacked: true
            }]
        }
    }
});

Dataset Properties

The following dataset properties are specific to stacked bar charts.

Name Type Description
stack String The ID of the group to which this dataset belongs to (when stacked, each group will be a separate stack).

Horizontal Bar Chart

A horizontal bar chart is a variation on a vertical bar chart. It is sometimes used to show trend data, and the comparison of multiple data sets side by side.

Example

var myBarChart = new Chart(ctx, {
    type: 'horizontalBar',
    data: data,
    options: options
});

Config Options

The configuration options for the horizontal bar chart are the same as for the bar chart. However, any options specified on the x axis in a bar chart, are applied to the y axis in a horizontal bar chart.

The default horizontal bar configuration is specified in Chart.defaults.horizontalBar.

results matching ""

    No results matching ""