Chart.js
入门指南
入门
安装
集成
分步指南
概览
可访问性(Accessibility)
颜色(Colors)
数据结构(Data structures)
字体(Fonts)
选项(Options)
内边距(Padding)
性能(Performance)
图表配置
配置(Configuration)
动画(Animations)
画布背景(Canvas background)
数据抽取(Data Decimation)
设备像素比率(Device Pixel Ratio)
通用配置(Elements)
互动(Interactions)
布局(Layout)
图例(Legend)
本地化(Locale)
响应式图表(Responsive Charts)
副标题(Subtitle)
标题(Title)
提示(Tooltip)
Charts
面积图(Area Chart)
柱状/条形图(Bar Chart)
气泡图(Bubble Chart)
环形&饼图(Doughnut and Pie Charts)
折线图(Line Chart)
混合图表(Mixed Chart Types)
极地图(Polar Area Chart)
雷达图(Radar Chart)
离散图(Scatter Chart)
坐标轴
轴(Axes)
笛卡尔坐标(Cartesian)
笛卡尔坐标轴(Cartesian Axes)
分类轴(Category Axis)
线性轴(Linear Axis)
对数轴(Logarithmic Axis)
时间笛卡尔轴(Time Cartesian Axis)
时间序列轴(Time Series Axis)
径向(Radial)
径向轴(Radial Axes)
线性径向轴(Linear Radial Axis)
标签轴(Labelling Axes)
样式(Styling)
开发者
开发者(Developers)
Chart.js API
坐标轴扩展
图表扩展
贡献
插件
发布扩展
更新 Charts
迁移
4.x迁移指南
3.x迁移指南
示例
Chart.js Samples
Bar Charts
Bar Chart Border Radius
Floating Bars
Horizontal Bar Chart
Stacked Bar Chart
Stacked Bar Chart with Groups
Vertical Bar Chart
Line Charts
Interpolation Modes
Line Chart
Multi Axis Line Chart
Point Styling
Line Segment Styling
Stepped Line Charts
Line Styling
Other charts
Bubble
Combo bar/line
Doughnut
Multi Series Pie
Pie
Polar area
Polar area centered point labels
Radar
Radar skip points
Scatter
Scatter - Multi axis
Stacked bar/line
Area charts
Line Chart Boundaries
Line Chart Datasets
Line Chart drawTime
Line Chart Stacked
Radar Chart Stacked
Scales
Linear Scale - Min-Max
Linear Scale - Suggested Min-Max
Linear Scale - Step Size
Log Scale
Stacked Linear / Category
Time Scale
Time Scale - Max Span
Time Scale - Combo Chart
Scale Options
Center Positioning
Grid Configuration
Tick Configuration
Title Configuration
Legend
Events
HTML Legend
Point Style
Position
Alignment and Title Position
Title
Alignment
Subtitle
Basic
Tooltip
Custom Tooltip Content
External HTML Tooltip
Interaction Modes
Point Style
Position
Scriptable Options
Bar Chart
Bubble Chart
Line Chart
Pie Chart
Polar Area Chart
Radar Chart
Animations
Delay
Drop
Loop
Progressive Line
Progressive Line With Easing
Advanced
Data Decimation
Derived Axis Type
Derived Chart Type
Linear Gradient
Programmatic Event Triggers
Animation Progress Bar
Radial Gradient
Plugins
Chart Area Border
Doughnut Empty State
Quadrants
Utils
柱状/条形图(Bar Chart) - Chart.js中文文档 - 笔下光年
网站首页
柱状/条形图(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. <iframe src="http://example.itshubao.com/inexample/324.html" width="100%" height="860px" frameborder="0" scrolling="no"></iframe> ## 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. | Name | Type | Scriptable | Indexable | Default | |----------------------|------------------------------------|------------|-----------|----------------------| | backgroundColor | Color | Yes | Yes | 'rgba(0, 0, 0, 0.1)' | | base | number | Yes | Yes | | | barPercentage | number | - | - | 0.9 | | barThickness | number | string | - | - | | | borderColor | Color | Yes | Yes | 'rgba(0, 0, 0, 0.1)' | | borderSkipped | string | boolean | Yes | Yes | 'start' | | borderWidth | number | object | Yes | Yes | 0 | | borderRadius | number | object | Yes | Yes | 0 | | categoryPercentage | number | - | - | 0.8 | | clip | number | object | false | - | - | | | data | object | object[] | number[] | string[] | - | - | required | | grouped | boolean | - | - | true | | hoverBackgroundColor | Color | Yes | Yes | | | hoverBorderColor | Color | Yes | Yes | | | hoverBorderWidth | number | Yes | Yes | 1 | | hoverBorderRadius | number | Yes | Yes | 0 | | indexAxis | string | - | - | 'x' | | inflateAmount | number | 'auto' | Yes | Yes | 'auto' | | maxBarThickness | number | - | - | | | minBarLength | number | - | - | | | label | string | - | - | '' | | order | number | - | - | 0 | | pointStyle | pointStyle | Yes | - | 'circle' | | skipNull | boolean | - | - | | | stack | string | - | - | 'bar' | | xAxisID | string | - | - | first x axis | | yAxisID | string | - | - | first y axis | All these values, if undefined, fallback to the scopes described in option resolution ### Example dataset configuration ```javascript data: { datasets: [{ barPercentage: 0.5, barThickness: 6, maxBarThickness: 8, minBarLength: 2, data: [10, 20, 30, 40, 50, 60, 70] }] }; ``` ### General | Name | Description | |-----------|-------------------------| | base | Base value for the bar in data units along the value axis. If not set, defaults to the value axis base value. | | clip | How 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} | | grouped | Should 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. | | indexAxis | The base axis of the dataset. 'x' for vertical bars and 'y' for horizontal bars. | | label | The label for the dataset which appears in the legend and tooltips. | | order | The drawing order of dataset. Also affects order for stacking, tooltip and legend. more | | skipNull | If true, null or undefined values will not be used for spacing calculations when determining bar size. | | stack | The ID of the group to which this dataset belongs to (when stacked, each group will be a separate stack). more | | 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). | | borderRadius | The bar border radius (in pixels). | | minBarLength | Set this to ensure that bars have a minimum length in pixels. | | pointStyle | Style 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: | Name | Description | |----------------------|----------------| | hoverBackgroundColor | The bar background color when hovered. | | hoverBorderColor | The bar border color when hovered. | | hoverBorderWidth | The bar border width when hovered (in pixels). | | hoverBorderRadius | The 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: | Name | Type | Default | Description | |-------------|---------|---------|---------------| | offset | boolean | true | If true, extra space is added to both edges and the axis is scaled to fit into the chart area. | | grid.offset | 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 scale configuration ```javascript options = { scales: { x: { grid: { offset: true } } } }; ``` ### 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. ```javascript // 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.0||1.0| Category: | .5 | 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. ```javascript const stackedBar = new Chart(ctx, { type: 'bar', data: data, options: { scales: { x: { stacked: true }, y: { stacked: true } } } }); ``` ## 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. <iframe src="http://example.itshubao.com/inexample/325.html" width="100%" height="860px" frameborder="0" scrolling="no"></iframe> ### 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.
上一篇:
面积图(Area Chart)
下一篇:
气泡图(Bubble Chart)