import { Component, OnInit, ViewChild } from "@angular/core"; import { ENgxPrintComponent } from "e-ngx-print"; import { CommomService } from "src/app/core/services/commom.service"; import { ChartOptionService } from "src/app/core/services/chartServices/chart-option.service"; import { ChartMenuService } from "src/app/core/services/chartServices/chart-menu.service"; import { addMilliseconds, format } from "date-fns"; import { fromEvent } from "rxjs"; import { debounceTime } from "rxjs/operators"; @Component({ selector: "app-sub-annotations-line-chart", templateUrl: "./sub-annotations-line-chart.component.html", styleUrls: ["./sub-annotations-line-chart.component.scss"], }) export class SubAnnotationsLineChartComponent implements OnInit { @ViewChild("print1", { static: false }) print1Component: ENgxPrintComponent; constructor( private commomService: CommomService, private chartOptionService: ChartOptionService, private chartMenuService: ChartMenuService ) {} ngOnInit() { this.initEchart(this.commomService.changeFontSize()); fromEvent(window, "resize") .pipe(debounceTime(200)) .subscribe(() => { let style = this.commomService.changeFontSize(); this.optionInit.setOption({ legend: { itemWidth: style.fontSize, itemHeight: style.fontSize, }, toolbox: { itemSize: style.toolboxSize, }, grid: style.interval, }); }); } optionInit; onOptionInit(ec) { this.optionInit = ec; } echartOption; // 图名 chartName = "噪声变化趋势"; // echart配置项 initEchart(style) { // 获取模拟数据 let leqDatas = this.makeData(1000, 50.8); let lmaxDatas = this.makeData(1000, 53.8); let simulateData = [leqDatas.datas, lmaxDatas.datas]; let markPoints = [leqDatas.markPoints, lmaxDatas.markPoints]; style.interval.right = "30"; //参数传递 let params = { customGrid: style.interval, style: style, unit: "dB", xAxisName: "", yAxisName: "noise level(dB)", yNameGap: 30, datas: simulateData, seriesNames: ["Leq"], markPoints: markPoints, }; this.echartOption = this.chartOptionService.getOptionAnnotationLineChart(params); } closePanel(event) { event.fromElement.setAttribute("style", "display:none"); } downloadImage(imageType: string, fileName: string) { this.chartMenuService.downloadEchartImage( this.optionInit, fileName, imageType ); } printComplete() {} customPrint() { this.chartMenuService.customPrint(this.optionInit, this.print1Component); } fullScreen(event) { this.chartMenuService.fullScreen( event.srcElement.parentNode.parentNode.parentNode.parentNode ); } private makeData(num, level) { let datas = []; let markPoints = []; let a, b; let dateTime = new Date("2020-10-01 00:00:00"); for (let i = 0; i < num; i++) { if (i % 200 === 0) { a = 2 * Math.random(); } if (i % 300 === 0) { b = 2 * Math.random(); } dateTime = addMilliseconds(dateTime, 1000); let value = ( level + (3 * i) / 200 + 2 * Math.sin(i / 50) + a + b + Math.random() ).toFixed(2); datas.push([format(dateTime, "yyyy-MM-dd HH:mm:ss"), value]); if (i % 200 == 0) { markPoints.push({ name: "event" + (i + 1), coord: [format(dateTime, "yyyy-MM-dd HH:mm:ss"), value], }); } } return { datas: datas, markPoints: markPoints, }; } }