added vertical line plugin in policyplot

This commit is contained in:
Niko Feith 2023-05-25 17:52:03 +02:00
parent 8b21c795fb
commit 94af7725bd

View File

@ -7,11 +7,37 @@ import { onMounted, watch } from "vue";
import { usePStore } from "@/store/PolicyStore"; import { usePStore } from "@/store/PolicyStore";
import { Chart } from "chart.js/auto"; import { Chart } from "chart.js/auto";
import { computeRbfCurve } from "@/js_funs/online_rbf_policy"; import { computeRbfCurve } from "@/js_funs/online_rbf_policy";
import { useRLStore } from "@/store/RLStore";
const store = usePStore(); const store = usePStore();
const rlstore = useRLStore();
let chartHandle; let chartHandle;
let verticalLinePlugin = {
id: "verticalLinePlugin",
afterDraw: function (chart, args, options) {
let y_Scale = chart.scales["y"];
let x_Scale = chart.scales["x"];
let ctx = chart.ctx;
let xValue = options.xValue;
let x = x_Scale.getPixelForValue(xValue);
let top = y_Scale.top;
let bottom = y_Scale.bottom;
ctx.save();
ctx.beginPath();
ctx.moveTo(x, top);
ctx.lineTo(x, bottom);
ctx.lineWidth = 1;
ctx.strokeStyle = "#00ff00";
ctx.stroke();
ctx.restore();
},
};
Chart.register(verticalLinePlugin);
function buildChart() { function buildChart() {
const policy = store.getPolicy; const policy = store.getPolicy;
const policy_labels = Array(policy.length) const policy_labels = Array(policy.length)
@ -41,6 +67,9 @@ function buildChart() {
legend: { legend: {
display: false, display: false,
}, },
verticalLinePlugin: {
xValue: rlstore.current_time,
},
}, },
lineTension: 0, lineTension: 0,
tooltip: { tooltip: {
@ -75,7 +104,7 @@ onMounted(() => {
}); });
watch( watch(
() => store.getPolicy, () => rlstore.current_time,
() => { () => {
const policy = store.getPolicy; const policy = store.getPolicy;
chartHandle.options.scales.x.labels = Array(policy.length) chartHandle.options.scales.x.labels = Array(policy.length)