在Chart Js图上标记X值

时间:2018-02-21 14:22:07

标签: javascript chart.js

我有这张图表: existing chart

我想为用户标记给定的X值。例如,我想要这样的东西:

wanted chart

图表代码:

var chart = new Chart(document.getElementById("featureChart"), {
    type: 'scatter',
    data: {
        datasets: [{
            label: "Benign_Cross_Entropy",
            data: customize_date
        }]
    },
    options: {
        responsive: true
    }

});

我该怎么办?感谢

编辑:我试图使用注释,但我不确定是什么问题。 海狸在评论中给出的例子很好看

    var ann = [1];
var ann_values = ["your data"];

var annotations_array = ann.map(function(date, index) {
    return {
        type: 'line',
        id: 'vline' + index,
        mode: 'vertical',
        scaleID: 'x-axis-0',
        value: date,
        borderColor: 'green',
        borderWidth: 1,
        label: {
            enabled: true,
            position: "center",
            content: ann_values[index]
        }
    }
});

var chart = new Chart(document.getElementById("featureChart"), {
    type: 'scatter',
    data: {
        datasets: [{
            label: "Benign_Cross_Entropy",
            data: customize_date,
            backgroundColor: "rgba(75,192,192,0.4)",
            borderColor: "rgba(75,192,192,1)",
            borderCapStyle: 'butt',
            borderJoinStyle: 'miter'
            // pointBorderColor: "rgba(75,192,192,1)",
            // pointBackgroundColor: "#fff"
        }
        ]
    },
    options: {
        responsive: true,
        elements: { point: { radius: 0 } },
        annotation: {
            drawTime: 'afterDatasetsDraw',
            annotations: annotations_array,
        }
    }

});

1 个答案:

答案 0 :(得分:1)

以下是annotation plugin的正确用法:



var ann = [1];
var ann_labels = ["your data"];

var annotations_array = ann.map(function(value, index) {
    return {
        type: 'line',
        id: 'vline' + index,
        mode: 'vertical',
        scaleID: 'x-axis-0',
        value: value,
        borderColor: 'red',
        borderWidth: 2,
        label: {
            enabled: true,
            position: "center",
            content: ann_labels[index]
        }
    }
});
console.log(annotations_array)

var data = [{
            x: 0,
            y: 5
         }, {
            x: 1,
            y: 6
         }, {
            x: 2,
            y: 8
         }, {
            x: 3,
            y: 9
         }];

var chart = new Chart(document.getElementById("ctx"), {
    type: 'scatter',
    data: {
        datasets: [{
            label: "Benign_Cross_Entropy",
            data: data,
            borderWidth: 2,
            showLine: true,
            backgroundColor: "rgba(75,192,192,0.4)",
            borderColor: "rgba(75,192,192,1)",
            //borderCapStyle: 'butt',
            //borderJoinStyle: 'miter'
            // pointBorderColor: "rgba(75,192,192,1)",
            // pointBackgroundColor: "#fff"
        }]
    },
    options: {
        responsive: true,
        //elements: { point: { radius: 0 } },
        annotation: {
            drawTime: 'afterDatasetsDraw',
            annotations: annotations_array,
        },
        scales: {
          xAxes: [{
            type: 'linear',
            id: 'x-axis-0',
          }]
        }
    }

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/0.5.5/chartjs-plugin-annotation.min.js"></script>

<canvas id="ctx"></canvas>
&#13;
&#13;
&#13;

这也是一个jsfiddle:https://jsfiddle.net/beaver71/e3b1Ldms/