This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapColorizer.cs
130 lines (121 loc) · 5.5 KB
/
MapColorizer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using DevExpress.Utils.Drawing;
using DevExpress.XtraMap;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DevExpress.VoteApp {
public class VoteMapColorizer {
public Color DemColor { get { return Color.FromArgb(255, Color.FromArgb(0x007abc)); } }
public Color RepColor { get { return Color.FromArgb(255, Color.FromArgb(0xca0002)); } }
public Color OtherColor { get { return Color.FromArgb(255, Color.Orange); } }
public StateInfo StateFromMapAttribute(MapItemAttributeCollection attributes) {
if(attributes == null) return null;
var postalAttr = attributes["STUSPS"];
if(postalAttr != null) {
return StateInfo.GetState((postalAttr.Value ?? "").ToString());
}
return null;
}
public CountyInfo CountyFromMapAttribute(MapItemAttributeCollection attributes) {
if(attributes == null) return null;
var countyAttr = attributes["GEOID"];
if(countyAttr != null) {
return CountyInfo.GetCounty((countyAttr.Value ?? "").ToString());
}
return null;
}
public void ColorizeStateItems(ElectionData electionData, List<MapItem> mapItems) {
if(electionData == null) return;
foreach(var item in mapItems) {
var state = StateFromMapAttribute(item.Attributes);
if(state != null) {
var results = electionData.Votes.FirstOrDefault(q => q.State == state.State);
if(results != null) {
ColorizeStateItem(item, results);
continue;
}
}
ColorizeItemAsUnknown(item);
}
}
public void ColorizeCountyItems(ElectionData electionData, List<MapItem> mapItems) {
if(electionData == null) return;
foreach(var item in mapItems) {
var county = CountyFromMapAttribute(item.Attributes);
if(county != null) {
var results = electionData.Votes.FirstOrDefault(q => q.State == county.State);
if(results != null && results.HasCountyVotes) {
var countyResults = results.CountyVotes.FirstOrDefault(q => q.FIPS == county.FIPS);
if(countyResults != null) ColorizeCountyItem(item, countyResults);
continue;
}
}
ColorizeItemAsUnknown(item);
}
}
public bool IsColorByTotalVotes { get { return true; } }
void ColorizeCountyItem(MapItem item, CountyVote results) {
var maxVotes = results.Votes.Max(q => q.VotesCount);
CandidateVote winnerVotes = maxVotes > 0 ? results.Votes.FirstOrDefault(q => q.VotesCount == maxVotes) : null;
if(winnerVotes == null) {
ColorizeItemAsUnknown(item);
return;
}
ColorizeItemByWinner(item, results.TotalVotesCount, winnerVotes);
}
void ColorizeStateItem(MapItem item, StateVote results) {
var maxVotes = results.Votes.Max(q => q.VotesCount);
var maxElectoral = results.Votes.Max(q => q.ElectoralVotes);
CandidateVote winnerElectoral = maxElectoral > 0 ? results.Votes.FirstOrDefault(q => q.ElectoralVotes == maxElectoral) : null;
CandidateVote winnerVotes = maxVotes > 0 ? results.Votes.FirstOrDefault(q => q.VotesCount == maxVotes) : null;
if(IsColorByTotalVotes || winnerElectoral == null) {
if(winnerVotes == null) {
ColorizeItemAsUnknown(item);
return;
}
ColorizeItemByWinner(item, results.TotalVotesCount, winnerVotes);
return;
}
if(winnerElectoral != null) {
ColorizeItemByWinner(item, results.TotalVotesCount, winnerVotes, true);
}
}
void ColorizeItemByWinner(MapItem item, int totalVotes, CandidateVote winnerVotes, bool ignoreGradient = false) {
Color baseColor = ColorByType(winnerVotes.CandidateType);
if(!ignoreGradient) {
decimal percent = Helpers.GetPercent(winnerVotes.VotesCount, totalVotes);
if(percent > 0.85m) {
baseColor = HSLColor.Darken(baseColor, 0.5);
}
else {
if(percent > 0.7m) {
baseColor = HSLColor.Darken(baseColor, 0.8);
}
}
}
item.Fill = baseColor;
item.HighlightedFill = baseColor;
item.SelectedFill = baseColor;
}
void ColorizeItemAsUnknown(MapItem item) {
item.SelectedFill = item.HighlightedFill = item.Fill = Color.LightGray; //todo
}
public Color ColorByType(CandidateType candidateType) {
switch(candidateType) {
case CandidateType.Republican: return RepColor;
case CandidateType.Democrat: return DemColor;
}
return OtherColor;
}
internal Image GenerateImage(CandidateType candidateType) {
Bitmap bi = new Bitmap(16, 16);
using(Graphics g = Graphics.FromImage(bi)) {
g.Clear(ColorByType(candidateType));
}
return bi;
}
}
}