123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- import {
- defineComponent,
- nextTick,
- onMounted,
- ref,
- shallowRef,
- watch
- } from 'vue'
- import styles from './index.module.less'
- import * as echarts from 'echarts/core'
- import {
- LineChart
- // LineSeriesOption
- } from 'echarts/charts'
- // import { PieChart } from 'echarts/charts'
- import {
- TitleComponent,
- // 组件类型的定义后缀都为 ComponentOption
- // TitleComponentOption,
- TooltipComponent,
- // TooltipComponentOption,
- GridComponent,
- // 数据集组件
- DatasetComponent,
- // DatasetComponentOption,
- // 内置数据转换器组件 (filter, sort)
- // TransformComponent,
- LegendComponent,
- ToolboxComponent,
- DataZoomComponent
- } from 'echarts/components'
- import { LabelLayout } from 'echarts/features'
- import { CanvasRenderer } from 'echarts/renderers'
- import { formatSecToHMS } from '..'
- // 注册必须的组件
- echarts.use([
- TitleComponent,
- TooltipComponent,
- GridComponent,
- DatasetComponent,
- // TransformComponent,
- LabelLayout,
- // UniversalTransition,
- CanvasRenderer,
- // PieChart,
- ToolboxComponent,
- LegendComponent,
- DataZoomComponent,
- LineChart
- ])
- const lineChartOption = (params: {
- xAxisData: any
- seriesData: any
- colors: {
- lineColor?: string
- startColor?: string
- endColor?: string
- unit?: string
- }
- }) => {
- return {
- title: {
- text: '单位:' + (params.colors.unit || '人'),
- textStyle: {
- color: '#777777',
- fontSize: 13,
- fontWeight: 400
- }
- },
- legend: { show: false },
- emphasis: { lineStyle: { width: 2 } },
- xAxis: {
- boundaryGap: false,
- data: params.xAxisData,
- type: 'category',
- axisLine: { lineStyle: { color: '#8C8C8C' } },
- lineStyle: { color: '#F2F2F2' }
- },
- color: [
- params.colors.lineColor || '#2DC7AA'
- // '#FF6079'
- // '#2DC7AA',
- // '#FF602C',
- // '#91DD1C',
- // '#FFA92C',
- // '#BE7E2E',
- // '#1C96DD',
- // '#D22CFF',
- // '#FF3C3C',
- // '#1AEE3E',
- // '#00c9ff'
- ],
- series: [
- {
- lineStyle: { width: 1 },
- data: params.seriesData,
- symbol: 'circle',
- name: '购买次数',
- type: 'line',
- areaStyle: {
- color: {
- type: 'linear',
- x: 0,
- y: 0,
- x2: 0,
- y2: 1,
- colorStops: [
- {
- offset: 0,
- color: params.colors.startColor || 'rgba(45, 199, 170, 0.23)'
- // 0% 处的颜色
- },
- {
- offset: 1,
- // 100% 处的颜色
- color: params.colors.endColor || 'rgba(45, 199, 170, 0)'
- }
- ]
- }
- },
- emphasis: { lineStyle: { width: 1 } }
- }
- ],
- grid: {
- bottom: '3%',
- containLabel: true,
- left: '3%',
- right: '5%',
- top: '40'
- },
- tooltip: {
- trigger: 'axis',
- confine: true,
- formatter: function (params: any) {
- return params[0].name
- },
- backgroundColor: '#FF6079',
- borderWidth: 0,
- borderRadius: 24,
- padding: [1, 4],
- textStyle: {
- color: '#FFFFFF',
- fontSize: 12
- }
- },
- yAxis: {
- type: 'value',
- splitLine: {
- axisLine: { lineStyle: { color: '#8C8C8C' } },
- lineStyle: { color: ['#f2f2f2'], type: 'dashed' }
- }
- },
- dataZoom: [{ type: 'inside', throttle: 100 }],
- toolbox: { feature: { saveAsImage: { show: false } } }
- }
- }
- export default defineComponent({
- name: 'eChats-model',
- props: {
- obj: {
- type: Object,
- default: () => ({})
- },
- type: {
- type: String as PropType<'TIME' | 'NUM'>,
- default: 'TIME'
- }
- },
- setup(props) {
- const chartId = 'eChart_' + Date.now() + props.type
- const color = props.type === 'NUM' ? '#FF955D' : '#2DC7AA'
- const statisticCounts = ref({
- time: '' as any,
- count: 0 as any
- })
- let myChart: echarts.ECharts
- nextTick(() => {
- myChart = echarts.init(document.getElementById(chartId) as HTMLDivElement)
- })
- const _initData = () => {
- nextTick(() => {
- statisticCounts.value.time = props.obj.time || ''
- statisticCounts.value.count =
- props.type === 'NUM'
- ? props.obj.count
- : formatSecToHMS(props.obj.count).all
- myChart.clear()
- lineChartOption &&
- myChart.setOption(
- lineChartOption({
- xAxisData: props.obj.xAxisData,
- seriesData: props.obj.yAxisData,
- colors: {
- lineColor: color,
- startColor:
- props.type === 'NUM'
- ? 'rgba(255, 149, 93, 0.23)'
- : 'rgba(45, 199, 170, 0.23)',
- endColor:
- props.type === 'NUM'
- ? 'rgba(255, 149, 93, 0)'
- : 'rgba(45, 199, 170, 0)',
- unit: props.type === 'NUM' ? '人' : '分钟'
- }
- })
- )
- myChart.on('highlight', function (params: any) {
- const batch = params.batch || []
- // const options: any = myChart.getOption()
- batch.forEach((item: any) => {
- const batchIndex = item.dataIndex
- const count =
- props.type === 'NUM'
- ? props.obj.yAxisData[batchIndex]
- : formatSecToHMS(props.obj.yAxisData[batchIndex] || 0).all
- const time = props.obj.xAxisData[batchIndex]
- statisticCounts.value = {
- count,
- time
- }
- })
- })
- })
- }
- nextTick(() => {
- myChart = echarts.init(document.getElementById(chartId) as HTMLDivElement)
- _initData()
- })
- watch(
- () => props.obj,
- () => {
- _initData()
- },
- {
- deep: true
- }
- )
- return () => (
- <div class={styles.eChartSection}>
- <div class={styles.eChartTitle}>
- <div class={styles.left}>
- <div class={styles.item} style={{ '--color': color } as any}>
- {/* <span class={styles.line}></span> */}
- {props.type === 'NUM' ? (
- <>
- <span class={styles.text}>
- {statisticCounts.value.time} 练习人数
- </span>
- <span class={styles.num}>
- {statisticCounts.value.count || 0}人
- </span>
- </>
- ) : (
- <>
- <span class={styles.text}>
- {statisticCounts.value.time} 练习时长
- </span>
- <span class={styles.num}>{statisticCounts.value.count}</span>
- </>
- )}
- </div>
- </div>
- </div>
- <div class={styles.eChart}>
- <div id={chartId} style="width: 100%; height: 100%;"></div>
- </div>
- </div>
- )
- }
- })
|