123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <template>
- <div class="echartLine">
- <div class="line" ref="line"></div>
- </div>
- </template>
- <script lang="ts" setup>
- import * as echarts from 'echarts';
- import { ref, nextTick, reactive, watch, defineProps } from 'vue';
- let props = defineProps({
- echartDataSg: {
- type: Object,
- },
- lengedDataName: {
- type: String,
- },
- maxY: {
- type: Number,
- },
- });
- //获取dom元素节点
- let line = ref<any>();
- let echartDataSgs = reactive({});
- watch(
- () => props.echartDataSg,
- (data) => {
- echartDataSgs = Object.assign({}, data);
- getOption();
- },
- { immediate: true, deep: true }
- );
- function getOption() {
- nextTick(() => {
- const myChart = echarts.init(line.value);
- let option = {
- grid: {
- top: '8%',
- left: '5%',
- bottom: '14%',
- right: '5%',
- // containLabel: true,
- },
- tooltip: {
- trigger: 'axis',
- backgroundColor: 'rgba(0, 0, 0, .6)',
- textStyle: {
- color: '#fff',
- fontSize: 12,
- },
- },
- legend: {
- align: 'left',
- right: '50%',
- top: '0%',
- type: 'plain',
- textStyle: {
- color: '#7ec7ff',
- fontSize: 14,
- },
- // icon:'rect',
- itemGap: 25,
- itemWidth: 18,
- icon: 'path://M0 2a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2z',
- data: [
- {
- name: echartDataSgs.lengedData ? echartDataSgs.lengedData : '',
- },
- ],
- },
- xAxis: [
- {
- type: 'category',
- boundaryGap: false,
- axisLabel: {
- // formatter: '{value}',
- fontSize: 14,
- margin: 10,
- textStyle: {
- color: '#b3b8cc',
- },
- // interval: 0,
- formatter: function (params) {
- let newParamsName = ref(''); // 最终拼接成的字符串
- let paramsNameNumber = ref(params.length); // 实际标签的个数
- let provideNumber = ref(10); // 每行能显示的字的个数
- let rowNumber = Math.ceil(paramsNameNumber.value / provideNumber.value); // 换行的话,需要显示几行,向上取整
- /**
- * 判断标签的个数是否大于规定的个数, 如果大于,则进行换行处理 如果不大于,即等于或小于,就返回原标签
- */
- // 条件等同于rowNumber>1
- if (paramsNameNumber.value > provideNumber.value) {
- /** 循环每一行,p表示行 */
- for (var p = 0; p < rowNumber; p++) {
- var tempStr = ''; // 表示每一次截取的字符串
- var start = p * provideNumber.value; // 开始截取的位置
- var end = start + provideNumber.value; // 结束截取的位置
- // 此处特殊处理最后一行的索引值
- if (p == rowNumber - 1) {
- // 最后一次不换行
- tempStr = params.substring(start, paramsNameNumber.value);
- } else {
- // 每一次拼接字符串并换行
- tempStr = params.substring(start, end) + '\n';
- }
- newParamsName.value += tempStr; // 最终拼成的字符串
- }
- } else {
- // 将旧标签的值赋给新标签
- newParamsName.value = params;
- }
- //将最终的字符串返回
- return newParamsName.value;
- },
- },
- axisLine: {
- lineStyle: {
- color: '#244a94',
- },
- },
- splitLine: {
- show: true,
- lineStyle: {
- color: '#0d2973',
- type: 'dashed',
- },
- },
- axisTick: {
- show: false,
- },
- data: echartDataSgs.xData,
- },
- ],
- yAxis: [
- {
- boundaryGap: false,
- name: props.lengedDataName ? props.lengedDataName : '',
- type: 'value',
- max: props.maxY,
- axisLabel: {
- textStyle: {
- color: '#b3b8cc',
- },
- },
- nameTextStyle: {
- color: '#fff',
- fontSize: 12,
- lineHeight: 5,
- },
- splitLine: {
- lineStyle: {
- color: '#0d2973',
- type: 'dashed',
- },
- },
- axisLine: {
- show: true,
- lineStyle: {
- color: '#244a94',
- },
- },
- axisTick: {
- show: false,
- },
- },
- ],
- series: [
- {
- name: echartDataSgs.lengedData ? echartDataSgs.lengedData : '',
- type: 'line',
- smooth: false,
- showSymbol: false,
- zlevel: 3,
- itemStyle: {
- color: '#02f2de',
- borderColor: '#a3c8d8',
- },
- lineStyle: {
- normal: {
- width: 3,
- color: '#02f2de',
- },
- },
- areaStyle: {
- normal: {
- color: new echarts.graphic.LinearGradient(
- 0,
- 0,
- 0,
- 1,
- [
- {
- offset: 0,
- color: 'rgba(2, 242, 222,0.8)',
- },
- {
- offset: 0.5,
- color: 'rgba(2, 242, 222,0.4)',
- },
- {
- offset: 0.9,
- color: 'rgba(2, 242, 222,0)',
- },
- ],
- false
- ),
- },
- },
- data: echartDataSgs.yData,
- },
- ],
- };
- myChart.setOption(option);
- window.onresize = function () {
- myChart.resize();
- };
- });
- }
- </script>
- <style scoped lang="less">
- .echartLine {
- width: 100%;
- height: 100%;
- .line {
- width: 100%;
- height: 100%;
- }
- }
- </style>
|