javascript - Click events on Pie Charts in Chart.js -


I got a question question chart.

I have prepared many recommendations using the documentation provided. I was thinking that on click of any one of the pieces, I can call an AJAX call based on the value of that slice. ?

For example, if this is my data

  var data = [[value: 300, color: "# F7464A", highlight : "Green"}, {value: 100, color: "#FDB 45C", "# 5F5A5E", label: "red"}, {value: 50, color: "# 46BFBD", highlight: "# 5AD3D1 "Highlight:" # FFC 870 ", label:" yellow "},   

Is it possible to click on the label of Red and Call the URL below: example.com?label=red&value=300 ? If so, how do I know about this?

Update: Like @ Soham Shetty comment, getSegmentsAtEvent (event ) only works for 1.x and should be used for 2.x.

.getElementsAtEvent (e)

is looking for an element under the event point, then returns all the elements on the same data index. It is internally used for highlighting the 'label' mode.

Calling getElementsAtEvent (event) on your chart example, which passes through the argument of an event, or jQuery event, the point element in that state of that event.

  canvas.com = function (evt) {var active points = myLineChart.getElementsAtEvent (evt); // = & gt; There are several points on the active point canvas which are in the same position as the click event. }; Example:  

Basic Answer (chart. Valid for JS version 1.x): < / P>

You can get this getSegmentsAtEvent (event)

Calling getSegmentsAtEvent (event) on your chart example An argument is returned to an event, or section of the jQuery phenomenon, which is in the same place of that event.

From:

So you can:

  $ ("# mychart"). Click (function (evt) {var activePoints = myNewChart.getSegmentsAtEvent (evt); / * something * /});   

Here is a complete work example:

  & lt; Html & gt; & Lt; Top & gt; & Lt; Script type = "text / javascript" src = "http://code.jquery.com/jquery-2.0.2.js" & gt; & Lt; / Script & gt; & Lt; Script type = "text / javascript" src = "chart.js" & gt; & Lt; / Script & gt; & Lt; Script type = "text / javascript" & gt; Var data = [{value: 300, color: "# F 7464A", highlight: "#FF5A5A", label: "red"}, {value: 50, color: "# 46BFBD", highlight Do: "# 5AD3D1", label: "green"}, {value: 100, color: "# fdb 45c", highlight: "#ffc 870", label: "yellow"}]; $ (Document) .ready (function () {var ctx = document.getElementById ("mychart"). GetContext ("2d"); var myNewChart = new chart (ctx) .pie (data); $ ("# mychart" ) .click (function (evt) {var activePoints = myNewChart.getSegmentsAtEvent (evt); var url = "http://example.com/?label=" + activePoints [0] .label + "& Price =" + Active points [0] value; alert (url);});}); & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Canvas ID = "My Chart" width = "400" height = "400" & gt; & Lt; / Canvas & gt; & Lt; / Body & gt; & Lt; / Html & gt;    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -