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

  1. var myBarChart = new Chart(ctx, {
  2. type: 'bar',
  3. data: data,
  4. options: options
  5. });

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

Some properties can be specified as an array. If these are set to an array value, the first value applies to the first bar, the second value to the second bar, and so on.

NameTypeDescription
labelStringThe label for the dataset which appears in the legend and tooltips.
xAxisIDStringThe ID of the x axis to plot this dataset on. If not specified, this defaults to the ID of the first found x axis
yAxisIDStringThe ID of the y axis to plot this dataset on. If not specified, this defaults to the ID of the first found y axis.
backgroundColorColor/Color[]The fill color of the bar. See Colors
borderColorColor/Color[]The color of the bar border. See Colors
borderWidthNumber/Number[]The stroke width of the bar in pixels.
borderSkippedStringWhich edge to skip drawing the border for. more…
hoverBackgroundColorColor/Color[]The fill colour of the bars when hovered.
hoverBorderColorColor/Color[]The stroke colour of the bars when hovered.
hoverBorderWidthNumber/Number[]The stroke width of the bars when hovered.

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'

Configuration Options

The bar chart defines the following configuration options. These options are merged with the global chart configuration options, Chart.defaults.global, to form the options passed to the chart.

NameTypeDefaultDescription
barPercentageNumber0.9Percent (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…
categoryPercentageNumber0.8Percent (0-1) of the available width each category should be within the sample width. more…
barThicknessNumberManually set width of each bar in pixels. If not set, the base sample widths are calculated automatically so that they take the full available widths without overlap. Then, the bars are sized using barPercentage and categoryPercentage.
maxBarThicknessNumberSet this to ensure that bars are not sized thicker than this.
gridLines.offsetGridLinesBooleantrueIf 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…

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 bar chart while false for other charts by default.

This setting applies to the axis configuration. If axes are added to the chart, this setting will need to be set for each new axis.

  1. options = {
  2. scales: {
  3. xAxes: [{
  4. gridLines: {
  5. offsetGridLines: true
  6. }
  7. }]
  8. }
  9. }

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.

  1. // categoryPercentage: 1.0
  2. // barPercentage: 1.0
  3. Bar: | 1.0 | 1.0 |
  4. Category: | 1.0 |
  5. Sample: |===========|
  6. // categoryPercentage: 1.0
  7. // barPercentage: 0.5
  8. Bar: |.5| |.5|
  9. Category: | 1.0 |
  10. Sample: |==============|
  11. // categoryPercentage: 0.5
  12. // barPercentage: 1.0
  13. Bar: |1.||1.|
  14. Category: | .5 |
  15. 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.

  1. data: [20, 10]

You can also specify the dataset as x/y coordinates.

  1. 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.

  1. var stackedBar = new Chart(ctx, {
  2. type: 'bar',
  3. data: data,
  4. options: {
  5. scales: {
  6. xAxes: [{
  7. stacked: true
  8. }],
  9. yAxes: [{
  10. stacked: true
  11. }]
  12. }
  13. }
  14. });

Dataset Properties

The following dataset properties are specific to stacked bar charts.

NameTypeDescription
stackStringThe 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

  1. var myBarChart = new Chart(ctx, {
  2. type: 'horizontalBar',
  3. data: data,
  4. options: options
  5. });

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.