ChartAct / web /Chart.js /scatter /chart_0055_scatter.inline01.js
wumumu's picture
Add files using upload-large-folder tool
dae061a verified
Raw History Blame Contribute Delete
12.3 kB
const components = { Tooltip: Chart.Tooltip };
const Utils = (() => {
let _seed = 1;
const valueOrDefault = (value, defaultValue) => (value === undefined ? defaultValue : value);
const MONTHS = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
];
const COLORS = [
'#4dc9f6', '#f67019', '#f53794', '#537bc4',
'#acc236', '#166a8f', '#00a950', '#58595b', '#8549ba'
];
const CHART_COLORS = {
red: 'rgb(255, 99, 132)',
orange: 'rgb(255, 159, 64)',
yellow: 'rgb(255, 205, 86)',
green: 'rgb(75, 192, 192)',
blue: 'rgb(54, 162, 235)',
purple: 'rgb(153, 102, 255)',
grey: 'rgb(201, 203, 207)'
};
const NAMED_COLORS = [
CHART_COLORS.red,
CHART_COLORS.orange,
CHART_COLORS.yellow,
CHART_COLORS.green,
CHART_COLORS.blue,
CHART_COLORS.purple,
CHART_COLORS.grey
];
function srand(seedValue) {
const normalized = Number(seedValue);
_seed = Number.isFinite(normalized) ? (normalized >>> 0) : 1;
}
function rand(min, max) {
const low = valueOrDefault(min, 0);
const high = valueOrDefault(max, 0);
_seed = (_seed * 9301 + 49297) % 233280;
return low + (_seed / 233280) * (high - low);
}
function numbers(config) {
const cfg = config || {};
const min = valueOrDefault(cfg.min, 0);
const max = valueOrDefault(cfg.max, 100);
const from = valueOrDefault(cfg.from, []);
const count = valueOrDefault(cfg.count, 8);
const decimals = valueOrDefault(cfg.decimals, 8);
const continuity = valueOrDefault(cfg.continuity, 1);
const dfactor = Math.pow(10, decimals) || 0;
const data = [];
for (let i = 0; i < count; ++i) {
const value = (from[i] || 0) + rand(min, max);
if (rand(0, 1) <= continuity) {
data.push(Math.round(dfactor * value) / dfactor);
} else {
data.push(null);
}
}
return data;
}
function points(config) {
const xs = numbers(config);
const ys = numbers(config);
return xs.map((x, i) => ({x, y: ys[i]}));
}
function bubbles(config) {
const cfg = config || {};
return points(cfg).map((pt) => ({...pt, r: rand(cfg.rmin, cfg.rmax)}));
}
function labels(config) {
const cfg = config || {};
const min = valueOrDefault(cfg.min, 0);
const max = valueOrDefault(cfg.max, 100);
const count = valueOrDefault(cfg.count, 8);
const decimals = valueOrDefault(cfg.decimals, 8);
const prefix = valueOrDefault(cfg.prefix, '');
const step = (max - min) / count;
const dfactor = Math.pow(10, decimals) || 0;
const values = [];
for (let i = min; i < max; i += step) {
values.push(prefix + Math.round(dfactor * i) / dfactor);
}
return values;
}
function months(config) {
const cfg = config || {};
const count = valueOrDefault(cfg.count, 12);
const section = cfg.section;
const values = [];
for (let i = 0; i < count; ++i) {
const value = MONTHS[Math.ceil(i) % 12];
values.push(value.substring(0, section));
}
return values;
}
function color(index) {
return COLORS[index % COLORS.length];
}
function parseColorToRgb(input) {
const value = String(input || '').trim();
const hex3 = /^#([0-9a-f]{3})$/i.exec(value);
if (hex3) {
const r = parseInt(hex3[1][0] + hex3[1][0], 16);
const g = parseInt(hex3[1][1] + hex3[1][1], 16);
const b = parseInt(hex3[1][2] + hex3[1][2], 16);
return [r, g, b];
}
const hex6 = /^#([0-9a-f]{6})$/i.exec(value);
if (hex6) {
const intValue = parseInt(hex6[1], 16);
const r = (intValue >> 16) & 255;
const g = (intValue >> 8) & 255;
const b = intValue & 255;
return [r, g, b];
}
const rgb = /^rgba?\(([^)]+)\)$/i.exec(value);
if (rgb) {
const parts = rgb[1].split(',').map((part) => Number(part.trim()));
if (parts.length >= 3) {
return [parts[0] || 0, parts[1] || 0, parts[2] || 0];
}
}
const helper = document.createElement('canvas').getContext('2d');
helper.fillStyle = value || '#000000';
const normalized = helper.fillStyle;
const fallback = /^rgba?\(([^)]+)\)$/i.exec(normalized);
if (fallback) {
const parts = fallback[1].split(',').map((part) => Number(part.trim()));
if (parts.length >= 3) {
return [parts[0] || 0, parts[1] || 0, parts[2] || 0];
}
}
return [0, 0, 0];
}
function transparentize(value, opacity) {
const alpha = opacity === undefined ? 0.5 : 1 - opacity;
const [r, g, b] = parseColorToRgb(value);
return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + alpha + ')';
}
const BASE_DATE_MS = Date.UTC(2021, 0, 1, 0, 0, 0, 0);
function newDate(days) {
return new Date(BASE_DATE_MS + (Number(days) || 0) * 24 * 60 * 60 * 1000);
}
function newDateString(days) {
return newDate(days).toISOString();
}
function parseISODate(str) {
const millis = Date.parse(str);
return {
toMillis() {
return millis;
}
};
}
return {
srand,
rand,
numbers,
points,
bubbles,
labels,
months,
color,
transparentize,
CHART_COLORS,
namedColor(index) {
return NAMED_COLORS[index % NAMED_COLORS.length];
},
newDate,
newDateString,
parseISODate
};
})();
window.components = components;
window.Utils = Utils;
Utils.srand(101064);
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);