Skip to content

Commit fb6a425

Browse files
committed
refactor: Remove Flot, use Chart.js for corpus chooser time graph
1 parent 7fcd4e7 commit fb6a425

File tree

10 files changed

+220
-223
lines changed

10 files changed

+220
-223
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- Improved UI reactivity for Simple search
1717
- Make the search button(s) more visible [#308](https://github.com/spraakbanken/korp-frontend/issues/308)
1818
- Use native checkboxes in corpus chooser, not images [#362](https://github.com/spraakbanken/korp-frontend/issues/362)
19+
- Replaced JQuery Flot library with Chart.js, used in corpus chooser time graph
1920
- Added TypeScript definitions for Korp backend parameters and responses
2021
- Wrapped `GraphProxy`, `KwicProxy`, `LemgramProxy`, `StatsProxy` and `TimeProxy` with factories; see [@/util/Factory](./app/scripts/util.ts)
2122
- Removed the `stats_rewrite` config option, as the change above eliminated the need for this

app/scripts/components/corpus_chooser/corpus-chooser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import * as authenticationProxy from "@/components/auth/auth"
77
import { html } from "@/util"
88
import { loc } from "@/i18n"
99
import * as treeUtil from "./util"
10+
import "@/components/corpus_chooser/corpus-time-graph"
1011
import "@/components/corpus_chooser/info-box"
11-
import "@/components/corpus_chooser/time-graph"
1212
import "@/components/corpus_chooser/tree"
1313

1414
angular.module("korpApp").component("corpusChooser", {
@@ -52,7 +52,7 @@ angular.module("korpApp").component("corpusChooser", {
5252
<div ng-if="$ctrl.showChooser" class="corpus-chooser flex bg-white">
5353
<div class="popupchecks shrink-0 p-4 h-full">
5454
<div class="flex">
55-
<cc-time-graph ng-if="$ctrl.showTimeGraph"></cc-time-graph>
55+
<corpus-time-graph ng-if="$ctrl.showTimeGraph"></corpus-time-graph>
5656
<div class="p-2">
5757
<button ng-click="$ctrl.selectAll()" class="btn btn-default btn-sm w-full mb-2">
5858
<span class="fa-solid fa-check"></span>
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
})

app/scripts/components/corpus_chooser/time-graph.js

Lines changed: 0 additions & 190 deletions
This file was deleted.

0 commit comments

Comments
 (0)