summaryrefslogtreecommitdiffstats
path: root/web_src/js/components/RepoActivityTopAuthors.vue
blob: 3752bc582b7d709d2c407846dbe6459cd074b0fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<script>
import {Bar} from 'vue-chartjs';
import {
  Chart,
  Tooltip,
  BarElement,
  CategoryScale,
  LinearScale,
} from 'chart.js';
import {chartJsColors} from '../utils/color.js';
import {createApp} from 'vue';

Chart.defaults.color = chartJsColors.text;
Chart.defaults.borderColor = chartJsColors.border;

Chart.register(
  CategoryScale,
  LinearScale,
  BarElement,
  Tooltip,
);

const sfc = {
  components: {Bar},
  props: {
    locale: {
      type: Object,
      required: true,
    },
  },
  data: () => ({
    colors: {
      barColor: 'green',
    },

    // possible keys:
    // * avatar_link: (...)
    // * commits: (...)
    // * home_link: (...)
    // * login: (...)
    // * name: (...)
    activityTopAuthors: window.config.pageData.repoActivityTopAuthors || [],
    i18nCommitActivity: this,
  }),
  methods: {
    graphPoints() {
      return {
        datasets: [{
          label: this.locale.commitActivity,
          data: this.activityTopAuthors.map((item) => item.commits),
          backgroundColor: this.colors.barColor,
          barThickness: 40,
          borderWidth: 0,
          tension: 0.3,
        }],
        labels: this.activityTopAuthors.map((item) => item.name),
      };
    },
    getOptions() {
      return {
        responsive: true,
        maintainAspectRatio: false,
        animation: true,
        scales: {
          x: {
            type: 'category',
            grid: {
              display: false,
            },
            ticks: {
              // Disable the drawing of the labels on the x-asis and force them all
              // of them to be 'shown', this avoids them being internally skipped
              // for some data points. We rely on the internally generated ticks
              // to know where to draw our own ticks. Set rotation to 90 degree
              // and disable autoSkip. autoSkip is disabled to ensure no ticks are
              // skipped and rotation is set to avoid messing with the width of the chart.
              color: 'transparent',
              minRotation: 90,
              maxRotation: 90,
              autoSkip: false,
            },
          },
          y: {
            ticks: {
              stepSize: 1,
            },
          },
        },
      };
    },
  },
  mounted() {
    const refStyle = window.getComputedStyle(this.$refs.style);
    this.colors.barColor = refStyle.backgroundColor;

    for (const item of this.activityTopAuthors) {
      const img = new Image();
      img.src = item.avatar_link;
      item.avatar_img = img;
    }

    Chart.register({
      id: 'image_label',
      afterDraw: (chart) => {
        const xAxis = chart.boxes[0];
        const yAxis = chart.boxes[1];
        for (const [index] of xAxis.ticks.entries()) {
          const x = xAxis.getPixelForTick(index);
          const img = this.activityTopAuthors[index].avatar_img;

          chart.ctx.save();
          chart.ctx.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight, x - 10, yAxis.bottom + 10, 20, 20);
          chart.ctx.restore();
        }
      },
      beforeEvent: (chart, args) => {
        const event = args.event;
        if (event.type !== 'mousemove' && event.type !== 'click') return;

        const yAxis = chart.boxes[1];
        if (event.y < yAxis.bottom + 10 || event.y > yAxis.bottom + 30) {
          chart.canvas.style.cursor = '';
          return;
        }

        const xAxis = chart.boxes[0];
        const pointIdx = xAxis.ticks.findIndex((_, index) => {
          const x = xAxis.getPixelForTick(index);
          return event.x >= x - 10 && event.x <= x + 10;
        });

        if (pointIdx === -1) {
          chart.canvas.style.cursor = '';
          return;
        }

        chart.canvas.style.cursor = 'pointer';
        if (event.type === 'click' && this.activityTopAuthors[pointIdx].home_link) {
          window.location.href = this.activityTopAuthors[pointIdx].home_link;
        }
      },
    });
  },
};

export function initRepoActivityTopAuthorsChart() {
  const el = document.getElementById('repo-activity-top-authors-chart');
  if (el) {
    createApp(sfc, {
      locale: {
        commitActivity: el.getAttribute('data-locale-commit-activity'),
      },
    }).mount(el);
  }
}

export default sfc; // activate the IDE's Vue plugin
</script>
<template>
  <div>
    <div class="activity-bar-graph" ref="style" style="width: 0; height: 0;"/>
    <Bar height="150px" :data="graphPoints()" :options="getOptions()"/>
  </div>
</template>