Sfoglia il codice sorgente

Add js binding: it's a widget!

master
Garrick Aden-Buie 6 anni fa
parent
commit
6f141c4341
4 ha cambiato i file con 1229 aggiunte e 5 eliminazioni
  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 Vedi File

@@ -421,3 +421,18 @@ but the chart won't.
Point out the random ID.
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 Vedi File

@@ -2,7 +2,7 @@
output: js4shiny::html_document_plain
---

```{r frappeChart}
```{r frappeChart, message=FALSE}
library(dplyr)
library(tidyr)
library(babynames)
@@ -18,5 +18,13 @@ data <-
ungroup() %>%
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 soppresso perché troppo grande
Vedi File


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

@@ -12,8 +12,23 @@ HTMLWidgets.widget({

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)

},

@@ -25,4 +40,4 @@ HTMLWidgets.widget({

};
}
});
});

Loading…
Annulla
Salva