Commit 13018b5d authored by André Espaze's avatar André Espaze
Browse files

Plotly rendering from Elm side

parent 1f2dce6d7265
......@@ -6,10 +6,11 @@ import Dict
import Html.Styled exposing (..)
import Html.Styled.Events exposing (onClick)
import Http
import Json.Decode as Decode
import Json.Decode as Decode exposing (Decoder)
import KeywordMultiSelector
import KeywordSelector
import Tachyons.Classes as T
import Task
import Time
import Url
import Url.Builder as UB
......@@ -29,6 +30,15 @@ type alias SeriesCatalog =
Dict.Dict String String
type alias Serie =
Dict.Dict String Float
serieDecoder : Decoder Serie
serieDecoder =
Decode.dict Decode.float
type Msg
= CatalogReceived (Result Http.Error SeriesCatalog)
| ToggleSelection
......@@ -37,6 +47,33 @@ type Msg
| MakeSearch
| OnApply
| GotPlot (Result Http.Error String)
| RenderPlot (Result String (List Serie))
type alias Trace =
{ type_ : String
, name : String
, x : List String
, y : List Float
, mode : String
}
type alias TraceArgs =
String -> List String -> List Float -> String -> Trace
scatterPlot : TraceArgs
scatterPlot =
Trace "scatter"
type alias PlotArgs =
{ data : List Trace
}
port renderPlot : PlotArgs -> Cmd msg
type alias RenderArgs =
......@@ -96,7 +133,32 @@ update msg model =
newModel { model | activeSelection = not model.activeSelection }
ToggleItem x ->
newModel { model | selectedSeries = toggleItem x model.selectedSeries }
let
selectedSeries =
toggleItem x model.selectedSeries
getSerie serieName =
Http.task
{ method = "GET"
, url =
UB.crossOrigin
model.urlPrefix
[ "api", "series", "state" ]
[ UB.string "name" serieName ]
, headers = []
, body = Http.emptyBody
, timeout = Nothing
, resolver =
Http.stringResolver <|
Common.decodeJsonMessage serieDecoder
}
getSeries =
Task.sequence <| List.map getSerie selectedSeries
in
( { model | selectedSeries = selectedSeries }
, Task.attempt RenderPlot getSeries
)
SearchSeries x ->
newModel { model | searchString = x }
......@@ -104,6 +166,29 @@ update msg model =
MakeSearch ->
newModel { model | searchedSeries = keywordMatch model.searchString model.series }
RenderPlot (Ok xs) ->
let
vals =
List.map2
(\name x ->
scatterPlot
name
(Dict.keys x)
(Dict.values x)
"lines"
)
model.selectedSeries
xs
in
( model, renderPlot <| PlotArgs vals )
RenderPlot (Err x) ->
let
_ =
Debug.log "Error on RenderPlot" x
in
newModel model
OnApply ->
( model, Http.get { url = plotUrl, expect = Http.expectString GotPlot } )
......
......@@ -10,6 +10,17 @@
node: document.getElementById("series_selector"),
flags: ""
});
app.ports.renderPlot.subscribe(function(args) {
Plotly.newPlot(
"plot",
args.data.map(function(o) {
o.type = o.type_;
return o;
}),
{showLegend: true},
{displaylogo: false, modeBarButtonsToRemove: ["sendDataToCloud"]}
)
});
app.ports.renderPlotly.subscribe(function(args) {
let $target = $('#output')
$target.html(args.plotlyResponse)
......@@ -24,6 +35,8 @@
</script>
</form>
<div id="plot" class="plotly-graph-div"></div>
<div id="output" style="margin-top: 1em; padding-bottom:2em;">
No data yet.
</div>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment