Javascript 向SavetoFile生成的图像添加时间戳
您好,我正在查看LightningChartJs的chartXY文档,似乎找不到向saveToFile保存的图像添加时间戳的方法。 提前感谢。LightningChart JS不支持添加时间戳 您可以通过实施自己的储蓄来实现这一点 执行此操作的步骤是:Javascript 向SavetoFile生成的图像添加时间戳,javascript,lightningchart,Javascript,Lightningchart,您好,我正在查看LightningChartJs的chartXY文档,似乎找不到向saveToFile保存的图像添加时间戳的方法。 提前感谢。LightningChart JS不支持添加时间戳 您可以通过实施自己的储蓄来实现这一点 执行此操作的步骤是: 获取对运行JS的canvas LightningChart的引用 const chartCanvas=chart.engine.container.querySelector('canvas')) 使用将画布内容转换为数据url const s
const chartCanvas=chart.engine.container.querySelector('canvas'))
const sc=chartCanvas.toDataURL('image/png'))
const secondaryCanvas=document.createElement('canvas')
const ctx=secondaryCanvas.getContext('2d')
常量img=新图像()
img.src=sc
img.onload=()=>{
//将屏幕截图加载到另一个画布
ctx.canvas.width=宽度
ctx.canvas.height=高度
ctx.drawImage(img,0,0)
}
const timeNow=new Date().toISOString()
ctx.fillStyle='#fff'
ctx.fillText(timeNow,0,高度)
const timestamp=ctx.canvas.toDataURL('image/png'))
常量文件名='chart.png'
常量a=window.document.createElement('a')
window.document.body.appendChild(a)
常量url=时间戳
a、 href=url
a、 下载=文件名
a、 单击()
请参见下面的一个工作示例,单击图表中心的按钮时会存储一个屏幕截图
const{
光照图,
UIElementBuilder
}=lcjs
常量图表=lightningChart().ChartXY()
const secondaryCanvas=document.createElement('canvas')
const ctx=secondaryCanvas.getContext('2d')
const chartCanvas=chart.engine.container.querySelector('canvas')
document.body.appendChild(secondaryCanvas)
const scButton=chart.addUIElement(UIElementBuilders.ButtonBox)
scButton.setText('拍摄带有时间戳的屏幕截图')
设置位置({x:50,y:50})
scButton.onMouseClick(()=>{
常量宽度=chartCanvas.clientWidth
常量高度=chartCanvas.clientHeight
//画布截图
const sc=chartCanvas.toDataURL('image/png')
常量img=新图像()
img.src=sc
img.onload=()=>{
//将屏幕截图加载到另一个画布
ctx.canvas.width=宽度
ctx.canvas.height=高度
ctx.drawImage(img,0,0)
//添加时间戳
const timeNow=new Date().toISOString()
ctx.fillStyle='#fff'
ctx.fillText(timeNow,0,高度)
//保存到文件
const timestamp=ctx.canvas.toDataURL('image/png')
常量文件名='chart.png'
常量a=window.document.createElement('a')
window.document.body.appendChild(a)
常量url=时间戳
a、 href=url
a、 下载=文件名
a、 单击()
}
})
我没有得到图表,我只是看到了时间戳,上面的例子在它下面做了一个副本,我只想下载它。