Toggle navigation
背景图测试
By
決***0
2021-02-23 02:47:55
脚本
16
21
作品使用的第三方脚本
数据管理
上传数据
支持小于 5M 任意格式(csv, xlsx, json, xml, ...)的数据文件
上传后可以通过生成的文件链接异步获取托管的数据。
历史数据
0 条
无历史数据
代码修改记录
信息提示
保存作品
对当前截图不满意?你还可以
上传本地截图
重新截图
作品名称
作品描述
标签
geo
grid
legend
markLine
markPoint
bar
effectScatter
line
lines
map
timeline
title
toolbox
tooltip
visualMap
作品默认版本
最新
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
20:11:45
图表已生成
整理代码
刷新
代码
var uploadedDataURL = "/asset/get/s/data-1614828284945-i_KeAfQtX.png"; var fullImage = new Image(); let waterLevel = 68; //水位 let sensorX = 491; //传感器在原图中的横向位置 const calcMargin = (height, width, margin = 0) => { //边留空白计算,margin要与grid设置一致 let mHeight = height * margin, mWidth = width * margin, outerWidth = width * window.devicePixelRatio, outerHeight = height * window.devicePixelRatio, innerWidth = (width - 2 * mWidth) * window.devicePixelRatio, innerHeight = (height - 2 * mHeight) * window.devicePixelRatio; return { outerWidth, outerHeight, innerHeight, innerWidth, marginHeight: mHeight * window.devicePixelRatio, marginWidth: mWidth * window.devicePixelRatio }; }; const marginObj = calcMargin(myChart.getHeight(), myChart.getWidth(), 0.1); console.log(marginObj); const getImage = (url, { outerWidth, outerHeight, innerHeight, innerWidth, marginHeight, marginWidth }) => { //绘制背景图片 let img = new Image(); let canvas = document.createElement('canvas'); let ctx = canvas.getContext('2d'); canvas.width = outerWidth; canvas.height = outerHeight; return new Promise((resolve, reject) => { img.onload = function() { ctx.drawImage(img, 0, 0, img.width, img.height, marginWidth, marginHeight, innerWidth, innerHeight ); resolve({ src: canvas.toDataURL(), width: img.width, height: img.height }); }; img.src = url; }); }; const setData = (xs, ys, xScale, yScale) => { let nXs = xs.map(x => Math.round(xScale * x)); let nYs = ys.map(y => Math.round(yScale * y)) let res = nXs.map((x, i) => { xValue = x; yValue = ys[i] || null; return { value: [xValue, yValue], }; }); return res; }; const getIntersectPoint = (xScale, yScale, yScaleWater) => { let ctx = myChart._dom.querySelector('canvas[data-zr-dom-id="zr_1"]').getContext('2d'); const { outerWidth, outerHeight, innerHeight, innerWidth, marginHeight, marginWidth } = marginObj; let x = xScale * sensorX + marginWidth, y = marginHeight + yScaleWater, width = innerWidth - xScale * sensorX; let { data: arrs } = ctx.getImageData(x, y, width, 1); // debugger let colors = []; arrs = arrs.reverse(); for (let i = 0; i < arrs.length; i) { let temp = arrs.slice(i, i += 4); let prev = i > 0 ? colors[colors.length - 1] : null; if (prev && (prev.join('') !== temp.join(''))) { break; } colors.push(temp); } console.log(arrs); console.log(colors); return colors.length; } option = { title: { text: 'Awesome Chart' }, grid: [{ show: true, left: '10%', bottom: '10%', top: '10%', zlevel: 1, borderWidth: 0, backgroundColor: { image: fullImage, // 支持为 HTMLImageElement, HTMLCanvasElement,不支持路径字符串 repeat: 'no-repeat' // 是否平铺,可以是 'repeat-x', 'repeat-y', 'no-repeat' }, }], xAxis: { show: false, type: "value", //开启x轴坐标 min: 0, max: 1200, axisPointer: { show: false }, zlevel: 1, }, yAxis: { show: true, type: "value", min: 0, max: 1200, //开启y轴坐标 axisPointer: { show: false }, zlevel: 1, }, series: [{ zlevel: 1, type: 'line', smooth: true, symbolSize: 15, data: [], symbol: 'circle', itemStyle: { color: 'red' }, label: { show: true, position: 'top' }, }, { zlevel: 0, type: 'line', symbolSize: 0, "color": "#2dc3b5", "areaStyle": { "color": "#2dc3b5", // "opacity":1 }, }] }; getImage(uploadedDataURL, marginObj) .then(({ src, width, height }) => { const { outerWidth, outerHeight, innerHeight, innerWidth, marginHeight, marginWidth } = marginObj; let yMax = 84, yMin = 45.56, xScale = innerWidth / width, yScale = innerHeight / height, yScaleWater = innerHeight * (yMax - waterLevel) / (yMax - yMin); fullImage.src = src; option.xAxis.max = innerWidth; option.yAxis.max = yMax; option.yAxis.min = yMin; let waterY = waterLevel; option.series[1]['data'] = [ [0, waterY], [innerWidth, waterY] ]; myChart.setOption(option); myChart.resize(); setTimeout(() => { let x = getIntersectPoint(xScale, yScale, yScaleWater); let data = setData([sensorX, (innerWidth - x - 5) / xScale], [64, waterLevel], xScale, yScale); option.series[0]['data'] = data; myChart.setOption(option); console.log(data); // console.log(myChart._dom.querySelector('canvas[data-zr-dom-id="zr_1"]')); }, 1000); });