【发布时间】:2022-01-06 07:01:07
【问题描述】:
这是我在 bootstrap 和 chartjs 框架内构建的 Chartjs 画布。问题是我无法在移动设备等小屏幕上下载全宽图像。
---- 引导是必备的
--- 注意:如果您将示例中的屏幕宽度设置为小,然后单击下载,则输出将是该尺寸且小。
HTML:
<div class="card">
<div class="card-body">
<h4 class="card-title mb-0"> Average </h4>
<a data-name="avg-chart" id="downloadavg" download="searchtrends-average-position-chart.jpg" href="" class="download">
<button class="btn btn-primary" type="button">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="white" class=" bi bi-cloud-arrow-down-fill" viewBox="0 0 16 16">
<path d="M8 2a5.53 5.53 0 0 0-3.594 1.342c-.766.66-1.321 1.52-1.464 2.383C1.266 6.095 0 7.555 0 9.318 0 11.366 1.708 13 3.781 13h8.906C14.502 13 16 11.57 16 9.773c0-1.636-1.242-2.969-2.834-3.194C12.923 3.999 10.69 2 8 2zm2.354 6.854-2 2a.5.5 0 0 1-.708 0l-2-2a.5.5 0 1 1 .708-.708L7.5 9.293V5.5a.5.5 0 0 1 1 0v3.793l1.146-1.147a.5.5 0 0 1 .708.708z" />
</svg>
</button>
</a>
<div class="c-chart-wrapper" id="avg-chart_parent">
<canvas class="chart" id="avg-chart" style="height:300px;width:100%" height="300"></canvas>
</div>
</div>
</div>
JS:
new Chart(document.getElementById("avg-chart"),{"type":"line",
"data":{"labels":["2020.11.1","2020.11.2","2020.11.3","2020.11.4","2020.11.5","2020.11.6","2020.11.7","2020.11.7","2020.11.9","2020.11.10","2020.11.11","2020.11.12","2020.11.13","2020.11.14","2020.11.15","2020.11.16","2020.11.17","2020.11.18","2020.11.19","2020.11.20","2020.11.21","2020.11.22","2020.11.23","2020.11.24","2020.11.25","2020.11.26","2020.11.27","2020.11.28","2020.11.29","2020.11.30",],
"datasets":[{"label":"Average Position ",
"data":["11","12","13","14","15","16","17","18","19","19","19","19","19","19","19","19","19","19","19","20","21","22","23","24","25","26","27","28","29","30",],
"fill":false,"borderColor":"rgb(37,154,71)","lineTension":0.1}]}, options: {
scales: {
yAxes: [{
ticks: {
reverse: true,
}
}],
xAxes: [{
type: 'time',
distribution: 'series',
time: {
tooltipFormat:'MM/DD/YYYY',
}
}]
},
legend: {
display: false,
maxWidth: 9999
}
}});
function downloadpng(chartname,chartid){
var canvas = document.getElementById(chartname);
var url_base64jp =canvas.toDataURL("image/jpg");
var a = document.getElementById(chartid);
a.href = url_base64jp;
}
//Download Chart Image
$('.download').on( 'click', function (event) {
var chartname = $(this).attr('data-name');
var chartid = $(this).attr('id');
downloadpng(chartname,chartid);
});
我在这里做了一个例子:
http://jsfiddle.net/aneeshtan/vu27aew3/49/
如您所见,我无法将全尺寸画布 (Chartjs) 保存在小屏幕中并自动更改比例。
我怎样才能下载完整尺寸的 chartjs 画布。
【问题讨论】:
-
定义“全尺寸”。
-
像显示所有图例标签的桌面版本一样。最小尺寸 1200 像素?
-
或在其他定义中显示 xAxes 中的图例标签的完整列表。
-
所以当我们把屏幕变小(移动版)时,输出会很小,但输出应该是全尺寸版本,就像桌面输出一样。
-
你不需要向我解释这个问题。我只是不明白你为什么要在
1440px宽显示器上显示1440px宽图像和在4k 显示器上显示3840px图表。无论使用什么设备,您在下载时不想要图表图像的标准尺寸吗?如果你这样做,那标准是什么? “全宽” 是您现在在手机上获得的。显然,你不想那样。
标签: javascript html twitter-bootstrap chart.js