Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

67 lines
1.6KB

  1. HTMLWidgets.widget({
  2. name: 'frappeChart',
  3. type: 'output',
  4. factory: function(el, width, height) {
  5. // TODO: define shared variables for this instance
  6. // we'll create chart in renderValue but want to access it elsewhere
  7. let chart = null
  8. // helper function to prep the data
  9. const prepareChartData = function(data) {
  10. const chartData = {labels: [], datasets: []}
  11. // Get keys of data, assume that first entry is for labels, the rest are data
  12. let labelColumn = Object.keys(data)[0]
  13. let columns = Object.keys(data).slice(1)
  14. // First column in x.data is the labels
  15. chartData.labels = data[labelColumn]
  16. // Create an appropriate object for each column, reformat data and add to chartData
  17. columns.forEach(function(col) {
  18. chartData.datasets.push({name: col, values: data[col]})
  19. })
  20. return chartData
  21. }
  22. return {
  23. renderValue: function(x) {
  24. el.widget = this
  25. x.data = prepareChartData(x.data)
  26. chart = new frappe.Chart(el, x)
  27. },
  28. resize: function(width, height) {
  29. // TODO: code to re-render the widget with a new size
  30. },
  31. update: function(data) {
  32. if (!chart) {
  33. console.error("The chart has not yet been initialized.")
  34. }
  35. let chartData = prepareChartData(data)
  36. chart.update(chartData)
  37. },
  38. chart: () => chart
  39. };
  40. }
  41. });
  42. if (HTMLWidgets.shinyMode) {
  43. Shiny.addCustomMessageHandler('frappeCharts:update', function({id, data}) {
  44. let el = document.getElementById(id)
  45. el.widget.update(data)
  46. })
  47. }