Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

07-Pie-Doughnut-Chart.md 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ---
  2. title: Pie & Doughnut Charts
  3. anchor: doughnut-pie-chart
  4. ---
  5. ### Introduction
  6. Pie and doughnut charts are probably the most commonly used chart there are. They are divided into segments, the arc of each segment shows the proportional value of each piece of data.
  7. They are excellent at showing the relational proportions between data.
  8. Pie and doughnut charts are effectively the same class in Chart.js, but have one different default value - their `cutoutPercentage`. This equates what percentage of the inner should be cut out. This defaults to `0` for pie charts, and `50` for doughnuts.
  9. They are also registered under two aliases in the `Chart` core. Other than their different default value, and different alias, they are exactly the same.
  10. <div class="canvas-holder half">
  11. <canvas width="250" height="125"></canvas>
  12. </div>
  13. <div class="canvas-holder half">
  14. <canvas width="250" height="125"></canvas>
  15. </div>
  16. <br>
  17. ### Example Usage
  18. ```javascript
  19. // For a pie chart
  20. var myPieChart = new Chart(ctx,{
  21. type: 'pie',
  22. data: data,
  23. options: options
  24. });
  25. ```
  26. ```javascript
  27. // And for a doughnut chart
  28. var myDoughnutChart = new Chart(ctx, {
  29. type: 'doughnut',
  30. data: data,
  31. options: options
  32. });
  33. ```
  34. ### Data Structure
  35. Property | Type | Usage
  36. --- | --- | ---
  37. data | `Array<Number>` | The data to plot as bars
  38. label | `String` | The label for the dataset which appears in the legend and tooltips
  39. backgroundColor | `Array<Color>` | The fill color of the arcs. See [Colors](#getting-started-colors)
  40. borderColor | `Array<Color>` | Arc border color
  41. borderWidth | `Array<Number>` | Border width of arcs in pixels
  42. hoverBackgroundColor | `Array<Color>` | Arc background color when hovered
  43. hoverBorderColor | `Array<Color>` | Arc border color when hovered
  44. hoverBorderWidth | `Array<Number>` | Border width of arc when hovered
  45. An example data object using these attributes is shown below.
  46. ```javascript
  47. var data = {
  48. labels: [
  49. "Red",
  50. "Blue",
  51. "Yellow"
  52. ],
  53. datasets: [
  54. {
  55. data: [300, 50, 100],
  56. backgroundColor: [
  57. "#FF6384",
  58. "#36A2EB",
  59. "#FFCE56"
  60. ],
  61. hoverBackgroundColor: [
  62. "#FF6384",
  63. "#36A2EB",
  64. "#FFCE56"
  65. ]
  66. }]
  67. };
  68. ```
  69. For a pie chart, datasets need to contain an array of data points. The data points should be a number, Chart.js will total all of the numbers and calculate the relative proportion of each. You can also add an array of background colors. The color attributes should be a string. Similar to CSS, for this string you can use HEX notation, RGB, RGBA or HSL.
  70. ### Chart Options
  71. These are the customisation options specific to Pie & Doughnut charts. These options are merged with the [global chart configuration options](#global-chart-configuration), and form the options of the chart.
  72. Name | Type | Default | Description
  73. --- | --- | --- | ---
  74. cutoutPercentage | Number | 50 - for doughnut, 0 - for pie | The percentage of the chart that is cut out of the middle.
  75. rotation | Number | -0.5 * Math.PI | Starting angle to draw arcs from
  76. circumference | Number | 2 * Math.PI | Sweep to allow arcs to cover
  77. *animation*.animateRotate | Boolean |true | If true, will animate the rotation of the chart.
  78. *animation*.animateScale | Boolean | false | If true, will animate scaling the Doughnut from the centre.
  79. *legend*.*labels*.generateLabels | Function | `function(chart) {} ` | Returns a label for each item to be displayed on the legend.
  80. *legend*.onClick | Function | function(event, legendItem) {} ` | Handles clicking an individual legend item
  81. You can override these for your `Chart` instance by passing a second argument into the `Doughnut` method as an object with the keys you want to override.
  82. For example, we could have a doughnut chart that animates by scaling out from the centre like so:
  83. ```javascript
  84. new Chart(ctx,{
  85. type:"doughnut",
  86. animation:{
  87. animateScale:true
  88. }
  89. });
  90. // This will create a chart with all of the default options, merged from the global config,
  91. // and the Doughnut chart defaults but this particular instance will have `animateScale` set to `true`.
  92. ```
  93. We can also change these default values for each Doughnut type that is created, this object is available at `Chart.defaults.doughnut`. Pie charts also have a clone of these defaults available to change at `Chart.defaults.pie`, with the only difference being `cutoutPercentage` being set to 0.