0

I am trying to show the Y value above a scatter plot point in a permanent box. It should look like this black box here (ignore the red line)

https://i.stack.imgur.com/994AB.png

So I essentially have two points that create the vertical blue line and on top of the blue line I want to show the Y value of the top point. Is something like this possible?

Here is the JS:

var ctx = document.getElementById("myChart").getContext("2d");

//Adding functionality to add vertical line

// Create gradient
    grd = ctx.createLinearGradient(0, 400.000, 0, 100.000);

// Add colors
    grd.addColorStop(0.000, 'rgba(0, 255, 0, 1.000)');
    grd.addColorStop(0.200, 'rgba(191, 255, 0, 1.000)');
    grd.addColorStop(0.400, 'rgba(221, 255, 0, 1.000)');
    grd.addColorStop(0.600, 'rgba(255, 229, 0, 1.000)');
    grd.addColorStop(0.800, 'rgba(255, 144, 0, 1.000)');
    grd.addColorStop(1.000, 'rgba(255, 50, 0, 1.000)');




    var data = {
        labels: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"],
        datasets: [
            {
                lineTension: 0,
                backgroundColor: 'rgba(0, 0, 0, 0)',
                borderCapStyle: 'butt',
                borderColor: "red",
                borderDash: [],
                borderDashOffset: 0.0,
                borderJoinStyle: 'miter',
                borderWidth: 4,
                pointBorderColor: "rgba(75,192,192,1)",
                pointBackgroundColor: "#fff",
                pointRadius: 0,
                pointBorderWidth: 0,
                pointHoverRadius: 0,
                pointHoverBackgroundColor: "rgba(75,192,192,1)",
                pointHoverBorderColor: "rgba(220,220,220,1)",
                pointHoverBorderWidth: 0,
                pointHitRadius: 0,
                data: [ {x: 0, y: 1300}, {x: 12, y: 1300}],
                showLines: false,
            },
            {
                lineTension: 0,
                backgroundColor: 'rgba(0, 0, 0, 0)',
                borderCapStyle: 'butt',
                borderColor: "#42bff4",
                borderDash: [],
                borderDashOffset: 0.0,
                borderJoinStyle: 'miter',
                borderWidth: 4,
                pointBorderColor: "rgba(75,192,192,1)",
                pointBackgroundColor: "#fff",
                pointRadius: 0,
                pointBorderWidth: 0,
                pointHoverRadius: 0,
                pointHoverBackgroundColor: "rgba(75,192,192,1)",
                pointHoverBorderColor: "rgba(220,220,220,1)",
                pointHoverBorderWidth: 0,
                pointHitRadius: 0,
                data: [ {x: 13, y: 0}, {x: 13, y: 1100}],
                showLines: false,
            },
            {
                lineTension: 0,
                backgroundColor: grd,
                borderCapStyle: 'butt',
                borderColor: "rgba(0, 0, 0, 0)",
                borderDash: [],
                borderDashOffset: 0.0,
                borderJoinStyle: 'miter',
                pointBorderColor: "rgba(75,192,192,1)",
                pointBackgroundColor: "#fff",
                pointRadius: 0,
                pointBorderWidth: 1,
                pointHoverRadius: 5,
                pointHoverBackgroundColor: "rgba(75,192,192,1)",
                pointHoverBorderColor: "rgba(220,220,220,1)",
                pointHoverBorderWidth: 2,
                pointHitRadius: 10,
                data: [{x: 0, y: 0}, {x: 1, y: 100}, {x: 2, y: 250}, {x: 3, y: 400}, {x: 4, y: 400}, {x: 5, y: 400}, {x: 6, y: 500}, {x: 7, y: 700}, {x: 8, y: 900},
                    {x: 9, y: 1000}, {x: 10, y: 1000}, {x: 11, y: 1300}, {x: 12, y: 1300}, {x: 13, y: 1100},],
                spanGaps: false,
            },
            {
                lineTension: 0,
                backgroundColor: '#ededed',
                borderCapStyle: 'butt',
                borderColor: "rgba(0, 0, 0, 0)",
                borderDash: [],
                borderDashOffset: 0.0,
                borderJoinStyle: 'miter',
                pointBorderColor: "rgba(75,192,192,1)",
                pointBackgroundColor: "#fff",
                pointRadius: 0,
                pointBorderWidth: 1,
                pointHoverRadius: 5,
                pointHoverBackgroundColor: "rgba(75,192,192,1)",
                pointHoverBorderColor: "rgba(220,220,220,1)",
                pointHoverBorderWidth: 2,
                pointHitRadius: 10,
                data: [{x: 0, y: 0}, {x: 1, y: 100}, {x: 2, y: 250}, {x: 3, y: 400}, {x: 4, y: 400}, {x: 5, y: 400}, {x: 6, y: 500}, {x: 7, y: 700}, {x: 8, y: 900},
                    {x: 9, y: 1000}, {x: 10, y: 1000}, {x: 11, y: 1300}, {x: 12, y: 1300}, {x: 13, y: 1100}, {x: 14, y: 900}, {x: 15, y: 700}, {x: 16, y: 500}, {x: 17, y: 300},
                    {x: 18, y: 100}, {x: 19, y: 50}, {x: 20, y: 0}],
                spanGaps: false,
            },
        ]
    };


    var myChart = new Chart(ctx, {
        type: 'scatter',
        data: data,
        options: {
            elements: {
                point: {
                    radius: 0,
                }
            },
            maintainAspectRatio: false,
            legend: {
                display: false
            },
            scales: {
                yAxes: [{
                    display: false,
                    ticks: {
                        beginAtZero:true,
                        callback: function(value, index, values) {
                            return value + '°';
                        },
                        mirror: true,
                    },
                    gridLines: {
                        display: false,
                        drawBorder: false,

                    },
                }],
                xAxes: [{
                    type: 'linear',
                    position: 'bottom',
                    display: true,
                    gridLines: {
                        display: false,
                        zeroLineColor:"black",
                        color: "black"
                    },
                    ticks: {
                        display: false
                    }
                }]
            },
            // annotation: {
            //     drawTime: 'afterDatasetsDraw',
            //     events: ['click'],
            //     dblClickSpeed: 350,
            //     annotations: [{
            //         drawTime: 'afterDraw', // overrides annotation.drawTime if set
            //         id: 'a-line-1', // optional
            //         type: 'line',
            //         mode: 'horizontal',
            //         scaleID: 'y-axis-0',
            //         value: '1300',
            //         borderColor: 'red',
            //         borderWidth: 2,
            //
            //         // Fires when the user clicks this annotation on the chart
            //         // (be sure to enable the event in the events array below).
            //         onClick: function(e) {
            //             // `this` is bound to the annotation element
            //         }
            //     }]
            // },


        },
    });

Here is a Fiddle with my chart https://jsfiddle.net/f209t48m/

RP12
  • 408
  • 5
  • 20
  • You may take a look at this [post](https://stackoverflow.com/a/42168122/4454454) – MaxZoom Jul 14 '17 at 15:56
  • Seems like its only applicable for pie charts. – RP12 Jul 14 '17 at 16:01
  • Look at the documentation for tooltips, specifically the section for External (custom) Tooltips http://www.chartjs.org/docs/latest/configuration/tooltip.html It should allow you to write some code to achieve your goal. – Our_Benefactors Jul 18 '17 at 21:04

0 Answers0