Plotly.js
Plotly.js 是一个包含 40 多种图表类型的图表库
- 水平和垂直条形图
- 饼图和甜甜圈图
- 折线图
- 散点图和气泡图
- 方程图
- 3D 图表
- 统计图
- SVG 地图
- ...
Plotly.js 在 MIT 许可证下是免费且开源的。安装和使用均不收费。您可以使用 Github 查看源代码、报告问题并贡献代码。
柱状图
源代码
const xArray = ["意大利","法国","西班牙","美国","阿根廷"];
const yArray = [55, 49, 44, 24, 15];
const data = [{
x: xArray,
y: yArray,
type: "bar",
orientation:"v",
marker: {color:"rgba(0,0,255)"}
}];
const layout = {title:"全球葡萄酒产量"};
Plotly.newPlot("myPlot", data, layout);
水平条形图
源代码
const xArray = [55, 49, 44, 24, 15];
const yArray = ["意大利","法国","西班牙","美国","阿根廷"];
const data = [{
x: xArray,
y: yArray,
type: "bar",
orientation: "h",
marker: {color:"rgba(255,0,0,0.6)"}
}];
const layout = {title:"全球葡萄酒产量"};
Plotly.newPlot("myPlot", data, layout);
饼图
要显示饼图而不是条形图,请将 x 和 y 更改为标签和值,并将 type 改为 "pie"
const data = [{
labels: xArray,
values: yArray,
type: "pie"
}];
甜甜圈图
要显示甜甜圈图而不是饼图,请添加一个孔
const data = [{
labels: xArray,
values: yArray,
hole: .4,
type: "pie"
}];
绘制方程
源代码
let exp = "Math.sin(x)";
// 生成值
const xValues = [];
const yValues = [];
for (let x = 0; x <= 10; x += 0.1) {
xValues.push(x);
yValues.push(eval(exp));
}
// 使用 Plotly 显示
const data = [{x:xValues, y:yValues, mode:"lines"}];
const layout = {title: "y = " + exp};
Plotly.newPlot("myPlot", data, layout);
自己动手试一试 »
要显示散点图而不是折线图,请将 mode 改为 markers
// 使用 Plotly 显示
const data = [{x:xValues, y:yValues, mode:"markers"}];
const layout = {title: "y = " + exp};
Plotly.newPlot("myPlot", data, layout);
自己动手试一试 »
散点图
源代码
const xArray = [50,60,70,80,90,100,110,120,130,140,150];
const yArray = [7,8,8,9,9,9,10,11,14,14,15];
// 定义数据
const data = [{
x: xArray,
y: yArray,
mode:"markers",
type:"scatter"
}];
// 定义布局
const layout = {
xaxis: {range: [40, 160], title: "平方米"},
yaxis: {range: [5, 16], title: "百万美元"},
title: "房价与面积"
};
Plotly.newPlot("myPlot", data, layout);
折线图
源代码
const xArray = [50,60,70,80,90,100,110,120,130,140,150];
const yArray = [7,8,8,9,9,9,10,11,14,14,15];
// 定义数据
const data = [{
x: xArray,
y: yArray,
mode: "lines",
type: "scatter"
}];
// 定义布局
const layout = {
xaxis: {range: [40, 160], title: "平方米"},
yaxis: {range: [5, 16], title: "百万美元"},
title: "房价与面积"
};
// 使用 Plotly 显示
Plotly.newPlot("myPlot", data, layout);
气泡图
气泡图是散点图,其标记具有可变的颜色、大小和符号。
它是一种三维图表,只有两个轴(x 和 y),气泡的大小传达了第三个维度。
源代码
const xArray = [1,2,3,4];
const yArray = [10,20,30,40];
const trace1 = {
x: xArray,
y: yArray,
mode: 'markers',
marker: {
color: ['red', 'green', 'blue', 'orange'],
size: [20, 30, 40, 50]
}
};
const data = [trace1];
const layout = {
title: "绘制气泡"
};
Plotly.newPlot('myPlot', data, layout);
线性图
源代码
let exp = "x + 17";
// 生成值
const xValues = [];
const yValues = [];
for (let x = 0; x <= 10; x += 1) {
yValues.push(eval(exp));
xValues.push(x);
}
// 定义数据
const data = [{
x: xValues,
y: yValues,
mode: "lines"
}];
// 定义布局
const layout = {title: "y = " + exp};
// 使用 Plotly 显示
Plotly.newPlot("myPlot", data, layout);
多行
源代码
let exp1 = "x";
let exp2 = "1.5*x";
let exp3 = "1.5*x + 7";
// 生成值
const x1Values = [];
const x2Values = [];
const x3Values = [];
const y1Values = [];
const y2Values = [];
const y3Values = [];
for (let x = 0; x <= 10; x += 1) {
x1Values.push(x);
x2Values.push(x);
x3Values.push(x);
y1Values.push(eval(exp1));
y2Values.push(eval(exp2));
y3Values.push(eval(exp3));
}
// 定义数据
const data = [
{x: x1Values, y: y1Values, mode:"lines"},
{x: x2Values, y: y2Values, mode:"lines"},
{x: x3Values, y: y3Values, mode:"lines"}
];
// 定义布局
const layout = {title: "[y=" + exp1 + "] [y=" + exp2 + "] [y=" + exp3 + "]"};
// 使用 Plotly 显示
Plotly.newPlot("myPlot", data, layout);