1. 程式人生 > >echarts環形餅圖

echarts環形餅圖

圖表效果如下:

具體程式碼如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>關係圖案例</title>
  6. <!-- 引入 ECharts 檔案 -->
  7. <script src="js/echarts4.0.js" type="text/javascript" charset="utf-8"></script>
  8. </head>
  9. <body>
  10. <!-- 為 ECharts 準備一個具備大小(寬高)的 容器 -->
  11. <div id="chart1" style="width: 80%;height: 400px;top: 50px;left: 10%;border: 3px solid #000;"></div>
  12. </body>
  13. </html>
  14. <script type="text/javascript">
  15. // 基於準備好的容器(這裡的容器是id為chart1的div),初始化echarts例項
  16. var chart1 = echarts.init(document.getElementById("chart1"));
  17. // 指定圖表的配置項和資料
  18. var option = {
  19. title: {
  20. text: '男女所佔比例',
  21. left : '3%', // 標題距離左側邊距
  22. top : '3%', // 標題距頂部邊距
  23. textStyle: {
  24. color: '#000'
  25. }
  26. },
  27. series: [{
  28. type: "pie", // 系列1型別: 餅圖
  29. center: ["25%","50%"], // 餅圖的中心(圓心)座標,陣列的第一項是橫座標,第二項是縱座標。[ default: ['50%', '50%'] ]
  30. radius: ["49%","50%"], // 餅圖的半徑,陣列的第一項是內半徑,第二項是外半徑。[ default: [0, '75%'] ]
  31. // 可以將內半徑設大顯示成圓環圖(Donut chart)。
  32. clockWise: false, // 餅圖的扇區是否是順時針排布。[ default: true ]
  33. startAngle: 90, // 起始角度,支援範圍[0, 360]。 [ default: 90 ]
  34. hoverAnimation: true, // 是否開啟 hover 在扇區上的放大動畫效果。[ default: true ]
  35. itemStyle: { // 圖形樣式
  36. normal: {
  37. color: "#5886f0", // 圖形的顏色
  38. borderColor: "#5886f0", // 圖形的描邊顏色
  39. borderWidth: 20, // 描邊線寬。為 0 時無描邊。[ default: 0 ]
  40. borderType: 'solid', // 柱條的描邊型別,預設為實線,支援 'solid', 'dashed', 'dotted'。
  41. label: { // 圖形內部標籤
  42. show: true, // 是否顯示標籤
  43. textStyle: { // 標籤文字樣式
  44. fontSize: 15,
  45. fontWeight: "bold" // 標籤字型加粗,'normal','bold','bolder','lighter',100 | 200 | 300 | 400...
  46. },
  47. position: "center" // 標籤的位置,'outside'(餅圖扇區外側,通過視覺引導線連到相應的扇區)
  48. // 'inside'(餅圖扇區內部); 'inner' 同 'inside'。
  49. // 'center'(在餅圖中心位置。)
  50. },
  51. labelLine: { // 標籤的視覺引導線樣式,在 label 位置 設定為'outside'的時候會顯示視覺引導線。
  52. show: false
  53. }
  54. },
  55. emphasis: { // 高亮的扇區和標籤樣式(起強調作用)
  56. color: "#5886f0",
  57. borderColor: "#5886f0",
  58. borderWidth: 20,
  59. borderType: 'solid',
  60. label: {
  61. textStyle: {
  62. fontSize: 15,
  63. fontWeight: "bold"
  64. }
  65. }
  66. }
  67. },
  68. data: [{value: 52.7,name: "男(480人) 比率52.7%"},
  69. {name: " ",value: 47.3,
  70. itemStyle: {
  71. normal: {
  72. color: "#5886f0",
  73. borderColor: "#5886f0",
  74. borderWidth: 0,
  75. label: {
  76. show: false
  77. },
  78. labelLine: {
  79. show: false
  80. }
  81. },
  82. emphasis: {
  83. color: "#5886f0",
  84. borderColor: "#5886f0",
  85. borderWidth: 0
  86. }
  87. }
  88. }
  89. ]
  90. }, {
  91. type: "pie", // 系列2型別: 餅圖
  92. center: [
  93. "75.0%",
  94. "50%"
  95. ],
  96. radius: [
  97. "49%",
  98. "50%"
  99. ],
  100. clockWise: false,
  101. hoverAnimation: true,
  102. itemStyle: {
  103. normal: {
  104. label: {
  105. show: true,
  106. textStyle: {
  107. fontSize: 15,
  108. fontWeight: "bold"
  109. },
  110. position: "center"
  111. },
  112. labelLine: {
  113. show: false
  114. },
  115. color: "#ee3a3a",
  116. borderColor: "#ee3a3a",
  117. borderWidth: 20
  118. },
  119. emphasis: {
  120. label: {
  121. textStyle: {
  122. fontSize: 15,
  123. fontWeight: "bold"
  124. }
  125. },
  126. color: "#ee3a3a",
  127. borderColor: "#ee3a3a",
  128. borderWidth: 20
  129. }
  130. },
  131. data: [{
  132. value: 47.3,
  133. name: "女(421人) 佔率47.3%"
  134. },
  135. {
  136. name: " ",
  137. value: 52.7,
  138. itemStyle: {
  139. normal: {
  140. label: {
  141. show: false
  142. },
  143. labelLine: {
  144. show: false
  145. },
  146. color: "#ee3a3a",
  147. borderColor: "#ee3a3a",
  148. borderWidth: 0
  149. },
  150. emphasis: {
  151. color: "#ee3a3a",
  152. borderColor: "#ee3a3a",
  153. borderWidth: 0
  154. }
  155. }
  156. }
  157. ]
  158. }
  159. ]
  160. };
  161. // 使用剛指定的配置項和資料顯示圖表
  162. chart1.setOption(option);
  163. </script>

想要使用該圖表,只需要  複製以上程式碼  ,再下載    echarts.js  在頁面檔案中引入即可. 

注:本文的一個關鍵點就是環形圖內部標籤位置的設定,相關的配置項是 position,其取值分別為:

    'outside'(餅圖扇區外側,通過視覺引導線連到相應的扇區);

    'inside'(餅圖扇區內部);      

    'inner' 同 'inside'; 

    'center'(在餅圖中心位置);