|
| 1 | +(** |
| 2 | +--- |
| 3 | +title: Images |
| 4 | +category: Simple Charts |
| 5 | +categoryindex: 3 |
| 6 | +index: 9 |
| 7 | +--- |
| 8 | +*) |
| 9 | + |
| 10 | +(*** hide ***) |
| 11 | + |
| 12 | +(*** condition: prepare ***) |
| 13 | +#r "nuget: Newtonsoft.JSON, 12.0.3" |
| 14 | +#r "nuget: DynamicObj" |
| 15 | +#r "../bin/Plotly.NET/netstandard2.0/Plotly.NET.dll" |
| 16 | + |
| 17 | +(*** condition: ipynb ***) |
| 18 | +#if IPYNB |
| 19 | +#r "nuget: Plotly.NET, {{fsdocs-package-version}}" |
| 20 | +#r "nuget: Plotly.NET.Interactive, {{fsdocs-package-version}}" |
| 21 | +#endif // IPYNB |
| 22 | + |
| 23 | +(** |
| 24 | +# Images |
| 25 | +
|
| 26 | +[](https://mybinder.org/v2/gh/plotly/Plotly.NET/gh-pages?filepath={{fsdocs-source-basename}}.ipynb)  |
| 27 | +[]({{root}}{{fsdocs-source-basename}}.fsx)  |
| 28 | +[]({{root}}{{fsdocs-source-basename}}.ipynb) |
| 29 | +
|
| 30 | +*Summary:* This example shows how to create image charts in F#. |
| 31 | +
|
| 32 | +There are multiple ways of generating image charts: |
| 33 | + - From 3 Dimensional color collections, where the inner arrays contain 3 (color dimensions without alpha channel) or 4 (color dimensions and alpha channel) values. The color model can be set separately as shown below. |
| 34 | + - From a 2 dimensional collection Plotly.NETs `ARGB` type that represents rgba values |
| 35 | + - From a base64 encoded image data source |
| 36 | +
|
| 37 | +## Creating Image charts from raw color arrays |
| 38 | +*) |
| 39 | + |
| 40 | +// 3d collection containing color values |
| 41 | +open Plotly.NET |
| 42 | + |
| 43 | +let colors = [ |
| 44 | + [[0 ;0 ;255]; [255;255;0 ]; [0 ;0 ;255]] |
| 45 | + [[255;0 ;0 ]; [255;0 ;255]; [255;0 ;255]] |
| 46 | + [[0 ;255;0 ]; [0 ;255;255]; [255;0 ;0 ]] |
| 47 | +] |
| 48 | + |
| 49 | +let imageRaw = |
| 50 | + Chart.Image(Z=colors) |
| 51 | + |> Chart.withTitle "Image chart from raw color component arrays" |
| 52 | + |
| 53 | +(*** condition: ipynb ***) |
| 54 | +#if IPYNB |
| 55 | +imageRaw |
| 56 | +#endif // IPYNB |
| 57 | + |
| 58 | +(***hide***) |
| 59 | +imageRaw |> GenericChart.toChartHTML |
| 60 | +(***include-it-raw***) |
| 61 | + |
| 62 | +(** |
| 63 | +To change the color model to HSL for example, add the `ColorModel` argument: |
| 64 | +*) |
| 65 | + |
| 66 | +let imageRawHSL = |
| 67 | + Chart.Image(Z=colors, ColorModel=StyleParam.ColorModel.HSL) |
| 68 | + |> Chart.withTitle "HSL color model" |
| 69 | + |
| 70 | +(*** condition: ipynb ***) |
| 71 | +#if IPYNB |
| 72 | +imageRawHSL |
| 73 | +#endif // IPYNB |
| 74 | + |
| 75 | +(***hide***) |
| 76 | +imageRawHSL |> GenericChart.toChartHTML |
| 77 | +(***include-it-raw***) |
| 78 | + |
| 79 | +(** |
| 80 | +## Creating Image charts from ARGB arrays |
| 81 | +
|
| 82 | +Note that this way of creating image charts uses the RGBA color model. |
| 83 | +*) |
| 84 | + |
| 85 | +let argbs = [ |
| 86 | + [ColorKeyword.AliceBlue ; ColorKeyword.CornSilk ; ColorKeyword.LavenderBlush ] |> List.map ARGB.fromKeyword |
| 87 | + [ColorKeyword.DarkGray ; ColorKeyword.Snow ; ColorKeyword.MidnightBlue ] |> List.map ARGB.fromKeyword |
| 88 | + [ColorKeyword.LightSteelBlue; ColorKeyword.DarkKhaki; ColorKeyword.LightAkyBlue ] |> List.map ARGB.fromKeyword |
| 89 | +] |
| 90 | + |
| 91 | +let imageARGB = |
| 92 | + Chart.Image(argbs) |
| 93 | + |> Chart.withTitle "ARGB image chart" |
| 94 | + |
| 95 | +(*** condition: ipynb ***) |
| 96 | +#if IPYNB |
| 97 | +imageARGB |
| 98 | +#endif // IPYNB |
| 99 | + |
| 100 | +(***hide***) |
| 101 | +imageARGB |> GenericChart.toChartHTML |
| 102 | +(***include-it-raw***) |
| 103 | + |
| 104 | +(** |
| 105 | +## Creating Image charts from base64 encoded images |
| 106 | +*) |
| 107 | +open System |
| 108 | +open System.IO |
| 109 | + |
| 110 | +let imageSource = $@"{__SOURCE_DIRECTORY__}/img/logo.png" |
| 111 | + |
| 112 | +let base64String = |
| 113 | + imageSource |
| 114 | + |> File.ReadAllBytes |
| 115 | + |> System.Convert.ToBase64String |
| 116 | + |
| 117 | +let logoImage = |
| 118 | + Chart.Image( |
| 119 | + Source=($"data:image/jpg;base64,{base64String}") |
| 120 | + ) |
| 121 | + |> Chart.withTitle "This is Plotly.NET:" |
| 122 | + |
| 123 | +(*** condition: ipynb ***) |
| 124 | +#if IPYNB |
| 125 | +logoImage |
| 126 | +#endif // IPYNB |
| 127 | + |
| 128 | +(***hide***) |
| 129 | +logoImage |> GenericChart.toChartHTML |
| 130 | +(***include-it-raw***) |
0 commit comments