misskey/src/client/widgets/trends.chart.vue

90 lines
2.0 KiB
Vue
Raw Normal View History

2018-06-11 04:24:29 +02:00
<template>
2018-06-11 05:52:47 +02:00
<svg :viewBox="`0 0 ${ viewBoxX } ${ viewBoxY }`" style="overflow:visible">
2018-06-11 04:24:29 +02:00
<defs>
<linearGradient :id="gradientId" x1="0" x2="0" y1="1" y2="0">
<stop offset="0%" stop-color="hsl(200, 80%, 70%)"></stop>
<stop offset="100%" stop-color="hsl(90, 80%, 70%)"></stop>
</linearGradient>
<mask :id="maskId" x="0" y="0" :width="viewBoxX" :height="viewBoxY">
<polygon
:points="polygonPoints"
fill="#fff"
fill-opacity="0.5"/>
<polyline
:points="polylinePoints"
fill="none"
stroke="#fff"
2018-06-11 05:52:47 +02:00
stroke-width="2"/>
2018-06-11 04:24:29 +02:00
<circle
:cx="headX"
:cy="headY"
2018-06-11 05:52:47 +02:00
r="3"
2018-06-11 04:24:29 +02:00
fill="#fff"/>
</mask>
</defs>
<rect
2018-06-11 05:52:47 +02:00
x="-10" y="-10"
:width="viewBoxX + 20" :height="viewBoxY + 20"
2018-06-11 04:24:29 +02:00
:style="`stroke: none; fill: url(#${ gradientId }); mask: url(#${ maskId })`"/>
</svg>
</template>
<script lang="ts">
import Vue from 'vue';
import { v4 as uuid } from 'uuid';
2018-06-11 04:24:29 +02:00
export default Vue.extend({
props: {
src: {
type: Array,
required: true
}
},
data() {
return {
viewBoxX: 50,
viewBoxY: 30,
gradientId: uuid(),
maskId: uuid(),
polylinePoints: '',
polygonPoints: '',
headX: null,
2018-06-11 19:18:29 +02:00
headY: null,
clock: null
2018-06-11 04:24:29 +02:00
};
},
watch: {
src() {
this.draw();
}
},
created() {
this.draw();
2018-06-11 19:18:29 +02:00
// Vueが何故かWatchを発動させない場合があるので
this.clock = setInterval(this.draw, 1000);
},
beforeDestroy() {
clearInterval(this.clock);
2018-06-11 04:24:29 +02:00
},
methods: {
draw() {
const stats = this.src.slice().reverse();
const peak = Math.max.apply(null, stats) || 1;
2018-06-11 05:52:47 +02:00
const polylinePoints = stats.map((n, i) => [
i * (this.viewBoxX / (stats.length - 1)),
(1 - (n / peak)) * this.viewBoxY
]);
2018-06-11 04:24:29 +02:00
this.polylinePoints = polylinePoints.map(xy => `${xy[0]},${xy[1]}`).join(' ');
2018-06-11 05:52:47 +02:00
this.polygonPoints = `0,${ this.viewBoxY } ${ this.polylinePoints } ${ this.viewBoxX },${ this.viewBoxY }`;
2018-06-11 04:24:29 +02:00
this.headX = polylinePoints[polylinePoints.length - 1][0];
this.headY = polylinePoints[polylinePoints.length - 1][1];
}
}
});
</script>