Skip to content

Commit 630c2dd

Browse files
committed
Add zoom/pan support to charts
1 parent bc396c0 commit 630c2dd

File tree

3 files changed

+71
-15
lines changed

3 files changed

+71
-15
lines changed

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"asciinema-player": "3.4.0",
2121
"chart.js": "4.3.0",
2222
"chartjs-adapter-dayjs-4": "1.0.4",
23+
"chartjs-plugin-zoom": "2.0.1",
2324
"clippie": "4.0.1",
2425
"css-loader": "6.8.1",
2526
"dayjs": "1.11.8",

web_src/js/components/RepoContributors.vue

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div>
3-
<Line :data="mainGraphData" :options="data.options" />
3+
<Line :data="mainGraphData" :options="options" />
44

55
<div class="ui attached segment two column grid">
66
<div
@@ -9,7 +9,7 @@
99
class="column stats-table"
1010
>
1111
<div class="ui top attached header gt-df gt-f1">
12-
<b class="ui right">#{{index + 1}}</b>
12+
<b class="ui right">#{{ index + 1 }}</b>
1313
<a :href="contributor.home_link">
1414
<img
1515
height="40"
@@ -34,7 +34,7 @@
3434
</div>
3535
</div>
3636
<div class="ui attached segment">
37-
<Line :data="graph(contributor.weeks)" :options="data.options" />
37+
<Line :data="graph(contributor.weeks)" :options="options" />
3838
</div>
3939
</div>
4040
</div>
@@ -56,6 +56,7 @@ import {
5656
LineElement,
5757
Filler,
5858
} from "chart.js";
59+
import zoomPlugin from "chartjs-plugin-zoom";
5960
import { Bar, Line } from "vue-chartjs";
6061
import "chartjs-adapter-dayjs-4/dist/chartjs-adapter-dayjs-4.esm";
6162
@@ -69,23 +70,55 @@ ChartJS.register(
6970
Legend,
7071
PointElement,
7172
LineElement,
72-
Filler
73+
Filler,
74+
zoomPlugin
7375
);
7476
7577
const sfc = {
7678
components: { Line },
7779
data: () => ({
7880
data: {
79-
options: {
81+
},
82+
83+
masterChartData: window.config.pageData.repoContributorsCommitStats || [],
84+
type: window.config.pageData.contributionType,
85+
individualChartsData:
86+
window.config.pageData.repoContributorsCommitStats || [],
87+
}),
88+
computed: {
89+
options() {
90+
return {
8091
responsive: true,
8192
plugins: {
8293
legend: {
8394
display: false,
84-
}
95+
},
96+
zoom: {
97+
pan: {
98+
enabled: true,
99+
mode: "x",
100+
threshold: 20,
101+
},
102+
limits: {
103+
x: {
104+
min: "original",
105+
max: "original",
106+
minRange: 1000000000,
107+
},
108+
},
109+
zoom: {
110+
wheel: {
111+
enabled: true,
112+
},
113+
pinch: {
114+
enabled: true,
115+
},
116+
mode: "x",
117+
},
118+
},
85119
},
86120
scales: {
87121
x: {
88-
// min: '2021-11-06',
89122
type: "time",
90123
time: {
91124
// unit: 'year'
@@ -94,17 +127,11 @@ const sfc = {
94127
},
95128
y: {
96129
min: 0,
130+
max: this.maxMainGraph(),
97131
},
98132
},
99-
},
133+
}
100134
},
101-
102-
masterChartData: window.config.pageData.repoContributorsCommitStats || [],
103-
type: window.config.pageData.contributionType,
104-
individualChartsData:
105-
window.config.pageData.repoContributorsCommitStats || [],
106-
}),
107-
computed: {
108135
mainGraphData() {
109136
return {
110137
datasets: [
@@ -134,6 +161,14 @@ const sfc = {
134161
},
135162
},
136163
methods: {
164+
maxMainGraph() {
165+
const maxValue = Math.max(...this.masterChartData[""].weeks.map(o => o[this.type]))
166+
const [cooefficient, exp] = maxValue.toExponential().split('e')
167+
if (Number(cooefficient) % 1 == 0) {
168+
return maxValue
169+
}
170+
return (1 - Number(cooefficient) % 1) * 10 ** Number(exp) + maxValue
171+
},
137172
additions(data) {
138173
return Object.values(data).reduce((acc, item) => {
139174
return acc + item.additions;

0 commit comments

Comments
 (0)