ChartAct / web /Chart.js /bar /chart_0063_bar.html
wumumu's picture
Add files using upload-large-folder tool
dae061a verified
Raw History Blame Contribute Delete
4.09 kB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Programmatic Event Triggers</title>
<style>
:root {
color-scheme: light;
}
body {
margin: 0;
padding: 20px;
font-family: Segoe UI, Arial, sans-serif;
background: #f5f7fb;
color: #1f2a37;
}
h1 {
margin: 0 0 10px 0;
font-size: 20px;
font-weight: 600;
}
#legend-container {
margin-bottom: 8px;
}
#chart-wrap {
position: relative;
width: min(1100px, 100%);
height: 520px;
background: #ffffff;
border: 1px solid #e2e8f0;
border-radius: 10px;
padding: 12px;
box-sizing: border-box;
}
#chart {
width: 100%;
height: 100%;
}
</style>
<script src="./luxon.min.js"></script>
<script src="./chart.umd.min.js"></script>
<script src="./chartjs-adapter-luxon.umd.min.js"></script>
</head>
<body>
<h1>Programmatic Event Triggers</h1>
<div id="legend-container"></div>
<div id="chart-wrap">
<canvas id="chart"></canvas>
</div>
<script type="text/plain" data-chart-inline="true">
function triggerHover(chart) {
if (chart.getActiveElements().length > 0) {
chart.setActiveElements([]);
} else {
chart.setActiveElements([
{
datasetIndex: 0,
index: 0,
}, {
datasetIndex: 1,
index: 0,
}
]);
}
chart.update();
}
function triggerTooltip(chart) {
const tooltip = chart.tooltip;
if (tooltip.getActiveElements().length > 0) {
tooltip.setActiveElements([], {x: 0, y: 0});
} else {
const chartArea = chart.chartArea;
tooltip.setActiveElements([
{
datasetIndex: 0,
index: 2,
}, {
datasetIndex: 1,
index: 2,
}
],
{
x: (chartArea.left + chartArea.right) / 2,
y: (chartArea.top + chartArea.bottom) / 2,
});
}
chart.update();
}
const DATA_COUNT = 7;
const NUMBER_CFG = {count: DATA_COUNT, min: -100, max: 100};
const labels = ["January", "February", "March", "April", "May", "June", "July"];
const data = {
labels: labels,
datasets: [
{
label: 'Dataset 1',
data: [-32.92009602, 90.24005487, 42.82407407, 79.15294925, 46.14026063, -61.15226337, -10.62585734],
borderColor: Utils.CHART_COLORS.red,
backgroundColor: Utils.transparentize(Utils.CHART_COLORS.red, 0.5),
hoverBorderWidth: 5,
hoverBorderColor: 'green',
},
{
label: 'Dataset 2',
data: [89.07750343, -55.86934156, -35.58984911, 85.71844993, -74.66049383, 70.9276406, -97.2702332],
borderColor: Utils.CHART_COLORS.blue,
backgroundColor: Utils.transparentize(Utils.CHART_COLORS.blue, 0.5),
hoverBorderWidth: 5,
hoverBorderColor: 'green',
}
]
};
const config = {
type: 'bar',
data: data,
options: {
},
};
function forceEnableHoverTooltip(chartConfig) {
if (!chartConfig || typeof chartConfig !== 'object') {
return;
}
if (!chartConfig.options || typeof chartConfig.options !== 'object') {
chartConfig.options = {};
}
const options = chartConfig.options;
if (!options.plugins || typeof options.plugins !== 'object') {
options.plugins = {};
}
const plugins = options.plugins;
if (plugins.tooltip === false) {
plugins.tooltip = {};
}
if (!plugins.tooltip || typeof plugins.tooltip !== 'object') {
plugins.tooltip = {};
}
const hasExternalTooltip = typeof plugins.tooltip.external === 'function';
if (!hasExternalTooltip) {
plugins.tooltip.enabled = true;
}
}
forceEnableHoverTooltip(config);
const sampleCanvas = document.getElementById('chart');
const sampleChart = new Chart(sampleCanvas, config);
</script>
<script src="./chart_0063_bar.inline01.js"></script>
</body>
</html>