瀏覽代碼

Add js binding: it's a widget!

master
Garrick Aden-Buie 6 年之前
父節點
當前提交
6f141c4341
共有 4 個檔案被更改,包括 1229 行新增5 行删除
  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 查看文件

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 查看文件

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
文件差異過大導致無法顯示
查看文件


+ 18
- 3
inst/htmlwidgets/frappeChart.js 查看文件



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…
取消
儲存