2023-07-27 08:31:52 +03:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2023-01-03 07:08:52 +02:00
|
|
|
<template>
|
|
|
|
<div :class="$style.root">
|
|
|
|
<button v-for="item in items" class="_button item" :class="{ disabled: item.hidden }" @click="onClick(item)">
|
2024-01-30 12:53:53 +02:00
|
|
|
<span class="box" :style="{ background: type === 'line' ? item.strokeStyle?.toString() : item.fillStyle?.toString() }"></span>
|
2023-01-03 07:08:52 +02:00
|
|
|
{{ item.text }}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2023-12-07 07:42:09 +02:00
|
|
|
import { shallowRef } from 'vue';
|
2023-01-03 07:08:52 +02:00
|
|
|
import { Chart, LegendItem } from 'chart.js';
|
|
|
|
|
2023-12-07 07:42:09 +02:00
|
|
|
const chart = shallowRef<Chart>();
|
2024-01-30 12:53:53 +02:00
|
|
|
const type = shallowRef<string>();
|
2023-12-07 07:42:09 +02:00
|
|
|
const items = shallowRef<LegendItem[]>([]);
|
2023-01-03 07:08:52 +02:00
|
|
|
|
|
|
|
function update(_chart: Chart, _items: LegendItem[]) {
|
2023-12-07 07:42:09 +02:00
|
|
|
chart.value = _chart,
|
|
|
|
items.value = _items;
|
2024-01-30 12:53:53 +02:00
|
|
|
if ('type' in _chart.config) type.value = _chart.config.type;
|
2023-01-03 07:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function onClick(item: LegendItem) {
|
2023-12-07 07:42:09 +02:00
|
|
|
if (chart.value == null) return;
|
2024-01-30 12:53:53 +02:00
|
|
|
if (type.value === 'pie' || type.value === 'doughnut') {
|
2023-01-03 07:08:52 +02:00
|
|
|
// Pie and doughnut charts only have a single dataset and visibility is per item
|
2024-02-02 11:15:25 +02:00
|
|
|
if (item.index != null) chart.value.toggleDataVisibility(item.index);
|
2023-01-03 07:08:52 +02:00
|
|
|
} else {
|
2024-02-02 11:15:25 +02:00
|
|
|
if (item.datasetIndex != null) chart.value.setDatasetVisibility(item.datasetIndex, !chart.value.isDatasetVisible(item.datasetIndex));
|
2023-01-03 07:08:52 +02:00
|
|
|
}
|
2023-12-07 07:42:09 +02:00
|
|
|
chart.value.update();
|
2023-01-03 07:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
update,
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" module>
|
|
|
|
.root {
|
|
|
|
display: flex;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
justify-content: center;
|
|
|
|
gap: 8px;
|
|
|
|
|
|
|
|
&:global {
|
|
|
|
> .item {
|
|
|
|
font-size: 85%;
|
|
|
|
padding: 4px 12px 4px 8px;
|
|
|
|
border: solid 1px var(--divider);
|
2023-10-31 20:44:34 +02:00
|
|
|
border-radius: var(--radius-ellipse);
|
2023-01-03 07:08:52 +02:00
|
|
|
|
|
|
|
&:hover {
|
|
|
|
border-color: var(--inputBorderHover);
|
|
|
|
}
|
|
|
|
|
|
|
|
&.disabled {
|
|
|
|
text-decoration: line-through;
|
2023-01-05 06:59:48 +02:00
|
|
|
opacity: 0.5;
|
2023-01-03 07:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
> .box {
|
|
|
|
display: inline-block;
|
|
|
|
width: 12px;
|
|
|
|
height: 12px;
|
2023-10-31 20:44:34 +02:00
|
|
|
border-radius: var(--radius-full);
|
2023-01-03 07:08:52 +02:00
|
|
|
vertical-align: -10%;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-06 13:19:27 +02:00
|
|
|
|
|
|
|
@container (max-width: 500px) {
|
|
|
|
.root {
|
|
|
|
font-size: 90%;
|
|
|
|
gap: 6px;
|
|
|
|
}
|
|
|
|
}
|
2023-01-03 07:08:52 +02:00
|
|
|
</style>
|