literyzjs-timeline/bbtimeline-background-canvas.js

168 lines
4.3 KiB
JavaScript
Raw Normal View History

2023-10-18 13:44:49 +00:00
class BBTimelineBackgroundCanvas extends BBTimelineCanvas {
constructor(parentEl, el) {
super(parentEl, el);
const a = this;
a.GraphRectangle = { X: 0, Y: 0, W: 0, H: 0 };
a.Margin = 0;
a.StepHeight = 0;
a.NoStep = 0;
a.initialiseComponents();
}
initialiseComponents() {
super.initialiseComponents();
const a = this;
a.ClientRectangle = {
X: a.Parent.Padding.Left,
Y: a.Parent.Padding.Top,
W: (a.Parent.Size.W - (a.Parent.Padding.Left + a.Parent.Padding.Right)),
H: (a.Parent.Size.H - (a.Parent.Padding.Top + a.Parent.Padding.Bottom))
};
a.GraphRectangle = {
X: a.ClientRectangle.X,
Y: a.ClientRectangle.Y,
W: a.ClientRectangle.W,
2023-10-19 23:22:29 +00:00
H: (a.ClientRectangle.H - a.XAxisHeight)
2023-10-18 13:44:49 +00:00
};
a.Margin = (a.Parent.Marker.BorderWidth * 2);
a.StepHeight = a.Parent.Marker.Width + a.Margin;
a.NoStep = Math.floor(a.GraphRectangle.H / a.StepHeight);
a.Invalidate();
}
2023-10-19 23:22:29 +00:00
get XAxisHeight() {
const a = this;
const labelSize = a.measureText(a.Parent.Axis.Font, "0");
return labelSize.Height + a.Parent.Axis.LabelSpacing + (a.Parent.Axis.X.DayLineHeight * 2);
}
get XAxisPositions() {
const a = this;
const endPosX = (a.GraphRectangle.X + a.GraphRectangle.W);
let result = [];
let x = a.GraphRectangle.X;
let date = a.Parent.ConvertToDate(a.Parent.ShowDate);
// Rollback one day
date.setDate(date.getDate() - 1);
while (true) {
if (x >= endPosX) {
break;
}
result.push({
Date: a.Parent.DateToInternalString(date),
X: x
});
x += (a.Parent.Axis.X.HourLineSpace * a.Parent.Axis.X.NoPartPerDay);
date.setDate(date.getDate() + 1);
}
return result;
}
2023-10-18 13:44:49 +00:00
Invalidate() {
const a = this;
a.Clear();
a.drawAxis();
a.drawXAxisTicks();
a.drawXAxisLabels();
if (a.Parent.Debug) a.drawRectangle(a.ClientRectangle, "red");
if (a.Parent.Debug) a.drawRectangle(a.GraphRectangle, "red");
}
drawAxis() {
const a = this;
a.CTX.beginPath();
a.CTX.moveTo(a.GraphRectangle.X, a.GraphRectangle.Y);
a.CTX.lineTo(a.GraphRectangle.X, (a.GraphRectangle.H + a.GraphRectangle.Y));
a.CTX.lineTo((a.GraphRectangle.W + a.GraphRectangle.X), (a.GraphRectangle.H + a.GraphRectangle.Y));
a.CTX.lineWidth = a.Parent.Axis.LineWidth;
a.CTX.strokeStyle = a.Parent.Axis.LineColour1;
a.CTX.stroke();
}
drawXAxisLabels() {
const a = this;
const posY = (a.GraphRectangle.Y + a.GraphRectangle.H) + a.Parent.Axis.LineWidth;
2023-10-19 23:22:29 +00:00
a.XAxisPositions.forEach(function(e, i) {
2023-10-18 13:44:49 +00:00
const date = a.Parent.ConvertToDate(e.Date);
let writeLabel = false;
if ((i == 0)) {
// Don't label first entry if too close to the next month
if (date.getDate() < 25) {
writeLabel = true;
}
} else if (date.getDate() == 1) {
writeLabel = true;
}
// if (i == 0) {
// return;
// }
const labelSize = a.drawText(e.X, (posY + a.Parent.Axis.X.DayLineHeight), a.Parent.DateToString(date, "dd"), a.Parent.Axis.Font, a.Parent.Axis.LabelColour, "center");
// Write month on first of the month
if (writeLabel) {
a.drawText(e.X, (posY + a.Parent.Axis.X.DayLineHeight + labelSize.Height + a.Parent.Axis.LabelSpacing), a.Parent.DateToString(date, "MMMM yyyy"), a.Parent.Axis.Font, a.Parent.Axis.LabelColour, "left");
}
});
}
2023-10-19 23:22:29 +00:00
drawXAxisTicks() {
2023-10-18 13:44:49 +00:00
const a = this;
2023-10-19 23:22:29 +00:00
let startPosX = a.GraphRectangle.X;
const endPosX = (a.GraphRectangle.X + a.GraphRectangle.W);
const posY = (a.GraphRectangle.Y + a.GraphRectangle.H) + a.Parent.Axis.LineWidth;
2023-10-18 13:44:49 +00:00
2023-10-19 23:22:29 +00:00
let i = 0;
2023-10-18 13:44:49 +00:00
while (true) {
2023-10-19 23:22:29 +00:00
if (startPosX >= endPosX) {
2023-10-18 13:44:49 +00:00
break;
}
2023-10-19 23:22:29 +00:00
a.CTX.beginPath();
a.CTX.moveTo(startPosX, posY);
2023-10-18 13:44:49 +00:00
2023-10-19 23:22:29 +00:00
if ((i % a.Parent.Axis.X.NoPartPerDay) == 0) {
a.CTX.lineTo(startPosX, (posY + a.Parent.Axis.X.DayLineHeight));
a.CTX.strokeStyle = a.Parent.Axis.X.DayLineColour;
} else {
a.CTX.lineTo(startPosX, (posY + a.Parent.Axis.X.HourLineHeight));
a.CTX.strokeStyle = a.Parent.Axis.X.HourLineColour;
}
2023-10-18 13:44:49 +00:00
2023-10-19 23:22:29 +00:00
a.CTX.lineWidth = a.Parent.Axis.LineWidth;
a.CTX.stroke();
2023-10-18 13:44:49 +00:00
2023-10-19 23:22:29 +00:00
startPosX += a.Parent.Axis.X.HourLineSpace;
2023-10-18 13:44:49 +00:00
2023-10-19 23:22:29 +00:00
i++;
}
2023-10-18 13:44:49 +00:00
}
}