{ "hash": "445fdcf8bf8833cc50c9d4d23547cbf0", "result": { "markdown": "# JavaScript based plotting libraries\n\n\n\n:::{.callout-note}\n## Not working with quarto\nCurrently, the plots generated here are not rendering within quarto.\n\n:::\n\nThis section uses this add-on package:\n\n``` {.julia .cell-code}\nusing PlotlyLight\n```\n\n\nTo avoid a dependence on the `CalculusWithJulia` package, we load two utility packages:\n\n``` {.julia .cell-code}\nusing PlotUtils\nusing SplitApplyCombine\n```\n\n\n---\n\n\n`Julia` has different interfaces to a few JavaScript plotting libraries, notably the [vega](https://vega.github.io/vega/) and [vega-lite](https://vega.github.io/vega-lite/) through the [VegaLite.jl](https://github.com/queryverse/VegaLite.jl) package, and [plotly](https://plotly.com/javascript/) through several interfaces: `Plots.jl`, `PlotlyJS.jl`, and `PlotlyLight.jl`. These all make web-based graphics, for display through a web browser.\n\n\nThe `Plots.jl` interface is a backend for the familiar `Plots` package, making the calling syntax familiar, as is used throughout these notes. The `plotly()` command, from `Plots`, switches to this backend.\n\n\nThe `PlotlyJS.jl` interface offers direct translation from `Julia` structures to the underlying `JSON` structures needed by plotly, and has mechanisms to call back into `Julia` from `JavaScript`. This allows complicated interfaces to be produced.\n\n\nHere we discuss `PlotlyLight` which conveniently provides the translation from `Julia` structures to the `JSON` structures needed in a light-weight package, which plots quickly, without the delays due to compilation of the more complicated interfaces. Minor modifications would be needed to adjust the examples to work with `PlotlyJS` or `PlotlyBase`. The documentation for the `JavaScript` [library](https://plotly.com/javascript/) provides numerous examples which can easily be translated. The [one-page-reference](https://plotly.com/javascript/reference/) gives specific details, and is quoted from below, at times.\n\n\nThis discussion covers the basic of graphing for calculus purposes. It does not cover, for example, the faceting common in statistical usages, or the chart types common in business and statistics uses. The `plotly` library is much more extensive than what is reviewed below.\n\n\n## Julia dictionaries to JSON\n\n\n`PlotlyLight` uses the `JavaScript` interface for the `plotly` libraries. Unlike more developed interfaces, like the one for `Python`, `PlotlyLight` only manages the translation from `Julia` structures to `JavaScript` structures and the display of the results.\n\n\nThe key to translation is the mapping for `Julia`'s dictionaries to the nested `JSON` structures needed by the `JavaScript` library.\n\n\nFor example, an introductory [example](https://plotly.com/javascript/line-and-scatter/) for a scatter plot includes this `JSON` structure:\n\n``` {.julia .cell-code}\nvar trace1 = {\n x: [1, 2, 3, 4],\n y: [10, 15, 13, 17],\n mode: 'markers',\n type: 'scatter'\n};\n```\n\n\nThe `{}` create a list, the `[]` an Array (or vector, as it does with `Julia`), the `name:` are keys. The above is simply translated via:\n\n::: {.cell execution_count=5}\n``` {.julia .cell-code}\nConfig(x = [1,2,3,4],\n y = [10, 15, 13, 17],\n mode = \"markers\",\n type = \"scatter\"\n )\n```\n\n::: {.cell-output .cell-output-display execution_count=5}\n```\nConfig with 4 entries:\n :x => [1, 2, 3, 4]\n :y => [10, 15, 13, 17]\n :mode => \"markers\"\n :type => \"scatter\"\n```\n:::\n:::\n\n\nThe `Config` constructor (from the `EasyConfig` package loaded with `PlotlyLight`) is an interface for a dictionary whose keys are symbols, which are produced by the named arguments passed to `Config`. By nesting `Config` statements, nested `JavaScript` structures can be built up. As well, these can be built on the fly using `.` notation, as in:\n\n::: {.cell hold='true' execution_count=6}\n``` {.julia .cell-code}\ncfg = Config()\ncfg.key1.key2.key3 = \"value\"\ncfg\n```\n\n::: {.cell-output .cell-output-display execution_count=6}\n```\nConfig with 1 entry:\n :key1 => Config(:key2=>Config(:key3=>\"value\"))\n```\n:::\n:::\n\n\nTo produce a figure with `PlotlyLight` then is fairly straightforward: data and, optionally, a layout are created using `Config`, then passed along to the `Plot` command producing a `Plot` object which has `display` methods defined for it. This will be illustrated through the examples.\n\n\n## Scatter plot\n\n\nA basic scatter plot of points $(x,y)$ is created as follows:\n\n::: {.cell hold='true' execution_count=7}\n``` {.julia .cell-code}\nxs = 1:5\nys = rand(5)\ndata = Config(x = xs,\n y = ys,\n type=\"scatter\",\n mode=\"markers\"\n )\nPlot(data)\n```\n\n::: {.cell-output .cell-output-display execution_count=7}\n```{=html}\n