Category talk:Wren-math: Difference between revisions
Content added Content deleted
(→Source code: Added Nums.barChart method.) |
(→Source code: Nums.barChart method tweaked to deal better with zero data.) |
||
Line 932: | Line 932: | ||
// Draws a horizontal bar chart on the terminal representing a list of numerical 'data' |
// Draws a horizontal bar chart on the terminal representing a list of numerical 'data' |
||
// which must be non-negative. 'labels' |
// which must be non-negative. 'labels' is a list of the corresponding text for each bar. |
||
// When printed 'labels' are always right justified within their maximum length. |
// When printed 'labels' are always right justified within their maximum length. |
||
// 'width' is the total width of the chart in characters to which the labels/data will |
// 'width' is the total width of the chart in characters to which the labels/data will |
||
// automatically be scaled. 'symbol' is the character to be used to draw each bar |
// automatically be scaled. 'symbol' is the character to be used to draw each bar, |
||
// |
// 'symbol2' is used to represent non-zero data and 'symbol3' zero data which would |
||
// otherwise be blank. The actual data is printed at the end of each bar. |
// otherwise be blank. The actual data is printed at the end of each bar. |
||
static barChart (title, width, labels, data, symbol, symbol2) { |
static barChart (title, width, labels, data, symbol, symbol2, symbol3) { |
||
var barCount = labels.count |
var barCount = labels.count |
||
if (data.count != barCount) Fiber.abort("Mismatch between labels and data.") |
if (data.count != barCount) Fiber.abort("Mismatch between labels and data.") |
||
Line 950: | Line 950: | ||
for (i in 0...barCount) { |
for (i in 0...barCount) { |
||
var bar = symbol * scaledData[i] |
var bar = symbol * scaledData[i] |
||
if (bar == "") bar = data[i] > 0 ? symbol2 : |
if (bar == "") bar = data[i] > 0 ? symbol2 : symbol3 |
||
System.print(labels[i] + " " + bar + " " + data[i].toString) |
System.print(labels[i] + " " + bar + " " + data[i].toString) |
||
} |
} |
||
Line 958: | Line 958: | ||
// Convenience version of the above method which uses default symbols. |
// Convenience version of the above method which uses default symbols. |
||
static barChart(title, width, labels, data) { |
static barChart(title, width, labels, data) { |
||
barChart(title, width, labels, data, "■", "◧") |
barChart(title, width, labels, data, "■", "◧", "□") |
||
} |
} |
||
} |
} |