-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Replace Raphael with Chart.js for pie chart
- Loading branch information
Showing
10 changed files
with
99 additions
and
402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** @format */ | ||
import angular, { IController, IRootScopeService } from "angular" | ||
import { Chart } from "chart.js" | ||
import { html } from "@/util" | ||
|
||
const defaultMode: Mode = "relative" | ||
|
||
angular.module("korpApp").component("corpusDistributionChart", { | ||
template: html` | ||
<div class="flex flex-col gap-2 items-center"> | ||
<canvas id="distribution-chart"></canvas> | ||
<div id="statistics_switch"> | ||
<a data-mode="relative" href="javascript:"> {{ 'statstable_relfigures' | loc }} </a> | ||
<a data-mode="absolute" href="javascript:"> {{ 'statstable_absfigures' | loc }} </a> | ||
</div> | ||
</div> | ||
`, | ||
bindings: { | ||
row: "<", | ||
}, | ||
controller: [ | ||
"$rootScope", | ||
function ($rootScope: IRootScopeService) { | ||
const $ctrl = this as CorpusDistributionChartController | ||
let chart: Chart<"pie"> | ||
|
||
const getValues = (mode: Mode) => $ctrl.row.map((corpus) => corpus.values[mode == "relative" ? 1 : 0]) | ||
|
||
$ctrl.$onInit = () => { | ||
chart = new Chart("distribution-chart", { | ||
type: "pie", | ||
data: { | ||
labels: $ctrl.row.map((corpus) => corpus.title), | ||
datasets: [{ data: getValues(defaultMode) }], | ||
}, | ||
options: { | ||
locale: $rootScope["lang"], | ||
plugins: { | ||
legend: { | ||
display: false, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
setTimeout(() => { | ||
const radioList = ($("#statistics_switch") as any).radioList({ | ||
selected: defaultMode, | ||
change: () => { | ||
const mode = radioList.radioList("getSelected").attr("data-mode") | ||
chart.data.datasets[0].data = getValues(mode) | ||
chart.update() | ||
}, | ||
}) | ||
}) | ||
} | ||
}, | ||
], | ||
}) | ||
|
||
type CorpusDistributionChartController = IController & { | ||
row: { title: string; values: [number, number] }[] | ||
} | ||
|
||
type Mode = "relative" | "absolute" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.