2023-07-04 19:46:38 +08:00
|
|
|
/**
|
|
|
|
|
* 根据位置获取这个点在图表的哪个轴线上
|
|
|
|
|
* @param offsetX
|
|
|
|
|
* @param offsetY
|
|
|
|
|
*/
|
|
|
|
|
export function getXAxisAndYAxisByPosition(chart, offsetX, offsetY, seriesIndex = 0) {
|
|
|
|
|
const pointInPixel = [offsetX, offsetY]
|
|
|
|
|
if (
|
|
|
|
|
chart.containPixel(
|
|
|
|
|
{
|
|
|
|
|
seriesIndex: 0
|
|
|
|
|
},
|
|
|
|
|
pointInPixel
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
const [xAxis, yAxis] = chart.convertFromPixel({ seriesIndex }, pointInPixel)
|
|
|
|
|
return [xAxis, yAxis]
|
|
|
|
|
}
|
|
|
|
|
return null
|
2023-07-26 19:35:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将图表导出为图片
|
|
|
|
|
* @param {import("echarts").ECharts} chartInstance
|
|
|
|
|
* @param {'png' | 'jpeg' | 'svg'} type
|
|
|
|
|
*/
|
2023-07-31 19:23:25 +08:00
|
|
|
export function exportEchartImg(chartInstance, type = 'png', backgroundColor = '#022024') {
|
2023-07-26 19:35:36 +08:00
|
|
|
const dataURL = chartInstance.getDataURL({
|
|
|
|
|
type,
|
|
|
|
|
pixelRatio: 2,
|
2023-07-31 19:23:25 +08:00
|
|
|
backgroundColor
|
2023-07-26 19:35:36 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const link = document.createElement('a')
|
|
|
|
|
link.style.display = 'none'
|
|
|
|
|
link.href = dataURL
|
|
|
|
|
link.setAttribute('download', 'export.png')
|
|
|
|
|
document.body.appendChild(link)
|
|
|
|
|
link.click()
|
|
|
|
|
document.body.removeChild(link) //下载完成移除元素
|
|
|
|
|
|
2023-08-08 19:11:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 按份数分割轴线
|
|
|
|
|
* @param {Number} max
|
|
|
|
|
* @param {Number} min
|
|
|
|
|
* @param {Number} cnt
|
|
|
|
|
* @param {Number} maxBoundaryGap
|
|
|
|
|
*/
|
|
|
|
|
export function splitAxis(max, min, cnt, maxBoundaryGap = 1.1) {
|
|
|
|
|
const _min = Math.floor(min)
|
|
|
|
|
const interval = Math.ceil((Math.ceil(max * maxBoundaryGap) - _min) / cnt)
|
|
|
|
|
const _max = _min + interval * cnt
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
min: _min,
|
|
|
|
|
interval,
|
|
|
|
|
max: _max
|
|
|
|
|
}
|
2023-07-04 19:46:38 +08:00
|
|
|
}
|