選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

04-Bar-Chart.md 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. ---
  2. title: Bar Chart
  3. anchor: bar-chart
  4. ---
  5. ### Introduction
  6. A bar chart is a way of showing data as bars.
  7. It is sometimes used to show trend data, and the comparison of multiple data sets side by side.
  8. <div class="canvas-holder">
  9. <canvas width="250" height="125"></canvas>
  10. </div>
  11. ### Example Usage
  12. ```javascript
  13. var myBarChart = new Chart(ctx, {
  14. type: 'bar',
  15. data: data,
  16. options: options
  17. });
  18. ```
  19. Or if you want horizontal bars.
  20. ```javascript
  21. var myBarChart = new Chart(ctx, {
  22. type: 'horizontalBar',
  23. data: data,
  24. options: options
  25. });
  26. ```
  27. ### Data Structure
  28. The following options can be included in a bar chart dataset to configure options for that specific dataset.
  29. 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.
  30. Property | Type | Usage
  31. --- | --- | ---
  32. data | `Array<Number>` | The data to plot as bars
  33. label | `String` | The label for the dataset which appears in the legend and tooltips
  34. xAxisID | `String` | The ID of the x axis to plot this dataset on
  35. yAxisID | `String` | The ID of the y axis to plot this dataset on
  36. backgroundColor | `Color or Array<Color>` | The fill color of the bars. See [Colors](#getting-started-colors)
  37. borderColor | `Color or Array<Color>` | Bar border color
  38. borderWidth | `Number or Array<Number>` | Border width of bar in pixels
  39. borderSkipped | `String or Array<String>` | Which edge to skip drawing the border for. Options are 'bottom', 'left', 'top', and 'right'
  40. hoverBackgroundColor | `Color or Array<Color>` | Bar background color when hovered
  41. hoverBorderColor | `Color or Array<Color>` | Bar border color when hovered
  42. hoverBorderWidth | `Number or Array<Number>` | Border width of bar when hovered
  43. An example data object using these attributes is shown below.
  44. ```javascript
  45. var data = {
  46. labels: ["January", "February", "March", "April", "May", "June", "July"],
  47. datasets: [
  48. {
  49. label: "My First dataset",
  50. backgroundColor: [
  51. 'rgba(255, 99, 132, 0.2)',
  52. 'rgba(54, 162, 235, 0.2)',
  53. 'rgba(255, 206, 86, 0.2)',
  54. 'rgba(75, 192, 192, 0.2)',
  55. 'rgba(153, 102, 255, 0.2)',
  56. 'rgba(255, 159, 64, 0.2)'
  57. ],
  58. borderColor: [
  59. 'rgba(255,99,132,1)',
  60. 'rgba(54, 162, 235, 1)',
  61. 'rgba(255, 206, 86, 1)',
  62. 'rgba(75, 192, 192, 1)',
  63. 'rgba(153, 102, 255, 1)',
  64. 'rgba(255, 159, 64, 1)'
  65. ],
  66. borderWidth: 1
  67. data: [65, 59, 80, 81, 56, 55, 40],
  68. }
  69. ]
  70. };
  71. ```
  72. The bar chart has the a very similar data structure to the line chart, and has an array of datasets, each with colours and an array of data.
  73. We have an array of labels too for display. In the example, we are showing the same data as the previous line chart example.
  74. ### Chart Options
  75. These are the customisation options specific to Bar charts. These options are merged with the [global chart configuration options](#global-chart-configuration), and form the options of the chart.
  76. The default options for bar chart are defined in `Chart.defaults.bar`.
  77. Name | Type | Default | Description
  78. --- |:---:| --- | ---
  79. *hover*.mode | String | "label" | Label's hover mode. "label" is used since the x axis displays data by the index in the dataset.
  80. scales | Object | - | -
  81. *scales*.xAxes | Array | | The bar chart officially supports only 1 x-axis but uses an array to keep the API consistent. Use a scatter chart if you need multiple x axes.
  82. *Options for xAxes* | | |
  83. type | String | "Category" | As defined in [Scales](#scales-category-scale).
  84. display | Boolean | true | If true, show the scale.
  85. id | String | "x-axis-0" | Id of the axis so that data can bind to it
  86. stacked | Boolean | false | If true, bars are stacked on the x-axis
  87. categoryPercentage | Number | 0.8 | Percent (0-1) of the available width (the space between the gridlines for small datasets) for each data-point to use for the bars. [Read More](#bar-chart-barpercentage-vs-categorypercentage)
  88. barPercentage | Number | 0.9 | Percent (0-1) of the available width each bar should be within the category percentage. 1.0 will take the whole category width and put the bars right next to each other. [Read More](#bar-chart-barpercentage-vs-categorypercentage)
  89. gridLines | Object | [See Scales](#scales) |
  90. *gridLines*.offsetGridLines | Boolean | true | If true, the bars for a particular data point fall between the grid lines. If false, the grid line will go right down the middle of the bars.
  91. | | |
  92. *scales*.yAxes | Array | `[{ type: "linear" }]` |
  93. *Options for xAxes* | | |
  94. type | String | "linear" | As defined in [Scales](#scales-linear-scale).
  95. display | Boolean | true | If true, show the scale.
  96. id | String | "y-axis-0" | Id of the axis so that data can bind to it.
  97. stacked | Boolean | false | If true, bars are stacked on the y-axis
  98. You can override these for your `Chart` instance by passing a second argument into the `Bar` method as an object with the keys you want to override.
  99. For example, we could have a bar chart without a stroke on each bar by doing the following:
  100. ```javascript
  101. new Chart(ctx, {
  102. type: "bar",
  103. data: data,
  104. options: {
  105. scales: {
  106. xAxes: [{
  107. stacked: true
  108. }],
  109. yAxes: [{
  110. stacked: true
  111. }]
  112. }
  113. }
  114. }
  115. });
  116. // This will create a chart with all of the default options, merged from the global config,
  117. // and the Bar chart defaults but this particular instance will have `stacked` set to true
  118. // for both x and y axes.
  119. ```
  120. We can also change these defaults values for each Bar type that is created, this object is available at `Chart.defaults.bar`. For horizontal bars, this object is available at `Chart.defaults.horizontalBar`.
  121. The default options for horizontal bar charts are defined in `Chart.defaults.horizontalBar` and are same as those of the bar chart, but with `xAxes` and `yAxes` swapped and the following additional options.
  122. Name | Type | Default | Description
  123. --- |:---:| --- | ---
  124. *Options for xAxes* | | |
  125. position | String | "bottom" |
  126. *Options for yAxes* | | |
  127. position | String | "left" |
  128. ### barPercentage vs categoryPercentage
  129. The following shows the relationship between the bar percentage option and the category percentage option.
  130. ```text
  131. // categoryPercentage: 1.0
  132. // barPercentage: 1.0
  133. Bar: | 1.0 | 1.0 |
  134. Category: | 1.0 |
  135. Sample: |===========|
  136. // categoryPercentage: 1.0
  137. // barPercentage: 0.5
  138. Bar: |.5| |.5|
  139. Category: | 1.0 |
  140. Sample: |==============|
  141. // categoryPercentage: 0.5
  142. // barPercentage: 1.0
  143. Bar: |1.||1.|
  144. Category: | .5 |
  145. Sample: |==============|
  146. ```