如何修复图表js中的模糊图表问题?

Add*_*ddy 6 javascript php jquery charts bar-chart

我正在使用chart.js创建条形图.但是这张图表在我的屏幕上看起来很模糊.下面是我的html和js代码:

<canvas id="myChart" style="padding-left: 0;padding-right: 0;margin-left: auto; margin-right: auto; display: block;width: 90%;height:350px;"></canvas>
Run Code Online (Sandbox Code Playgroud)

用于创建图表栏的Js代码:

window.onload = function () {       
var data = {
labels: [],
datasets: [
    {
        label: "My First dataset",
        fillColor: "rgba(220,220,220,2)",
        strokeColor: "rgba(220,220,220,2)",
        pointColor: "rgba(220,220,220,2)",
        pointStrokeColor: "#fff",
        pointHighlightFill: "#fff",
        pointHighlightStroke: "rgba(220,220,220,2)"
    },
    {
        label: "My Second dataset",
        fillColor: "rgba(12, 18, 51, 1)",
        strokeColor: "rgba(12, 18, 51, 1)",
        pointColor: "rgba(12, 18, 51, 1)",
        pointStrokeColor: "#fff",
        pointHighlightFill: "#fff",
        pointHighlightStroke: "rgba(12, 18, 51, 1)"
    }
]
};

var ctx = jQuery("#myChart")[0].getContext('2d');
var options = {   
scaleBeginAtZero : true,   
scaleShowGridLines : true,    
scaleGridLineColor : "rgba(0,0,0,.05)",  
scaleGridLineWidth : 1,    
scaleShowHorizontalLines: true,   
scaleShowVerticalLines: false,   
barShowStroke : true,
barStrokeWidth : 2,   
barValueSpacing : 10,    
barDatasetSpacing : 1,

legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].fillColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"

}
var myLineChart = new Chart(ctx).Bar(data, options);
<?php foreach($resultGraph as $share){?>

myLineChart.addData([<?php echo $share->shCOunt;?>, <?php echo $share->tt;?>], "<?php echo $share->post_date1;?>"); 
<?php } ?>

//myLineChart.addData([30, 50], "January");

}   

</script>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

小智 15

2022年的解决方案:

将其添加到您的 ChartJS 选项中,它就像魔术一样工作。

devicePixelRatio: 4
Run Code Online (Sandbox Code Playgroud)

  • 荒谬的是,这有效。这/s是什么魔法?多谢 (3认同)

Sai*_*LHI 7

确保您没有向 canvas 元素添加一些 css。就我而言,我发现我向画布元素添加了边框属性,这是导致文本和条形模糊问题的原因。

不要使用类似的东西:

canvas { border: 1px solid #000 }
Run Code Online (Sandbox Code Playgroud)

或在您的示例中使用 id :

#myChart { border: 1px solid #000 }
Run Code Online (Sandbox Code Playgroud)


1cg*_*nza 1

尝试将 0.5 添加到 x 坐标值。请参阅此处的解释