Category talk:Wren-plot: Difference between revisions

Content added Content deleted
(Added source code for new 'Wren-plot' module.)
 
(Several changes including new methods: rect and rectfill.)
Line 15: Line 15:
// 3. ranges of x and y values: rangeX and rangeY.
// 3. ranges of x and y values: rangeX and rangeY.
// If x-values are non-numeric or discrete, null should be passed for rangeX.
// If x-values are non-numeric or discrete, null should be passed for rangeX.
// In that event a range of 0..lengthX will be used by those methods that need it.
construct new(originX, originY, lengthX, lengthY, rangeX, rangeY) {
construct new(originX, originY, lengthX, lengthY, rangeX, rangeY) {
_ox = originX
_ox = originX
Line 20: Line 21:
_lx = lengthX
_lx = lengthX
_ly = lengthY
_ly = lengthY
_rx = rangeX
_rn = (rangeX == null)
_rx = _rn ? 0.._lx : rangeX
_ry = rangeY
_ry = rangeY
}
}
Line 89: Line 91:
y2 = (y2 - _ry.from) * scaleY
y2 = (y2 - _ry.from) * scaleY
Canvas.line(_ox + x1, _oy - y1, _ox + x2, _oy - y2, color, thickness)
Canvas.line(_ox + x1, _oy - y1, _ox + x2, _oy - y2, color, thickness)
}

// Draws a rectangle with the top-left corner at scaled point (x, y)
// relative to the origin and a width of w and height of h in color c.
rect(x, y, w, h, c) {
x = (x - _rx.from) * scaleX
y = (y - _ry.from) * scaleY
Canvas.rect(_ox + x, _oy - y, w, h, c)
}

// Draws a filled rectangle with the top-left corner at scaled point (x, y)
// relative to the origin and a width of w and height of h in color c.
rectfill(x, y, w, h, c) {
x = (x - _rx.from) * scaleX
y = (y - _ry.from) * scaleY
Canvas.rectfill(_ox + x, _oy - y, w, h, c)
}
}


Line 168: Line 186:
// 'barColors' list and with a border color of 'brdColor'.
// 'barColors' list and with a border color of 'brdColor'.
// The interval between bars is calculated by the method.
// The interval between bars is calculated by the method.
// If _rx is null, the method prints the labels on the x-axis at the appropriate positions.
// If _rn is true, the method prints the labels on the x-axis at the appropriate positions.
// 'yMarks' are drawn on the y-axis at the appropriate positions in color 'mrkColor'
// 'yMarks' are drawn on the y-axis at the appropriate positions in color 'mrkColor'
// and thickness 'mrkThick'. Labels on both the x and y axes are printed in 'lblColor.'
// and thickness 'mrkThick'. Labels on both the x and y axes are printed in 'lblColor.'
Line 180: Line 198:
Canvas.rectfill(x, _oy - v, barWidth, v, barColors[cix])
Canvas.rectfill(x, _oy - v, barWidth, v, barColors[cix])
Canvas.rect(x, _oy - v, barWidth, v, brdColor)
Canvas.rect(x, _oy - v, barWidth, v, brdColor)
if (!_rx) {
if (_rn) {
var label = d[0].toString
var label = d[0].toString
var w = Canvas.getPrintArea(label).x
var w = Canvas.getPrintArea(label).x
Line 198: Line 216:
// between the bars and 'barWidth' is therefore calculated by the method.
// between the bars and 'barWidth' is therefore calculated by the method.
// Otherwise the parameters and remarks are the same as for a bar-chart.
// Otherwise the parameters and remarks are the same as for a bar-chart.
// If _rx isn't null, 'xmarks' are deduced and drawn on the x-axis at the appropriate intervals.
// If _rn is false, 'xmarks' are deduced and drawn on the x-axis at the appropriate intervals.
histogram(data, barColors, brdColor, yMarks, mrkColor, mrkThick, lblColor) {
histogram(data, barColors, brdColor, yMarks, mrkColor, mrkThick, lblColor) {
var n = data.count
var n = data.count
var rectWidth = _lx/n
var rectWidth = _lx/n
barChart(data, rectWidth, rectColors, brdColor, yMarks, mrkColor, mrkThick, lblColor)
barChart(data, rectWidth, rectColors, brdColor, yMarks, mrkColor, mrkThick, lblColor)
if (_rx) {
if (!_rn) {
var interval = (_rx.to - _rx.from) / n
var interval = (_rx.to - _rx.from) / n
var xMarks = (0..n+1).map { |i| i * interval }.toList
var xMarks = (0..n+1).map { |i| i * interval }.toList