与其使用JSCharting (一种具有缩小代码的专有图表框架),不如从 Chart.js之类的开源库开始。
下面的图表来自文档。我修改为符合一些基本数据。
用户脚本
// ==UserScript==
// @name Chart Example
// @namespace http://tampermonkey.net/
// @version 1.0.0
// @description try to take over the world!
// @author You
// @match https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/*
// @require https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js
// @resource CHART_JS_CSS https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.css
// @grant GM_addStyle
// @grant GM_getResourceText
// ==/UserScript==
(function() {
/* global Chart */
/* eslint-disable no-multi-spaces, no-return-assign */
'use strict';
GM_addStyle(`${GM_getResourceText('CHART_JS_CSS')}
.chart-wrapper {
position: fixed;
width: 400px;
height: 200px;
z-index: 1000;
right: 1em;
top: 1em;
background: rgba(255, 255, 255, 0.9);
border: thin solid grey;
}
`);
const main = () => {
document.body.prepend(createElement('div', {
props: {
className: 'chart-wrapper'
},
children: [
createElement('canvas', {
attrs: { id: 'my-canvas' }
})
]
}));
const chartData = {
label: '# of Votes',
data: [
{ key: 'Red' , value: 12 , backgroundColor: 'rgba(255, 99, 132, 0.2)' , borderColor: 'rgba(255, 99, 132, 1)' },
{ key: 'Blue' , value: 19 , backgroundColor: 'rgba(54, 162, 235, 0.2)' , borderColor: 'rgba(54, 162, 235, 1)' },
{ key: 'Yellow' , value: 3 , backgroundColor: 'rgba(255, 206, 86, 0.2)' , borderColor: 'rgba(255, 206, 86, 1)' },
{ key: 'Green' , value: 5 , backgroundColor: 'rgba(75, 192, 192, 0.2)' , borderColor: 'rgba(75, 192, 192, 1)' },
{ key: 'Purple' , value: 2 , backgroundColor: 'rgba(153, 102, 255, 0.2)' , borderColor: 'rgba(153, 102, 255, 1)' },
{ key: 'Orange' , value: 3 , backgroundColor: 'rgba(255, 159, 64, 0.2)' , borderColor: 'rgba(255, 159, 64, 1)' }
]
};
const myChart = createSimpleBarChart('#my-canvas', chartData);
};
const createChart = (canvas, settings) => new Chart((typeof canvas === 'string' ? document.querySelector(canvas) : canvas).getContext('2d'), settings);
const createElement = (tagName, config = {}) => {
const el = document.createElement(tagName);
if (config.attrs) Object.entries(config.attrs).forEach(([attr, val]) => el.setAttribute(attr, val));
if (config.props) Object.entries(config.props).forEach(([prop, val]) => el[prop] = val);
if (config.css) Object.entries(config.css).forEach(([prop, val]) => el.style[prop] = val);
if (config.children) config.children.forEach(child => el.append(child));
return el;
};
const createSimpleBarChart = (selector, chartData) => {
const { data, label } = chartData;
return createChart(selector, {
type: 'bar',
data: {
labels: data.map(({ key }) => key),
datasets: [{
label: label,
data: data.map(({ value }) => value),
backgroundColor: data.map(({ backgroundColor }) => backgroundColor),
borderColor: data.map(({ borderColor }) => borderColor),
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
};
main();
})();
演示
const main = () => {
document.body.prepend(createElement('div', {
props: {
className: 'chart-wrapper'
},
children: [
createElement('canvas', {
attrs: { id: 'my-canvas' }
})
]
}));
const chartData = {
label: '# of Votes',
data: [
{ key: 'Red' , value: 12 , backgroundColor: 'rgba(255, 99, 132, 0.2)' , borderColor: 'rgba(255, 99, 132, 1)' },
{ key: 'Blue' , value: 19 , backgroundColor: 'rgba(54, 162, 235, 0.2)' , borderColor: 'rgba(54, 162, 235, 1)' },
{ key: 'Yellow' , value: 3 , backgroundColor: 'rgba(255, 206, 86, 0.2)' , borderColor: 'rgba(255, 206, 86, 1)' },
{ key: 'Green' , value: 5 , backgroundColor: 'rgba(75, 192, 192, 0.2)' , borderColor: 'rgba(75, 192, 192, 1)' },
{ key: 'Purple' , value: 2 , backgroundColor: 'rgba(153, 102, 255, 0.2)' , borderColor: 'rgba(153, 102, 255, 1)' },
{ key: 'Orange' , value: 3 , backgroundColor: 'rgba(255, 159, 64, 0.2)' , borderColor: 'rgba(255, 159, 64, 1)' }
]
};
const myChart = createSimpleBarChart('#my-canvas', chartData);
};
const createChart = (canvas, settings) => new Chart((typeof canvas === 'string' ? document.querySelector(canvas) : canvas).getContext('2d'), settings);
const createElement = (tagName, config = {}) => {
const el = document.createElement(tagName);
if (config.attrs) Object.entries(config.attrs).forEach(([attr, val]) => el.setAttribute(attr, val));
if (config.props) Object.entries(config.props).forEach(([prop, val]) => el[prop] = val);
if (config.css) Object.entries(config.css).forEach(([prop, val]) => el.style[prop] = val);
if (config.children) config.children.forEach(child => el.append(child));
return el;
};
const createSimpleBarChart = (selector, chartData) => {
const { data, label } = chartData;
return createChart(selector, {
type: 'bar',
data: {
labels: data.map(({ key }) => key),
datasets: [{
label: label,
data: data.map(({ value }) => value),
backgroundColor: data.map(({ backgroundColor }) => backgroundColor),
borderColor: data.map(({ borderColor }) => borderColor),
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
};
main();
.chart-wrapper {
position: fixed;
width: 400px;
height: 200px;
z-index: 1000;
right: 1em;
top: 1em;
background: rgba(255, 255, 255, 0.9);
border: thin solid grey;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>