Browse Source

Add js binding: it's a widget!

master
Garrick Aden-Buie 6 years ago
parent
commit
6f141c4341
4 changed files with 1229 additions and 5 deletions
  1. +15
    -0
      dev/dev.Rmd
  2. +10
    -2
      dev/widget_demo.Rmd
  3. +1186
    -0
      dev/widget_demo.html
  4. +18
    -3
      inst/htmlwidgets/frappeChart.js

+ 15
- 0
dev/dev.Rmd View File

Point out the random ID. Point out the random ID.
Then go back and change it so we can find the element better. Then go back and change it so we can find the element better.


## Write JavaScript binding

**FILE:** `inst/htmlwidgets/frappeChart.js`

The final step is to move the Javascript we wrote before into the js binding.

* Just put in `console.log(x)`, rebuild, rerender
* Verify that this `x` looks the same as our `opts` from before
* Copy all of the JS we wrote to reconfigure the data into the widget
* Use `el` instead of `#chart`
* Rebuild, rerender
* it works!
* Try adding other options



+ 10
- 2
dev/widget_demo.Rmd View File

output: js4shiny::html_document_plain output: js4shiny::html_document_plain
--- ---


```{r frappeChart}
```{r frappeChart, message=FALSE}
library(dplyr) library(dplyr)
library(tidyr) library(tidyr)
library(babynames) library(babynames)
ungroup() %>% ungroup() %>%
pivot_wider(year, name, values_from = n) pivot_wider(year, name, values_from = n)


frappeCharts::frappeChart(data, title = "This. Is. Awwesome.", elementId = "name-chart")
frappeCharts::frappeChart(
data,
title = "This. Is. Awwesome.",
elementId = "name-chart",
lineOptions = list(regionFill = TRUE),
axisOptions = list(xIsSeries = TRUE),
colors = c("#466683", "#44bc96"),
width = "100%"
)
``` ```

+ 1186
- 0
dev/widget_demo.html
File diff suppressed because it is too large
View File


+ 18
- 3
inst/htmlwidgets/frappeChart.js View File



renderValue: function(x) { renderValue: function(x) {


// TODO: code to render the widget, e.g.
el.innerText = x.message;
const chartData = {labels: [], datasets: []}

// Get keys of data, assume that first entry is for labels, the rest are data
let labelColumn = Object.keys(x.data)[0]
let columns = Object.keys(x.data).slice(1)

// First column in x.data is the labels
chartData.labels = x.data[labelColumn]

// Create an appropriate object for each column, reformat data and add to chartData
columns.forEach(function(col) {
chartData.datasets.push({name: col, values: x.data[col]})
})

x.data = chartData

const chart = new frappe.Chart(el, x)


}, },




}; };
} }
});
});

Loading…
Cancel
Save