How do I leave the clicked point highlighted in dy

2019-07-21 22:06发布

问题:

I am using the selected shapes to draw a larger diamond shape on my graph. When a user clicks a point. I display the data in another div, but I want to leave the clicked point highlighted. In other words, I want to 'toggle' data behind the points on and off and the clicked points need to show if they are included in the dataset. I believe I have seen this somewhere but I cannot find it. Is there a 'standard' way of leaving a clicked point in the 'highlight' state when you mouse away after clicking?

Here is my code. The pointClickCallback is getting the data through ajax and displaying it in another div. That part works. I just want to leave the point highlighted so I know which points I have clicked on.

I also need the point to revert back to normal when I click a second time. This is a toggle that allows me to select and unselect points.

EDIT: I found the interaction model example but when I add it to my code I lose my pointClickCallback functionality. I saw the call to captureCanvas and the interaction model structure.

    var g = new Dygraph(document.getElementById('rothmangraph'), lines, {
        //showRangeSelector: true,
        title: "Personal Wellness Index (PWI)",
        labels: ['Date', 'Index'],
        color: ['#006699'],
        valueRange: [0, 101],
        axisLabelFontSize: 12,
        drawPoints: true,
        gridLineColor: "#aaaaaa",
        includeZero: true,
        strokeWidth: 2,
        rightGap: 20,
        pointSize: 4,
        highlightCircleSize: 8,
        series : {
            Index: {
                drawHighlightPointCallback : Dygraph.Circles.DIAMOND
            },
        },
        axes: {
            y: {
                pixelsPerLabel: 20,
            },
            x: {
                valueFormatter: function(ms) {
                    return ' ' + strftime('%m/%d/%Y %r',new Date(ms)) + ' ';
                },
                axisLabelWidth: 60,
                axisLabelFormatter: function(d, gran) {
                    return strftime('%m/%d %I:%M %p',new Date(d.getTime())) ;
                }
            }
        },
        underlayCallback: function (canvas, area, g) {
            var warning = g.toDomCoords(0,41);
            var critical = g.toDomCoords(0,66);

            // set background color
            canvas.fillStyle = graphCol;
            canvas.fillRect(area.x, area.y, area.w, area.h);

            // critical threshold line
            canvas.fillStyle = "#cc0000";
            canvas.fillRect(area.x,warning[1],area.w,2);

            // warning threshold line
            canvas.fillStyle = "#cccc00";
            canvas.fillRect(area.x,critical[1],area.w,2);
        },
        pointClickCallback: function(e,point) {
            var idx = point.idx;
            var line = lines[idx];
            var sqltime = strftime('%Y-%m-%d %H:%M:%S',new Date(line[0]));
            var dispdate = strftime('%m/%d %r',new Date(line[0]));
            _secureAjax({
                url: '/ajax/getDataPoint',
                data: {'patient_id': pid, "rdate": sqltime},
                success: function (result) {
                    // parse and add row to table if not exists.
                    var data = JSON.parse(result);
                    var aid = data['id'];
                    var indexCol = "#a9cced"
                    if (line[1] <= 65) indexCol = "#ede1b7";
                    if (line[1] <= 40) indexCol = "#e5bfcc";
                    var headerinfo = '<th class="'+aid+'"><span class="showindex" style="background-color:'+indexCol+'">'+line[1]+'</span></th>';
                    var fixdate = dispdate.replace(' ','<br>');
                    var headerdate = '<th class="'+aid+'">'+fixdate+'</th>';

                    // skip if already exists
                    var found = false;
                    var whichone = false;
                    $('#headerdate tr th').each(function(idx, item) {
                        if (fixdate == $(this).html()) {
                            found = true;
                            whichone = idx;
                        }
                    });

                    if (!found) {
                        $.each(data, function (idx, item) {
                            $('#' + idx).append('<td  class="'+aid+'" style="width:70px">' + item + '</td>');
                        });
                        $('#headerdate tr').append(headerdate);
                        $('#headerinfo tr').append(headerinfo);
                    } else {
                        $('tr').each(function() {
                            $('.'+aid).remove();
                        });
                    }
                }
            });
        }
    });
}