-
Notifications
You must be signed in to change notification settings - Fork 0
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
Heatmap, color customisation and marker aggregation #7
Changes from 5 commits
2ecb45c
7623622
cfbc09a
e5bfe19
7d4f12f
aed1092
2433068
af97142
f9ccc02
8fce8cd
8e8a9d9
6fb773b
1cee863
f896f45
d09b8fc
e52577b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import React, { useEffect, useContext, useState } from "react"; | |
import { PlatformStateContext, NerdGraphQuery } from "nr1"; | ||
|
||
import { nerdGraphMarkerQuery } from "../queries"; | ||
import { FETCH_INTERVAL_DEFAULT } from "../constants"; | ||
import { FETCH_INTERVAL_DEFAULT, MARKER_COLOURS } from "../constants"; | ||
|
||
import { generateTooltipConfig } from "../utils/map"; | ||
import { deriveStatus, formatValues } from "../utils/dataFormatting"; | ||
|
@@ -13,23 +13,33 @@ import countries from "../geo/countries.geojson.json"; | |
import geoUSStates from "../geo/us-states/us-states"; | ||
import allUKRegions from "../geo/uk-regions/all-uk-regions"; | ||
|
||
import Gradient from "javascript-color-gradient"; | ||
|
||
const Regions = () => { | ||
const { accountId, regionsQuery, fetchInterval, ignorePicker, defaultSince } = | ||
useProps(); | ||
const { accountId, regionsQuery, fetchInterval, ignorePicker, defaultSince, heatMapSteps, regionColors } = useProps(); | ||
|
||
const [regions, setRegions] = useState([]); | ||
const [HMSteps, setHMSteps] = useState(0); | ||
const [HMColors,setHMColors] = useState([]); | ||
const [customColors,setCustomColors] = useState(null); | ||
|
||
|
||
const defSinceString = | ||
defaultSince === undefined || defaultSince === null | ||
? "" | ||
: " " + defaultSince; | ||
if (regionsQuery === null || regionsQuery === undefined) { | ||
return null; | ||
} | ||
|
||
const { timeRange } = useContext(PlatformStateContext); | ||
|
||
useEffect(() => { | ||
|
||
const defSinceString = | ||
defaultSince === undefined || defaultSince === null | ||
? "" | ||
: " " + defaultSince; | ||
if (regionsQuery === null || regionsQuery === undefined) { | ||
return null; | ||
} | ||
|
||
setHMSteps(heatMapSteps && heatMapSteps!="" ? parseInt(heatMapSteps) : 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This state setting was all to try and force a re-render whne config options changed. It seemed unnecessary to me but i couldnt find anbother way to trigger the re-render on prop change. |
||
setHMColors(regionColors && regionColors!="" ? regionColors.split(",") : MARKER_COLOURS.heatMapDefault); | ||
setCustomColors(regionColors && regionColors!="" ? regionColors.split(",") : null); | ||
const fetchData = async () => { | ||
const query = nerdGraphMarkerQuery( | ||
regionsQuery, | ||
|
@@ -68,7 +78,7 @@ const Regions = () => { | |
} else { | ||
return null; | ||
} | ||
}, [timeRange, fetchInterval]); | ||
}, [timeRange, fetchInterval, heatMapSteps,regionColors,regionsQuery]); | ||
|
||
if (!regions || regions.length == 0) { | ||
return null; //no regions to display | ||
|
@@ -77,6 +87,34 @@ const Regions = () => { | |
|
||
let geoFeatureLocations = []; | ||
|
||
// ---- heat map configuration ------- | ||
|
||
const gradientSteps=HMSteps; | ||
let getGradientColor=null; | ||
|
||
if(gradientSteps > 0) { | ||
let maxValue=-Infinity, minValue=Infinity; | ||
regions.forEach(location=>{ | ||
maxValue = location.value > maxValue ? location.value : maxValue; | ||
minValue = location.value < minValue ? location.value : minValue; | ||
}) | ||
|
||
if(HMColors.length > 1) { | ||
|
||
const gradientArray = new Gradient() | ||
.setColorGradient(...HMColors) | ||
.setMidpoint(gradientSteps) | ||
.getColors(); | ||
|
||
getGradientColor = (value) => { | ||
let ratio = (value - minValue) / (maxValue-minValue); | ||
let element = Math.floor((gradientSteps-1)*ratio); | ||
return gradientArray[element]; | ||
} | ||
} | ||
} | ||
|
||
|
||
regions.forEach((location, index) => { | ||
// World Countries | ||
if (location.geoISOCountry && location.geoISOCountry != "") { | ||
|
@@ -94,6 +132,10 @@ const Regions = () => { | |
location={location} | ||
tooltipConfig={tooltipConfig} | ||
defaultHeader={feature.properties.ADMIN} | ||
heatMap={getGradientColor} | ||
heatMapSteps={gradientSteps} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Theres probbaly a better way to do this. In order for the region color to update when the config changed i had to pass ll this information over to use in the useEffect trigger. I cant help but feel the gradient generator function should live within Region.tsx, but its here because it needs information form all the regions for the gradient bucketing to be derived. |
||
heatMapColors={HMColors} | ||
customColors={customColors} | ||
/>, | ||
); | ||
} else { | ||
|
@@ -118,6 +160,10 @@ const Regions = () => { | |
location={location} | ||
tooltipConfig={tooltipConfig} | ||
defaultHeader={feature.properties.NAME} | ||
heatMap={getGradientColor} | ||
heatMapSteps={gradientSteps} | ||
heatMapColors={HMColors} | ||
customColors={customColors} | ||
/>, | ||
); | ||
} else { | ||
|
@@ -139,6 +185,10 @@ const Regions = () => { | |
location={location} | ||
tooltipConfig={tooltipConfig} | ||
defaultHeader={region.name} | ||
heatMap={getGradientColor} | ||
heatMapSteps={gradientSteps} | ||
heatMapColors={HMColors} | ||
customColors={customColors} | ||
/>, | ||
); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this so that if the color changes it re-renders, is there a better way?