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

05-Pie-Doughnut-Chart.md 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 `percentageInnerCutout`. 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. ### Example usage
  17. ```javascript
  18. // For a pie chart
  19. var myPieChart = new Chart(ctx[0]).Pie(data,options);
  20. // And for a doughnut chart
  21. var myDoughnutChart = new Chart(ctx[1]).Doughnut(data,options);
  22. ```
  23. ### Data structure
  24. ```javascript
  25. var data = [
  26. {
  27. value: 300,
  28. color:"#F7464A",
  29. highlight: "#FF5A5E",
  30. label: "Red"
  31. },
  32. {
  33. value: 50,
  34. color: "#46BFBD",
  35. highlight: "#5AD3D1",
  36. label: "Green"
  37. },
  38. {
  39. value: 100,
  40. color: "#FDB45C",
  41. highlight: "#FFC870",
  42. label: "Yellow"
  43. }
  44. ]
  45. ```
  46. For a pie chart, you must pass in an array of objects with a value and a color property. The value attribute should be a number, Chart.js will total all of the numbers and calculate the relative proportion of each. The color attribute should be a string. Similar to CSS, for this string you can use HEX notation, RGB, RGBA or HSL.
  47. ### Chart options
  48. These are the customisation options specific to Pie & Doughnut charts. These options are merged with the [global chart configuration options](#getting-started-global-chart-configuration), and form the options of the chart.
  49. ```javascript
  50. {
  51. //Boolean - Whether we should show a stroke on each segment
  52. segmentShowStroke : true,
  53. //String - The colour of each segment stroke
  54. segmentStrokeColor : "#fff",
  55. //Number - The width of each segment stroke
  56. segmentStrokeWidth : 2,
  57. //Number - The percentage of the chart that we cut out of the middle
  58. percentageInnerCutout : 50, // This is 0 for Pie charts
  59. //Number - Amount of animation steps
  60. animationSteps : 100,
  61. //String - Animation easing effect
  62. animationEasing : "easeOutBounce",
  63. //Boolean - Whether we animate the rotation of the Doughnut
  64. animateRotate : true,
  65. //Boolean - Whether we animate scaling the Doughnut from the centre
  66. animateScale : false,
  67. {% raw %}
  68. //String - A legend template
  69. legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>"
  70. {% endraw %}
  71. }
  72. ```
  73. 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.
  74. For example, we could have a doughnut chart that animates by scaling out from the centre like so:
  75. ```javascript
  76. new Chart(ctx).Doughnut(data, {
  77. animateScale: true
  78. });
  79. // This will create a chart with all of the default options, merged from the global config,
  80. // and the Doughnut chart defaults but this particular instance will have `animateScale` set to `true`.
  81. ```
  82. 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 `percentageInnerCutout` being set to 0.
  83. ### Prototype methods
  84. #### .getSegmentsAtEvent( event )
  85. Calling `getSegmentsAtEvent(event)` on your Chart instance passing an argument of an event, or jQuery event, will return the segment elements that are at the same position of that event.
  86. ```javascript
  87. canvas.onclick = function(evt){
  88. var activePoints = myDoughnutChart.getSegmentsAtEvent(evt);
  89. // => activePoints is an array of segments on the canvas that are at the same position as the click event.
  90. };
  91. ```
  92. This functionality may be useful for implementing DOM based tooltips, or triggering custom behaviour in your application.
  93. #### .update( )
  94. 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.
  95. ```javascript
  96. myDoughnutChart.segments[1].value = 10;
  97. // Would update the first dataset's value of 'Green' to be 10
  98. myDoughnutChart.update();
  99. // Calling update now animates the circumference of the segment 'Green' from 50 to 10.
  100. // and transitions other segment widths
  101. ```
  102. #### .addData( segmentData, index )
  103. Calling `addData(segmentData, index)` on your Chart instance passing an object in the same format as in the constructor. There is an optional second argument of 'index', this determines at what index the new segment should be inserted into the chart.
  104. ```javascript
  105. // An object in the same format as the original data source
  106. myDoughnutChart.addData({
  107. value: 130,
  108. color: "#B48EAD",
  109. highlight: "#C69CBE",
  110. label: "Purple"
  111. });
  112. // The new segment will now animate in.
  113. ```
  114. #### .removeData( index )
  115. Calling `removeData(index)` on your Chart instance will remove segment at that particular index. If none is provided, it will default to the last segment.
  116. ```javascript
  117. myDoughnutChart.removeData();
  118. // Other segments will update to fill the empty space left.
  119. ```