Fixed chartjs breaking changes

This commit is contained in:
2022-08-09 01:05:29 +02:00
parent 0015588f9c
commit 81bead113f
2 changed files with 75 additions and 56 deletions

View File

@@ -4,7 +4,34 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, defineProps, onMounted, watch } from "vue"; import { ref, computed, defineProps, onMounted, watch } from "vue";
import Chart from "chart.js"; import {
Chart,
LineElement,
BarElement,
PointElement,
LineController,
BarController,
LinearScale,
CategoryScale,
Legend,
Title,
Tooltip,
ChartType
} from "chart.js";
Chart.register(
LineElement,
BarElement,
PointElement,
LineController,
BarController,
LinearScale,
CategoryScale,
Legend,
Title,
Tooltip
);
import {} from "chart.js";
import type { Ref } from "vue"; import type { Ref } from "vue";
enum GraphValueTypes { enum GraphValueTypes {
@@ -25,7 +52,7 @@
interface Props { interface Props {
name?: string; name?: string;
data: IGraphData; data: IGraphData;
type: string; // TODO import types from chart.js type: ChartType;
stacked: boolean; stacked: boolean;
datasetDescriptionSuffix: string; datasetDescriptionSuffix: string;
@@ -33,14 +60,14 @@
graphValueType?: GraphValueTypes; graphValueType?: GraphValueTypes;
} }
Chart.defaults.global.elements.point.radius = 0; Chart.defaults.elements.point.radius = 0;
Chart.defaults.global.elements.point.hitRadius = 10; Chart.defaults.elements.point.hitRadius = 10;
Chart.defaults.global.elements.point.pointHoverRadius = 10; // Chart.defaults.elements.point.pointHoverRadius = 10;
Chart.defaults.global.elements.point.hoverBorderWidth = 4; Chart.defaults.elements.point.hoverBorderWidth = 4;
const props = defineProps<Props>(); const props = defineProps<Props>();
const graphInstance: Ref<any> = ref(null);
const graphCanvas: Ref<HTMLCanvasElement> = ref(null); const graphCanvas: Ref<HTMLCanvasElement> = ref(null);
let graphInstance = null;
onMounted(() => generateGraph()); onMounted(() => generateGraph());
watch(() => props.data, generateGraph); watch(() => props.data, generateGraph);
@@ -49,17 +76,20 @@
{ {
backgroundColor: "rgba(54, 162, 235, 0.2)", backgroundColor: "rgba(54, 162, 235, 0.2)",
borderColor: "rgba(54, 162, 235, 1)", borderColor: "rgba(54, 162, 235, 1)",
borderWidth: 1 borderWidth: 1,
tension: 0.4
}, },
{ {
backgroundColor: "rgba(255, 159, 64, 0.2)", backgroundColor: "rgba(255, 159, 64, 0.2)",
borderColor: "rgba(255, 159, 64, 1)", borderColor: "rgba(255, 159, 64, 1)",
borderWidth: 1 borderWidth: 1,
tension: 0.4
}, },
{ {
backgroundColor: "rgba(255, 99, 132, 0.2)", backgroundColor: "rgba(255, 99, 132, 0.2)",
borderColor: "rgba(255, 99, 132, 1)", borderColor: "rgba(255, 99, 132, 1)",
borderWidth: 1 borderWidth: 1,
tension: 0.4
} }
]; ];
const gridColor = getComputedStyle(document.documentElement).getPropertyValue( const gridColor = getComputedStyle(document.documentElement).getPropertyValue(
@@ -85,16 +115,15 @@
const graphOptions = { const graphOptions = {
maintainAspectRatio: false, maintainAspectRatio: false,
tooltips: { plugins: {
tooltip: {
callbacks: { callbacks: {
title: (tooltipItem, data) => `Watch date: ${tooltipItem[0].label}`, // title: (tooltipItem, data) => `Watch date: ${tooltipItem[0].label}`,
label: (tooltipItem, data) => { label: tooltipItem => {
let label = data.datasets[tooltipItem.datasetIndex].label; const context = tooltipItem.dataset.label.split(" ")[0];
let value = tooltipItem.value;
const context = label.split(" ")[0];
const text = `${context} ${props.tooltipDescriptionSuffix}`; const text = `${context} ${props.tooltipDescriptionSuffix}`;
let value = tooltipItem.raw;
if (props.graphValueType === GraphValueTypes.Time) { if (props.graphValueType === GraphValueTypes.Time) {
value = convertSecondsToHumanReadable(value); value = convertSecondsToHumanReadable(value);
} }
@@ -102,19 +131,21 @@
return ` ${text}: ${value}`; return ` ${text}: ${value}`;
} }
} }
}
}, },
scales: { scales: {
yAxes: [ xAxes: {
{ stacked: props.stacked,
gridLines: { gridLines: {
color: gridColor display: false
}
}, },
yAxes: {
stacked: props.stacked, stacked: props.stacked,
ticks: { ticks: {
// suggestedMax: 10000,
callback: (value, index, values) => { callback: (value, index, values) => {
if (props.graphValueType === GraphValueTypes.Time) { if (props.graphValueType === GraphValueTypes.Time) {
return convertSecondsToHumanReadable(value, values); return convertSecondsToHumanReadable(value);
} }
return value; return value;
@@ -122,31 +153,22 @@
beginAtZero: true beginAtZero: true
} }
} }
],
xAxes: [
{
stacked: props.stacked,
gridLines: {
display: false
}
}
]
} }
}; };
const chartData = { const chartData = {
labels: props.data.labels, labels: props.data.labels.toString().split(","),
datasets datasets
}; };
if (graphInstance.value) { if (graphInstance) {
graphInstance.value.clear(); graphInstance.clear();
graphInstance.value.data = chartData; graphInstance.data = chartData;
graphInstance.value.update(); graphInstance.update("none");
return; return;
} }
graphInstance.value = new Chart(graphCanvas.value, { graphInstance = new Chart(graphCanvas.value, {
type: props.type, type: props.type,
data: chartData, data: chartData,
options: graphOptions options: graphOptions

View File

@@ -63,15 +63,12 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, onMounted } from "vue"; import { ref, computed, onMounted } from "vue";
import Chart from "chart.js";
import { useStore } from "vuex"; import { useStore } from "vuex";
import Graph from "@/components/Graph.vue"; import Graph from "@/components/Graph.vue";
import ToggleButton from "@/components/ui/ToggleButton.vue"; import ToggleButton from "@/components/ui/ToggleButton.vue";
import IconStop from "@/icons/IconStop.vue"; import IconStop from "@/icons/IconStop.vue";
import { fetchGraphData } from "../api"; import { fetchGraphData } from "../api";
import type { Ref } from "vue"; import type { Ref } from "vue";
import type { ChartType } from "chart.js";
enum GraphTypes { enum GraphTypes {
Plays = "plays", Plays = "plays",