前端折线图中背景,Chart.js折线图设置背景颜色
I'm working with Chart.js and want to convert a line chart to a PNG. The problem is that the image always downloads with a transparent background, which is not what I need. I tried many options, nothing really worked.
And suggestions?
解决方案
Here's one way to get a fully-opaque version of your ChartJS:
Wait until the chart is fully animated out and complete. You can do this by adding the onAnimationComplete property to the chart.
In the onAnimationComplete function:
Create an in-memory temporary canvas of equal size as your chart.
Fill the temp canvas with white
drawImage the ChartJS canvas over the white-filled temp canvas
Create an image from the temp canvas.
Here's how that might be done:
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx).Line(lineChartData, {
responsive: true,
onAnimationComplete:function(){
var tcanvas=document.createElement('canvas');
var tctx=tcanvas.getContext('2d');
tcanvas.width=ctx.canvas.width;
tcanvas.height=ctx.canvas.height;
tctx.fillStyle='white';
tctx.fillRect(0,0,tcanvas.width,tcanvas.height);
tctx.drawImage(canvas,0,0);
var img=new Image();
img.οnlοad=function(){
document.body.appendChild(img);
}
img.src=tcanvas.toDataURL();
}
});
Here's example code and a Demo:
var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
var randomColorFactor = function(){ return Math.round(Math.random()*255)};
var lineChartData = {
labels : ["January","February","March","April","May","June","July"],
datasets : [
{
label: "My First dataset",
fillColor : "rgba(220,220,220,0.2)",
strokeColor : "rgba(220,220,220,1)",
pointColor : "rgba(220,220,220,1)",
pointStrokeColor : "#fff",
pointHighlightFill : "#fff",
pointHighlightStroke : "rgba(220,220,220,1)",
data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
},
{
label: "My Second dataset",
fillColor : "rgba(151,187,205,0.2)",
strokeColor : "rgba(151,187,205,1)",
pointColor : "rgba(151,187,205,1)",
pointStrokeColor : "#fff",
pointHighlightFill : "#fff",
pointHighlightStroke : "rgba(151,187,205,1)",
data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
}
]
}
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx).Line(lineChartData, {
responsive: true,
onAnimationComplete:function(){
var tcanvas=document.createElement('canvas');
var tctx=tcanvas.getContext('2d');
tcanvas.width=ctx.canvas.width;
tcanvas.height=ctx.canvas.height;
tctx.fillStyle='white';
tctx.fillRect(0,0,tcanvas.width,tcanvas.height);
tctx.drawImage(canvas,0,0);
var img=new Image();
img.οnlοad=function(){
document.body.appendChild(img);
}
img.src=tcanvas.toDataURL();
}
});
ChartJS line chart
Fully opaque chart as image
前端折线图中背景,Chart.js折线图设置背景颜色相关推荐
- 如何在 Vue 中使用 Chart.js - 手把手教你搭可视化数据图表
本文首发:<如何在 Vue 中使用 Chart.js - 手把手教你搭可视化数据图表> 使用 Chart.js 在 Vue 搭建的后台管理工具里添加炫酷的图表,是所有数据展示类后台必备的功 ...
- R语言使用ggradar包可视化基本雷达图(radar chart、蜘蛛图spider plot)、可视化单个数据对象的雷达图
R语言使用ggradar包可视化基本雷达图(radar chart.蜘蛛图spider plot).可视化单个数据对象的雷达图 目录
- R语言使用ggplot2可视化凹凸图(bumps chart、凹凸图是一种特殊形式的线图,旨在探索随着时间的推移等级的变化)、并设置凹凸图的线条为曲线而不是直线(change into curves)
R语言使用ggplot2可视化凹凸图(bumps chart.凹凸图是一种特殊形式的线图,旨在探索随着时间的推移等级的变化).并设置凹凸图的线条为曲线而不是直线(change bumps chart ...
- R语言使用ggradar包可视化基本雷达图(radar chart、蜘蛛图spider plot)、可视化单个数据对象的雷达图、自定义雷达图的线条类型、线条宽度、数据点大小、色彩等
R语言使用ggradar包可视化基本雷达图(radar chart.蜘蛛图spider plot).可视化单个数据对象的雷达图.自定义雷达图的线条类型.线条宽度.数据点大小.色彩等(Customize ...
- R语言使用fmsb包、gradar包可视化雷达图(radar chart、蜘蛛图spider plot)、ggpubr包可视化点图、GGally包可视化多变量的平行坐标轴图
R语言使用fmsb包.gradar包可视化雷达图(radar chart.蜘蛛图spider plot).ggpubr包可视化点图.GGally包可视化多变量的平行坐标轴图 目录
- R语言使用fmsb包可视化基本雷达图(radar chart、蜘蛛图spider plot)、数据预处理(计算每个变量的最大值和最小值、并合并到原数据集)、可视化单个数据对象的雷达图
R语言使用fmsb包可视化基本雷达图(radar chart.蜘蛛图spider plot).数据预处理(计算每个变量的最大值和最小值.并合并到原数据集).可视化单个数据对象的雷达图 目录
- 原生html js css绘制折现图,HTML5教程 Chart.js自定义提示折线图
本篇教程探讨了HTML5教程 Chart.js自定义提示折线图,希望阅读本篇文章以后大家有所收获,帮助大家HTML5+CSS3从入门到精通 . < var randomScalingFactor ...
- java uml图中的关系_UML类图中的关系
UML类图中的关系分为四种:泛化.依赖.关联.实现:关联关系又可以细化为聚合和组合. 1. 泛化(Generalization) 泛化是父类和子类之间的关系,子类继承父类的所有结构和行为.在子类中可以 ...
- 弧形背景html,JS实现带圆弧背景渐变效果的导航菜单代码
本文实例讲述了JS实现带圆弧背景渐变效果的导航菜单代码.分享给大家供大家参考.具体如下: 这是一款效果个性的JS+CSS导航菜单,鼠标经过时出现有趣弧形背景,实际上,这里用CSS调用了背景,用Java ...
最新文章
- AMD与intel CPU型号大全(接口)
- 每日一皮:两个实习生的结对编程...
- ftp shell lcd不起作用
- es创建索引库,无法使用InetSocketTransportAddress
- 2006的最后一篇之学车大结局
- 初识kbmmw 中的ORM
- 【JAVA SE】第六章 面向对象、对象和类以及封装
- linux moveto回收站,linux添加回收站(脚本)
- 服务器关于数据流转方式
- RabbitMq学习笔记005---登录rabbitmq报错User can only log in via localhost
- 不可阻挡的PowerShell :Red Teamer告诉你如何突破简单的AppLocker策略
- OpenCV imread读取图片,imshow展示图片,出现cv:Exception at memory location异常
- combineByKey实例详解
- PHP统计订单表,订单售后表金额
- 计算机制作ppt教程,电脑制作ppt详细步骤_新手制作ppt详细步骤图文教程
- bandicam的延迟问题和画质问题
- 按键精灵执行cmd命令_学会用按键精灵制作脚本:用脚本运行可执行程序
- Python练习题——第六题:编写函数计算弧长的计算公式。弧长计算公式是一个数学公式,为L=n(圆心角度数)× π×2 r(半径)/360(角度制)。其中n是圆心角度数,r是半径,L是圆心角弧长。
- 3DMAX渲染时,vray渲染有光斑、噪点?几招就解决。
- 最佳实践:银尔达YED-S724 网红4G DTU 接入 ThingsKit 物联网平台
热门文章
- 蓝桥杯 ALGO-71 算法训练 比较字符串
- 1076. Wifi密码 (15)-PAT乙级真题
- 【软件测试】如何用场景法进行黑盒测试
- Oracle自制数据仓库,自治数据库:Oracle 的自治数据仓库云-ADWC体验
- mysql主库从库在同一台服务器_mysql数据库从一台服务器迁移到另一台服务器上...
- 数据仓库专题19-数据建模语言Information Engineering - IE模型(转载)
- DIV+CSS如何让文字垂直居中
- 关于挽救爱情的一次求学尝试
- 初学linux网络服务之HTTP服务实验
- IP地址基础和子网规划之其一