Category talk:Wren-math: Difference between revisions
Content added Content deleted
m (→Source code: Change to comment.) |
(→Source code: Nums.barChart method now allows for left or right alignment of labels,) |
||
Line 933: | Line 933: | ||
// 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' is a list of the corresponding text for each bar. |
// which must be non-negative. 'labels' is a list of the corresponding text for each bar. |
||
// When printed |
// When printed and unless they are all the same length, 'labels' are right justified |
||
// within their maximum length if 'rjust' is true, otherwise they are left justified. |
|||
// '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 scaled non-zero data and 'symbol3' zero data which |
// 'symbol2' is used to represent scaled non-zero data and 'symbol3' zero data which |
||
// would otherwise be blank. The actual data is printed at the end of each bar. |
// would otherwise be blank. The actual data is printed at the end of each bar. |
||
static barChart (title, width, labels, data, symbol, symbol2, symbol3) { |
static barChart (title, width, labels, data, rjust, 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.") |
||
var maxLabelLen = max(labels.map { |l| l.count }) |
var maxLabelLen = max(labels.map { |l| l.count }) |
||
if (labels.any { |l| l.count < maxLabelLen }) { |
|||
if (rjust) { |
|||
labels = labels.map { |l| (" " * (maxLabelLen - l.count)) + l }.toList |
|||
} else { |
|||
labels = labels.map { |l| l + (" " * (maxLabelLen - l.count)) }.toList |
|||
} |
|||
} |
|||
var maxData = max(data) |
var maxData = max(data) |
||
var maxLen = width - maxLabelLen - maxData.toString.count - 2 |
var maxLen = width - maxLabelLen - maxData.toString.count - 2 |
||
Line 957: | Line 964: | ||
// 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, rjust) { |
|||
barChart(title, width, labels, data, rjust, "■", "◧", "□") |
|||
} |
|||
// As above method but uses right justification for labels. |
|||
static barChart(title, width, labels, data) { |
static barChart(title, width, labels, data) { |
||
barChart(title, width, labels, data, "■", "◧", "□") |
barChart(title, width, labels, data, true, "■", "◧", "□") |
||
} |
} |
||
} |
} |