| 1 |
# |
| 2 |
# canvas.draw library - scale module for speed tape etc. |
| 3 |
# created 12/2018 by jsb |
| 4 |
# |
| 5 |
# The Scale class combines draw.marks* with text labels to create scales/gauges |
| 6 |
# to build speed tape, compass etc. |
| 7 |
|
| 8 |
var Scale = { |
| 9 |
|
| 10 |
# see also canvas.draw.marksStyle |
| 11 |
Style: { |
| 12 |
new: func() { |
| 13 |
var obj = { |
| 14 |
parents: [Scale.Style, canvas.draw.marksStyle.new()], |
| 15 |
orientation: "u", |
| 16 |
spacing: 20, # spacing in pixel |
| 17 |
label_interval: 1, # one txt every n marks |
| 18 |
label_distance_abs: 0, # distance between ticks and text labels in px |
| 19 |
label_distance_rel: 1.1, # distance in %mark_length |
| 20 |
mark_color: [255,255,255,1], |
| 21 |
label_color: [255,255,255,1], |
| 22 |
fontsize: 16, # fontsize for labels |
| 23 |
}; |
| 24 |
return obj; |
| 25 |
}, |
| 26 |
|
| 27 |
# the 'draw direction' for a linear scale (up, down, left, right) |
| 28 |
setOrientation: func(value) { |
| 29 |
me.orientation = chr(string.tolower(value[0])); |
| 30 |
if (me.orientation == "v") me.orientation = "d"; |
| 31 |
if (me.orientation == "h") me.orientation = "r"; |
| 32 |
return me; |
| 33 |
}, |
| 34 |
|
| 35 |
setFontSize: func(value) { |
| 36 |
me.fontsize = num(value) or 16; |
| 37 |
return me; |
| 38 |
}, |
| 39 |
|
| 40 |
setSpacing: func(value) { |
| 41 |
me.spacing = num(value) or 10; |
| 42 |
return me; |
| 43 |
}, |
| 44 |
|
| 45 |
# draw one text label every <value> marks |
| 46 |
setLabelInterval: func(value) { |
| 47 |
me.label_interval = num(value) or 1; |
| 48 |
return me; |
| 49 |
}, |
| 50 |
|
| 51 |
# Distance of text label to mark in pixel (be careful when resizing). |
| 52 |
# See also setLabelDistanceRel() |
| 53 |
setLabelDistanceAbs: func(value) { |
| 54 |
me.label_distance_abs = num(value) or 0; |
| 55 |
return me; |
| 56 |
}, |
| 57 |
|
| 58 |
# Distance of text label to mark in %mark_length (easy scaling). |
| 59 |
setLabelDistanceRel: func(value) { |
| 60 |
me.label_distance_rel = num(value) or 1.1; |
| 61 |
return me; |
| 62 |
}, |
| 63 |
|
| 64 |
# get alignment of text label for linear scales based on style params |
| 65 |
getAlignmentString: func() { |
| 66 |
if (me.orientation == "d") { |
| 67 |
return (me.mark_offset < 0 ? "right" : "left")~"-center"; |
| 68 |
} |
| 69 |
elsif (me.orientation == "u") { |
| 70 |
return (me.mark_offset < 0 ? "right" : "left")~"-center"; |
| 71 |
} |
| 72 |
elsif (me.orientation == "r") { |
| 73 |
return "center-"~(me.mark_offset > 0 ? "top" : "bottom"); |
| 74 |
} |
| 75 |
elsif (me.orientation == "l") { |
| 76 |
return "center-"~(me.mark_offset > 0 ? "top" : "bottom"); |
| 77 |
} |
| 78 |
else { |
| 79 |
return "center-center"; |
| 80 |
} |
| 81 |
}, |
| 82 |
}, |
| 83 |
}; |
| 84 |
|
| 85 |
|
| 86 |
# draw a scale on canvas group |
| 87 |
# start first value of scale |
| 88 |
# count number of values to draw |
| 89 |
# increment value to add (can be negative) |
| 90 |
Scale.draw = func(cgroup, start, count, increment, style=nil) { |
| 91 |
if (style == nil) { |
| 92 |
style = me.Style.new(); |
| 93 |
} |
| 94 |
var label_count = count; #math.floor(math.abs(stop - start) / increment); |
| 95 |
if (label_count < 2) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
var marks = canvas.draw.marksLinear(cgroup, style.orientation, label_count * style.label_interval, style.spacing, style); |
| 99 |
marks.setStrokeLineWidth(style.mark_width); |
| 100 |
var labels = cgroup.createChild("group", "labels") |
| 101 |
.createChildren("text", label_count); |
| 102 |
|
| 103 |
var length = style.spacing * style.mark_length; |
| 104 |
var offset = style.mark_offset * length * style.label_distance_rel; |
| 105 |
offset += (offset > 0) ? style.label_distance_abs : -style.label_distance_abs; |
| 106 |
var alignment = style.getAlignmentString(); |
| 107 |
forindex (i; labels) { |
| 108 |
var txt = sprintf("%d", start + i * increment); |
| 109 |
var translation = i * style.spacing * style.label_interval; |
| 110 |
labels[i] |
| 111 |
.setText(txt) |
| 112 |
.setAlignment(alignment) |
| 113 |
.setFontSize(style.fontsize) |
| 114 |
.setColor(style.label_color); |
| 115 |
if (style.orientation == "d") { |
| 116 |
labels[i].setTranslation(offset, translation); |
| 117 |
} |
| 118 |
elsif (style.orientation == "u") { |
| 119 |
labels[i].setTranslation(offset, -translation); |
| 120 |
} |
| 121 |
elsif (style.orientation == "r") { |
| 122 |
labels[i].setTranslation(translation, offset); |
| 123 |
} |
| 124 |
elsif (style.orientation == "l") { |
| 125 |
labels[i].setTranslation(-translation, offset); |
| 126 |
} |
| 127 |
} |
| 128 |
return cgroup; |
| 129 |
}; |