Bar Chart

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.

Bar Chart - 图1

config setup

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

Dataset Properties

Namespaces:

  • data.datasets[index] - options for this dataset only
  • options.datasets.bar - options for all bar datasets
  • options.elements.bar - options for all bar elements
  • options - options for the whole chart

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. Only the data option needs to be specified in the dataset namespace.

NameTypeScriptableIndexableDefault
backgroundColorColorYesYes‘rgba(0, 0, 0, 0.1)’
basenumberYesYes
barPercentagenumber--0.9
barThicknessnumber|string--
borderColorColorYesYes‘rgba(0, 0, 0, 0.1)’
borderSkippedstring|booleanYesYes‘start’
borderWidthnumber|objectYesYes0
borderRadiusnumber|objectYesYes0
categoryPercentagenumber--0.8
clipnumber|object|false--
dataobject|object[]| number[]|string[]--required
groupedboolean--true
hoverBackgroundColorColorYesYes
hoverBorderColorColorYesYes
hoverBorderWidthnumberYesYes1
hoverBorderRadiusnumberYesYes0
indexAxisstring--‘x’
inflateAmountnumber|‘auto’YesYes‘auto’
maxBarThicknessnumber--
minBarLengthnumber--
labelstring--‘’
ordernumber--0
pointStylepointStyleYes-‘circle’
skipNullboolean--
stackstring--‘bar’
xAxisIDstring--first x axis
yAxisIDstring--first y axis

All these values, if undefined, fallback to the scopes described in option resolution

Example dataset configuration

  1. data: {
  2. datasets: [{
  3. barPercentage: 0.5,
  4. barThickness: 6,
  5. maxBarThickness: 8,
  6. minBarLength: 2,
  7. data: [10, 20, 30, 40, 50, 60, 70]
  8. }]
  9. };

General

NameDescription
baseBase value for the bar in data units along the value axis. If not set, defaults to the value axis base value.
clipHow to clip relative to chartArea. Positive value allows overflow, negative value clips that many pixels inside chartArea. 0 = clip at chartArea. Clipping can also be configured per side: clip: {left: 5, top: false, right: -2, bottom: 0}
groupedShould the bars be grouped on index axis. When true, all the datasets at same index value will be placed next to each other centering on that index value. When false, each bar is placed on its actual index-axis value.
indexAxisThe base axis of the dataset. ‘x’ for vertical bars and ‘y’ for horizontal bars.
labelThe label for the dataset which appears in the legend and tooltips.
orderThe drawing order of dataset. Also affects order for stacking, tooltip and legend. more
skipNullIf true, null or undefined values will not be used for spacing calculations when determining bar size.
stackThe ID of the group to which this dataset belongs to (when stacked, each group will be a separate stack). more
xAxisIDThe ID of the x-axis to plot this dataset on.
yAxisIDThe ID of the y-axis to plot this dataset on.

Styling

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

NameDescription
backgroundColorThe bar background color.
borderColorThe bar border color.
borderSkippedThe edge to skip when drawing bar.
borderWidthThe bar border width (in pixels).
borderRadiusThe bar border radius (in pixels).
minBarLengthSet this to ensure that bars have a minimum length in pixels.
pointStyleStyle of the point for legend. more…

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

borderSkipped

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

Note

For negative bars in a vertical chart, top and bottom are flipped. Same goes for left and right in a horizontal chart.

Options are:

  • 'start'
  • 'end'
  • 'middle' (only valid on stacked bars: the borders between bars are skipped)
  • 'bottom'
  • 'left'
  • 'top'
  • 'right'
  • false (don’t skip any borders)
  • true (skip all borders)

borderWidth

If this value is a number, it is applied to all sides of the rectangle (left, top, right, bottom), except borderSkipped. If this value is an object, the left property defines the left border width. Similarly, the right, top, and bottom properties can also be specified. Omitted borders and borderSkipped are skipped.

borderRadius

If this value is a number, it is applied to all corners of the rectangle (topLeft, topRight, bottomLeft, bottomRight), except corners touching the borderSkipped. If this value is an object, the topLeft property defines the top-left corners border radius. Similarly, the topRight, bottomLeft, and bottomRight properties can also be specified. Omitted corners and those touching the borderSkipped are skipped. For example if the top border is skipped, the border radius for the corners topLeft and topRight will be skipped as well.

Stacked Charts

When the border radius is supplied as a number and the chart is stacked, the radius will only be applied to the bars that are at the edges of the stack or where the bar is floating. The object syntax can be used to override this behavior.

inflateAmount

This option can be used to inflate the rects that are used to draw the bars. This can be used to hide artifacts between bars when barPercentage(#barpercentage) * categoryPercentage(#categorypercentage) is 1. The default value 'auto' should work in most cases.

Interactions

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

NameDescription
hoverBackgroundColorThe bar background color when hovered.
hoverBorderColorThe bar border color when hovered.
hoverBorderWidthThe bar border width when hovered (in pixels).
hoverBorderRadiusThe bar border radius when hovered (in pixels).

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

barPercentage

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

Percent (0-1) of the available width each category should be within the sample width. more…

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.

maxBarThickness

Set this to ensure that bars are not sized thicker than this.

Scale Configuration

The bar chart sets unique default values for the following configuration from the associated scale options:

NameTypeDefaultDescription
offsetbooleantrueIf true, extra space is added to both edges and the axis is scaled to fit into the chart area.
grid.offsetbooleantrueIf 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 scale configuration

  1. options = {
  2. scales: {
  3. x: {
  4. grid: {
  5. offset: true
  6. }
  7. }
  8. }
  9. };

Offset Grid Lines

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.overrides.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.0||1.0|
  14. Category: | .5 |
  15. Sample: |==================|

Data Structure

All of the supported data structures can be used with bar charts.

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. const stackedBar = new Chart(ctx, {
  2. type: 'bar',
  3. data: data,
  4. options: {
  5. scales: {
  6. x: {
  7. stacked: true
  8. },
  9. y: {
  10. stacked: true
  11. }
  12. }
  13. }
  14. });

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. 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 vertical bars.

Bar Chart - 图2

config setup

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

Horizontal Bar Chart 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.

Internal data format

{x, y, _custom} where _custom is an optional object defining stacked bar properties: {start, end, barStart, barEnd, min, max}. start and end are the input values. Those two are repeated in barStart (closer to origin), barEnd (further from origin), min and max.