map: merge color generation code stuff

This commit is contained in:
Adam Goldsmith 2018-07-31 20:56:50 +01:00
parent 9e863b8ec7
commit 5ecd2d69da

View File

@ -29,21 +29,18 @@ let map = new olMap({
}) })
}); });
let colorGen = { class ColorGenerator {
hues: null, constructor(count) {
get: function (totalNum) { let mult = Math.floor(360 / count);
if (this.hues === null) { this.hues = Array.from(Array(count).keys()).map(x => x * mult);
let mult = Math.floor(360 / totalNum);
this.hues = Array.from(Array(totalNum).keys())
.map(x => x * mult);
// Shuffle (this is not a great shuffle, but I don't care) // Shuffle (this is not a great shuffle, but I don't care)
this.hues.forEach((current, index, arr) => { this.hues.forEach((current, index, arr) => {
let randomIndex = Math.floor(Math.random() * index); let randomIndex = Math.floor(Math.random() * index);
[arr[index], arr[randomIndex]] = [arr[index], arr[randomIndex]] = [arr[randomIndex], arr[index]];
[arr[randomIndex], arr[index]];
}); });
} }
get() {
return this.hues.pop(); return this.hues.pop();
} }
}; };
@ -73,6 +70,8 @@ function pathStyle(feature) {
return styles; return styles;
} }
let colorGen;
function plotPaths(packets) { function plotPaths(packets) {
let vector_layer = new VectorLayer({ let vector_layer = new VectorLayer({
title: "Station Paths", title: "Station Paths",
@ -97,9 +96,10 @@ function plotPaths(packets) {
}, new Map()) }, new Map())
// plot on map // plot on map
.forEach((points, callsign, map) => { .forEach((points, callsign, map) => {
colorGen = colorGen || new ColorGenerator(map.size);
let pathFeature = new Feature({ let pathFeature = new Feature({
geometry: new LineString(points), geometry: new LineString(points),
hue: colorGen.get(map.size) hue: colorGen.get()
}); });
vector_layer.getSource().addFeature(pathFeature); vector_layer.getSource().addFeature(pathFeature);
@ -131,11 +131,10 @@ function plotPacketPaths(packets) {
[packet.data.longitude, packet.data.latitude]), [packet.data.longitude, packet.data.latitude]),
new Map()); new Map());
// TODO: merge with ColorGenerator let colorGen = new ColorGenerator(digiPos.size);
let mult = Math.floor(360 / digiPos.size); let colorMap = Array.from(digiPos.keys()).reduce(
let colors = Array.from(Array(digiPos.size).keys()) .map(x => x * mult); (acc, callsign, index) =>
let colorMap = Array.from(digiPos.keys()) acc.set(callsign, colorGen.hues[index]), new Map());
.reduce((acc, callsign, index) => acc.set(callsign, colors[index]), new Map());
console.log(colorMap); console.log(colorMap);