| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- /**
- * 将请求返回的图表展示数据解析为前台展示用的config
- */
- import moment from 'moment'
- import EllipsisTooltip from '../components/common/ellipsisTooltip/index'
- import { deepAssign } from '../utils/baseUtils'
- import STATISTICS_OPTION from '../components/chartDesigner/sections/statisticsOption.json'
- import themes from './theme/index'
- export default function(viewType, data, chartConfig, themeName, styleConfig) {
- if(!data) {
- return {};
- }
- try {
- let theme = themes[themeName] || themes['default']
- let o, themeConfig;
- switch(viewType) {
- case 'bar': {
- themeConfig = Object.assign({}, theme.base, theme.bar);
- o = barOption(data, chartConfig, themeConfig, styleConfig);
- break;
- }
- case 'pie': {
- themeConfig = Object.assign({}, theme.base, theme.pie);
- o = pieOption(data, chartConfig, themeConfig, styleConfig);
- break;
- }
- case 'line': {
- themeConfig = Object.assign({}, theme.base, theme.line);
- o = lineOption(data, chartConfig, themeConfig, styleConfig);
- break;
- }
- case 'scatter': {
- themeConfig = Object.assign({}, theme.base, theme.scatter);
- o = scatterOption(data, chartConfig, themeConfig, styleConfig);
- break;
- }
- case 'aggregateTable': {
- themeConfig = theme.aggregateTable;
- o = aggregateTableOption(data, chartConfig, themeConfig, styleConfig);
- break;
- }case 'dataView' : {
- themeConfig = theme.dataView;
- o = dataViewOption(data, chartConfig, themeConfig, styleConfig);
- break;
- }
- default:{
- o = {};
- break;
- }
- }
- return o;
- }catch(e) {
- console.error(e);
- }
- }
- function barOption(data, barConfig, themeConfig, styleConfig) {
- let xTitle = barConfig.xAxis?`${barConfig.xAxis.column.label}${barConfig.xAxis.granularity.value?'('+barConfig.xAxis.granularity.label+')':''}`:null
- let yTitle = barConfig.yAxis?`${barConfig.yAxis.column.label}${barConfig.yAxis.gauge.value?'('+barConfig.yAxis.gauge.label+')':''}`:null
- data.serieses = data.serieses || [];
- let option = deepAssign({
- tooltip : {
- trigger: "axis",
- axisPointer: {
- type: "cross",
- label: {
- backgroundColor: "#6a7985"
- }
- }
- },
- legend: {
- show: data.serieses.length > 1
- },
- grid: {
- left: '10%',
- right: '10%',
- top: 60,
- bottom: 60,
- containLabel: true
- },
- xAxis: [{
- type: 'category',
- data: data.xAxis.map(d => {
- let gv= barConfig.xAxis.granularity.value;
- let xv = d || '空';
- if(gv === 'halfYear') {
- let arr = d.split('-H');
- xv = arr[0] + ['上半年', '下半年'][arr[1] - 1]
- }else if(gv === 'month') {
- xv = d.replace('-M', '-');
- }else if(gv === 'quarter') {
- let arr = d.split('-Q');
- xv = arr[0] + '-' + ['一', '二', '三', '四'][arr[1] - 1] + '季度'
- }else if(gv === 'week') {
- let arr = d.split('-W');
- xv = arr[0] + '-' + arr[1] + '周'
- }
- return xv;
- }),
- name: xTitle || '横轴',
- }],
- yAxis: [{
- name: yTitle || '纵轴',
- type: 'value'
- }],
- series: data.serieses.map(s => {
- return {
- name: barConfig.groupBy ? s.name : (barConfig.yAxis.column.label || s.name),
- type: 'bar',
- data: s.value,
- // stack: s.stack
- }
- }),
- dataZoom: {
- show: false
- }
- }, themeConfig, styleConfig);
-
- return option;
- }
- function pieOption(data, pieConfig, themeConfig, styleConfig) {
- let columnName = pieConfig.xAxis.column.label + (pieConfig.xAxis.granularity.value ? '('+pieConfig.xAxis.granularity.label+')' : '');
-
- let option = deepAssign({
- tooltip : {
- trigger: 'item',
- formatter: "{a} <br/>{b} : {c} ({d}%)"
- },
- legend: {
- show: true,
- orient: 'vertical',
- right: '30%',
- top: '100',
- data: data.xAxis.map(d => {
- let gv= pieConfig.xAxis.granularity.value;
- let xv = d || '空';
- if(gv === 'halfYear') {
- let arr = d.split('-H');
- xv = arr[0] + ['上半年', '下半年'][arr[1] - 1]
- }else if(gv === 'month') {
- xv = d.replace('-M', '-');
- }else if(gv === 'quarter') {
- let arr = d.split('-Q');
- xv = arr[0] + '-' + ['一', '二', '三', '四'][arr[1] - 1] + '季度'
- }else if(gv === 'week') {
- let arr = d.split('-W');
- xv = arr[0] + '-' + arr[1] + '周'
- }
- return xv;
- }),
- },
- grid: {
- left: '10%',
- right: '10%',
- top: 100,
- bottom: 60,
- containLabel: true
- },
- series : [
- {
- name: columnName,
- type: 'pie',
- center: ['30%', '50%'],
- radius: [0, '65%'],
- // radius : '55%',
- // center: ['50%', '60%'],
- // data: (data.serieses || [{ value: [] }])[0].value,
- // data: (data.serieses || [{ value: [] }])[0].value.map(v => ({ ...v, name: v.name || '空' })),
- data: (data.serieses || [{ value: [] }])[0].value.map(v => {
- let obj = { ...v };
- if(!v.name) {
- obj.name = '空'
- }else {
- let gv= pieConfig.xAxis.granularity.value;
- if(gv === 'halfYear') {
- let arr = v.name.split('-H');
- obj.name = arr[0] + ['上半年', '下半年'][arr[1] - 1]
- }else if(gv === 'month') {
- obj.name = v.name.replace('-M', '-');
- }else if(gv === 'quarter') {
- let arr = v.name.split('-');
- obj.name = arr[0] + '-' + ['一', '二', '三', '四'][arr[1] - 1] + '季度'
- }else if(gv === 'week') {
- let arr = v.name.split('-');
- obj.name = arr[0] + '-' + arr[1] + '周'
- }
- }
- return obj;
- }),
- itemStyle: {
- emphasis: {
- shadowBlur: 10,
- shadowOffsetX: 0,
- shadowColor: 'rgba(0, 0, 0, 0.5)'
- }
- }
- }
- ]
- }, themeConfig, styleConfig);
- return option;
- }
- function lineOption(data, lineConfig, themeConfig, styleConfig) {
- let xTitle = lineConfig.xAxis?`${lineConfig.xAxis.column.label}${lineConfig.xAxis.granularity.value?'('+lineConfig.xAxis.granularity.label+')':''}`:null
- let yTitle = lineConfig.yAxis?`${lineConfig.yAxis.column.label}${lineConfig.yAxis.gauge.value?'('+lineConfig.yAxis.gauge.label+')':''}`:null
- data.serieses = data.serieses || [];
- let option = deepAssign({
- tooltip: {
- trigger: 'axis',
- axisPointer: {
- type: 'cross'
- }
- },
- legend: {
- show: data.serieses.length > 1
- },
- xAxis: {
- name: xTitle || '横轴',
- type: 'time'
- },
- yAxis: {
- name: yTitle || '纵轴',
- type: 'value'
- },
-
- series: (data.serieses || []).map(s => {
- return {
- name: s.name,
- type: 'line',
- data: s.mdata.map(m => {
- return [m.date, m.value]
- }).sort((a, b) => {return new Date(a[0]).getTime() - new Date(b[0]).getTime()} )
- }
- }),
- dataZoom: {
- show: false
- }
- }, themeConfig, styleConfig);
- return option;
- }
- function scatterOption(data, scatterConfig, themeConfig, styleConfig) {
- let xTitle = scatterConfig.xAxis?`${scatterConfig.xAxis.column.label}${scatterConfig.xAxis.granularity.value?'('+scatterConfig.xAxis.granularity.label+')':''}`:null
- let yTitle = scatterConfig.yAxis?`${scatterConfig.yAxis.column.label}${scatterConfig.yAxis.gauge.value?'('+scatterConfig.yAxis.gauge.label+')':''}`:null
-
- let option = deepAssign({
- tooltip : {
- showDelay : 0,
- axisPointer:{
- show: true,
- type : 'cross',
- lineStyle: {
- type : 'dashed',
- width : 1
- }
- }
- },
- legend: {
- show: true
- },
- xAxis : [
- {
- type : 'value',
- name: xTitle || '横轴',
- scale:true,
- splitLine: {
- show: true
- }
- }
- ],
- yAxis : [
- {
- type : 'value',
- name: yTitle || '纵轴',
- scale:true,
- splitLine: {
- show: true
- }
- }
- ],
- series : (data.serieses || []).map(s => {
- return {
- name: s.name,
- type: 'scatter',
- data: s.mdata.map(m => {
- return [m.date, m.value]
- })
- }
- }),
- dataZoom: {
- show: false
- }
- }, themeConfig, styleConfig);
- return option;
- }
- function aggregateTableOption( data, aggregateTableConfig, themeConfig, styleConfig) {
- const resData = data.valueList;
- const { targetColumn, statistics: statisticsNames } = aggregateTableConfig;
- const { direction } = styleConfig;
-
- let statistics = STATISTICS_OPTION.filter(o => statisticsNames.indexOf(o.value) !== -1);
- let option = {
- targetColumn: targetColumn,
- statistics: statistics.map(s => ({
- name: s.value,
- label: s.label,
- value: resData[s.value]
- })),
- direction: direction || 'vertical'
- };
- return option;
- }
- function dataViewOption(data, dataViewConfig, themeConfig, styleConfig) {
- const { list, pageNum, pageSize, pages, total } = data.valueList;
- let columns = dataViewConfig.viewColumns || [];
- let dataSource = list || [];
- let option = {
- columns: columns.map(c => {
- let obj = {
- title: c.label,
- dataIndex: c.name,
- }
- if(c.type === 'time') {
- obj.render = (v, r, i) => {
- let text = v === null ? '空' : moment(v).isValid() ? moment(v).format('YYYY-MM-DD') : v
- return <EllipsisTooltip title={text}>{text}</EllipsisTooltip>
- }
- }else {
- obj.render = v => {
- let text = v === null ? '空' : v
- return <EllipsisTooltip title={text}>{text}</EllipsisTooltip>
- }
- }
- return obj;
- }),
- dataSource: dataSource.map((d, i) => {
- return { ...d, key: i}
- }),
- page: pageNum,
- pageSize,
- pages,
- total,
- };
- return option;
- }
|