| Send back `index` and `values` from the event. | Send back `index` and `values` from the event. | ||||
| Add `verbatimTextOutput('selected')` to show `input$chart_selected`. | Add `verbatimTextOutput('selected')` to show `input$chart_selected`. | ||||
| 1. You would probably want to do some work for the user and return more meaningful values. | |||||
| We won't cover this in the workshop, but I've demonstrated a potential method. | |||||
| This function basically reverses the chart processing and | |||||
| and returns a list that should be a dataframe. | |||||
| ```js | |||||
| if (HTMLWidgets.shinyMode && x.isNavigable) { | |||||
| el.addEventListener('data-select', function(ev) { | |||||
| let {index, values} = ev | |||||
| let chart = el.widget.chart() | |||||
| let label = chart.data.labels[index] | |||||
| let names = chart.data.datasets.map(d => d.name) | |||||
| let data = values.reduce(function(acc, v, idx) { | |||||
| acc[names[idx]] = v | |||||
| return acc | |||||
| }, {}) | |||||
| data[labelsName] = label | |||||
| Shiny.setInputValue(el.id + '_selected', data) | |||||
| }) | |||||
| } | |||||
| ``` |
| renderValue: function(x) { | renderValue: function(x) { | ||||
| el.widget = this | el.widget = this | ||||
| let labelsName = Object.keys(x.data)[0] | |||||
| x.data = prepareChartData(x.data) | x.data = prepareChartData(x.data) | ||||
| chart = new frappe.Chart(el, x) | chart = new frappe.Chart(el, x) | ||||
| if (HTMLWidgets.shinyMode && x.isNavigable) { | if (HTMLWidgets.shinyMode && x.isNavigable) { | ||||
| el.addEventListener('data-select', function(ev) { | el.addEventListener('data-select', function(ev) { | ||||
| let {index, values} = ev | let {index, values} = ev | ||||
| Shiny.setInputValue(el.id + '_selected', {index, values}) | |||||
| let chart = el.widget.chart() | |||||
| let label = chart.data.labels[index] | |||||
| let names = chart.data.datasets.map(d => d.name) | |||||
| let data = values.reduce(function(acc, v, idx) { | |||||
| acc[names[idx]] = v | |||||
| return acc | |||||
| }, {}) | |||||
| data[labelsName] = label | |||||
| Shiny.setInputValue(el.id + '_selected', data) | |||||
| }) | }) | ||||
| } | } | ||||
| }, | }, |