【问题标题】:Chart.JS - show values on top of pointsChart.JS - 在点上显示值
【发布时间】:2017-07-11 15:07:11
【问题描述】:

晚上好,

我正在尝试构建一个表示 API 响应时间的折线图。问题是我在 Chart.JS 文档中没有找到任何解决方案。是否有任何本机解决方案或任何使用 canvas api 的解决方案?

我想让图表看起来像这样:

这是我用来生成图表的代码

<script>
    var ctx = document.getElementById("myChart");
    var myChart = new Chart(ctx, {
        type: 'line',
        data: {
            labels: hoursArrFirst,

            datasets: [{
                label: 'First Brand API',
                data: timeArrProftit,
                borderWidth: 1,
                backgroundColor: [
                    'rgba(255, 99, 132, 0.05)',
                    'rgba(255, 159, 64, 0.05)'
                ],
                borderColor: [
                    'rgba(255,99,132,1)',
                    'rgba(255, 59, 64, 1)'
                ]
            },{
                label: 'Second Brand API',
                data: timeArrSecond,
                borderWidth: 1,
                backgroundColor: [
                    'rgba(132, 255, 99, 0.05)',
                    'rgba(64, 255, 159, 0.05)'
                ],
                borderColor: [
                    'rgba(32,155,99,1)',
                    'rgba(64,155, 59, 1)'
                ]
            },{
                label: 'Third Brand API' ,
                data: timeArrThird,
                borderWidth: 1,
                backgroundColor: [
                    'rgba(32, 99, 255, 0.05)',
                    'rgba(64, 59, 255, 0.05)'
                ],
                borderColor: [
                    'rgba(32, 99, 120, 1)',
                    'rgba(64, 59, 120, 1)'
                ]
            }]


        },
        options: {
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero:true
                    }
                }]
            }
        }
    });


</script>

【问题讨论】:

    标签: javascript jquery canvas charts chart.js


    【解决方案1】:

    在动画期间和之后调用函数

    var options = {
        onAnimationProgress: function() { drawDatasetPointsLabels() },
        onAnimationComplete: function() { drawDatasetPointsLabels() }
    }
    

    function drawDatasetPointsLabels() {
            ctx.font = '.9rem sans-serif';
            ctx.fillStyle = '#AAA';
            ctx.textAlign="center";
            $(Trends.datasets).each(function(idx,dataset){
                $(dataset.points).each(function(pdx,pointinfo){
                    // First dataset is shifted off the scale line. 
                    // Don't write to the canvas for the null placeholder.
                    if ( pointinfo.value !== null ) { 
                        ctx.fillText(pointinfo.value,pointinfo.x,pointinfo.y - 15);
                    }
                });
            });         
         }
    

    【讨论】:

      猜你喜欢
      • 2015-10-16
      • 1970-01-01
      • 2022-07-22
      • 2021-12-01
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多