Bar

    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.

    NameDescription
    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.
    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:

    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.

    Note: for negative bars in vertical chart, top and are flipped. Same goes for left and right in horizontal chart.

    Options are:

    • 'left'
    • 'top'
    • 'right'
    • false

    borderWidth

    If this value is a number, it is applied to all sides of the rectangle (left, top, right, bottom), except . 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.

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

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

    Dataset Configuration

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

    Example Usage

    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. };

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

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

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

    Example Usage

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

    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 data property of a dataset for a bar chart is specified as 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 when using the .

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

    You can also specify the dataset for a bar chart as arrays of two numbers. This will force rendering of bars with gaps between them (floating-bars). First and second numbers in array will correspond the start and the end point of a bar respectively.

    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. });

    The following dataset properties are specific to stacked bar charts.

    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.

    Bar - 图2

    1. var myBarChart = new Chart(ctx, {
    2. type: 'horizontalBar',
    3. data: data,

    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.