如何添加到chart.js(Donut Charts)的链接? [英] How to add links to chart.js (Doughnut Charts)?

查看:523
本文介绍了如何添加到chart.js(Donut Charts)的链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加指向甜甜圈图表的链接,以便能够向用户发送包含已点击选项过滤的记录的页面。

I would like to add links to doughnut charts to be able to send the user for a page with the records filtered by the clicked option.

例如这里,如果用户点击绿色,我想发送用户到一个将显示所有绿色记录的页面。

For example here, if the user click on "Green", I want to send the user to a page that will show all "Green" records.

我没有找到一种简单的方法来做到这一点,并尝试了类似这样的工作然而:

I didn't find a easy way to do that, and tried something like this that isn't working yet:

(我添加了一个带有id的属性filter,我需要对其进行过滤)

(I added a attribute "filter" with the "id" that I need to filter it)

var data = [
  {
    value: 300,
    color:"#F7464A",
    highlight: "#FF5A5E",
    label: "Red",
    filter: 1
  },
  {
    value: 50,
    color: "#46BFBD",
    highlight: "#5AD3D1",
    label: "Green",
    filter: 2
  },
  {
    value: 100,
    color: "#FDB45C",
    highlight: "#FFC870",
    label: "Yellow",
    filter: 3
  }
]

$(document).ready(
    function () {
        $("#chart").click(
            function(evt){
                var activePoints = chart.getSegmentsAtEvent(evt);
                var url = "http://example.com/?grid[f][collor][]=" + activePoints[0].filter
                alert(url);
            }
        );
    }
);

我无法使用activePoints [0] .filter获取属性filter

I'm not being able to get the attribute "filter" using "activePoints[0].filter"

谢谢。

推荐答案

在JSON中添加自定义属性是可能位于v2路线图上的功能( https://github.com/nnnick /Chart.js/issues/1185 )。按照目前的情况,你可以在javascript中添加这样的属性:

Adding custom properties in JSON is a feature that may be on the roadmap for v2 (https://github.com/nnnick/Chart.js/issues/1185). As it currently stands, you can add properties in javascript doing something like this:

var segments = chart.segments;
for (var i = 0; i < segments.length; i++) {
  segments[i].filter = i+1;
}

这是一个在url中加载filter / id属性的jsfiddle( http://jsfiddle.net/tcy74pcc/1/ ):

Here's a jsfiddle with the filter/id property loading in the url (http://jsfiddle.net/tcy74pcc/1/):

如果你想用基于点而不是段的图表来做这个,这里有一个类似行的解决方案:
在chart.js的工具提示中显示自定义数据集属性

If you want to do this with a chart based on points rather than segments, here's a post with a similar solution for lines: Displaying custom dataset properties in tooltip in chart.js

希望有所帮助。祝你好运!

Hope that helps. Best of luck!

这篇关于如何添加到chart.js(Donut Charts)的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆