diff --git a/src/App.vue b/src/App.vue index a47c6fec005556bfd9d02bac25d055b1be024d79..112aacc1d9341bd8d4da868059b01a12bffc1990 100644 --- a/src/App.vue +++ b/src/App.vue @@ -2,11 +2,12 @@ @@ -54,16 +72,16 @@ const shotAction = () => {
- +
diff --git a/src/components/visualPerformance/index.vue b/src/components/visualPerformance/index.vue index 8e5c4a7dd5dc4cefbe6a2ca2e69a7ad0ed011009..ea7198f2b3b8e40956401d341f2a548703f0dfc9 100644 --- a/src/components/visualPerformance/index.vue +++ b/src/components/visualPerformance/index.vue @@ -2,15 +2,82 @@ import { reactive, onMounted } from 'vue'; import * as echarts from 'echarts'; const state = reactive({ - title: '性能指标可视化', + leftTitle: '原生的performance', + leftDomId: 'visual-performance-id', + + rightTitle: '性能指标可视化', + rightDomId: 'visual-performance-id-right', + chartTitle: '性能指标', echartInstance: null, - domId: 'visual-performance-id' + }) -const initChart = () => { +const initLeftChart = () => { + // 基于准备好的dom,初始化echarts实例 + const domInstance = document.getElementById(state.leftDomId) + if (domInstance) { + domInstance.removeAttribute('_echarts_instance_') + } + else { + return + } + console.log(performance) + console.log(Object.keys(performance.timing)) + const label = [] + const data = [] + for (let key in performance.timing) { + label.push(key) + data.push(performance.timing[key]) + } + const myChart = echarts.init(domInstance); + const option = { + title: { + text: 'performance' + }, + tooltip: { + trigger: 'axis', + axisPointer: { + type: 'shadow' + } + }, + legend: {}, + grid: { + left: '3%', + right: '4%', + bottom: '3%', + containLabel: true + }, + xAxis: { + type: 'value', + boundaryGap: [0, 0.01] + }, + yAxis: { + type: 'category', + data: label + }, + series: [ + { + name: 'performance', + type: 'bar', + data: data + } + ] + }; + console.log('option', option) + // 使用刚指定的配置项和数据显示图表。 + myChart.setOption(option, true); + // 监听 + state.echartInstance = myChart; + myChart.on('click', function (params) { + console.log('params', params) + }); + window.onresize = myChart.resize; +} + +const initRightChart = () => { // 基于准备好的dom,初始化echarts实例 - const domInstance = document.getElementById(state.domId) + const domInstance = document.getElementById(state.rightDomId) if (domInstance) { domInstance.removeAttribute('_echarts_instance_') } @@ -20,7 +87,7 @@ const initChart = () => { const myChart = echarts.init(domInstance); const option = { title: { - text: 'World Population' + text: state.chartTitle }, tooltip: { trigger: 'axis', @@ -62,18 +129,28 @@ const initChart = () => { window.onresize = myChart.resize; } + onMounted(() => { - initChart() + initLeftChart() + initRightChart() }) \ No newline at end of file diff --git a/src/utils/index.js b/src/utils/index.js index 87948abf7aed6b5032b97e10abf6cafe39ecf846..c4020d729009751476ac5559152c99ea570878c0 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -10,9 +10,10 @@ class ListenDomChange { this.dom = dom; this.callbackDomChange = callbackDomChange; // 初始化为{} - this.performanceConfig={} + this.performanceConfig = {} // 计算performanceConfig this.calcPerformance() + this.mutationListen() } // 监听 dom变化 mutationListen() { @@ -24,6 +25,7 @@ class ListenDomChange { // 当观察到变动时执行的回调函数 const callback = function (mutationsList, observer) { + console.log('change_______________', mutationsList) // Use traditional 'for loops' for IE 11 for (let mutation of mutationsList) { if (mutation.type === "childList") { @@ -32,25 +34,25 @@ class ListenDomChange { console.log("The " + mutation.attributeName + " attribute was modified."); } } - console.log('change', change) + }; // 创建一个观察器实例并传入回调函数 this.observer = new MutationObserver(callback); // 以上述配置开始观察目标节点 - this.observer.observer.observe(targetNode, config); + this.observer.observe(targetNode, config); // 之后,可停止观察 - this.observer.observer.disconnect(); + this.observer.disconnect(); } - getPerformance(){ - return performance + getPerformance() { + return this.performanceConfig } - setPerformance(key,value){ - this.calcPerformance[key]=value + setPerformance(key, value) { + this.performanceConfig[key] = value } calcPerformance() { @@ -70,19 +72,21 @@ class ListenDomChange { console.log( "time to first-contentful-paint: " + (firstContentfulPaint - activationStart), ); - setPerformance('time to first paint',firstPaint - activationStart) - setPerformance('time to first-contentful-paint',firstContentfulPaint - activationStart) + this.setPerformance('time to first paint', firstPaint - activationStart) + this.setPerformance('time to first-contentful-paint', firstContentfulPaint - activationStart) const entries = performance.getEntriesByType("navigation"); + const that = this entries.forEach((entry) => { console.log(`${entry.name}: domComplete time: ${entry.domComplete}ms`); + that.setPerformance('domComplete time', entry.domComplete) const domContentLoadedTime = entry.domContentLoadedEventEnd - entry.domContentLoadedEventStart; console.log( `${entry.name}: DOMContentLoaded processing time: ${domContentLoadedTime}ms`, ); - setPerformance(entry.name,domContentLoadedTime) + that.setPerformance(entry.name, domContentLoadedTime) }); } }