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.

04-Polar-Area-Chart.md 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. ---
  2. title: Polar Area Chart
  3. anchor: polar-area-chart
  4. ---
  5. ### Introduction
  6. Polar area charts are similar to pie charts, but each segment has the same angle - the radius of the segment differs depending on the value.
  7. This type of chart is often useful when we want to show a comparison data similar to a pie chart, but also show a scale of values for context.
  8. <div class="canvas-holder">
  9. <canvas width="250" height="125"></canvas>
  10. </div>
  11. ### Example usage
  12. ```javascript
  13. new Chart(ctx).PolarArea(data, options);
  14. ```
  15. ### Data structure
  16. ```javascript
  17. var data = [
  18. {
  19. value: 300,
  20. color:"#F7464A",
  21. highlight: "#FF5A5E",
  22. label: "Red"
  23. },
  24. {
  25. value: 50,
  26. color: "#46BFBD",
  27. highlight: "#5AD3D1",
  28. label: "Green"
  29. },
  30. {
  31. value: 100,
  32. color: "#FDB45C",
  33. highlight: "#FFC870",
  34. label: "Yellow"
  35. },
  36. {
  37. value: 40,
  38. color: "#949FB1",
  39. highlight: "#A8B3C5",
  40. label: "Grey"
  41. },
  42. {
  43. value: 120,
  44. color: "#4D5360",
  45. highlight: "#616774",
  46. label: "Dark Grey"
  47. }
  48. ];
  49. ```
  50. As you can see, for the chart data you pass in an array of objects, with a value and a colour. The value attribute should be a number, while the color attribute should be a string. Similar to CSS, for this string you can use HEX notation, RGB, RGBA or HSL.
  51. ### Chart options
  52. These are the customisation options specific to Polar Area charts. These options are merged with the [global chart configuration options](#getting-started-global-chart-configuration), and form the options of the chart.
  53. ```javascript
  54. {
  55. //Boolean - Show a backdrop to the scale label
  56. scaleShowLabelBackdrop : true,
  57. //String - The colour of the label backdrop
  58. scaleBackdropColor : "rgba(255,255,255,0.75)",
  59. // Boolean - Whether the scale should begin at zero
  60. scaleBeginAtZero : true,
  61. //Number - The backdrop padding above & below the label in pixels
  62. scaleBackdropPaddingY : 2,
  63. //Number - The backdrop padding to the side of the label in pixels
  64. scaleBackdropPaddingX : 2,
  65. //Boolean - Show line for each value in the scale
  66. scaleShowLine : true,
  67. //Boolean - Stroke a line around each segment in the chart
  68. segmentShowStroke : true,
  69. //String - The colour of the stroke on each segement.
  70. segmentStrokeColor : "#fff",
  71. //Number - The width of the stroke value in pixels
  72. segmentStrokeWidth : 2,
  73. //Number - Amount of animation steps
  74. animationSteps : 100,
  75. //String - Animation easing effect.
  76. animationEasing : "easeOutBounce",
  77. //Boolean - Whether to animate the rotation of the chart
  78. animateRotate : true,
  79. //Boolean - Whether to animate scaling the chart from the centre
  80. animateScale : false,
  81. {% raw %}
  82. //String - A legend template
  83. 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>"
  84. {% endraw %}
  85. }
  86. ```
  87. You can override these for your `Chart` instance by passing a second argument into the `PolarArea` method as an object with the keys you want to override.
  88. For example, we could have a polar area chart with a black stroke on each segment like so:
  89. ```javascript
  90. new Chart(ctx).PolarArea(data, {
  91. segmentStrokeColor: "#000000"
  92. });
  93. // This will create a chart with all of the default options, merged from the global config,
  94. // and the PolarArea chart defaults but this particular instance will have `segmentStrokeColor` set to `"#000000"`.
  95. ```
  96. We can also change these defaults values for each PolarArea type that is created, this object is available at `Chart.defaults.PolarArea`.
  97. ### Prototype methods
  98. #### .getSegmentsAtEvent( event )
  99. Calling `getSegmentsAtEvent(event)` on your Chart instance passing an argument of an event, or jQuery event, will return the segment elements that are at that the same position of that event.
  100. ```javascript
  101. canvas.onclick = function(evt){
  102. var activePoints = myPolarAreaChart.getSegmentsAtEvent(evt);
  103. // => activePoints is an array of segments on the canvas that are at the same position as the click event.
  104. };
  105. ```
  106. This functionality may be useful for implementing DOM based tooltips, or triggering custom behaviour in your application.
  107. #### .update( )
  108. 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.
  109. ```javascript
  110. myPolarAreaChart.segments[1].value = 10;
  111. // Would update the first dataset's value of 'Green' to be 10
  112. myPolarAreaChart.update();
  113. // Calling update now animates the position of Green from 50 to 10.
  114. ```
  115. #### .addData( segmentData, index )
  116. Calling `addData(segmentData, index)` on your Chart instance passing an object in the same format as in the constructor. There is an option second argument of 'index', this determines at what index the new segment should be inserted into the chart.
  117. ```javascript
  118. // An object in the same format as the original data source
  119. myPolarAreaChart.addData({
  120. value: 130,
  121. color: "#B48EAD",
  122. highlight: "#C69CBE",
  123. label: "Purple"
  124. });
  125. // The new segment will now animate in.
  126. ```
  127. #### .removeData( index )
  128. 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.
  129. ```javascript
  130. myPolarAreaChart.removeData();
  131. // Other segments will update to fill the empty space left.
  132. ```