Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

punchcard.js: convert to typescript #4080

Merged
merged 5 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import * as d3 from "d3";

const containerSelector = "#punchcard-container";
const margin = { top: 10, right: 10, bottom: 20, left: 70 };
const labelsX = [...Array(24).keys()];

function initPunchcard(url) {
type chartType = d3.Selection<SVGGElement, unknown, HTMLElement, any>;

function initPunchcard(url: string): void {
// If this is defined outside of a function, the locale always defaults to "en".
const labelsY = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"].map(k => I18n.t(`js.weekdays.long.${k}`));

const container = d3.select(containerSelector);
const width = container.node().getBoundingClientRect().width;
const width = (container.node() as Element).getBoundingClientRect().width;
const innerWidth = width - margin.left - margin.right;
const unitSize = innerWidth / 24;
const innerHeight = unitSize * 7;
Expand Down Expand Up @@ -37,7 +38,7 @@ function initPunchcard(url) {
.attr("y", innerHeight / 2)
.style("text-anchor", "middle");

const processor = data => {
const processor = (data): void => {
if (data["status"] === "not available yet") {
setTimeout(() => d3.json(url).then(processor), 1000);
return;
Expand All @@ -50,18 +51,18 @@ function initPunchcard(url) {
const xAxis = d3.axisBottom(x)
.ticks(24)
.tickSize(0)
.tickFormat((d, i) => labelsX[i])
.tickFormat(hour => `${hour < 10 ? "0" : ""}${hour}:00`)
.tickPadding(10);
const yAxis = d3.axisLeft(y)
.ticks(7)
.tickSize(0)
.tickFormat((d, i) => labelsY[i])
.tickFormat((_d, i) => labelsY[i])
.tickPadding(10);

renderAxes(xAxis, yAxis, chart, innerHeight);
}

function renderAxes(xAxis, yAxis, chart, innerHeight) {
function renderAxes(xAxis: d3.Axis<d3.NumberValue>, yAxis: d3.Axis<d3.NumberValue>, chart: chartType, innerHeight: number): void {
chart.append("g")
.attr("class", "axis")
.attr("transform", `translate(0, ${innerHeight})`)
Expand All @@ -75,7 +76,7 @@ function renderAxes(xAxis, yAxis, chart, innerHeight) {
.style("display", "none");
}

function renderCard(data, unitSize, chart, x, y) {
function renderCard(data: Array<[string, number]>, unitSize: number, chart: chartType, x: d3.ScaleLinear<number, number>, y: d3.ScaleLinear<number, number>): void {
const maxVal = d3.max(data, d => d[1]);
const radius = d3.scaleSqrt()
.domain([0, maxVal])
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/packs/visualisations.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { initHeatmap } from "heatmap.ts";
import { initPunchcard } from "punchcard.js";
import { initPunchcard } from "punchcard.ts";

window.dodona.initHeatmap = initHeatmap;
window.dodona.initPunchcard = initPunchcard;