Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

49 lines
1.1KB

  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. x.data = prepareChartData(x.data)
  25. chart = new frappe.Chart(el, x)
  26. },
  27. resize: function(width, height) {
  28. // TODO: code to re-render the widget with a new size
  29. }
  30. };
  31. }
  32. });