简体   繁体   English

我可以使用chartkick的图表链接到其他网址吗?

[英]Can I link to other URL's using chartkick's charts?

I've got a rails 4 app and I'm using chartkick. 我有一个rails 4应用程序,我正在使用chartkick。 I'm currently using pie charts, and I'm wondering if there is a way I can get each section of my pie chart to navigate to an endpoint or a url or anything really. 我目前正在使用饼图,我想知道是否有一种方法可以让我的饼图的每个部分导航到端点或网址或任何其他内容。

How would someone tackle that? 怎么会有人解决这个问题? Would it be done in the ERB file, or the controller? 它会在ERB文件或控制器中完成吗? And how would it be done? 它将如何完成?

I've spent a lot of time trying to figure this one out myself. 我花了很多时间试图自己解决这个问题。 Unfortunately, there does not seem to be a simple 'add the link here and go' kind of option. 不幸的是,似乎没有一个简单的'在这里添加链接,去'有点选择。

Chartkick allows you to specify whether you want to use the Google Charts Library or the Highcharts library. Chartkick允许您指定是否要使用Google Charts Library或Highcharts库。 Both of these libraries allow you to draw charts where you can specify the 'click' action of a particular data point / data series. 这两个库都允许您绘制图表,您可以在其中指定特定数据点/数据系列的“单击”操作。 To utilize this, you can draw your chart using straight JQuery/JS -- it's much easier than it seems if you're only used to the standard chartkick stuff. 为了利用这一点,你可以使用直接的JQuery / JS绘制你的图表 - 如果你只习惯标准的chartkick东西,它会比看起来容易得多。

Here is the HighCharts documentation for this: http://api.highcharts.com/highcharts#plotOptions.pie.events.click 以下是HighCharts的文档: http ://api.highcharts.com/highcharts#plotOptions.pie.events.click

Here is an awesome blog post showing how to setup the JS/Jquery (you can convert this to Coffescript really easy using http://js2.coffee ) http://birdchan.com/home/2012/09/07/highcharts-pie-charts-can-have-url-links/ 这是一篇很棒的博客文章,展示了如何设置JS / Jquery(您可以使用http://js2.coffee将此转换为Coffescript非常简单) http://birdchan.com/home/2012/09/07/highcharts-饼图,可以具备的,URL链接/

His approach there works great, and, you can make it easier by creating an endpoint on your resource's controller that serves up the data hash you'll need for the chart, replacing: 他的方法效果很好,而且,您可以通过在资源控制器上创建一个端点来更轻松地提供图表所需的数据哈希,替换:

data: [
            {name: 'Not Tested', color: '#FFA850', y: 87, url: 'http://my_site1.com'},
            {name: 'Fail', color: '#FF2929', y: 2, url: 'http://my_site2.com'},
            {name: 'Pass', color: '#31FF4F', y: 32, url: 'http://my_site3.com'}
        ]

With something like: 有类似的东西:

$.ajax {
url: "/place/resources/resource_id/top_members.json"
success: (data) ->
  draw_some_pie_chart(data)
}

Or you can also use the GON gem to make that hash available as a variable in your Coffeescript/JS file. 或者您也可以使用GON gem将该哈希值作为Coffeescript / JS文件中的变量提供。

I'm sure there is a more specific/thorough way to answer your question. 我确信有更具体/彻底的方式来回答你的问题。 I can update my answer if you'd like to post samples of the code you're trying to use 如果您要发布您尝试使用的代码示例,我可以更新我的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM