This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
cyber-narrator-cn-ui/src/components/charts/chart-options.js

296 lines
5.7 KiB
JavaScript
Raw Normal View History

/**
* @author 陈劲松
* @date 2021/6/16
* @description chart option和一些工具
*/
2021-07-05 22:58:12 +08:00
import { format } from 'echarts'
import _ from 'lodash'
2021-07-06 10:55:09 +08:00
export const chartColor = ['#73A0FA', '#73DEB3', '#F7C739', '#EB7E65', '#FFAB67', '#A285D2', '#FFA8CB']
export function getChartColor (index) {
return chartColor[index % chartColor.length]
}
const line = {
2021-07-06 15:57:27 +08:00
tooltip: {
appendToBody: true,
trigger: 'axis',
textStyle: {
2021-07-06 16:34:10 +08:00
width: '20px',
2021-07-06 15:57:27 +08:00
overflow: 'truncate'
},
// formatter: () =>{
// return '1'
// }
2021-07-06 16:34:10 +08:00
className: 'nz-chart-tooltip',
extraCssText: 'box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);max-width: 300px !important'
2021-07-06 15:57:27 +08:00
},
xAxis: {
type: 'time'
},
yAxis: {
type: 'value'
},
2021-07-05 22:58:12 +08:00
animation: false,
grid: {
left: 55,
bottom: 45,
top: 30
},
2021-07-06 10:55:09 +08:00
color: chartColor,
legend: {
show: true,
right: 30,
top: 'top',
orient: 'horizontal',
icon: 'circle',
itemGap: 20,
itemWidth: 10,
textStyle: {
padding: [0, 0, 0, 5],
fontSize: 14
2021-07-05 22:58:12 +08:00
},
formatter: tooLongFormatter
},
axisLabel: {
fontSize: 14
},
series: [
{
2021-06-29 19:45:44 +08:00
name: '',
type: 'line',
smooth: true,
symbol: 'none',
2021-06-29 19:45:44 +08:00
data: []
}
]
}
2021-07-05 17:40:43 +08:00
const lineWithStatistics = {
2021-07-06 15:57:27 +08:00
tooltip: {
appendToBody: true,
trigger: 'axis',
textStyle: {
2021-07-06 16:34:10 +08:00
width: '20px',
2021-07-06 15:57:27 +08:00
overflow: 'truncate'
},
// formatter: () =>{
// return '1'
// }
2021-07-06 16:34:10 +08:00
className: 'nz-chart-tooltip',
extraCssText: 'box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);max-width: 300px !important'
2021-07-06 15:57:27 +08:00
},
2021-07-05 17:40:43 +08:00
xAxis: {
type: 'time'
},
2021-07-05 22:58:12 +08:00
animation: false,
2021-07-05 17:40:43 +08:00
yAxis: {
type: 'value'
},
2021-07-06 10:55:09 +08:00
color: chartColor,
2021-07-05 22:58:12 +08:00
grid: {
left: 55,
bottom: 45,
top: 30
},
2021-07-05 17:40:43 +08:00
legend: {
2021-07-05 22:58:12 +08:00
show: false
2021-07-05 17:40:43 +08:00
},
axisLabel: {
fontSize: 14
},
series: [
{
name: '',
type: 'line',
smooth: true,
symbol: 'none',
data: []
}
]
}
const lineStack = {
2021-07-06 15:57:27 +08:00
tooltip: {
appendToBody: true,
trigger: 'axis',
textStyle: {
2021-07-06 16:34:10 +08:00
width: '20px',
2021-07-06 15:57:27 +08:00
overflow: 'truncate'
},
// formatter: () =>{
// return '1'
// }
2021-07-06 16:34:10 +08:00
className: 'nz-chart-tooltip',
extraCssText: 'box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);max-width: 300px !important'
2021-07-06 15:57:27 +08:00
},
xAxis: {
type: 'time'
},
2021-07-06 10:55:09 +08:00
color: chartColor,
yAxis: {
type: 'value'
},
grid: {
left: 55,
bottom: 45,
top: 10,
right: 180
},
legend: {
show: true,
right: 30,
top: 'middle',
orient: 'vertical',
icon: 'circle',
itemGap: 20,
itemWidth: 10,
2021-07-05 22:58:12 +08:00
formatter: tooLongFormatter,
textStyle: {
padding: [0, 0, 0, 5],
fontSize: 14
}
},
axisLabel: {
fontSize: 14
},
series: [
{
2021-07-05 22:58:12 +08:00
name: '',
type: 'line',
stack: 'value',
areaStyle: {},
symbol: 'none',
2021-07-05 22:58:12 +08:00
data: []
}
]
}
2021-06-23 10:41:11 +08:00
const pieWithTable = {
tooltip: {
2021-07-06 15:57:27 +08:00
appendToBody: true,
2021-06-23 10:41:11 +08:00
trigger: 'item'
},
2021-07-06 10:55:09 +08:00
color: chartColor,
2021-07-05 22:58:12 +08:00
animation: false,
2021-06-23 10:41:11 +08:00
legend: {
orient: 'vertical',
2021-06-23 15:57:34 +08:00
type: 'plain',
2021-07-05 22:58:12 +08:00
left: '60%',
top: 'middle',
2021-06-23 10:41:11 +08:00
icon: 'circle',
itemWidth: 10, // 设置宽度
itemHeight: 10, // 设置高度
2021-07-05 22:58:12 +08:00
itemGap: 20,
selectedMode: false,
formatter: tooLongFormatter
2021-06-23 10:41:11 +08:00
},
series: [
{
type: 'pie',
2021-06-25 19:11:00 +08:00
radius: ['42%', '70%'],
center: ['30%', '50%'],
data: [],
2021-07-05 22:58:12 +08:00
label: {
formatter: '{d}%'
},
2021-06-23 10:41:11 +08:00
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
}
const typeOptionMappings = [
{ value: 11, option: line }, // 常规折线图
2021-07-05 17:40:43 +08:00
{ value: 12, option: lineWithStatistics }, // 带统计表格的折线图
{ value: 13, option: lineStack }, // 折线堆叠图
2021-07-01 21:39:10 +08:00
{ value: 31, option: pieWithTable } // 常规折线图
]
const typeCategory = {
2021-06-24 17:59:51 +08:00
MAP: 'map',
TABLE: 'table',
ECHARTS: 'echarts',
TITLE: 'title',
2021-07-01 21:39:10 +08:00
SINGLE: 'singleValue',
TABS: 'tabs'
}
export function getTypeCategory (type) {
2021-06-24 17:59:51 +08:00
if (isMap(type)) {
return typeCategory.MAP
} else if (isEcharts(type)) {
return typeCategory.ECHARTS
} else if (isTable(type)) {
return typeCategory.TABLE
} else if (isSingleValue(type)) {
return typeCategory.SINGLE
} else if (isTitle(type)) {
return typeCategory.TITLE
2021-07-01 21:39:10 +08:00
} else if (isTabs(type)) {
return typeCategory.TABS
}
}
/* 饼图柱状图等 */
export function isEcharts (type) {
2021-06-24 17:59:51 +08:00
return type >= 11 && type <= 50
}
/* 地图 */
export function isMap (type) {
return type >= 1 && type <= 10
}
/* 连线地图 */
export function isMapLine (type) {
return type === 1
}
/* 带统计的折线图 */
export function isEchartsWithStatistics (type) {
return type === 12
}
/* 单值 */
export function isSingleValue (type) {
return type >= 51 && type <= 60
}
/* 带折线图的单值 */
export function isSingleValueWithEcharts (type) {
return type === 52
}
/* 带Table的饼图 */
export function isEchartsWithTable (type) {
return type === 31
}
/* table */
export function isTable (type) {
return type >= 61 && type <= 70
}
2021-07-01 21:39:10 +08:00
/* title */
export function isTitle (type) {
2021-07-01 21:39:10 +08:00
return type === 93
}
/* tabs */
export function isTabs (type) {
return type === 91
}
export function getOption (type) {
const mapping = typeOptionMappings.find(m => m.value === type)
2021-07-05 22:58:12 +08:00
return mapping && mapping.option ? _.cloneDeep(mapping.option) : null
}
export const layoutConstant = {
HEADER: 'header',
FOOTER: 'footer'
}
export function getLayout (type) {
const layout = []
if (!isSingleValue(type) && !isTitle(type)) {
layout.push(layoutConstant.HEADER)
}
if (type === 12 || type === 31) {
layout.push(layoutConstant.FOOTER)
}
return layout
}
2021-07-05 22:58:12 +08:00
function tooLongFormatter (name) {
return format.truncateText(name, 110, '12')
}
2021-07-06 15:57:27 +08:00
// function axiosFormatter(params) {
//
// }