123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- import React, { useEffect, useRef, useState } from 'react';
- import styles from './lineChartsCommon.less';
- import * as echarts from 'echarts';
- interface editPros {
- width: number;
- height: number;
- data: any;
- }
- /**
- * 折线图组件
- * @constructor
- */
- const LineChartsCommon: React.FC<editPros> = (props) => {
- const chartRef: any = useRef();
- const { width, height, data } = props;
- const [chartsData, setChartsData] = useState(data);
- const options = {
- color: ['#1890FF'],
- tooltip: {
- trigger: 'axis',
- axisPointer: {
- // 坐标轴指示器,坐标轴触发有效
- type: 'shadow', // 默认为直线,可选为:'line' | 'shadow'
- },
- },
- grid: {
- top: '15%',
- left: '5%',
- right: '0',
- bottom: '8%',
- containLabel: true,
- },
- xAxis: [
- {
- type: 'category',
- data: [],
- boundaryGap: false,
- axisTick: {
- show: false, // 不显示坐标轴刻度线
- },
- splitLine: {
- show: false,
- lineStyle: {
- type: 'solid',
- color: 'rgba(230, 247, 255, 0.20)',
- },
- },
- axisLine: {
- lineStyle: {
- color: '#27b4c2',
- },
- },
- //x底部文字
- axisLabel: {
- interval: 0,
- rotate: 30,
- },
- },
- ],
- yAxis: [
- {
- type: 'value',
- // y轴的分割线
- splitLine: {
- show: true,
- lineStyle: {
- type: 'solid',
- color: 'rgba(230, 247, 255, 0.20)',
- },
- },
- },
- ],
- series: [
- {
- type: 'line',
- smooth: false,
- symbol: 'circle', //拐点设置为实心
- symbolSize: 2, //拐点大小
- itemStyle: {
- normal: {
- borderColor: 'rgba(42,157,255,.2)', //拐点边框颜色
- borderWidth: 10, //拐点边框大小,
- color: '#1890FF',
- },
- },
- tooltip: {
- trigger: 'axis',
- axisPointer: {
- // 坐标轴指示器,坐标轴触发有效
- type: 'line', // 默认为直线,可选为:'line' | 'shadow'
- },
- },
- lineStyle: {
- normal: {
- width: 3,
- shadowColor: '#1890FF',
- shadowBlur: 20,
- },
- },
- areaStyle: {
- opacity: 1,
- //右下左上
- color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
- {
- offset: 0,
- color: 'rgba(24, 144, 255, .5)',
- },
- {
- offset: 0.3,
- color: 'rgba(24, 144, 255, 0.2)',
- },
- {
- offset: 1,
- color: 'rgba(24, 144, 255, 0)',
- },
- ]),
- },
- data: [],
- },
- {
- type: 'lines',
- coordinateSystem: 'cartesian2d',
- symbolSize: 8,
- polyline: true, // 多线段
- effect: {
- show: true,
- period: 5,
- trailLength: 0.3,
- symbolSize: 11,
- symbol: 'circle',
- color: '#0069d9',
- },
- lineStyle: {
- normal: {
- width: 1,
- opacity: 0,
- },
- },
- // 光点
- data: [
- {
- coords: [],
- },
- ],
- },
- ],
- };
- const setCharts = () => {
- const myChart = echarts.init(chartRef.current);
- if (chartsData && chartsData.length > 0) {
- const list: any = [];
- const value: any = [];
- const total: any = [];
- chartsData.forEach((res: { time: string; value: number }) => {
- list.push(res.time);
- value.push(res.value);
- total.push([res.time, res.value]);
- });
- options.xAxis[0].data = list;
- options.series[0].data = value;
- options.series[1].data[0].coords = total;
- myChart.setOption(options, true);
- }
- };
- useEffect(() => {
- setChartsData(data);
- setCharts();
- }, [data]);
- return (
- <div className={styles.container}>
- <div ref={chartRef} style={{ width: `${width}vw`, height: `${height}vh` }} />
- </div>
- );
- };
- export default LineChartsCommon;
|