123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- import { eachElement, getTextByPathList } from './utils'
- import { applyTint } from './color'
- function extractChartColors (serNode, warpObj) {
- if (serNode.constructor !== Array) serNode = [serNode]
- const schemeClrs = []
- for (const node of serNode) {
- let schemeClr = getTextByPathList(node, ['c:spPr', 'a:solidFill', 'a:schemeClr'])
- if (!schemeClr) schemeClr = getTextByPathList(node, ['c:spPr', 'a:ln', 'a:solidFill', 'a:schemeClr'])
- if (!schemeClr) schemeClr = getTextByPathList(node, ['c:marker', 'c:spPr', 'a:ln', 'a:solidFill', 'a:schemeClr'])
- let clr = getTextByPathList(schemeClr, ['attrs', 'val'])
- if (clr) {
- clr = getTextByPathList(warpObj['themeContent'], ['a:theme', 'a:themeElements', 'a:clrScheme', `a:${clr}`, 'a:srgbClr', 'attrs', 'val'])
- const tint = getTextByPathList(schemeClr, ['a:tint', 'attrs', 'val']) / 100000
- if (clr && !isNaN(tint)) {
- clr = applyTint(clr, tint)
- }
- } else clr = getTextByPathList(node, ['c:spPr', 'a:solidFill', 'a:srgbClr', 'attrs', 'val'])
- if (clr) clr = '#' + clr
- schemeClrs.push(clr)
- }
- return schemeClrs
- }
- function extractChartData (serNode) {
- const dataMat = []
- if (!serNode) return dataMat
- if (serNode['c:xVal']) {
- let dataRow = []
- eachElement(serNode['c:xVal']['c:numRef']['c:numCache']['c:pt'], innerNode => {
- dataRow.push(parseFloat(innerNode['c:v']))
- return ''
- })
- dataMat.push(dataRow)
- dataRow = []
- eachElement(serNode['c:yVal']['c:numRef']['c:numCache']['c:pt'], innerNode => {
- dataRow.push(parseFloat(innerNode['c:v']))
- return ''
- })
- dataMat.push(dataRow)
- } else {
- eachElement(serNode, (innerNode, index) => {
- const dataRow = []
- const colName = getTextByPathList(innerNode, ['c:tx', 'c:strRef', 'c:strCache', 'c:pt', 'c:v']) || index
- const rowNames = {}
- if (getTextByPathList(innerNode, ['c:cat', 'c:strRef', 'c:strCache', 'c:pt'])) {
- eachElement(innerNode['c:cat']['c:strRef']['c:strCache']['c:pt'], innerNode => {
- rowNames[innerNode['attrs']['idx']] = innerNode['c:v']
- return ''
- })
- } else if (getTextByPathList(innerNode, ['c:cat', 'c:numRef', 'c:numCache', 'c:pt'])) {
- eachElement(innerNode['c:cat']['c:numRef']['c:numCache']['c:pt'], innerNode => {
- rowNames[innerNode['attrs']['idx']] = innerNode['c:v']
- return ''
- })
- }
- if (getTextByPathList(innerNode, ['c:val', 'c:numRef', 'c:numCache', 'c:pt'])) {
- eachElement(innerNode['c:val']['c:numRef']['c:numCache']['c:pt'], innerNode => {
- dataRow.push({
- x: innerNode['attrs']['idx'],
- y: parseFloat(innerNode['c:v'])
- })
- return ''
- })
- }
- dataMat.push({
- key: colName,
- values: dataRow,
- xlabels: rowNames
- })
- return ''
- })
- }
- return dataMat
- }
- export function getChartInfo (plotArea, warpObj) {
- let chart = null
- for (const key in plotArea) {
- switch (key) {
- case 'c:lineChart':
- chart = {
- type: 'lineChart',
- data: extractChartData(plotArea[key]['c:ser']),
- colors: extractChartColors(plotArea[key]['c:ser'], warpObj),
- grouping: getTextByPathList(plotArea[key], ['c:grouping', 'attrs', 'val']),
- marker: !!plotArea[key]['c:marker']
- }
- break
- case 'c:line3DChart':
- chart = {
- type: 'line3DChart',
- data: extractChartData(plotArea[key]['c:ser']),
- colors: extractChartColors(plotArea[key]['c:ser'], warpObj),
- grouping: getTextByPathList(plotArea[key], ['c:grouping', 'attrs', 'val'])
- }
- break
- case 'c:barChart':
- chart = {
- type: 'barChart',
- data: extractChartData(plotArea[key]['c:ser']),
- colors: extractChartColors(plotArea[key]['c:ser'], warpObj),
- grouping: getTextByPathList(plotArea[key], ['c:grouping', 'attrs', 'val']),
- barDir: getTextByPathList(plotArea[key], ['c:barDir', 'attrs', 'val'])
- }
- break
- case 'c:bar3DChart':
- chart = {
- type: 'bar3DChart',
- data: extractChartData(plotArea[key]['c:ser']),
- colors: extractChartColors(plotArea[key]['c:ser'], warpObj),
- grouping: getTextByPathList(plotArea[key], ['c:grouping', 'attrs', 'val']),
- barDir: getTextByPathList(plotArea[key], ['c:barDir', 'attrs', 'val'])
- }
- break
- case 'c:pieChart':
- chart = {
- type: 'pieChart',
- data: extractChartData(plotArea[key]['c:ser']),
- colors: extractChartColors(plotArea[key]['c:ser']['c:dPt'], warpObj)
- }
- break
- case 'c:pie3DChart':
- chart = {
- type: 'pie3DChart',
- data: extractChartData(plotArea[key]['c:ser']),
- colors: extractChartColors(plotArea[key]['c:ser']['c:dPt'], warpObj)
- }
- break
- case 'c:doughnutChart':
- chart = {
- type: 'doughnutChart',
- data: extractChartData(plotArea[key]['c:ser']),
- colors: extractChartColors(plotArea[key]['c:ser']['c:dPt'], warpObj),
- holeSize: getTextByPathList(plotArea[key], ['c:holeSize', 'attrs', 'val'])
- }
- break
- case 'c:areaChart':
- chart = {
- type: 'areaChart',
- data: extractChartData(plotArea[key]['c:ser']),
- colors: extractChartColors(plotArea[key]['c:ser'], warpObj),
- grouping: getTextByPathList(plotArea[key], ['c:grouping', 'attrs', 'val'])
- }
- break
- case 'c:area3DChart':
- chart = {
- type: 'area3DChart',
- data: extractChartData(plotArea[key]['c:ser']),
- colors: extractChartColors(plotArea[key]['c:ser'], warpObj),
- grouping: getTextByPathList(plotArea[key], ['c:grouping', 'attrs', 'val'])
- }
- break
- case 'c:scatterChart':
- chart = {
- type: 'scatterChart',
- data: extractChartData(plotArea[key]['c:ser']),
- colors: extractChartColors(plotArea[key]['c:ser'], warpObj),
- style: getTextByPathList(plotArea[key], ['c:scatterStyle', 'attrs', 'val'])
- }
- break
- case 'c:bubbleChart':
- chart = {
- type: 'bubbleChart',
- data: extractChartData(plotArea[key]['c:ser']),
- colors: extractChartColors(plotArea[key]['c:ser'], warpObj)
- }
- break
- case 'c:radarChart':
- chart = {
- type: 'radarChart',
- data: extractChartData(plotArea[key]['c:ser']),
- colors: extractChartColors(plotArea[key]['c:ser'], warpObj),
- style: getTextByPathList(plotArea[key], ['c:radarStyle', 'attrs', 'val'])
- }
- break
- case 'c:surfaceChart':
- chart = {
- type: 'surfaceChart',
- data: extractChartData(plotArea[key]['c:ser']),
- colors: extractChartColors(plotArea[key]['c:ser'], warpObj)
- }
- break
- case 'c:surface3DChart':
- chart = {
- type: 'surface3DChart',
- data: extractChartData(plotArea[key]['c:ser']),
- colors: extractChartColors(plotArea[key]['c:ser'], warpObj)
- }
- break
- case 'c:stockChart':
- chart = {
- type: 'stockChart',
- data: extractChartData(plotArea[key]['c:ser']),
- colors: []
- }
- break
- default:
- }
- }
- return chart
- }
|