You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

02-Bar-Chart.md 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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).Bar(data, options);
  14. ```
  15. ### Data structure
  16. ```javascript
  17. var data = {
  18. labels: ["January", "February", "March", "April", "May", "June", "July"],
  19. datasets: [
  20. {
  21. label: "My First dataset",
  22. fillColor: "rgba(220,220,220,0.5)",
  23. strokeColor: "rgba(220,220,220,0.8)",
  24. highlightFill: "rgba(220,220,220,0.75)",
  25. highlightStroke: "rgba(220,220,220,1)",
  26. data: [65, 59, 80, 81, 56, 55, 40]
  27. },
  28. {
  29. label: "My Second dataset",
  30. fillColor: "rgba(151,187,205,0.5)",
  31. strokeColor: "rgba(151,187,205,0.8)",
  32. highlightFill: "rgba(151,187,205,0.75)",
  33. highlightStroke: "rgba(151,187,205,1)",
  34. data: [28, 48, 40, 19, 86, 27, 90]
  35. }
  36. ]
  37. };
  38. ```
  39. 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. Again, colours are in CSS format.
  40. We have an array of labels too for display. In the example, we are showing the same data as the previous line chart example.
  41. The label key on each dataset is optional, and can be used when generating a scale for the chart.
  42. ### Chart Options
  43. These are the customisation options specific to Bar charts. These options are merged with the [global chart configuration options](#getting-started-global-chart-configuration), and form the options of the chart.
  44. ```javascript
  45. {
  46. //Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
  47. scaleBeginAtZero : true,
  48. //Boolean - Whether grid lines are shown across the chart
  49. scaleShowGridLines : true,
  50. //String - Colour of the grid lines
  51. scaleGridLineColor : "rgba(0,0,0,.05)",
  52. //Number - Width of the grid lines
  53. scaleGridLineWidth : 1,
  54. //Boolean - Whether to show horizontal lines (except X axis)
  55. scaleShowHorizontalLines: true,
  56. //Boolean - Whether to show vertical lines (except Y axis)
  57. scaleShowVerticalLines: true,
  58. //Boolean - If there is a stroke on each bar
  59. barShowStroke : true,
  60. //Number - Pixel width of the bar stroke
  61. barStrokeWidth : 2,
  62. //Number - Spacing between each of the X value sets
  63. barValueSpacing : 5,
  64. //Number - Spacing between data sets within X values
  65. barDatasetSpacing : 1,
  66. {% raw %}
  67. //String - A legend template
  68. legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].fillColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
  69. {% endraw %}
  70. }
  71. ```
  72. 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.
  73. For example, we could have a bar chart without a stroke on each bar by doing the following:
  74. ```javascript
  75. new Chart(ctx).Bar(data, {
  76. barShowStroke: false
  77. });
  78. // This will create a chart with all of the default options, merged from the global config,
  79. // and the Bar chart defaults but this particular instance will have `barShowStroke` set to false.
  80. ```
  81. We can also change these defaults values for each Bar type that is created, this object is available at `Chart.defaults.Bar`.
  82. ### Prototype methods
  83. #### .getBarsAtEvent( event )
  84. Calling `getBarsAtEvent(event)` on your Chart instance passing an argument of an event, or jQuery event, will return the bar elements that are at that the same position of that event.
  85. ```javascript
  86. canvas.onclick = function(evt){
  87. var activeBars = myBarChart.getBarsAtEvent(evt);
  88. // => activeBars is an array of bars on the canvas that are at the same position as the click event.
  89. };
  90. ```
  91. This functionality may be useful for implementing DOM based tooltips, or triggering custom behaviour in your application.
  92. #### .update( )
  93. Calling `update()` on your Chart instance will re-render the chart with any updated values, allowing you to edit the value of multiple existing points, then render those in one animated render loop.
  94. ```javascript
  95. myBarChart.datasets[0].bars[2].value = 50;
  96. // Would update the first dataset's value of 'March' to be 50
  97. myBarChart.update();
  98. // Calling update now animates the position of March from 90 to 50.
  99. ```
  100. #### .addData( valuesArray, label )
  101. Calling `addData(valuesArray, label)` on your Chart instance passing an array of values for each dataset, along with a label for those bars.
  102. ```javascript
  103. // The values array passed into addData should be one for each dataset in the chart
  104. myBarChart.addData([40, 60], "August");
  105. // The new data will now animate at the end of the chart.
  106. ```
  107. #### .removeData( )
  108. Calling `removeData()` on your Chart instance will remove the first value for all datasets on the chart.
  109. ```javascript
  110. myBarChart.removeData();
  111. // The chart will now animate and remove the first bar
  112. ```