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

339 lines
6.8 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'
2021-07-15 19:21:23 +08:00
import { shortFormatter, tooltipShortFormatter } from './chartFormatter'
2021-07-05 22:58:12 +08:00
import _ from 'lodash'
export const chartColor = ['#5370C6', '#90CC74', '#FAC858', '#EE6666',
'#73BFDE', '#3BA172', '#FC8452', '#9960B4',
'#E97CCC', '#FEA69E', '#0F8AB2', '#57CBAC',
'#5888BC', '#63B6AC', '#EDC6B2', '#D5746B']
2021-07-06 10:55:09 +08:00
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: axiosFormatter,
2021-07-15 19:21:23 +08:00
show: true,
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',
axisLabel: {
formatter: shortFormatter
}
},
2021-07-05 22:58:12 +08:00
animation: false,
grid: {
left: 55,
2021-07-10 12:30:06 +08:00
bottom: 30,
top: 40,
right: 25
},
2021-07-06 10:55:09 +08:00
color: chartColor,
legend: {
2021-07-15 19:21:23 +08:00
tooltip: {
show: true,
formatter: '{a}'
},
show: true,
2021-07-15 19:21:23 +08:00
right: 23,
2021-07-10 12:30:06 +08:00
top: 8,
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',
2021-07-09 21:58:49 +08:00
smooth: false,
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: axiosFormatter,
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',
axisLabel: {
formatter: shortFormatter
}
2021-07-05 17:40:43 +08:00
},
2021-07-06 10:55:09 +08:00
color: chartColor,
2021-07-05 22:58:12 +08:00
grid: {
left: 55,
2021-07-10 11:18:40 +08:00
bottom: 30,
top: 20,
right: 20
2021-07-05 22:58:12 +08:00
},
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',
2021-07-09 21:58:49 +08:00
smooth: false,
2021-07-05 17:40:43 +08:00
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: axiosFormatter,
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',
axisLabel: {
formatter: shortFormatter
}
},
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-07-15 19:21:23 +08:00
trigger: 'item',
formatter: tooltipShortFormatter
2021-06-23 10:41:11 +08:00
},
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,
2021-07-10 12:30:06 +08:00
tooltip: {
show: true
},
formatter: tooLongFormatter
2021-06-23 10:41:11 +08:00
},
series: [
{
type: 'pie',
selectedMode: 'single',
2021-07-10 12:30:06 +08:00
radius: ['42%', '65%'],
2021-06-25 19:11:00 +08:00
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
}
2021-07-10 12:11:59 +08:00
/* 色块地图 */
export function isMapBlock (type) {
return type === 2
}
/* 带统计的折线图 */
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')
}
function axiosFormatter (params) {
let str = '<div>'
params.forEach((item, i) => {
const tData = item.data[0]
if (i === 0) {
str += '<div style="margin-bottom: 5px">'
str += window.$dayJs.tz(tData).format('YYYY-MM-DD HH:mm:ss')
str += '</div>'
}
str += '<div class="cn-chart-tooltip-box">'
str += item.marker
str += `<span class="cn-chart-tooltip-content">
${item.seriesName}
</span>`
str += `<span class="cn-chart-tooltip-value">
${shortFormatter(item.data[1])}
</span>`
str += '</div>'
})
str += '</div>'
return str
}