使用ChartJS在条形图中进行分页 [英] Pagination in Bar chart using ChartJS
问题描述
我正在使用chartJS使用条形图显示一些数据. chartJ存在一些问题.当我必须提交大量数据(例如100个或更多数据字段)并且标签具有持久标签时,它看起来就很难看.
I am using chartJS for display some data with Bar Chart. there are some issues with chartJs. When i have to much data-filed for example 100 or more data-field and it have long lasting label its looks like ugly.
如何配置条形图?
这是我的条形图屏幕截图
here is my bar chart screenshot
这是我的字段
在上面的示例中,我有100个数据字段,我可以对这个条形图进行分页吗?
in above example i have 100 data-filed, can i paginate this bar-chart ?
这是我的代码
var lbl = [];
var dt = [];
for(var i = 1;i<=100;i++){
lbl.push("this_is_my_lable_name_"+i);
}
for(var i = 1;i<=100;i++){
dt.push(Math.floor((Math.random() * 100) + 1));
}
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: lbl,
datasets: [{
label: '# of Votes',
data: dt,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
推荐答案
您可以尝试以下解决方案来考虑图表滚动:
You can try this solution to consider the chart scroll like this:
为y轴克隆另一个画布,以便在滚动时只有条会出现移动
Clone a different canvas for y axis so that when you scroll then only bars will appear to move
为y轴添加画布
<canvas id="myChartAxis" height="600" width="0"></canvas>
现在克隆轴
var sourceCanvas = myLiveChart.chart.canvas;
var copyWidth = myLiveChart.scales['y-axis-0'].width - 10;
var copyHeight = myLiveChart.scales['y-axis-0'].height + myLiveChart.scales['y-axis-0'].top + 10;
var targetCtx = document.getElementById("myChartAxis").getContext("2d");
targetCtx.canvas.width = copyWidth;
targetCtx.drawImage(sourceCanvas, 0, 0, copyWidth, copyHeight, 0, 0, copyWidth, copyHeight);
有关演示的信息,请参见 FIDDLE
For a Demo see this FIDDLE
为获得更好的可视化效果,最好避免在x轴上标注标签,并通过工具提示直观地显示此信息
For better visualization its better to avoid labels in x axis and show this info intuitive via tool tip anyhow
这篇关于使用ChartJS在条形图中进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!