Stripped personal data from development repository
Samo Penic
2019-02-20 83c3f647c35477564b77cbc5b36d37d793d5442a
commit | author | age
83c3f6 1 # Bar
SP 2 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.
3
4 {% chartjs %}
5 {
6     "type": "bar",
7     "data": {
8         "labels": [
9             "January",
10             "February",
11             "March",
12             "April",
13             "May",
14             "June",
15             "July"
16         ],
17         "datasets": [{
18             "label": "My First Dataset",
19             "data": [65, 59, 80, 81, 56, 55, 40],
20             "fill": false,
21             "backgroundColor": [
22                 "rgba(255, 99, 132, 0.2)",
23                 "rgba(255, 159, 64, 0.2)",
24                 "rgba(255, 205, 86, 0.2)",
25                 "rgba(75, 192, 192, 0.2)",
26                 "rgba(54, 162, 235, 0.2)",
27                 "rgba(153, 102, 255, 0.2)",
28                 "rgba(201, 203, 207, 0.2)"
29             ],
30             "borderColor": [
31                 "rgb(255, 99, 132)",
32                 "rgb(255, 159, 64)",
33                 "rgb(255, 205, 86)",
34                 "rgb(75, 192, 192)",
35                 "rgb(54, 162, 235)",
36                 "rgb(153, 102, 255)",
37                 "rgb(201, 203, 207)"
38             ],
39             "borderWidth": 1
40         }]
41     },
42     "options": {
43         "scales": {
44             "yAxes": [{
45                 "ticks": {
46                     "beginAtZero": true
47                 }
48             }]
49         }
50     }
51 }
52 {% endchartjs %}
53
54 ## Example Usage
55 ```javascript
56 var myBarChart = new Chart(ctx, {
57     type: 'bar',
58     data: data,
59     options: options
60 });
61 ```
62
63 ## Dataset Properties
64 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.
65
66 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.
67
68 | Name | Type | Description
69 | ---- | ---- | -----------
70 | `label` | `String` | The label for the dataset which appears in the legend and tooltips.
71 | `xAxisID` | `String` | The ID of the x axis to plot this dataset on. If not specified, this defaults to the ID of the first found x axis
72 | `yAxisID` | `String` | The ID of the y axis to plot this dataset on. If not specified, this defaults to the ID of the first found y axis.
73 | `backgroundColor` | `Color/Color[]` | The fill color of the bar. See [Colors](../general/colors.md#colors)
74 | `borderColor` | `Color/Color[]` | The color of the bar border. See [Colors](../general/colors.md#colors)
75 | `borderWidth` | `Number/Number[]` | The stroke width of the bar in pixels.
76 | `borderSkipped` | `String` | Which edge to skip drawing the border for. [more...](#borderskipped)
77 | `hoverBackgroundColor` | `Color/Color[]` | The fill colour of the bars when hovered.
78 | `hoverBorderColor` | `Color/Color[]` | The stroke colour of the bars when hovered.
79 | `hoverBorderWidth` | `Number/Number[]` | The stroke width of the bars when hovered.
80
81 ### borderSkipped
82 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.
83
84 Options are:
85 * 'bottom'
86 * 'left'
87 * 'top'
88 * 'right'
89
90 ## Configuration Options
91
92 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.
93
94 | Name | Type | Default | Description
95 | ---- | ---- | ------- | -----------
96 | `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...](#barpercentage-vs-categorypercentage)
97 | `categoryPercentage` | `Number` | `0.8` | Percent (0-1) of the available width each category should be within the sample width. [more...](#barpercentage-vs-categorypercentage)
98 | `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...](#barthickness)
99 | `maxBarThickness` | `Number` | | Set this to ensure that bars are not sized thicker than this.
100 | `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...](#offsetgridlines)
101
102 ### barThickness
103 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.
104
105 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.
106
107 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.
108
109 ### offsetGridLines
110 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.
111
112 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.
113
114 ```javascript
115 options = {
116     scales: {
117         xAxes: [{
118             gridLines: {
119                 offsetGridLines: true
120             }
121         }]
122     }
123 }
124 ```
125
126 ## Default Options
127
128 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.
129
130 ## barPercentage vs categoryPercentage
131
132 The following shows the relationship between the bar percentage option and the category percentage option.
133
134 ```text
135 // categoryPercentage: 1.0
136 // barPercentage: 1.0
137 Bar:        | 1.0 | 1.0 |
138 Category:   |    1.0    |
139 Sample:     |===========|
140
141 // categoryPercentage: 1.0
142 // barPercentage: 0.5
143 Bar:          |.5|  |.5|
144 Category:  |      1.0     |
145 Sample:    |==============|
146
147 // categoryPercentage: 0.5
148 // barPercentage: 1.0
149 Bar:            |1.||1.|
150 Category:       |  .5  |
151 Sample:     |==============|
152 ```
153
154 ## Data Structure
155
156 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.
157
158 ```javascript
159 data: [20, 10]
160 ```
161
162 You can also specify the dataset as x/y coordinates when using the [time scale](../axes/cartesian/time.md#time-cartesian-axis).
163
164 ```javascript
165 data: [{x:'2016-12-25', y:20}, {x:'2016-12-26', y:10}]
166 ```
167
168 # Stacked Bar Chart
169
170 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.
171
172 ```javascript
173 var stackedBar = new Chart(ctx, {
174     type: 'bar',
175     data: data,
176     options: {
177         scales: {
178             xAxes: [{
179                 stacked: true
180             }],
181             yAxes: [{
182                 stacked: true
183             }]
184         }
185     }
186 });
187 ```
188
189 ## Dataset Properties
190
191 The following dataset properties are specific to stacked bar charts.
192
193 | Name | Type | Description
194 | ---- | ---- | -----------
195 | `stack` | `String` | The ID of the group to which this dataset belongs to (when stacked, each group will be a separate stack)
196
197 # Horizontal Bar Chart
198 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.
199 {% chartjs %}
200 {
201     "type": "horizontalBar",
202     "data": {
203         "labels": ["January", "February", "March", "April", "May", "June", "July"],
204         "datasets": [{
205             "label": "My First Dataset",
206             "data": [65, 59, 80, 81, 56, 55, 40],
207             "fill": false,
208             "backgroundColor": [
209                 "rgba(255, 99, 132, 0.2)",
210                 "rgba(255, 159, 64, 0.2)",
211                 "rgba(255, 205, 86, 0.2)",
212                 "rgba(75, 192, 192, 0.2)",
213                 "rgba(54, 162, 235, 0.2)",
214                 "rgba(153, 102, 255, 0.2)",
215                 "rgba(201, 203, 207, 0.2)"
216             ],
217             "borderColor": [
218                 "rgb(255, 99, 132)",
219                 "rgb(255, 159, 64)",
220                 "rgb(255, 205, 86)",
221                 "rgb(75, 192, 192)",
222                 "rgb(54, 162, 235)",
223                 "rgb(153, 102, 255)",
224                 "rgb(201, 203, 207)"
225             ],
226             "borderWidth": 1
227         }]
228     },
229     "options": {
230         "scales": {
231             "xAxes": [{
232                 "ticks": {
233                     "beginAtZero": true
234                 }
235             }]
236         }
237     }
238 }
239 {% endchartjs %}
240
241 ## Example
242 ```javascript
243 var myBarChart = new Chart(ctx, {
244     type: 'horizontalBar',
245     data: data,
246     options: options
247 });
248 ```
249
250 ## Config Options
251 The configuration options for the horizontal bar chart are the same as for the [bar chart](#configuration-options). However, any options specified on the x axis in a bar chart, are applied to the y axis in a horizontal bar chart.
252
253 The default horizontal bar configuration is specified in `Chart.defaults.horizontalBar`.