-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlisting-9.9.js
45 lines (37 loc) · 1.73 KB
/
listing-9.9.js
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
"use strict";
const dataForge = require('data-forge');
const renderBarChart = require('./toolkit/charts.js').renderBarChart;
//
// Our function to create a distribution from a series and render a histogram from it.
//
function createDistribution (series, chartFileName) {
const bucketed = series.bucket(20); // Sort the series into 20 evenly spaced buckets (or bins).
const frequencyTable = bucketed
.deflate(r => r.Mid) // Extract the mid-point of each bin to a new series.
.detectValues() // Determine the frequency of values in the new series.
.orderBy(row => row.Value); // Order by ascending bin value, this is the correct order for rendering the histogram.
console.log(frequencyTable.toString()); // Print to console so we can double check.
const categories = frequencyTable
.deflate(r => r.Value.toFixed(2)) // Format x axis labels for display in the histogram.
.toArray();
return renderBarChart("Frequency", frequencyTable, categories, chartFileName); // Render the histogram.
};
function isWinter (monthNo) { // Determine if the requested month is a winter month.
return monthNo === 1 ||
monthNo === 2 ||
monthNo === 12;
};
const dataFrame = dataForge.readFileSync("./data/nyc-weather.csv")
.parseCSV()
.parseInts("Month")
.parseFloats(["MinTemp", "MaxTemp"])
.generateSeries({
AvgTemp: row => (row.MinTemp + row.MaxTemp) / 2
});
const winterTemperatures = dataFrame
.where(row => isWinter(row.Month)) // To create the full distribution, simply omit this filter.
.getSeries("AvgTemp");
createDistribution(winterTemperatures, "./output/nyc-winter-temperature-distribution.png")
.catch(err => {
console.error(err);
});