Skip to content

Commit 842c51e

Browse files
committed
feat: chart preview
1 parent 030724f commit 842c51e

3 files changed

Lines changed: 63 additions & 3 deletions

File tree

app/views/index.scala.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@()
22

3-
@main("Welcome to Play") {
4-
<h1>Welcome to Play!</h1>
3+
@main("Memorability Score vs Price") {
4+
<div id="chart"></div>
55
}

app/views/main.scala.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@
1313
<title>@title</title>
1414
<link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
1515
<link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png")">
16-
16+
<style>
17+
#chart {
18+
width: 100%;
19+
height: calc(100vh - 18px);
20+
}
21+
</style>
1722
</head>
1823
<body>
1924
@* And here's where we render the `Html` object containing
2025
* the page content. *@
2126
@content
2227

2328
<script src="@routes.Assets.versioned("javascripts/main.js")" type="text/javascript"></script>
29+
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
2430
</body>
2531
</html>

public/javascripts/main.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const prop = (key) => (obj) => obj[key];
2+
const map = (f) => (arr) => arr.map(f);
3+
4+
const remainingTimeHours = (data, now) =>
5+
map((item) => (new Date(item.auctionEndTimestamp) - now) / 1000 / 3600)(data);
6+
7+
const markerColor = (time) => (time <= 0 ? 'red' : 'blue');
8+
const markerSize = (minTime, maxTime, time) => {
9+
const minSize = 10;
10+
const maxSize = 200;
11+
const endedMarkerSize = 12;
12+
return time <= 0
13+
? endedMarkerSize
14+
: minSize + (maxSize - minSize) * ((time - minTime) / (maxTime - minTime));
15+
};
16+
17+
const createPlot = (data) => {
18+
const now = new Date();
19+
const hoursRemaining = remainingTimeHours(data, now);
20+
const minRemainingTime = Math.min(...hoursRemaining);
21+
const maxRemainingTime = Math.max(...hoursRemaining);
22+
23+
Plotly.newPlot(
24+
'chart',
25+
[
26+
{
27+
x: map(prop('memorabilityScore'))(data),
28+
y: map(prop('minimumBidInUSD'))(data),
29+
text: map(prop('number'))(data),
30+
mode: 'markers',
31+
marker: {
32+
size: map((time) =>
33+
markerSize(minRemainingTime, maxRemainingTime, time)
34+
)(hoursRemaining),
35+
color: map(markerColor)(hoursRemaining),
36+
sizemode: 'diameter',
37+
},
38+
type: 'scatter',
39+
},
40+
],
41+
{
42+
title: 'Memorability Score vs Price (Size represents time left)',
43+
xaxis: { title: 'Memorability Score' },
44+
yaxis: { title: 'Price (USD)', type: 'log' },
45+
showlegend: false,
46+
hovermode: 'closest',
47+
}
48+
);
49+
};
50+
51+
const onWebSocketMessage = (event) => createPlot(JSON.parse(event.data));
52+
53+
const webSocket = new WebSocket('wss://fragmenty.cloud/ws/auctions');
54+
webSocket.addEventListener('message', onWebSocketMessage);

0 commit comments

Comments
 (0)