Skip to content

Instantly share code, notes, and snippets.

@fabiomolinar
Last active December 21, 2021 17:22
Show Gist options
  • Save fabiomolinar/acb923455a87a16ae2a6ea9edd952a06 to your computer and use it in GitHub Desktop.
Save fabiomolinar/acb923455a87a16ae2a6ea9edd952a06 to your computer and use it in GitHub Desktop.
Code snippets related to my FossilFuels.hs module.
type Country = String
type Code = String
type Year = Int
type Value = Double
data Entry = Entry { country :: !Country
, code :: !Code
, year :: !Year
, value :: !Value
} deriving (Show)
type Trend = [Entry]
type Trends = [(Country, Trend)]
instance FromNamedRecord Entry where
parseNamedRecord r = Entry <$> r .: "country" <*> r .: "code" <*> r .: "year" <*> r .: "value"
instance DefaultOrdered Entry where
headerOrder _ = header ["country", "code", "year", "value"]
csvData <- BL.readFile "src/Squares/Projects/FossilFuels.csv"
case decodeByName csvData of
Left err -> putStrLn err
Right (_, v) -> do
let trendList = FossilFuels.orderByCountry $ V.toList v
-- Code below creates the image
let image = FossilFuels.createFossilImage trendList (m, s, h, w)
writeImage "dist/images/FossilFuels/fossilFuels.png" (image :: Image RPU RGB Double)
orderByCountry :: Trend -> Trends
orderByCountry t = map (\t -> (country (head t), t)) $ groupWith country t
fossilColor :: Trends
-> DrawingSettings
-> DrawingSize
-> (Int, Int)
-> Pixel RGB Double
fossilColor tl (m, s, h, w) (dh, dw) (y, x) = PixelRGB red green 0.0
where
red = 0.0 + (p/100.0)
green = 1.0 - (p/100.0)
p = value $ snd (tl !! country) !! year
country = max 0 ((y - m) `div` (h + s))
year = max 0 ((x - m) `div` (w + s))
createFossilImage :: Trends
-> DrawingSettings
-> Image RPU RGB Double
createFossilImage tl dSettings = createXYImage -- createXYImage comes from my FastDegrade module
(length tl, length tl1)
dSettings
(fossilColor tl dSettings)
where
tl1 = snd (head tl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment