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