1820 |
06 Feb 13 |
olle |
1 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws box plot in a canvas context of percentile data |
1820 |
06 Feb 13 |
olle |
* supplied as a JSON object. |
1820 |
06 Feb 13 |
olle |
4 |
*/ |
1880 |
22 Feb 13 |
nicklas |
function createBoxPlot(boxPlotJsonObject, canvas, draw_area_wdt, draw_area_hgt, draw_scale_factor) |
1820 |
06 Feb 13 |
olle |
6 |
{ |
1820 |
06 Feb 13 |
olle |
var ctx = canvas.getContext('2d'); |
1880 |
22 Feb 13 |
nicklas |
8 |
|
1880 |
22 Feb 13 |
nicklas |
draw_area_wdt *= draw_scale_factor; |
1880 |
22 Feb 13 |
nicklas |
draw_area_hgt *= draw_scale_factor; |
1880 |
22 Feb 13 |
nicklas |
11 |
|
1820 |
06 Feb 13 |
olle |
ctx.clearRect(0, 0, draw_area_wdt, draw_area_hgt); |
1820 |
06 Feb 13 |
olle |
// Reserve area for scale and titles |
1880 |
22 Feb 13 |
nicklas |
var headerTitleHgt = 25 * draw_scale_factor; |
1880 |
22 Feb 13 |
nicklas |
var drect_xoff = 60 * draw_scale_factor; |
1880 |
22 Feb 13 |
nicklas |
var drect_yoff = 70 * draw_scale_factor + headerTitleHgt; |
1880 |
22 Feb 13 |
nicklas |
var header_text_top_off = 5 * draw_scale_factor; |
1880 |
22 Feb 13 |
nicklas |
var text_axis_off = 40 * draw_scale_factor; |
1820 |
06 Feb 13 |
olle |
// Calculate remaining plot area |
1820 |
06 Feb 13 |
olle |
var drect_w = draw_area_wdt - 2*drect_xoff; |
1820 |
06 Feb 13 |
olle |
var drect_h = draw_area_hgt - 2*drect_yoff; |
1880 |
22 Feb 13 |
nicklas |
22 |
|
1880 |
22 Feb 13 |
nicklas |
// Font sizes |
1880 |
22 Feb 13 |
nicklas |
var base_font_size = 11 * draw_scale_factor; // 11px scaled |
1880 |
22 Feb 13 |
nicklas |
var header1_font_size = base_font_size + 5 * draw_scale_factor; // 16px scaled |
1880 |
22 Feb 13 |
nicklas |
var header2_font_size = base_font_size + 4 * draw_scale_factor; // 15px scaled |
1880 |
22 Feb 13 |
nicklas |
var top_right_font_size = base_font_size + 1 * draw_scale_factor; // 12px scaled |
1880 |
22 Feb 13 |
nicklas |
var top_left_font_size = base_font_size; // 11px scaled |
1880 |
22 Feb 13 |
nicklas |
var y_title_font_size = header2_font_size; // 15px scaled |
1880 |
22 Feb 13 |
nicklas |
var y_scale_font_size = base_font_size; // 11px scaled |
1880 |
22 Feb 13 |
nicklas |
var x_scale_font_size = base_font_size; // 11px scaled |
1880 |
22 Feb 13 |
nicklas |
32 |
|
1820 |
06 Feb 13 |
olle |
// Draw rectangle enclosing plot area |
1820 |
06 Feb 13 |
olle |
ctx.strokeStyle = "#000000"; |
1880 |
22 Feb 13 |
nicklas |
ctx.strokeRect(fixCoordinate(ctx, drect_xoff), fixCoordinate(ctx, drect_yoff), Math.floor(drect_w), Math.floor(drect_h)); |
1820 |
06 Feb 13 |
olle |
// Add top header title |
2608 |
28 Aug 14 |
nicklas |
var headerTitleTop = boxPlotJsonObject.headerTitleTop; |
1880 |
22 Feb 13 |
nicklas |
drawHeaderTitleTopCenter(headerTitleTop, header1_font_size, header_text_top_off, ctx, drect_xoff, drect_yoff, drect_w, drect_h); |
1820 |
06 Feb 13 |
olle |
// Add top plot title |
2608 |
28 Aug 14 |
nicklas |
var titleTop = boxPlotJsonObject.titleTop; |
1880 |
22 Feb 13 |
nicklas |
drawTitleTopCenter(titleTop, header2_font_size, text_axis_off, ctx, drect_xoff, drect_yoff, drect_w, drect_h); |
1820 |
06 Feb 13 |
olle |
// Add bottom plot title |
2608 |
28 Aug 14 |
nicklas |
var titleBottom = boxPlotJsonObject.titleBottom; |
1880 |
22 Feb 13 |
nicklas |
drawTitleBottomCenter(titleBottom, header2_font_size, text_axis_off, ctx, drect_xoff, drect_yoff, drect_w, drect_h); |
1820 |
06 Feb 13 |
olle |
// Add plot top right sub-title |
2608 |
28 Aug 14 |
nicklas |
var subTitleRight = boxPlotJsonObject.subTitleRight; |
1880 |
22 Feb 13 |
nicklas |
drawSubTitleTopRight(subTitleRight, top_right_font_size, text_axis_off, ctx, drect_xoff, drect_yoff, drect_w, drect_h); |
1820 |
06 Feb 13 |
olle |
// Add plot top left sub-titles |
2608 |
28 Aug 14 |
nicklas |
var subTitleLeft01 = boxPlotJsonObject.subTitleLeft01; |
2608 |
28 Aug 14 |
nicklas |
var subTitleLeft02 = boxPlotJsonObject.subTitleLeft02; |
2608 |
28 Aug 14 |
nicklas |
var subTitleLeft03 = boxPlotJsonObject.subTitleLeft03; |
1880 |
22 Feb 13 |
nicklas |
drawSubTitleTopLeft(subTitleLeft01, 0, top_left_font_size, text_axis_off, ctx, drect_xoff, drect_yoff, drect_w, drect_h); |
1880 |
22 Feb 13 |
nicklas |
drawSubTitleTopLeft(subTitleLeft02, 1, top_left_font_size, text_axis_off, ctx, drect_xoff, drect_yoff, drect_w, drect_h); |
1880 |
22 Feb 13 |
nicklas |
drawSubTitleTopLeft(subTitleLeft03, 2, top_left_font_size, text_axis_off, ctx, drect_xoff, drect_yoff, drect_w, drect_h); |
1820 |
06 Feb 13 |
olle |
// Add y-axis left title |
2608 |
28 Aug 14 |
nicklas |
var yAxisTitleLeft = boxPlotJsonObject.yAxisTitleLeft; |
1880 |
22 Feb 13 |
nicklas |
drawTitleYLeft(yAxisTitleLeft, y_title_font_size, ctx, drect_xoff-35*draw_scale_factor, drect_yoff, drect_w, drect_h); |
1820 |
06 Feb 13 |
olle |
// Add y-axis right title |
2608 |
28 Aug 14 |
nicklas |
var yAxisTitleRight = boxPlotJsonObject.yAxisTitleRight; |
1880 |
22 Feb 13 |
nicklas |
drawTitleYRight(yAxisTitleRight, y_title_font_size, ctx, drect_xoff+35*draw_scale_factor, drect_yoff, drect_w, drect_h); |
1820 |
06 Feb 13 |
olle |
// Calculate y-axis data value limits |
1820 |
06 Feb 13 |
olle |
var minDataValue = calculateMinDataValue(boxPlotJsonObject); |
1820 |
06 Feb 13 |
olle |
var maxDataValue = calculateMaxDataValue(boxPlotJsonObject); |
1820 |
06 Feb 13 |
olle |
// Calculate scale marker array |
1820 |
06 Feb 13 |
olle |
var numTicks = 7; |
1820 |
06 Feb 13 |
olle |
var scaleMarkerArr = calculateScaleMarkerArray(minDataValue, maxDataValue, numTicks); |
1820 |
06 Feb 13 |
olle |
// Add 2% buffer below min value and 5% buffer above max value |
1820 |
06 Feb 13 |
olle |
var dataValueRange = maxDataValue - minDataValue; |
1820 |
06 Feb 13 |
olle |
var min_y = minDataValue - 0.02*dataValueRange; |
1820 |
06 Feb 13 |
olle |
var max_y = maxDataValue + 0.05*dataValueRange; |
1820 |
06 Feb 13 |
olle |
// Trim scale marker array to only include values inside range |
1820 |
06 Feb 13 |
olle |
var trimmedScaleMarkerArr = trimScaleMarkerArray(scaleMarkerArr, min_y, max_y); |
1820 |
06 Feb 13 |
olle |
// Add small spacing buffer in pixels below and above value area |
1880 |
22 Feb 13 |
nicklas |
var min_yoff = 5 * draw_scale_factor; |
1880 |
22 Feb 13 |
nicklas |
var max_yoff = 5 * draw_scale_factor; |
1854 |
18 Feb 13 |
olle |
// Select number of decimals for scale marker text |
1856 |
19 Feb 13 |
olle |
// Select number of decimals to get one decimal digit different from 0, up to 3 decimals |
1854 |
18 Feb 13 |
olle |
var numberOfDecimals = 0; |
1854 |
18 Feb 13 |
olle |
for (i=0; i < trimmedScaleMarkerArr.length; i++) |
1854 |
18 Feb 13 |
olle |
80 |
{ |
1856 |
19 Feb 13 |
olle |
var scaleValueTmp = trimmedScaleMarkerArr[i]; |
1856 |
19 Feb 13 |
olle |
// Round scale value to 3 decimals (to make e.g. 2.99999993 -> 3.000) |
1856 |
19 Feb 13 |
olle |
scaleValueTmp = scaleValueTmp.toFixed(3); |
1856 |
19 Feb 13 |
olle |
var decimalPart = scaleValueTmp - Math.floor(scaleValueTmp); |
1854 |
18 Feb 13 |
olle |
if (decimalPart >= 0.05) |
1854 |
18 Feb 13 |
olle |
86 |
{ |
1854 |
18 Feb 13 |
olle |
if (numberOfDecimals == 0 || numberOfDecimals > 1) |
1854 |
18 Feb 13 |
olle |
88 |
{ |
1854 |
18 Feb 13 |
olle |
numberOfDecimals = 1; |
1854 |
18 Feb 13 |
olle |
90 |
} |
1854 |
18 Feb 13 |
olle |
91 |
} |
1854 |
18 Feb 13 |
olle |
else if (decimalPart >= 0.005) |
1854 |
18 Feb 13 |
olle |
93 |
{ |
1854 |
18 Feb 13 |
olle |
if (numberOfDecimals == 0 || numberOfDecimals > 2) |
1854 |
18 Feb 13 |
olle |
95 |
{ |
1854 |
18 Feb 13 |
olle |
numberOfDecimals = 2; |
1854 |
18 Feb 13 |
olle |
97 |
} |
1854 |
18 Feb 13 |
olle |
98 |
} |
1854 |
18 Feb 13 |
olle |
else if (decimalPart >= 0.0005) |
1854 |
18 Feb 13 |
olle |
100 |
{ |
1854 |
18 Feb 13 |
olle |
if (numberOfDecimals == 0 || numberOfDecimals > 3) |
1854 |
18 Feb 13 |
olle |
102 |
{ |
1854 |
18 Feb 13 |
olle |
numberOfDecimals = 3; |
1854 |
18 Feb 13 |
olle |
104 |
} |
1854 |
18 Feb 13 |
olle |
105 |
} |
1854 |
18 Feb 13 |
olle |
106 |
} |
1856 |
19 Feb 13 |
olle |
// Draw y-axis left scale markers with values |
1856 |
19 Feb 13 |
olle |
var scaleValue; |
1845 |
15 Feb 13 |
olle |
var markerText; |
1820 |
06 Feb 13 |
olle |
for (i=0; i < trimmedScaleMarkerArr.length; i++) |
1820 |
06 Feb 13 |
olle |
111 |
{ |
1820 |
06 Feb 13 |
olle |
scaleValue = trimmedScaleMarkerArr[i]; |
1854 |
18 Feb 13 |
olle |
markerText = scaleValue.toFixed(numberOfDecimals); |
1880 |
22 Feb 13 |
nicklas |
drawScaleMarkerYLeft(scaleValue, y_scale_font_size, markerText, min_y, max_y, min_yoff, max_yoff, ctx, drect_xoff, drect_yoff, drect_w, drect_h); |
1820 |
06 Feb 13 |
olle |
115 |
} |
1820 |
06 Feb 13 |
olle |
// Find number of boxes in box plot |
1820 |
06 Feb 13 |
olle |
var boxNumTot = 0; |
1820 |
06 Feb 13 |
olle |
for (var i in boxPlotJsonObject.percentileData) |
1820 |
06 Feb 13 |
olle |
119 |
{ |
1820 |
06 Feb 13 |
olle |
boxNumTot++; |
1820 |
06 Feb 13 |
olle |
121 |
} |
1820 |
06 Feb 13 |
olle |
// Draw box plot boxes |
1820 |
06 Feb 13 |
olle |
for (var i in boxPlotJsonObject.percentileData) |
1820 |
06 Feb 13 |
olle |
124 |
{ |
1820 |
06 Feb 13 |
olle |
var boxNum = i; |
1820 |
06 Feb 13 |
olle |
var name = boxPlotJsonObject.percentileData[i].name; |
1820 |
06 Feb 13 |
olle |
var numItems = boxPlotJsonObject.percentileData[i].numItems; |
1820 |
06 Feb 13 |
olle |
var v1 = boxPlotJsonObject.percentileData[i].v1; |
1820 |
06 Feb 13 |
olle |
var v2 = boxPlotJsonObject.percentileData[i].v2; |
1820 |
06 Feb 13 |
olle |
var v3 = boxPlotJsonObject.percentileData[i].v3; |
1820 |
06 Feb 13 |
olle |
var v4 = boxPlotJsonObject.percentileData[i].v4; |
1820 |
06 Feb 13 |
olle |
var v5 = boxPlotJsonObject.percentileData[i].v5; |
1820 |
06 Feb 13 |
olle |
// Draw box plot box |
1880 |
22 Feb 13 |
nicklas |
var x_pad = 10 * draw_scale_factor; |
1880 |
22 Feb 13 |
nicklas |
drawBoxPlotBoxY(boxNum, boxNumTot, v1, v2, v3, v4, v5, min_y, max_y, min_yoff, max_yoff, x_pad, ctx, drect_xoff, drect_yoff, drect_w, drect_h, draw_scale_factor); |
1880 |
22 Feb 13 |
nicklas |
drawBoxPlotBoxScaleMarkerXBottom(boxNum, boxNumTot, name, x_scale_font_size, x_pad, ctx, drect_xoff, drect_yoff, drect_w, drect_h); |
1880 |
22 Feb 13 |
nicklas |
drawBoxPlotBoxScaleMarkerXTop(boxNum, boxNumTot, numItems, x_scale_font_size, x_pad, ctx, drect_xoff, drect_yoff, drect_w, drect_h); |
1820 |
06 Feb 13 |
olle |
138 |
} |
1820 |
06 Feb 13 |
olle |
// Draw y-axis right scale markers with values and horizontal dotted guide lines |
1820 |
06 Feb 13 |
olle |
for (var i in boxPlotJsonObject.valueGuideLinesY) |
1820 |
06 Feb 13 |
olle |
141 |
{ |
2008 |
19 Jun 13 |
olle |
if (boxPlotJsonObject.valueGuideLinesY[i] != null) |
2008 |
19 Jun 13 |
olle |
143 |
{ |
2008 |
19 Jun 13 |
olle |
var value = boxPlotJsonObject.valueGuideLinesY[i].value; |
2008 |
19 Jun 13 |
olle |
var valueText = boxPlotJsonObject.valueGuideLinesY[i].text; |
2008 |
19 Jun 13 |
olle |
// Draw y-axis right scale markers with values |
2008 |
19 Jun 13 |
olle |
drawScaleMarkerYRight(value, valueText, y_scale_font_size, min_y, max_y, min_yoff, max_yoff, ctx, drect_xoff, drect_yoff, drect_w, drect_h); |
2008 |
19 Jun 13 |
olle |
// Draw dotted horizontal lines at y axis right scale marker values |
2008 |
19 Jun 13 |
olle |
drawDottedLineY(value, min_y, max_y, min_yoff, max_yoff, ctx, drect_xoff, drect_yoff, drect_w, drect_h); |
2008 |
19 Jun 13 |
olle |
150 |
} |
1820 |
06 Feb 13 |
olle |
151 |
} |
1820 |
06 Feb 13 |
olle |
152 |
} |
1820 |
06 Feb 13 |
olle |
153 |
|
1820 |
06 Feb 13 |
olle |
154 |
/** |
1820 |
06 Feb 13 |
olle |
* Calculate min data value for percentiles in box plot JSON object. |
1820 |
06 Feb 13 |
olle |
156 |
*/ |
1820 |
06 Feb 13 |
olle |
function calculateMinDataValue(boxPlotJsonObject) |
1820 |
06 Feb 13 |
olle |
158 |
{ |
1820 |
06 Feb 13 |
olle |
// Find number of boxes in box plot |
1820 |
06 Feb 13 |
olle |
var boxNumTot = 0; |
1820 |
06 Feb 13 |
olle |
for (var i in boxPlotJsonObject.percentileData) |
1820 |
06 Feb 13 |
olle |
162 |
{ |
1820 |
06 Feb 13 |
olle |
boxNumTot++; |
1820 |
06 Feb 13 |
olle |
164 |
} |
1820 |
06 Feb 13 |
olle |
// Calculate min percentile value |
1820 |
06 Feb 13 |
olle |
var minValue = 1000000000; |
1820 |
06 Feb 13 |
olle |
for (var i in boxPlotJsonObject.percentileData) |
1820 |
06 Feb 13 |
olle |
168 |
{ |
1820 |
06 Feb 13 |
olle |
var boxNum = i; |
1820 |
06 Feb 13 |
olle |
var v1 = boxPlotJsonObject.percentileData[i].v1; |
1820 |
06 Feb 13 |
olle |
var v5 = boxPlotJsonObject.percentileData[i].v5; |
1820 |
06 Feb 13 |
olle |
if (v1 < minValue) |
1820 |
06 Feb 13 |
olle |
173 |
{ |
1820 |
06 Feb 13 |
olle |
minValue = v1; |
1820 |
06 Feb 13 |
olle |
175 |
} |
1820 |
06 Feb 13 |
olle |
if (v5 < minValue) |
1820 |
06 Feb 13 |
olle |
177 |
{ |
1820 |
06 Feb 13 |
olle |
minValue = v5; |
1820 |
06 Feb 13 |
olle |
179 |
} |
1820 |
06 Feb 13 |
olle |
180 |
} |
1820 |
06 Feb 13 |
olle |
return minValue; |
1820 |
06 Feb 13 |
olle |
182 |
} |
1820 |
06 Feb 13 |
olle |
183 |
|
1820 |
06 Feb 13 |
olle |
184 |
/** |
1820 |
06 Feb 13 |
olle |
* Calculate max data value for percentiles in box plot JSON object. |
1820 |
06 Feb 13 |
olle |
186 |
*/ |
1820 |
06 Feb 13 |
olle |
function calculateMaxDataValue(boxPlotJsonObject) |
1820 |
06 Feb 13 |
olle |
188 |
{ |
1820 |
06 Feb 13 |
olle |
// Find number of boxes in box plot |
1820 |
06 Feb 13 |
olle |
var boxNumTot = 0; |
1820 |
06 Feb 13 |
olle |
for (var i in boxPlotJsonObject.percentileData) |
1820 |
06 Feb 13 |
olle |
192 |
{ |
1820 |
06 Feb 13 |
olle |
boxNumTot++; |
1820 |
06 Feb 13 |
olle |
194 |
} |
1820 |
06 Feb 13 |
olle |
// Calculate max percentile value |
1820 |
06 Feb 13 |
olle |
var maxValue = -1; |
1820 |
06 Feb 13 |
olle |
for (var i in boxPlotJsonObject.percentileData) |
1820 |
06 Feb 13 |
olle |
198 |
{ |
1820 |
06 Feb 13 |
olle |
var boxNum = i; |
1820 |
06 Feb 13 |
olle |
var v1 = boxPlotJsonObject.percentileData[i].v1; |
1820 |
06 Feb 13 |
olle |
var v5 = boxPlotJsonObject.percentileData[i].v5; |
1820 |
06 Feb 13 |
olle |
if (v1 > maxValue) |
1820 |
06 Feb 13 |
olle |
203 |
{ |
1820 |
06 Feb 13 |
olle |
maxValue = v1; |
1820 |
06 Feb 13 |
olle |
205 |
} |
1820 |
06 Feb 13 |
olle |
if (v5 > maxValue) |
1820 |
06 Feb 13 |
olle |
207 |
{ |
1820 |
06 Feb 13 |
olle |
maxValue = v5; |
1820 |
06 Feb 13 |
olle |
209 |
} |
1820 |
06 Feb 13 |
olle |
210 |
} |
1820 |
06 Feb 13 |
olle |
return maxValue; |
1820 |
06 Feb 13 |
olle |
212 |
} |
1820 |
06 Feb 13 |
olle |
213 |
|
1820 |
06 Feb 13 |
olle |
214 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws centered top header title in a canvas context, using coordinates |
1820 |
06 Feb 13 |
olle |
* where x increases to the right and y increases upwards, |
1820 |
06 Feb 13 |
olle |
* and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
218 |
*/ |
1880 |
22 Feb 13 |
nicklas |
function drawHeaderTitleTopCenter(text, font_size, text_top_off, context, xoff, yoff, wdt, hgt) |
1820 |
06 Feb 13 |
olle |
220 |
{ |
1820 |
06 Feb 13 |
olle |
// Add centered top header title |
1880 |
22 Feb 13 |
nicklas |
context.font = font_size + 'px sans-serif'; |
1820 |
06 Feb 13 |
olle |
context.textBaseline = 'top'; |
1820 |
06 Feb 13 |
olle |
context.textAlign="center"; |
1820 |
06 Feb 13 |
olle |
context.fillText(text, xoff + wdt/2, text_top_off); |
1820 |
06 Feb 13 |
olle |
226 |
} |
1820 |
06 Feb 13 |
olle |
227 |
|
1820 |
06 Feb 13 |
olle |
228 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws centered top title in a canvas context, using coordinates |
1820 |
06 Feb 13 |
olle |
* where x increases to the right and y increases upwards, |
1820 |
06 Feb 13 |
olle |
* and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
232 |
*/ |
1880 |
22 Feb 13 |
nicklas |
function drawTitleTopCenter(text, font_size, text_axis_off, context, xoff, yoff, wdt, hgt) |
1820 |
06 Feb 13 |
olle |
234 |
{ |
1820 |
06 Feb 13 |
olle |
// Add centered top title |
1820 |
06 Feb 13 |
olle |
var text_axis_yoff = -text_axis_off; |
1880 |
22 Feb 13 |
nicklas |
context.font = font_size + 'px sans-serif'; |
1820 |
06 Feb 13 |
olle |
context.textBaseline = 'bottom'; |
1820 |
06 Feb 13 |
olle |
context.textAlign="center"; |
1820 |
06 Feb 13 |
olle |
context.fillText(text, xoff + wdt/2, yoff + text_axis_yoff); |
1820 |
06 Feb 13 |
olle |
241 |
} |
1820 |
06 Feb 13 |
olle |
242 |
|
1820 |
06 Feb 13 |
olle |
243 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws centered bottom title in a canvas context, using coordinates |
1820 |
06 Feb 13 |
olle |
* where x increases to the right and y increases upwards, |
1820 |
06 Feb 13 |
olle |
* and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
247 |
*/ |
1880 |
22 Feb 13 |
nicklas |
function drawTitleBottomCenter(text, font_size, text_axis_off, context, xoff, yoff, wdt, hgt) |
1820 |
06 Feb 13 |
olle |
249 |
{ |
1820 |
06 Feb 13 |
olle |
// Add centered bottom title |
1820 |
06 Feb 13 |
olle |
var text_axis_yoff = text_axis_off; |
1880 |
22 Feb 13 |
nicklas |
context.font = font_size + 'px sans-serif'; |
1820 |
06 Feb 13 |
olle |
context.textBaseline = 'top'; |
1820 |
06 Feb 13 |
olle |
context.textAlign="center"; |
1820 |
06 Feb 13 |
olle |
context.fillText(text, xoff + wdt/2, yoff + hgt + text_axis_yoff); |
1820 |
06 Feb 13 |
olle |
256 |
} |
1820 |
06 Feb 13 |
olle |
257 |
|
1820 |
06 Feb 13 |
olle |
258 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws right-aligned top sub-title in a canvas context, using coordinates |
1820 |
06 Feb 13 |
olle |
* where x increases to the right and y increases upwards, |
1820 |
06 Feb 13 |
olle |
* and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
262 |
*/ |
1880 |
22 Feb 13 |
nicklas |
function drawSubTitleTopRight(text, font_size, text_axis_off, context, xoff, yoff, wdt, hgt) |
1820 |
06 Feb 13 |
olle |
264 |
{ |
1820 |
06 Feb 13 |
olle |
// Add right-aligned top title |
1820 |
06 Feb 13 |
olle |
var text_axis_yoff = -text_axis_off; |
1880 |
22 Feb 13 |
nicklas |
context.font = font_size + 'px sans-serif'; |
1820 |
06 Feb 13 |
olle |
context.textBaseline = 'bottom'; |
1820 |
06 Feb 13 |
olle |
context.textAlign="right"; |
1820 |
06 Feb 13 |
olle |
context.fillText(text, xoff + wdt, yoff + text_axis_yoff); |
1820 |
06 Feb 13 |
olle |
271 |
} |
1820 |
06 Feb 13 |
olle |
272 |
|
1820 |
06 Feb 13 |
olle |
273 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws left-aligned top sub-title in a canvas context, using coordinates |
1820 |
06 Feb 13 |
olle |
* where x increases to the right and y increases upwards, |
1820 |
06 Feb 13 |
olle |
* and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
277 |
*/ |
1880 |
22 Feb 13 |
nicklas |
function drawSubTitleTopLeft(text, sub_title_num, font_size, text_axis_off, context, xoff, yoff, wdt, hgt) |
1820 |
06 Feb 13 |
olle |
279 |
{ |
1820 |
06 Feb 13 |
olle |
// Add left-aligned top title |
1820 |
06 Feb 13 |
olle |
var text_axis_yoff = -text_axis_off; |
1820 |
06 Feb 13 |
olle |
//var text_yoff = 35 - 11*(2 - sub_title_num); |
1820 |
06 Feb 13 |
olle |
var text_axis_yoff = -text_axis_off; |
1880 |
22 Feb 13 |
nicklas |
var text_yoff = yoff + text_axis_yoff - (font_size+1)*(2 - sub_title_num); |
1880 |
22 Feb 13 |
nicklas |
context.font = font_size + 'px sans-serif'; |
1820 |
06 Feb 13 |
olle |
context.textBaseline = 'bottom'; |
1820 |
06 Feb 13 |
olle |
context.textAlign="left"; |
1820 |
06 Feb 13 |
olle |
context.fillText(text, xoff, text_yoff); |
1820 |
06 Feb 13 |
olle |
289 |
} |
1820 |
06 Feb 13 |
olle |
290 |
|
1820 |
06 Feb 13 |
olle |
291 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws vertical box plot box with five percentiles. |
1820 |
06 Feb 13 |
olle |
* Box numbers starts from 0 at the left. |
1820 |
06 Feb 13 |
olle |
294 |
*/ |
1880 |
22 Feb 13 |
nicklas |
function drawBoxPlotBoxY(boxNum, boxNumTot, v1, v2, v3, v4, v5, min, max, min_off, max_off, x_pad, context, xoff, yoff, wdt, hgt, draw_scale_factor) |
1820 |
06 Feb 13 |
olle |
296 |
{ |
1820 |
06 Feb 13 |
olle |
// Calculate value offsets |
1820 |
06 Feb 13 |
olle |
var eff_hgt = hgt - min_off - max_off; |
1820 |
06 Feb 13 |
olle |
var value_range = max - min; |
1820 |
06 Feb 13 |
olle |
var v1_yoff = min_off + eff_hgt*((v1 - min)/value_range); |
1820 |
06 Feb 13 |
olle |
var v2_yoff = min_off + eff_hgt*((v2 - min)/value_range); |
1820 |
06 Feb 13 |
olle |
var v3_yoff = min_off + eff_hgt*((v3 - min)/value_range); |
1820 |
06 Feb 13 |
olle |
var v4_yoff = min_off + eff_hgt*((v4 - min)/value_range); |
1820 |
06 Feb 13 |
olle |
var v5_yoff = min_off + eff_hgt*((v5 - min)/value_range); |
1820 |
06 Feb 13 |
olle |
// Calculate box width |
1820 |
06 Feb 13 |
olle |
var width_per_box = (wdt - 2*x_pad)/boxNumTot; |
1820 |
06 Feb 13 |
olle |
// Reserve 20% as space between boxes |
1820 |
06 Feb 13 |
olle |
var box_pad = width_per_box/10; |
1820 |
06 Feb 13 |
olle |
var box_wdt = width_per_box*4/5; |
1820 |
06 Feb 13 |
olle |
// Calculate box min, center, and max offsets |
1820 |
06 Feb 13 |
olle |
var box_xmin = x_pad + boxNum*width_per_box + box_pad; |
1820 |
06 Feb 13 |
olle |
var box_xcenter = box_xmin + box_wdt/2; |
1820 |
06 Feb 13 |
olle |
var box_xmax = box_xmin + box_wdt; |
1820 |
06 Feb 13 |
olle |
// Draw box rectangle |
1820 |
06 Feb 13 |
olle |
drawRect(box_xmin, v2_yoff, box_wdt, v4_yoff - v2_yoff, context, xoff, yoff, wdt, hgt); |
1820 |
06 Feb 13 |
olle |
// Draw median line (thick) |
1880 |
22 Feb 13 |
nicklas |
context.lineWidth = 3; |
1820 |
06 Feb 13 |
olle |
context.beginPath(); |
1820 |
06 Feb 13 |
olle |
drawLine(box_xmin, v3_yoff, box_xmax, v3_yoff, context, xoff, yoff, wdt, hgt); |
1820 |
06 Feb 13 |
olle |
context.stroke(); |
1820 |
06 Feb 13 |
olle |
// Draw line between first percentile and box (dashed line) |
1820 |
06 Feb 13 |
olle |
context.lineWidth = 1; |
1820 |
06 Feb 13 |
olle |
context.beginPath(); |
1820 |
06 Feb 13 |
olle |
//drawLine(box_xcenter, v1_yoff, box_xcenter, v2_yoff, context, xoff, yoff, wdt, hgt); |
1880 |
22 Feb 13 |
nicklas |
var dash_size = 5 * draw_scale_factor; |
1880 |
22 Feb 13 |
nicklas |
drawDashedLineVertical(box_xcenter, v1_yoff, v2_yoff, context, xoff, yoff, wdt, hgt, dash_size, dash_size); |
1820 |
06 Feb 13 |
olle |
// Draw short horizontal line at end of line |
1820 |
06 Feb 13 |
olle |
drawLine(box_xcenter - box_wdt/4, v1_yoff, box_xcenter + box_wdt/4, v1_yoff, context, xoff, yoff, wdt, hgt); |
1820 |
06 Feb 13 |
olle |
context.stroke(); |
1820 |
06 Feb 13 |
olle |
// Draw line between box and fifth percentile (dashed line) |
1820 |
06 Feb 13 |
olle |
context.lineWidth = 1; |
1820 |
06 Feb 13 |
olle |
context.beginPath(); |
1820 |
06 Feb 13 |
olle |
//drawLine(box_xcenter, v4_yoff, box_xcenter, v5_yoff, context, xoff, yoff, wdt, hgt); |
1880 |
22 Feb 13 |
nicklas |
drawDashedLineVertical(box_xcenter, v4_yoff, v5_yoff, context, xoff, yoff, wdt, hgt, dash_size, dash_size); |
1820 |
06 Feb 13 |
olle |
// Draw short horizontal line at end of line |
1820 |
06 Feb 13 |
olle |
drawLine(box_xcenter - box_wdt/4, v5_yoff, box_xcenter + box_wdt/4, v5_yoff, context, xoff, yoff, wdt, hgt); |
1820 |
06 Feb 13 |
olle |
context.stroke(); |
1820 |
06 Feb 13 |
olle |
338 |
} |
1820 |
06 Feb 13 |
olle |
339 |
|
1820 |
06 Feb 13 |
olle |
340 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws x-axis bottom scale marker with text in a canvas context |
1820 |
06 Feb 13 |
olle |
* for a box plot box, using coordinates where x increases to the right |
1820 |
06 Feb 13 |
olle |
* and y increases upwards, and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
344 |
*/ |
1880 |
22 Feb 13 |
nicklas |
function drawBoxPlotBoxScaleMarkerXBottom(boxNum, boxNumTot, value, font_size, x_pad, context, xoff, yoff, wdt, hgt) |
1820 |
06 Feb 13 |
olle |
346 |
{ |
1820 |
06 Feb 13 |
olle |
// Calculate box width |
1820 |
06 Feb 13 |
olle |
var width_per_box = (wdt - 2*x_pad)/boxNumTot; |
1820 |
06 Feb 13 |
olle |
// Reserve 20% as space between boxes |
1820 |
06 Feb 13 |
olle |
var box_pad = width_per_box/10; |
1820 |
06 Feb 13 |
olle |
var box_wdt = width_per_box*4/5; |
1820 |
06 Feb 13 |
olle |
// Calculate box min, center, and max offsets |
1820 |
06 Feb 13 |
olle |
var box_xmin = x_pad + boxNum*width_per_box + box_pad; |
1820 |
06 Feb 13 |
olle |
var box_xcenter = box_xmin + box_wdt/2; |
1820 |
06 Feb 13 |
olle |
var box_xmax = box_xmin + box_wdt; |
1820 |
06 Feb 13 |
olle |
// Draw scale marker |
1880 |
22 Feb 13 |
nicklas |
var marker_len = font_size; |
1820 |
06 Feb 13 |
olle |
context.lineWidth = 1; |
1820 |
06 Feb 13 |
olle |
context.beginPath(); |
1820 |
06 Feb 13 |
olle |
drawLine(box_xcenter, 0, box_xcenter, -marker_len, context, xoff, yoff, wdt, hgt); |
1820 |
06 Feb 13 |
olle |
context.stroke(); |
1820 |
06 Feb 13 |
olle |
// Draw scale marker value |
1820 |
06 Feb 13 |
olle |
var value_text = '' + value; |
1880 |
22 Feb 13 |
nicklas |
var text_axis_yoff = 1.5*font_size; |
1880 |
22 Feb 13 |
nicklas |
var text_marker_xoff = font_size / 2; |
1820 |
06 Feb 13 |
olle |
context.save(); |
1880 |
22 Feb 13 |
nicklas |
context.font = font_size + 'px sans-serif'; |
1820 |
06 Feb 13 |
olle |
context.textBaseline = 'bottom'; |
1820 |
06 Feb 13 |
olle |
context.translate(xoff + box_xcenter + text_marker_xoff, yoff + hgt + text_axis_yoff); |
1820 |
06 Feb 13 |
olle |
context.rotate(-Math.PI/2); |
1820 |
06 Feb 13 |
olle |
context.textAlign = "right"; |
1820 |
06 Feb 13 |
olle |
context.fillText(value_text, 0, 0); |
1820 |
06 Feb 13 |
olle |
context.restore(); |
1820 |
06 Feb 13 |
olle |
374 |
} |
1820 |
06 Feb 13 |
olle |
375 |
|
1820 |
06 Feb 13 |
olle |
376 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws x-axis top scale marker with text in a canvas context |
1820 |
06 Feb 13 |
olle |
* for a box plot box, using coordinates where x increases to the right |
1820 |
06 Feb 13 |
olle |
* and y increases upwards, and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
380 |
*/ |
1880 |
22 Feb 13 |
nicklas |
function drawBoxPlotBoxScaleMarkerXTop(boxNum, boxNumTot, value, font_size, x_pad, context, xoff, yoff, wdt, hgt) |
1820 |
06 Feb 13 |
olle |
382 |
{ |
1820 |
06 Feb 13 |
olle |
// Calculate box width |
1820 |
06 Feb 13 |
olle |
var width_per_box = (wdt - 2*x_pad)/boxNumTot; |
1820 |
06 Feb 13 |
olle |
// Reserve 20% as space between boxes |
1820 |
06 Feb 13 |
olle |
var box_pad = width_per_box/10; |
1820 |
06 Feb 13 |
olle |
var box_wdt = width_per_box*4/5; |
1820 |
06 Feb 13 |
olle |
// Calculate box min, center, and max offsets |
1820 |
06 Feb 13 |
olle |
var box_xmin = x_pad + boxNum*width_per_box + box_pad; |
1820 |
06 Feb 13 |
olle |
var box_xcenter = box_xmin + box_wdt/2; |
1820 |
06 Feb 13 |
olle |
var box_xmax = box_xmin + box_wdt; |
1820 |
06 Feb 13 |
olle |
// Draw scale marker |
1880 |
22 Feb 13 |
nicklas |
var marker_len = font_size; |
1820 |
06 Feb 13 |
olle |
context.lineWidth = 1; |
1820 |
06 Feb 13 |
olle |
context.beginPath(); |
1820 |
06 Feb 13 |
olle |
drawLine(box_xcenter, hgt, box_xcenter, hgt + marker_len, context, xoff, yoff, wdt, hgt); |
1820 |
06 Feb 13 |
olle |
context.stroke(); |
1820 |
06 Feb 13 |
olle |
// Draw scale marker value |
1820 |
06 Feb 13 |
olle |
var value_text = '' + value; |
1880 |
22 Feb 13 |
nicklas |
var text_axis_yoff = 1.5*font_size; |
1880 |
22 Feb 13 |
nicklas |
var text_marker_xoff = font_size / 2; |
1820 |
06 Feb 13 |
olle |
context.save(); |
1880 |
22 Feb 13 |
nicklas |
context.font = font_size + 'px sans-serif'; |
1820 |
06 Feb 13 |
olle |
context.textBaseline = 'bottom'; |
1820 |
06 Feb 13 |
olle |
context.translate(xoff + box_xcenter + text_marker_xoff, yoff - text_axis_yoff); |
1820 |
06 Feb 13 |
olle |
context.rotate(-Math.PI/2); |
1820 |
06 Feb 13 |
olle |
context.textAlign = "left"; |
1820 |
06 Feb 13 |
olle |
context.fillText(value_text, 0, 0); |
1820 |
06 Feb 13 |
olle |
context.restore(); |
1820 |
06 Feb 13 |
olle |
410 |
} |
1820 |
06 Feb 13 |
olle |
411 |
|
1820 |
06 Feb 13 |
olle |
412 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws y-axis left scale marker with text in a canvas context, using coordinates |
1820 |
06 Feb 13 |
olle |
* where x increases to the right and y increases upwards, |
1820 |
06 Feb 13 |
olle |
* and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
416 |
*/ |
1880 |
22 Feb 13 |
nicklas |
function drawScaleMarkerYLeft(value, font_size, markerText, min, max, min_off, max_off, context, xoff, yoff, wdt, hgt) |
1820 |
06 Feb 13 |
olle |
418 |
{ |
1820 |
06 Feb 13 |
olle |
var eff_hgt = hgt - min_off - max_off; |
1820 |
06 Feb 13 |
olle |
var value_range = max - min; |
1820 |
06 Feb 13 |
olle |
var marker_yoff = min_off + eff_hgt*((value - min)/value_range); |
1880 |
22 Feb 13 |
nicklas |
var marker_len = font_size; |
1820 |
06 Feb 13 |
olle |
// Draw scale marker |
1820 |
06 Feb 13 |
olle |
context.lineWidth = 1; |
1820 |
06 Feb 13 |
olle |
context.beginPath(); |
1820 |
06 Feb 13 |
olle |
drawLine(0, marker_yoff, -marker_len, marker_yoff, context, xoff, yoff, wdt, hgt); |
1820 |
06 Feb 13 |
olle |
context.stroke(); |
1820 |
06 Feb 13 |
olle |
// Draw scale marker value |
1845 |
15 Feb 13 |
olle |
var value_text = '' + markerText; |
1880 |
22 Feb 13 |
nicklas |
var text_axis_xoff = -1.5*font_size; |
1880 |
22 Feb 13 |
nicklas |
var text_marker_yoff = -font_size / 2; |
1880 |
22 Feb 13 |
nicklas |
context.font = font_size+'px sans-serif'; |
1820 |
06 Feb 13 |
olle |
context.textBaseline = 'bottom'; |
1820 |
06 Feb 13 |
olle |
context.textAlign="right"; |
1820 |
06 Feb 13 |
olle |
drawText(value_text, text_axis_xoff, marker_yoff + text_marker_yoff, context, xoff, yoff, wdt, hgt); |
1820 |
06 Feb 13 |
olle |
436 |
} |
1820 |
06 Feb 13 |
olle |
437 |
|
1820 |
06 Feb 13 |
olle |
438 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws y-axis right scale marker with text in a canvas context, using coordinates |
1820 |
06 Feb 13 |
olle |
* where x increases to the right and y increases upwards, |
1820 |
06 Feb 13 |
olle |
* and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
442 |
*/ |
1880 |
22 Feb 13 |
nicklas |
function drawScaleMarkerYRight(value, markerText, font_size, min, max, min_off, max_off, context, xoff, yoff, wdt, hgt) |
1820 |
06 Feb 13 |
olle |
444 |
{ |
1820 |
06 Feb 13 |
olle |
var eff_hgt = hgt - min_off - max_off; |
1820 |
06 Feb 13 |
olle |
var value_range = max - min; |
1820 |
06 Feb 13 |
olle |
var marker_yoff = min_off + eff_hgt*((value - min)/value_range); |
1880 |
22 Feb 13 |
nicklas |
var marker_len = font_size; |
1820 |
06 Feb 13 |
olle |
// Draw scale marker |
1820 |
06 Feb 13 |
olle |
context.lineWidth = 1; |
1820 |
06 Feb 13 |
olle |
context.beginPath(); |
1820 |
06 Feb 13 |
olle |
drawLine(wdt, marker_yoff, wdt + marker_len, marker_yoff, context, xoff, yoff, wdt, hgt); |
1820 |
06 Feb 13 |
olle |
context.stroke(); |
1820 |
06 Feb 13 |
olle |
// Draw scale marker value |
1845 |
15 Feb 13 |
olle |
var value_text = '' + markerText; |
1880 |
22 Feb 13 |
nicklas |
var text_axis_xoff = font_size * 1.5; |
1880 |
22 Feb 13 |
nicklas |
var text_marker_yoff = -font_size / 2; |
1880 |
22 Feb 13 |
nicklas |
context.font = font_size + 'px sans-serif'; |
1820 |
06 Feb 13 |
olle |
context.textBaseline = 'bottom'; |
1820 |
06 Feb 13 |
olle |
context.textAlign="left"; |
1820 |
06 Feb 13 |
olle |
drawText(value_text, wdt + text_axis_xoff, marker_yoff + text_marker_yoff, context, xoff, yoff, wdt, hgt); |
1820 |
06 Feb 13 |
olle |
462 |
} |
1820 |
06 Feb 13 |
olle |
463 |
|
1820 |
06 Feb 13 |
olle |
464 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws dotted line for y-value in a canvas context, using coordinates |
1820 |
06 Feb 13 |
olle |
* where x increases to the right and y increases upwards, |
1820 |
06 Feb 13 |
olle |
* and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
468 |
*/ |
1820 |
06 Feb 13 |
olle |
function drawDottedLineY(value, min, max, min_off, max_off, context, xoff, yoff, wdt, hgt) |
1820 |
06 Feb 13 |
olle |
470 |
{ |
1820 |
06 Feb 13 |
olle |
var eff_hgt = hgt - min_off - max_off; |
1820 |
06 Feb 13 |
olle |
var value_range = max - min; |
1820 |
06 Feb 13 |
olle |
var line_yoff = min_off + eff_hgt*((value - min)/value_range); |
1820 |
06 Feb 13 |
olle |
// Draw dotted line for y-value |
1820 |
06 Feb 13 |
olle |
context.lineWidth = 1; |
1820 |
06 Feb 13 |
olle |
context.beginPath(); |
1820 |
06 Feb 13 |
olle |
drawDottedLineHorizontal(0, wdt, line_yoff, context, xoff, yoff, wdt, hgt); |
1820 |
06 Feb 13 |
olle |
context.stroke(); |
1820 |
06 Feb 13 |
olle |
479 |
} |
1820 |
06 Feb 13 |
olle |
480 |
|
1820 |
06 Feb 13 |
olle |
481 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws y-axis left scale title in a canvas context, using coordinates |
1820 |
06 Feb 13 |
olle |
* where x increases to the right and y increases upwards, |
1820 |
06 Feb 13 |
olle |
* and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
485 |
*/ |
1880 |
22 Feb 13 |
nicklas |
function drawTitleYLeft(title, font_size, context, xoff, yoff, wdt, hgt) |
1820 |
06 Feb 13 |
olle |
487 |
{ |
1820 |
06 Feb 13 |
olle |
// Add y axis title |
1820 |
06 Feb 13 |
olle |
context.save(); |
1880 |
22 Feb 13 |
nicklas |
context.font = font_size + 'px sans-serif'; |
1820 |
06 Feb 13 |
olle |
context.textBaseline = 'bottom'; |
1880 |
22 Feb 13 |
nicklas |
context.translate(xoff, yoff + hgt/2); |
1820 |
06 Feb 13 |
olle |
context.rotate(-Math.PI/2); |
1820 |
06 Feb 13 |
olle |
context.textAlign = "center"; |
1820 |
06 Feb 13 |
olle |
context.fillText(title, 0, 0); |
1820 |
06 Feb 13 |
olle |
context.restore(); |
1820 |
06 Feb 13 |
olle |
497 |
} |
1820 |
06 Feb 13 |
olle |
498 |
|
1820 |
06 Feb 13 |
olle |
499 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws y-axis right scale title in a canvas context, using coordinates |
1820 |
06 Feb 13 |
olle |
* where x increases to the right and y increases upwards, |
1820 |
06 Feb 13 |
olle |
* and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
503 |
*/ |
1880 |
22 Feb 13 |
nicklas |
function drawTitleYRight(title, font_size, context, xoff, yoff, wdt, hgt) |
1820 |
06 Feb 13 |
olle |
505 |
{ |
1820 |
06 Feb 13 |
olle |
// Add y axis title |
1820 |
06 Feb 13 |
olle |
context.save(); |
1880 |
22 Feb 13 |
nicklas |
context.font = font_size + 'px sans-serif'; |
1820 |
06 Feb 13 |
olle |
context.textBaseline = 'top'; |
1880 |
22 Feb 13 |
nicklas |
context.translate(xoff + wdt, yoff + hgt/2); |
1820 |
06 Feb 13 |
olle |
context.rotate(-Math.PI/2); |
1820 |
06 Feb 13 |
olle |
context.textAlign = "center"; |
1820 |
06 Feb 13 |
olle |
context.fillText(title, 0, 0); |
1820 |
06 Feb 13 |
olle |
context.restore(); |
1820 |
06 Feb 13 |
olle |
515 |
} |
1820 |
06 Feb 13 |
olle |
516 |
|
1820 |
06 Feb 13 |
olle |
517 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws a rectangle in a canvas context, using coordinates |
1820 |
06 Feb 13 |
olle |
* where x increases to the right and y increases upwards, |
1820 |
06 Feb 13 |
olle |
* and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
* Rectangle input is given as coordinates for lower left corner |
1820 |
06 Feb 13 |
olle |
* and rectangle width and height. |
1820 |
06 Feb 13 |
olle |
523 |
*/ |
1820 |
06 Feb 13 |
olle |
function drawRect(x, y, rect_wdt, rect_hgt, context, xoff, yoff, wdt, hgt) |
1820 |
06 Feb 13 |
olle |
525 |
{ |
1880 |
22 Feb 13 |
nicklas |
var x_with_offset = fixCoordinate(context, x + xoff); |
1880 |
22 Feb 13 |
nicklas |
var y_with_offset = fixCoordinate(context, yoff + hgt - y - rect_hgt); |
1820 |
06 Feb 13 |
olle |
// Draw box rectangle |
1820 |
06 Feb 13 |
olle |
context.strokeStyle = "#000000"; |
1880 |
22 Feb 13 |
nicklas |
context.strokeRect(x_with_offset, y_with_offset, Math.floor(rect_wdt), Math.floor(rect_hgt)); |
1820 |
06 Feb 13 |
olle |
531 |
} |
1820 |
06 Feb 13 |
olle |
532 |
|
1820 |
06 Feb 13 |
olle |
533 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws a line in a canvas context, using coordinates |
1820 |
06 Feb 13 |
olle |
* where x increases to the right and y increases upwards, |
1820 |
06 Feb 13 |
olle |
* and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
537 |
*/ |
1820 |
06 Feb 13 |
olle |
function drawLine(x1, y1, x2, y2, context, xoff, yoff, wdt, hgt) |
1820 |
06 Feb 13 |
olle |
539 |
{ |
1880 |
22 Feb 13 |
nicklas |
var x1_with_offset = fixCoordinate(context, x1 + xoff); |
1880 |
22 Feb 13 |
nicklas |
var x2_with_offset = fixCoordinate(context, x2 + xoff); |
1880 |
22 Feb 13 |
nicklas |
var y1_with_offset = fixCoordinate(context, yoff + hgt - y1); |
1880 |
22 Feb 13 |
nicklas |
var y2_with_offset = fixCoordinate(context, yoff + hgt - y2); |
1820 |
06 Feb 13 |
olle |
context.moveTo(x1_with_offset, y1_with_offset); |
1820 |
06 Feb 13 |
olle |
context.lineTo(x2_with_offset, y2_with_offset); |
1820 |
06 Feb 13 |
olle |
546 |
} |
1880 |
22 Feb 13 |
nicklas |
547 |
|
1820 |
06 Feb 13 |
olle |
548 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws a dashed vertical line in a canvas context, using coordinates |
1820 |
06 Feb 13 |
olle |
* where x increases to the right and y increases upwards, |
1820 |
06 Feb 13 |
olle |
* and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
552 |
*/ |
1880 |
22 Feb 13 |
nicklas |
function drawDashedLineVertical(x, y1, y2, context, xoff, yoff, wdt, hgt, dash_size, space_size) |
1820 |
06 Feb 13 |
olle |
554 |
{ |
1820 |
06 Feb 13 |
olle |
var sign = 1; |
1820 |
06 Feb 13 |
olle |
if (y1 > y2) |
1820 |
06 Feb 13 |
olle |
557 |
{ |
1820 |
06 Feb 13 |
olle |
sign = -1; |
1820 |
06 Feb 13 |
olle |
559 |
} |
1820 |
06 Feb 13 |
olle |
var y = y1; |
1820 |
06 Feb 13 |
olle |
var y_new; |
1820 |
06 Feb 13 |
olle |
while (sign*(y2 - y) > 0) |
1820 |
06 Feb 13 |
olle |
563 |
{ |
1880 |
22 Feb 13 |
nicklas |
y_new = y + sign*dash_size; |
1820 |
06 Feb 13 |
olle |
if (sign*y_new > sign*y2) |
1820 |
06 Feb 13 |
olle |
566 |
{ |
1820 |
06 Feb 13 |
olle |
y_new = y2; |
1820 |
06 Feb 13 |
olle |
568 |
} |
1820 |
06 Feb 13 |
olle |
drawLine(x, y, x, y_new, context, xoff, yoff, wdt, hgt); |
1880 |
22 Feb 13 |
nicklas |
y = y_new + sign*space_size; |
1820 |
06 Feb 13 |
olle |
571 |
} |
1820 |
06 Feb 13 |
olle |
572 |
} |
1820 |
06 Feb 13 |
olle |
573 |
|
1820 |
06 Feb 13 |
olle |
574 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws a dotted horizontal line in a canvas context, using coordinates |
1820 |
06 Feb 13 |
olle |
* where x increases to the right and y increases upwards, |
1820 |
06 Feb 13 |
olle |
* and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
578 |
*/ |
1820 |
06 Feb 13 |
olle |
function drawDottedLineHorizontal(x1, x2, y, context, xoff, yoff, wdt, hgt) |
1820 |
06 Feb 13 |
olle |
580 |
{ |
1880 |
22 Feb 13 |
nicklas |
var dash_step1 = 2; |
1880 |
22 Feb 13 |
nicklas |
var space_step1 = 4; |
1820 |
06 Feb 13 |
olle |
var sign = 1; |
1820 |
06 Feb 13 |
olle |
if (x1 > x2) |
1820 |
06 Feb 13 |
olle |
585 |
{ |
1820 |
06 Feb 13 |
olle |
sign = -1; |
1820 |
06 Feb 13 |
olle |
587 |
} |
1820 |
06 Feb 13 |
olle |
var x = x1; |
1820 |
06 Feb 13 |
olle |
var x_new; |
1820 |
06 Feb 13 |
olle |
while (sign*(x2 - x) > 0) |
1820 |
06 Feb 13 |
olle |
591 |
{ |
1820 |
06 Feb 13 |
olle |
x_new = x + sign*dash_step1; |
1820 |
06 Feb 13 |
olle |
if (sign*x_new > sign*x2) |
1820 |
06 Feb 13 |
olle |
594 |
{ |
1820 |
06 Feb 13 |
olle |
x_new = x2; |
1820 |
06 Feb 13 |
olle |
596 |
} |
1820 |
06 Feb 13 |
olle |
drawLine(x, y, x_new, y, context, xoff, yoff, wdt, hgt); |
1820 |
06 Feb 13 |
olle |
x = x_new + sign*space_step1; |
1820 |
06 Feb 13 |
olle |
599 |
} |
1820 |
06 Feb 13 |
olle |
600 |
} |
1820 |
06 Feb 13 |
olle |
601 |
|
1820 |
06 Feb 13 |
olle |
602 |
/** |
1820 |
06 Feb 13 |
olle |
* Draws text in a canvas context, using coordinates |
1820 |
06 Feb 13 |
olle |
* where x increases to the right and y increases upwards, |
1820 |
06 Feb 13 |
olle |
* and where the offsets are added automatically. |
1820 |
06 Feb 13 |
olle |
606 |
*/ |
1820 |
06 Feb 13 |
olle |
function drawText(text, x1, y1, context, xoff, yoff, wdt, hgt) |
1820 |
06 Feb 13 |
olle |
608 |
{ |
1880 |
22 Feb 13 |
nicklas |
var x1_with_offset = fixCoordinate(context, x1 + xoff); |
1880 |
22 Feb 13 |
nicklas |
var y1_with_offset = fixCoordinate(context, yoff + hgt - y1); |
1820 |
06 Feb 13 |
olle |
context.fillText(text, x1_with_offset, y1_with_offset); |
1820 |
06 Feb 13 |
olle |
612 |
} |
1820 |
06 Feb 13 |
olle |
613 |
|
1820 |
06 Feb 13 |
olle |
614 |
/** |
1820 |
06 Feb 13 |
olle |
* Calculates an array of values for y-axis scale markers (ticks), |
1820 |
06 Feb 13 |
olle |
* given the min and max values of the data, and the numbers |
1820 |
06 Feb 13 |
olle |
* of markers (optional). |
1820 |
06 Feb 13 |
olle |
618 |
* |
1820 |
06 Feb 13 |
olle |
* Based on PHP example on |
1820 |
06 Feb 13 |
olle |
* http://stackoverflow.com/questions/326679/choosing-an-attractive-linear-scale-for-a-graphs-y-axis |
1820 |
06 Feb 13 |
olle |
* but with modified behavior for step size < 1. |
1820 |
06 Feb 13 |
olle |
622 |
*/ |
1820 |
06 Feb 13 |
olle |
function calculateScaleMarkerArray(minDataValue, maxDataValue, numTicks) |
1820 |
06 Feb 13 |
olle |
624 |
{ |
1820 |
06 Feb 13 |
olle |
if (numTicks == null || numTicks <= 0) |
1820 |
06 Feb 13 |
olle |
626 |
{ |
1820 |
06 Feb 13 |
olle |
numTicks = 10; |
1820 |
06 Feb 13 |
olle |
628 |
} |
1820 |
06 Feb 13 |
olle |
// Adjust ticks if needed |
1820 |
06 Feb 13 |
olle |
if (numTicks < 2) |
1820 |
06 Feb 13 |
olle |
631 |
{ |
1820 |
06 Feb 13 |
olle |
numTicks = 2; |
1820 |
06 Feb 13 |
olle |
633 |
} |
1820 |
06 Feb 13 |
olle |
else if (numTicks > 2) |
1820 |
06 Feb 13 |
olle |
635 |
{ |
1820 |
06 Feb 13 |
olle |
numTicks -= 2; |
1820 |
06 Feb 13 |
olle |
637 |
} |
1820 |
06 Feb 13 |
olle |
638 |
|
1820 |
06 Feb 13 |
olle |
// If min and max are identical, then adjust these values. |
1820 |
06 Feb 13 |
olle |
if (minDataValue == maxDataValue) |
1820 |
06 Feb 13 |
olle |
641 |
{ |
1820 |
06 Feb 13 |
olle |
minDataValue = minDataValue - 10; |
1820 |
06 Feb 13 |
olle |
maxDataValue = maxDataValue + 10; |
1820 |
06 Feb 13 |
olle |
644 |
} |
1820 |
06 Feb 13 |
olle |
645 |
|
1820 |
06 Feb 13 |
olle |
// Determine range |
1820 |
06 Feb 13 |
olle |
var range = maxDataValue - minDataValue; |
1820 |
06 Feb 13 |
olle |
// Get raw step value |
1820 |
06 Feb 13 |
olle |
var tempStep = range/numTicks; |
1820 |
06 Feb 13 |
olle |
// Calculate pretty step value |
1820 |
06 Feb 13 |
olle |
var mag = Math.floor(Math.log(tempStep)/Math.log(10) + 0.001); |
1820 |
06 Feb 13 |
olle |
var magPow = Math.pow(10, mag); |
1820 |
06 Feb 13 |
olle |
if (magPow >= 1) |
1820 |
06 Feb 13 |
olle |
654 |
{ |
1820 |
06 Feb 13 |
olle |
magPow = Math.floor(magPow + 0.001); |
1820 |
06 Feb 13 |
olle |
656 |
} |
1820 |
06 Feb 13 |
olle |
var magMsd = Math.floor(tempStep/magPow + 0.5); |
1820 |
06 Feb 13 |
olle |
var stepSize = magMsd*magPow; |
1820 |
06 Feb 13 |
olle |
// Correction for stepSize < 1000 |
1820 |
06 Feb 13 |
olle |
if (stepSize < 1000) |
1820 |
06 Feb 13 |
olle |
661 |
{ |
1820 |
06 Feb 13 |
olle |
if (stepSize >= 750) |
1820 |
06 Feb 13 |
olle |
663 |
{ |
1820 |
06 Feb 13 |
olle |
stepSize = 1000; |
1820 |
06 Feb 13 |
olle |
665 |
} |
1820 |
06 Feb 13 |
olle |
else if (stepSize >= 250) |
1820 |
06 Feb 13 |
olle |
667 |
{ |
1820 |
06 Feb 13 |
olle |
stepSize = 500; |
1820 |
06 Feb 13 |
olle |
669 |
} |
1820 |
06 Feb 13 |
olle |
else if (stepSize >= 75) |
1820 |
06 Feb 13 |
olle |
671 |
{ |
1820 |
06 Feb 13 |
olle |
stepSize = 100; |
1820 |
06 Feb 13 |
olle |
673 |
} |
1820 |
06 Feb 13 |
olle |
else if (stepSize >= 25) |
1820 |
06 Feb 13 |
olle |
675 |
{ |
1820 |
06 Feb 13 |
olle |
stepSize = 50; |
1820 |
06 Feb 13 |
olle |
677 |
} |
1820 |
06 Feb 13 |
olle |
else if (stepSize >= 7.5) |
1820 |
06 Feb 13 |
olle |
679 |
{ |
1820 |
06 Feb 13 |
olle |
stepSize = 10; |
1820 |
06 Feb 13 |
olle |
681 |
} |
1820 |
06 Feb 13 |
olle |
else if (stepSize >= 2.5) |
1820 |
06 Feb 13 |
olle |
683 |
{ |
1820 |
06 Feb 13 |
olle |
stepSize = 5; |
1820 |
06 Feb 13 |
olle |
685 |
} |
1820 |
06 Feb 13 |
olle |
else if (stepSize >= 0.75) |
1820 |
06 Feb 13 |
olle |
687 |
{ |
1820 |
06 Feb 13 |
olle |
stepSize = 1; |
1820 |
06 Feb 13 |
olle |
689 |
} |
1820 |
06 Feb 13 |
olle |
else if (stepSize >= 0.25) |
1820 |
06 Feb 13 |
olle |
691 |
{ |
1820 |
06 Feb 13 |
olle |
stepSize = 0.5; |
1820 |
06 Feb 13 |
olle |
693 |
} |
1820 |
06 Feb 13 |
olle |
else if (stepSize >= 0.075) |
1820 |
06 Feb 13 |
olle |
695 |
{ |
1820 |
06 Feb 13 |
olle |
stepSize = 0.1; |
1820 |
06 Feb 13 |
olle |
697 |
} |
1820 |
06 Feb 13 |
olle |
else if (stepSize >= 0.025) |
1820 |
06 Feb 13 |
olle |
699 |
{ |
1820 |
06 Feb 13 |
olle |
stepSize = 0.05; |
1820 |
06 Feb 13 |
olle |
701 |
} |
1820 |
06 Feb 13 |
olle |
else if (stepSize >= 0.0075) |
1820 |
06 Feb 13 |
olle |
703 |
{ |
1820 |
06 Feb 13 |
olle |
stepSize = 0.01; |
1820 |
06 Feb 13 |
olle |
705 |
} |
1820 |
06 Feb 13 |
olle |
else if (stepSize >= 0.0025) |
1820 |
06 Feb 13 |
olle |
707 |
{ |
1820 |
06 Feb 13 |
olle |
stepSize = 0.005; |
1820 |
06 Feb 13 |
olle |
709 |
} |
1820 |
06 Feb 13 |
olle |
else if (stepSize >= 0.00075) |
1820 |
06 Feb 13 |
olle |
711 |
{ |
1820 |
06 Feb 13 |
olle |
stepSize = 0.001; |
1820 |
06 Feb 13 |
olle |
713 |
} |
1820 |
06 Feb 13 |
olle |
else if (stepSize >= 0.00025) |
1820 |
06 Feb 13 |
olle |
715 |
{ |
1820 |
06 Feb 13 |
olle |
stepSize = 0.0005; |
1820 |
06 Feb 13 |
olle |
717 |
} |
1820 |
06 Feb 13 |
olle |
else if (stepSize >= 0.000075) |
1820 |
06 Feb 13 |
olle |
719 |
{ |
1820 |
06 Feb 13 |
olle |
stepSize = 0.0001; |
1820 |
06 Feb 13 |
olle |
721 |
} |
1820 |
06 Feb 13 |
olle |
722 |
} |
1820 |
06 Feb 13 |
olle |
723 |
|
1820 |
06 Feb 13 |
olle |
// Build y label array |
1820 |
06 Feb 13 |
olle |
// Lower and upper bounds calculations |
1820 |
06 Feb 13 |
olle |
var lowerBound = stepSize*Math.floor(minDataValue/stepSize); |
1820 |
06 Feb 13 |
olle |
var upperBound = stepSize*Math.ceil(maxDataValue/stepSize); |
1820 |
06 Feb 13 |
olle |
// Build array |
1820 |
06 Feb 13 |
olle |
var tickValuesArr = []; |
1820 |
06 Feb 13 |
olle |
var value = lowerBound; |
1820 |
06 Feb 13 |
olle |
var i = 0; |
1820 |
06 Feb 13 |
olle |
tickValuesArr[i] = value; |
1820 |
06 Feb 13 |
olle |
value += stepSize; |
1820 |
06 Feb 13 |
olle |
while(value <= upperBound) |
1820 |
06 Feb 13 |
olle |
735 |
{ |
1820 |
06 Feb 13 |
olle |
i++; |
1820 |
06 Feb 13 |
olle |
tickValuesArr[i] = value; |
1820 |
06 Feb 13 |
olle |
value += stepSize; |
1820 |
06 Feb 13 |
olle |
739 |
} |
1820 |
06 Feb 13 |
olle |
return tickValuesArr; |
1820 |
06 Feb 13 |
olle |
741 |
} |
1820 |
06 Feb 13 |
olle |
742 |
|
1820 |
06 Feb 13 |
olle |
743 |
/** |
1820 |
06 Feb 13 |
olle |
* Trims an array of values for y-axis scale markers (ticks), |
1820 |
06 Feb 13 |
olle |
* to only include values in given value range. |
1820 |
06 Feb 13 |
olle |
746 |
*/ |
1820 |
06 Feb 13 |
olle |
function trimScaleMarkerArray(origTickValuesArr, minValue, maxValue) |
1820 |
06 Feb 13 |
olle |
748 |
{ |
1820 |
06 Feb 13 |
olle |
// Build trimmed y label array |
1820 |
06 Feb 13 |
olle |
var tickValuesArr = []; |
1820 |
06 Feb 13 |
olle |
var value; |
1820 |
06 Feb 13 |
olle |
var k = 0; |
1820 |
06 Feb 13 |
olle |
for (i=0; i < origTickValuesArr.length; i++) |
1820 |
06 Feb 13 |
olle |
754 |
{ |
1820 |
06 Feb 13 |
olle |
value = origTickValuesArr[i]; |
1820 |
06 Feb 13 |
olle |
if (value >= minValue && value <= maxValue) |
1820 |
06 Feb 13 |
olle |
757 |
{ |
1820 |
06 Feb 13 |
olle |
// Add value to trimmed array |
1820 |
06 Feb 13 |
olle |
tickValuesArr[k] = value; |
1820 |
06 Feb 13 |
olle |
k++; |
1820 |
06 Feb 13 |
olle |
761 |
} |
1820 |
06 Feb 13 |
olle |
762 |
} |
1820 |
06 Feb 13 |
olle |
return tickValuesArr; |
1820 |
06 Feb 13 |
olle |
764 |
} |
1880 |
22 Feb 13 |
nicklas |
765 |
|
1880 |
22 Feb 13 |
nicklas |
766 |
/* |
1880 |
22 Feb 13 |
nicklas |
Fixes a plotting coordinate by aligning it so that anti-aliasing |
1880 |
22 Feb 13 |
nicklas |
is avoided. For 1-pixel line widths the coordate should alway be a |
1880 |
22 Feb 13 |
nicklas |
half-value, eg. 1.5, 10.5, etc. |
1880 |
22 Feb 13 |
nicklas |
770 |
*/ |
1880 |
22 Feb 13 |
nicklas |
function fixCoordinate(context, value) |
1880 |
22 Feb 13 |
nicklas |
772 |
{ |
1880 |
22 Feb 13 |
nicklas |
return Math.floor(value) + (context.lineWidth % 2) / 2; |
1880 |
22 Feb 13 |
nicklas |
774 |
} |
1880 |
22 Feb 13 |
nicklas |
775 |
|