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

标签 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>

创建图表栏的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>

enter image description here

最佳答案

确保您没有向 canvas 元素添加一些 css。 在我的例子中,我发现我正在向 canvas 元素添加边框属性,这是导致文本和栏模糊问题的原因。

不要使用类似的东西:

canvas { border: 1px solid #000 }

或像您的示例中那样使用 id :

#myChart { border: 1px solid #000 }

关于javascript - 如何修复图表 js 中的模糊图表问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33385398/

相关文章:

javascript - 在浏览器窗口/选项卡关闭上打开自定义弹出窗口

javascript - 适用于 google chrome,但不适用于 firefox 和 IE

javascript - 通过 jQuery cookie 脚本删除 cookie

jquery - 多个div拖动

javascript - jquery选择器:button vs button

javascript - React应用程序中自动脚本将js转换为jsx

php - 未知错误(in_array() 期望参数 2 为数组,给定字符串)

php - 如何逐步设置和使用 phpDocumentor

javascript - JavaScript中带括号和不带括号()的调用函数有什么区别

php - Laravel 中 morphs 列类型有什么用?