Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sentiment Heatmap as seperate page #42

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions frontend/css/heatmap.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
/* .mapbox-gl-toggle_sentiment {
background-repeat: no-repeat;
background-position: center;
pointer-events: auto;
background-image: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PHN2ZyAgIHhtbG5zOmRjPSJodHRwOi8vcHVybC5vcmcvZGMvZWxlbWVudHMvMS4xLyIgICB4bWxuczpjYz0iaHR0cDovL2NyZWF0aXZlY29tbW9ucy5vcmcvbnMjIiAgIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyIgICB4bWxuczpzdmc9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiAgIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgICB4bWxuczpzb2RpcG9kaT0iaHR0cDovL3NvZGlwb2RpLnNvdXJjZWZvcmdlLm5ldC9EVEQvc29kaXBvZGktMC5kdGQiICAgeG1sbnM6aW5rc2NhcGU9Imh0dHA6Ly93d3cuaW5rc2NhcGUub3JnL25hbWVzcGFjZXMvaW5rc2NhcGUiICAgd2lkdGg9IjIwIiAgIGhlaWdodD0iMjAiICAgdmlld0JveD0iMC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cmNsZSAgICAgICBzdHlsZT0iY29sb3I6IzAwMDAwMDtkaXNwbGF5OmlubGluZTtvdmVyZmxvdzp2aXNpYmxlO3Zpc2liaWxpdHk6dmlzaWJsZTtmaWxsOiMwMDAwMDA7ZmlsbC1vcGFjaXR5OjE7ZmlsbC1ydWxlOm5vbnplcm87c3Ryb2tlOm5vbmU7c3Ryb2tlLXdpZHRoOjEuNjAwMDAwMDI7bWFya2VyOm5vbmU7ZW5hYmxlLWJhY2tncm91bmQ6YWNjdW11bGF0ZSIgICAgICAgaWQ9InBhdGg0MzcyIiAgICAgICBjeD0iMTQiICAgICAgIGN5PSIxMDM4LjM2MjIiICAgICAgIHI9IjIiIC8+ICA8L2c+PC9zdmc+)
} */

#menu {
background: #fff;
position: absolute;
z-index: 1;
top: 10px;
right: 10px;
border-radius: 3px;
width: 120px;
border: 1px solid rgba(0, 0, 0, 0.4);
font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;
font-weight: bold;
text-anchor: middle;
}

#menu a {
font-size: 13px;
color: #404040;
display: block;
margin: 0;
padding: 0;
padding: 10px;
text-decoration: none;
border-bottom: 1px solid rgba(0, 0, 0, 0.25);
text-align: center;
}

#menu a:last-child {
border: none;
}

#menu a:hover {
background-color: #f8f8f8;
color: #404040;
}

#menu a.active {
background-color: #3887be;
color: #ffffff;
}

#menu a.active:hover {
background: #3074a4
}
1 change: 1 addition & 0 deletions frontend/data/cta.geojson

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions frontend/data/testData.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"username": "Username",
"timestamp": "00:00:00",
"message": "Hi",
"icon": "park",
"sentiment": 2
},
"geometry": {
"type": "Point",
"coordinates": [-87.623177, 41.881832]
}
},
{
"type": "Feature",
"properties": {
"username": "Username2",
"timestamp": "02:00:00",
"message": "Hello!",
"icon": "park",
"sentiment": -1.5
},
"geometry": {
"type": "Point",
"coordinates": [-87.625501, 41.871294]
}
}]
}
188 changes: 188 additions & 0 deletions frontend/js/heatmap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
mapboxgl.accessToken = 'pk.eyJ1IjoiYW5kcmV3c2NvaGVuIiwiYSI6ImNsYWlydHpkZjA1NjUzbnA4MWFyOHZqd24ifQ.2taNnYIsG1csIs8QmJ45SQ'
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/dark-v11',
center: [-87.65005, 41.85003],
zoom: 10
})

map.on('load', () => {
map.addSource('cta', {
type: 'geojson',
data: './data/cta.geojson'
// data: './data/testData.geojson'
})

map.addLayer(
{
id: 'Sentiment',
type: 'heatmap',
source: 'cta',
maxzoom: 15,
layout: {
// Make the layer visible by default.
visibility: 'none'
},
paint: {
// increase weight as diameter breast height increases
'heatmap-weight': {
property: 'sentiment',
type: 'exponential',
stops: [
[1, 0],
[62, 1]
]
},
// increase intensity as zoom level increases
'heatmap-intensity': {
stops: [
[11, 1],
[15, 3]
]
},
// use sequential color palette to use exponentially as the weight increases
'heatmap-color': [
'interpolate',
['linear'],
['heatmap-density'],
0,
'rgba(33,102,172,0)',
0.2,
'rgb(103,169,207)',
0.4,
'rgb(209,229,240)',
0.6,
'rgb(253,219,199)',
0.8,
'rgb(239,138,98)',
1,
'rgb(178,24,43)'
],
// increase radius as zoom increases
'heatmap-radius': {
stops: [
[11, 15],
[15, 20]
]
},
// decrease opacity to transition into the circle layer
'heatmap-opacity': {
default: 1,
stops: [
[14, 1],
[15, 0]
]
}
}
},
'waterway-label'
)

map.addLayer(
{
id: 'cta-point',
type: 'circle',
source: 'cta',
minzoom: 14,
paint: {
// increase the radius of the circle as the zoom level and route value increases
'circle-radius': {
property: 'sentiment',
type: 'exponential',
stops: [
[{ zoom: 15, value: 1 }, 5],
[{ zoom: 15, value: 62 }, 10],
[{ zoom: 22, value: 1 }, 20],
[{ zoom: 22, value: 62 }, 50]
]
},
'circle-color': {
property: 'sentiment',
type: 'exponential',
stops: [
[0, 'rgba(236,222,239,0)'],
[10, 'rgb(236,222,239)'],
[20, 'rgb(208,209,230)'],
[30, 'rgb(166,189,219)'],
[40, 'rgb(103,169,207)'],
[50, 'rgb(28,144,153)'],
[60, 'rgb(1,108,89)']
]
},
'circle-stroke-color': 'white',
'circle-stroke-width': 1,
'circle-opacity': {
stops: [
[14, 0],
[15, 1]
]
}
}
},
'waterway-label'
)
})

// click on tree to view route in a popup
map.on('click', 'cta-point', (event) => {
new mapboxgl.Popup()
.setLngLat(event.features[0].geometry.coordinates)
.setHTML(`<strong>Sentiment:</strong> ${event.features[0].properties.sentiment}`)
.addTo(map)
})

// After the last frame rendered before the map enters an "idle" state.
map.on('idle', () => {
// If these two layers were not added to the map, abort
if (!map.getLayer('Sentiment') || !map.getLayer('cta-point')) {
return
}

// Enumerate ids of the layers.
const toggleableLayerIds = ['Sentiment']

// Set up the corresponding toggle button for each layer.
for (const id of toggleableLayerIds) {
// Skip layers that already have a button set up.
if (document.getElementById(id)) {
continue
}

// Create a link.
const link = document.createElement('a')
link.id = id
link.href = '#'
link.textContent = id
link.className = ''

// Show or hide layer when the toggle is clicked.
link.onclick = function (e) {
const clickedLayer = this.textContent
e.preventDefault()
e.stopPropagation()

const visibility = map.getLayoutProperty(
clickedLayer,
'visibility'
)

// Toggle layer visibility by changing the layout object's visibility property.
if (visibility === 'visible') {
map.setLayoutProperty(clickedLayer, 'visibility', 'none')
this.className = ''
} else {
this.className = 'active'
map.setLayoutProperty(
clickedLayer,
'visibility',
'visible'
)
}
}

const layers = document.getElementById('menu')
layers.appendChild(link)
}
})

map.addControl(new mapboxgl.NavigationControl(), 'top-left')
2 changes: 0 additions & 2 deletions frontend/js/map.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
mapboxgl.accessToken = 'pk.eyJ1IjoiYWpjaHUyOCIsImEiOiJja3o2M3MzMWswd200MnZwNGdieTNlaHRjIn0.BePZiTybP8rLoo6yQKon_w'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if u remove this accessToken from Allie + Ying's code then their maps don't work. we need to put this back into their file to avoid breaking it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was a mistake, not sure when that happened, but prob explains why their map wasn't loading for me.


const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/ajchu28/cla8p13jr002s14qvhnow3727',
Expand Down
55 changes: 55 additions & 0 deletions frontend/js/sntmGeo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// async function getAllPoems () {
// const res = await fetch('/api/poems')
// const data = await res.json()
// data.forEach(poem => {
// poem.author
// });
// }

// Sentiment Analysis package (installed from npm)
async function getSentiment (text) {
const Sentiment = require('sentiment')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sentiment is a node.js module meant to be used on the backend (this won't work on the frontend), the require() function is how we import modules (ie. libraries) on the backend, but that function doesn't exist on the frontend (ie. on the "client side", meaning it won't work in a browser, only on a server)

what u would want to do here is create an endpoint on our REST API which u can query for sentiment data by fetch()ing for it on the frontend

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, I'll work on that now.

const sentiment = new Sentiment()
const result = sentiment.analyze(text)
return (result.comparative)
}

// Test Data of Poems
const testData = [
{
poem: 'a cat jumped over an evil demon',
date: 1,
author: 'nick',
lat: -87.65005,
long: 41.85003,
route: 123
},
{
poem: 'a dog licked a delicious lollipop',
date: 1,
author: 'tina',
lat: -107.65005,
long: 41.85003,
route: 124
}
]

// Run Sentiment Analysis on the Poems and convert out poem JSON to a GEOJSON for Mapping
async function getGeoJson() {
const outGeoJson = {}
const sentiment = []
const coords = []

for (const key in testData) {
const aPoem = testData[key]
aPoem.sentm = getSentiment(aPoem.poem)
sentiment.push(aPoem.sentm)
coords.push([aPoem.lat, aPoem.long])
}

outGeoJson.type = 'Feature'
outGeoJson.properties = sentiment
outGeoJson.geometry = ({ type: 'Point', coordinates: coords })

return outGeoJson
}
21 changes: 21 additions & 0 deletions frontend/sentimentHeatmap.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Demo: Make a heatmap with Mapbox GL JS</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://api.tiles.mapbox.com/mapbox-gl-js/v2.9.2/mapbox-gl.js"></script>
<link
href="https://api.tiles.mapbox.com/mapbox-gl-js/v2.9.2/mapbox-gl.css"
rel="stylesheet"
/>
<link href ="css/heatmap.css" rel="stylesheet"/>
</head>

<body>
<nav id="menu"></nav>
<div id="map"></div>
<script src="js/heatmap.js"></script>
</body>

</html>
Loading