chart.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { eachElement, getTextByPathList } from './utils'
  2. import { applyTint } from './color'
  3. function extractChartColors (serNode, warpObj) {
  4. if (serNode.constructor !== Array) serNode = [serNode]
  5. const schemeClrs = []
  6. for (const node of serNode) {
  7. let schemeClr = getTextByPathList(node, ['c:spPr', 'a:solidFill', 'a:schemeClr'])
  8. if (!schemeClr) schemeClr = getTextByPathList(node, ['c:spPr', 'a:ln', 'a:solidFill', 'a:schemeClr'])
  9. if (!schemeClr) schemeClr = getTextByPathList(node, ['c:marker', 'c:spPr', 'a:ln', 'a:solidFill', 'a:schemeClr'])
  10. let clr = getTextByPathList(schemeClr, ['attrs', 'val'])
  11. if (clr) {
  12. clr = getTextByPathList(warpObj['themeContent'], ['a:theme', 'a:themeElements', 'a:clrScheme', `a:${clr}`, 'a:srgbClr', 'attrs', 'val'])
  13. const tint = getTextByPathList(schemeClr, ['a:tint', 'attrs', 'val']) / 100000
  14. if (clr && !isNaN(tint)) {
  15. clr = applyTint(clr, tint)
  16. }
  17. } else clr = getTextByPathList(node, ['c:spPr', 'a:solidFill', 'a:srgbClr', 'attrs', 'val'])
  18. if (clr) clr = '#' + clr
  19. schemeClrs.push(clr)
  20. }
  21. return schemeClrs
  22. }
  23. function extractChartData (serNode) {
  24. const dataMat = []
  25. if (!serNode) return dataMat
  26. if (serNode['c:xVal']) {
  27. let dataRow = []
  28. eachElement(serNode['c:xVal']['c:numRef']['c:numCache']['c:pt'], innerNode => {
  29. dataRow.push(parseFloat(innerNode['c:v']))
  30. return ''
  31. })
  32. dataMat.push(dataRow)
  33. dataRow = []
  34. eachElement(serNode['c:yVal']['c:numRef']['c:numCache']['c:pt'], innerNode => {
  35. dataRow.push(parseFloat(innerNode['c:v']))
  36. return ''
  37. })
  38. dataMat.push(dataRow)
  39. } else {
  40. eachElement(serNode, (innerNode, index) => {
  41. const dataRow = []
  42. const colName = getTextByPathList(innerNode, ['c:tx', 'c:strRef', 'c:strCache', 'c:pt', 'c:v']) || index
  43. const rowNames = {}
  44. if (getTextByPathList(innerNode, ['c:cat', 'c:strRef', 'c:strCache', 'c:pt'])) {
  45. eachElement(innerNode['c:cat']['c:strRef']['c:strCache']['c:pt'], innerNode => {
  46. rowNames[innerNode['attrs']['idx']] = innerNode['c:v']
  47. return ''
  48. })
  49. } else if (getTextByPathList(innerNode, ['c:cat', 'c:numRef', 'c:numCache', 'c:pt'])) {
  50. eachElement(innerNode['c:cat']['c:numRef']['c:numCache']['c:pt'], innerNode => {
  51. rowNames[innerNode['attrs']['idx']] = innerNode['c:v']
  52. return ''
  53. })
  54. }
  55. if (getTextByPathList(innerNode, ['c:val', 'c:numRef', 'c:numCache', 'c:pt'])) {
  56. eachElement(innerNode['c:val']['c:numRef']['c:numCache']['c:pt'], innerNode => {
  57. dataRow.push({
  58. x: innerNode['attrs']['idx'],
  59. y: parseFloat(innerNode['c:v'])
  60. })
  61. return ''
  62. })
  63. }
  64. dataMat.push({
  65. key: colName,
  66. values: dataRow,
  67. xlabels: rowNames
  68. })
  69. return ''
  70. })
  71. }
  72. return dataMat
  73. }
  74. export function getChartInfo (plotArea, warpObj) {
  75. let chart = null
  76. for (const key in plotArea) {
  77. switch (key) {
  78. case 'c:lineChart':
  79. chart = {
  80. type: 'lineChart',
  81. data: extractChartData(plotArea[key]['c:ser']),
  82. colors: extractChartColors(plotArea[key]['c:ser'], warpObj),
  83. grouping: getTextByPathList(plotArea[key], ['c:grouping', 'attrs', 'val']),
  84. marker: !!plotArea[key]['c:marker']
  85. }
  86. break
  87. case 'c:line3DChart':
  88. chart = {
  89. type: 'line3DChart',
  90. data: extractChartData(plotArea[key]['c:ser']),
  91. colors: extractChartColors(plotArea[key]['c:ser'], warpObj),
  92. grouping: getTextByPathList(plotArea[key], ['c:grouping', 'attrs', 'val'])
  93. }
  94. break
  95. case 'c:barChart':
  96. chart = {
  97. type: 'barChart',
  98. data: extractChartData(plotArea[key]['c:ser']),
  99. colors: extractChartColors(plotArea[key]['c:ser'], warpObj),
  100. grouping: getTextByPathList(plotArea[key], ['c:grouping', 'attrs', 'val']),
  101. barDir: getTextByPathList(plotArea[key], ['c:barDir', 'attrs', 'val'])
  102. }
  103. break
  104. case 'c:bar3DChart':
  105. chart = {
  106. type: 'bar3DChart',
  107. data: extractChartData(plotArea[key]['c:ser']),
  108. colors: extractChartColors(plotArea[key]['c:ser'], warpObj),
  109. grouping: getTextByPathList(plotArea[key], ['c:grouping', 'attrs', 'val']),
  110. barDir: getTextByPathList(plotArea[key], ['c:barDir', 'attrs', 'val'])
  111. }
  112. break
  113. case 'c:pieChart':
  114. chart = {
  115. type: 'pieChart',
  116. data: extractChartData(plotArea[key]['c:ser']),
  117. colors: extractChartColors(plotArea[key]['c:ser']['c:dPt'], warpObj)
  118. }
  119. break
  120. case 'c:pie3DChart':
  121. chart = {
  122. type: 'pie3DChart',
  123. data: extractChartData(plotArea[key]['c:ser']),
  124. colors: extractChartColors(plotArea[key]['c:ser']['c:dPt'], warpObj)
  125. }
  126. break
  127. case 'c:doughnutChart':
  128. chart = {
  129. type: 'doughnutChart',
  130. data: extractChartData(plotArea[key]['c:ser']),
  131. colors: extractChartColors(plotArea[key]['c:ser']['c:dPt'], warpObj),
  132. holeSize: getTextByPathList(plotArea[key], ['c:holeSize', 'attrs', 'val'])
  133. }
  134. break
  135. case 'c:areaChart':
  136. chart = {
  137. type: 'areaChart',
  138. data: extractChartData(plotArea[key]['c:ser']),
  139. colors: extractChartColors(plotArea[key]['c:ser'], warpObj),
  140. grouping: getTextByPathList(plotArea[key], ['c:grouping', 'attrs', 'val'])
  141. }
  142. break
  143. case 'c:area3DChart':
  144. chart = {
  145. type: 'area3DChart',
  146. data: extractChartData(plotArea[key]['c:ser']),
  147. colors: extractChartColors(plotArea[key]['c:ser'], warpObj),
  148. grouping: getTextByPathList(plotArea[key], ['c:grouping', 'attrs', 'val'])
  149. }
  150. break
  151. case 'c:scatterChart':
  152. chart = {
  153. type: 'scatterChart',
  154. data: extractChartData(plotArea[key]['c:ser']),
  155. colors: extractChartColors(plotArea[key]['c:ser'], warpObj),
  156. style: getTextByPathList(plotArea[key], ['c:scatterStyle', 'attrs', 'val'])
  157. }
  158. break
  159. case 'c:bubbleChart':
  160. chart = {
  161. type: 'bubbleChart',
  162. data: extractChartData(plotArea[key]['c:ser']),
  163. colors: extractChartColors(plotArea[key]['c:ser'], warpObj)
  164. }
  165. break
  166. case 'c:radarChart':
  167. chart = {
  168. type: 'radarChart',
  169. data: extractChartData(plotArea[key]['c:ser']),
  170. colors: extractChartColors(plotArea[key]['c:ser'], warpObj),
  171. style: getTextByPathList(plotArea[key], ['c:radarStyle', 'attrs', 'val'])
  172. }
  173. break
  174. case 'c:surfaceChart':
  175. chart = {
  176. type: 'surfaceChart',
  177. data: extractChartData(plotArea[key]['c:ser']),
  178. colors: extractChartColors(plotArea[key]['c:ser'], warpObj)
  179. }
  180. break
  181. case 'c:surface3DChart':
  182. chart = {
  183. type: 'surface3DChart',
  184. data: extractChartData(plotArea[key]['c:ser']),
  185. colors: extractChartColors(plotArea[key]['c:ser'], warpObj)
  186. }
  187. break
  188. case 'c:stockChart':
  189. chart = {
  190. type: 'stockChart',
  191. data: extractChartData(plotArea[key]['c:ser']),
  192. colors: []
  193. }
  194. break
  195. default:
  196. }
  197. }
  198. return chart
  199. }