1

Chart.js v2 库下关于移动可用性的讨论,

我们可以在 RadarChart 中的那些“得分点”上模拟点击事件吗?我问这个是因为,桌面视图和平板电脑视图通过单击图表中的这些点来通过返回的索引触发外部交互性对于“得分”导航来说看起来相当不错。

可以找到一个流行的例子是

$('#ChartV2').click(function(e) {
            var activePoints = myRadarChart.getElementsAtEvent(e);                  
            var firstPoint = activePoints[0];
            console.log(firstPoint);
            if (firstPoint !== undefined){
               alert(firstPoint._index); 
               //so then we can use 
              // index to hide show results in the  html elements out of canvas , etc
            }

});

但是,如果您查看移动视图,在 responsive:true 下,通过自动调整大小,图表会变得更小,因此radarchart 内的点对于移动点击可能非常小,
与标签相同,所有内容都会变得更小且更难点击。

是否可以自定义让用户导航那些“得分点”(最初通过手动点击画布触发)

是否可以通过雷达图左右两侧的外部“上一个”、“下一个”导航按钮(例如那些滑块/轮播导航按钮)间接触发?

4

1 回答 1

4

您可以通过使用图表实例方法来做到这一点


脚本

var myRadarChart = new Chart(ctx, {
    ...

(function (chart) {
    var helpers = Chart.helpers;

    var currentDatasetIndex;
    var currentPointIndex;

    $('#ChartV2').click(function (e) {
        // try getting an element close to the click
        var activePoints = chart.getElementAtEvent(e);
        var firstPoint = activePoints[0];

        if (firstPoint === undefined) {
            // otherwise pick the first visible element
            helpers.each(chart.data.datasets, function (dataset, datasetIndex) {
                if (firstPoint === undefined && this.isDatasetVisible(datasetIndex)) {
                    var meta = this.getDatasetMeta(datasetIndex);
                    firstPoint = meta.data[0];
                }
            }, chart);
        }

        // need this check as we may have 0 visible elements
        if (firstPoint !== undefined) {
            currentDatasetIndex = firstPoint._datasetIndex;
            currentPointIndex = firstPoint._index;
            $('#prev, #next').removeAttr('disabled');
            updateView();
        }
    });

    $('#prev').click(function () {
        // we add (n - 1) and do a modulo n to move one step back in an n element array.
        if (currentPointIndex === 0)
            currentDatasetIndex = (currentDatasetIndex + chart.data.datasets.length - 1) % chart.data.datasets.length;
        currentPointIndex = (currentPointIndex + chart.data.labels.length - 1) % chart.data.labels.length;
        updateView();
    });

    $('#next').click(function () {
        currentPointIndex = (currentPointIndex + 1) % chart.data.labels.length;
        if (currentPointIndex === 0)
            currentDatasetIndex = (currentDatasetIndex + 1) % chart.data.datasets.length;
        updateView();
    });


    // this (hoisted) function will update the text and show the tooltip
    function updateView() {
        $('#value').text(
            chart.data.datasets[currentDatasetIndex].label + ' : ' +
            chart.data.labels[currentPointIndex] + ' : ' +
            chart.data.datasets[currentDatasetIndex].data[currentPointIndex]);

        // we mess around with an internal variable here - this may not work with a new version
        chart.tooltip._active = [ chart.getDatasetMeta(currentDatasetIndex).data[currentPointIndex] ];
        chart.tooltip.update();
        chart.render();
    }

}(myRadarChart));

如果您只希望此功能适用于小屏幕,只需将屏幕尺寸检查添加到上面的图表点击处理程序并使用媒体查询隐藏按钮和标签。


小提琴 - http://jsfiddle.net/uxqL6rwf/

于 2016-05-06T14:06:12.543 回答