|
| 1 | +/** @format */ |
| 2 | +import angular, { IRootScopeService } from "angular" |
| 3 | +import range from "lodash/range" |
| 4 | +import { Chart } from "chart.js/auto" |
| 5 | +import { getLang, loc } from "@/i18n" |
| 6 | +import { |
| 7 | + calculateYearTicks, |
| 8 | + getSeries, |
| 9 | + getSeriesSelected, |
| 10 | + getCountUndatedSelected, |
| 11 | + getSpan, |
| 12 | + getCountUndated, |
| 13 | +} from "@/timeseries" |
| 14 | +import { html } from "@/util" |
| 15 | + |
| 16 | +angular.module("korpApp").component("corpusTimeGraph", { |
| 17 | + template: html`<canvas id="time-graph-chart" height="80"></canvas>`, |
| 18 | + controller: [ |
| 19 | + "$rootScope", |
| 20 | + function ($rootScope: IRootScopeService) { |
| 21 | + const { min, max } = getSpan() |
| 22 | + |
| 23 | + const datasetsDated = [ |
| 24 | + { |
| 25 | + label: loc("corpselector_selected"), |
| 26 | + data: {}, |
| 27 | + backgroundColor: "navy", |
| 28 | + }, |
| 29 | + { |
| 30 | + label: loc("corpselector_all"), |
| 31 | + data: getSeries(), |
| 32 | + backgroundColor: "lightgrey", |
| 33 | + }, |
| 34 | + ] |
| 35 | + |
| 36 | + // If there is undated data, add another bar. |
| 37 | + // To include the undated part on the side, make it show as a year slightly higher than the max of dated data. |
| 38 | + const undatedFakeYear: number = max + Math.max(2, Math.ceil((max - min) / 60)) |
| 39 | + const datasetsUndated = [ |
| 40 | + { |
| 41 | + label: loc("corpselector_selected"), |
| 42 | + data: {}, |
| 43 | + backgroundColor: "#cd5c5c", |
| 44 | + }, |
| 45 | + { |
| 46 | + label: loc("corpselector_all"), |
| 47 | + data: { [undatedFakeYear]: getCountUndated() }, |
| 48 | + backgroundColor: "lightgrey", |
| 49 | + }, |
| 50 | + ] |
| 51 | + |
| 52 | + function updateSelectedData() { |
| 53 | + datasetsDated[0].data = getSeriesSelected() |
| 54 | + datasetsUndated[0].data[undatedFakeYear] = getCountUndatedSelected() || undefined |
| 55 | + } |
| 56 | + updateSelectedData() |
| 57 | + |
| 58 | + const chart = new Chart(document.getElementById("time-graph-chart") as HTMLCanvasElement, { |
| 59 | + type: "bar", |
| 60 | + data: getCountUndated() |
| 61 | + ? { |
| 62 | + // Labels need to be strings to match with the `{[year]: count}` format of the datasets. |
| 63 | + // `max + 1` because `range` excludes end value. |
| 64 | + labels: range(min, undatedFakeYear + 1).map(String), |
| 65 | + datasets: [...datasetsDated, ...datasetsUndated], |
| 66 | + } |
| 67 | + : { |
| 68 | + labels: range(min, max + 1).map(String), |
| 69 | + datasets: datasetsDated, |
| 70 | + }, |
| 71 | + options: { |
| 72 | + scales: { |
| 73 | + y: { |
| 74 | + display: false, |
| 75 | + beginAtZero: true, |
| 76 | + }, |
| 77 | + x: { |
| 78 | + ticks: { |
| 79 | + autoSkip: false, |
| 80 | + }, |
| 81 | + // Calculate what years to label. Subtract `min` because Chart.js wants the indices, not the labels. |
| 82 | + afterBuildTicks: (axis) => |
| 83 | + (axis.ticks = calculateYearTicks(min, max).map((v) => ({ value: v - min }))), |
| 84 | + }, |
| 85 | + }, |
| 86 | + datasets: { |
| 87 | + bar: { |
| 88 | + // Show bars behind each other, not next to. |
| 89 | + grouped: false, |
| 90 | + // Eliminate space between bars |
| 91 | + categoryPercentage: 1.0, |
| 92 | + barPercentage: 1.0, |
| 93 | + // Make low-resource years show more |
| 94 | + minBarLength: 2, |
| 95 | + }, |
| 96 | + }, |
| 97 | + locale: getLang(), |
| 98 | + plugins: { |
| 99 | + legend: { |
| 100 | + display: false, |
| 101 | + }, |
| 102 | + tooltip: { |
| 103 | + caretSize: 0, |
| 104 | + yAlign: "bottom", |
| 105 | + callbacks: { |
| 106 | + title: (items) => |
| 107 | + Number(items[0].label) < undatedFakeYear |
| 108 | + ? `${loc("corpselector_year")} ${items[0].label}` |
| 109 | + : loc("corpselector_undated"), |
| 110 | + }, |
| 111 | + // See `defaults` in https://github.com/chartjs/Chart.js/blob/master/src/plugins/plugin.tooltip.js |
| 112 | + animations: { |
| 113 | + opacity: { |
| 114 | + duration: 100, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + interaction: { |
| 120 | + mode: "nearest", |
| 121 | + axis: "x", |
| 122 | + intersect: false, |
| 123 | + }, |
| 124 | + animation: { |
| 125 | + duration: 200, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }) |
| 129 | + |
| 130 | + $rootScope.$on("corpuschooserchange", () => { |
| 131 | + updateSelectedData() |
| 132 | + // `'none'` to disable animations. Animations would be nice, but they look weird when new data has different min/max year. |
| 133 | + // TODO Do animations look better if data is given as array including empty years, not a record? |
| 134 | + chart.update("none") |
| 135 | + }) |
| 136 | + }, |
| 137 | + ], |
| 138 | +}) |
0 commit comments