ChartAct / web /Chart.js /scatter /chart_0055_scatter.html
wumumu's picture
Add files using upload-large-folder tool
dae061a verified
Raw History Blame Contribute Delete
9.19 kB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bubble Chart</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: 12px;
}
.legend-list {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
border: 1px solid #cbd5e1;
border-radius: 999px;
background: #ffffff;
color: #0f172a;
font: inherit;
cursor: pointer;
transition: background-color 0.2s ease, border-color 0.2s ease, opacity 0.2s ease;
}
.legend-item:hover {
background: #f8fafc;
border-color: #94a3b8;
}
.legend-item.is-hidden {
opacity: 0.45;
}
.legend-swatch {
width: 10px;
height: 10px;
border-radius: 999px;
flex: none;
}
#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>Bubble Chart</h1>
<div id="legend-container"></div>
<div id="chart-wrap">
<canvas id="chart"></canvas>
</div>
<script type="text/plain" data-chart-inline="true">
const DATA_COUNT = 16;
const MIN_XY = -150;
const MAX_XY = 100;
Utils.srand(110);
const __generatedDataValues = [
[{x: -0.7298096707819184, y: 14.870541838134443, v: 54.95970507544581}, {x: -52.115483539094654, y: 76.71789266117969, v: 23.799725651577504}, {x: -6.857638888888886, y: 19.930984224965727, v: 323.6582647462277}, {x: -10.78960905349794, y: -51.323516803840874, v: 371.20198902606313}, {x: 40.25527263374485, y: -32.878943758573385, v: 583.0975651577503}, {x: 0.44367283950614933, y: -70.56862997256515, v: 376.0116598079561}, {x: -26.057741769547334, y: -60.22590877914952, v: 166.61093964334705}, {x: 64.91769547325103, y: -147.6841135116598, v: 371.56207133058984}, {x: -122.4633487654321, y: 21.223422496570635, v: 407.53172153635114}, {x: 15.965792181069958, y: 50.66336591220852, v: 691.1865569272977}, {x: 84.37178497942386, y: 44.80238340192042, v: 639.193244170096}, {x: -63.078703703703695, y: -142.19285836762688, v: 668.2184499314129}, {x: -72.2190072016461, y: 93.8443072702332, v: 194.92884087791495}, {x: -88.88245884773663, y: -142.91945301783264, v: 635.9910836762689}, {x: -8.90239197530866, y: 1.6825274348422568, v: 408.0718449931413}, {x: 21.88786008230454, y: -118.18308470507544, v: 927.8377914951989}],
[{x: -142.34503600823047, y: -148.34962277091907, v: 611.9555898491084}, {x: -147.4344135802469, y: 15.349579903978054, v: 877.0919067215364}, {x: -139.2136059670782, y: -22.918810013717433, v: 139.9134087791495}, {x: -13.51594650205763, y: 91.01187414266118, v: 817.0867626886146}, {x: 83.8252314814815, y: -38.69170096021948, v: 325.2786351165981}, {x: 6.976594650205783, y: -57.86286865569272, v: 81.1556927297668}, {x: -139.8951903292181, y: -112.3349622770919, v: 501.3846021947874}, {x: -2.6234567901234698, y: -97.94131515775034, v: 2.632030178326475}, {x: 22.95846193415636, y: 89.48473936899865, v: 1.5646433470507546}, {x: 41.01723251028807, y: 54.109867969821664, v: 914.8491083676269}, {x: -94.28047839506172, y: -99.89926268861454, v: 159.15209190672152}, {x: -28.768004115226333, y: -18.375985939643357, v: 151.14026063100135}, {x: 91.7213220164609, y: -97.15363511659808, v: 307.48028120713303}, {x: -128.64583333333334, y: 17.934456447187927, v: 44.838820301783265}, {x: -85.70280349794238, y: -68.94504458161866, v: 779.8825445816186}, {x: 74.71707818930042, y: -3.6254715363511423, v: 929.2781207133058}]
];
let __generatedDataIndex = 0;
function generateData() {
const value = __generatedDataValues[__generatedDataIndex] ?? __generatedDataValues[__generatedDataValues.length - 1];
__generatedDataIndex += 1;
return JSON.parse(JSON.stringify(value));
}
const datasetLabels = ['数据集 A', '数据集 B'];
const data = {
datasets: datasetLabels.map((label, index) => ({
label,
legendColor: Utils.color(index),
data: generateData()
}))
};
function channelValue(x, y, values) {
return x < 0 && y < 0 ? values[0] : x < 0 ? values[1] : y < 0 ? values[2] : values[3];
}
function colorize(opaque, context) {
const value = context.raw;
const x = value.x / 100;
const y = value.y / 100;
const r = channelValue(x, y, [250, 150, 50, 0]);
const g = channelValue(x, y, [0, 50, 150, 250]);
const b = channelValue(x, y, [0, 150, 150, 250]);
const a = opaque ? 1 : 0.5 * value.v / 1000;
return 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
}
function getLegendItems(chart) {
return chart.data.datasets.map((dataset, datasetIndex) => ({
datasetIndex,
label: dataset.label || `数据集 ${datasetIndex + 1}`,
color: dataset.legendColor || Utils.color(datasetIndex),
hidden: !chart.isDatasetVisible(datasetIndex)
}));
}
const htmlLegendPlugin = {
id: 'htmlLegend',
afterUpdate(chart, args, options) {
const containerID = options && options.containerID;
if (!containerID) {
return;
}
const container = document.getElementById(containerID);
if (!container) {
return;
}
let list = container.querySelector('.legend-list');
if (!list) {
list = document.createElement('div');
list.className = 'legend-list';
container.appendChild(list);
}
while (list.firstChild) {
list.firstChild.remove();
}
getLegendItems(chart).forEach((item) => {
const button = document.createElement('button');
button.type = 'button';
button.className = item.hidden ? 'legend-item is-hidden' : 'legend-item';
button.setAttribute('aria-pressed', String(!item.hidden));
button.onclick = () => {
const visible = chart.isDatasetVisible(item.datasetIndex);
chart.setDatasetVisibility(item.datasetIndex, !visible);
chart.update();
};
const swatch = document.createElement('span');
swatch.className = 'legend-swatch';
swatch.style.background = item.color;
const label = document.createElement('span');
label.textContent = item.label;
button.appendChild(swatch);
button.appendChild(label);
list.appendChild(button);
});
}
};
const config = {
type: 'bubble',
plugins: [htmlLegendPlugin],
data: data,
options: {
aspectRatio: 1,
plugins: {
legend: {
display: false,
},
htmlLegend: {
containerID: 'legend-container',
},
tooltip: false,
},
elements: {
point: {
backgroundColor: colorize.bind(null, false),
borderColor: colorize.bind(null, true),
borderWidth: function(context) {
return Math.min(Math.max(1, context.datasetIndex + 1), 8);
},
hoverBackgroundColor: 'transparent',
hoverBorderColor: function(context) {
return Utils.color(context.datasetIndex);
},
hoverBorderWidth: function(context) {
return Math.round(8 * context.raw.v / 1000);
},
radius: function(context) {
const size = context.chart.width;
const base = Math.abs(context.raw.v) / 1000;
return (size / 24) * base;
}
}
}
}
};
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_0055_scatter.inline01.js"></script>
</body>
</html>