// echarts1 初始化
var dom1 = document.getElementById("echarts1");
let industryDataAxis = ["售前", "执行", "设计", "测试", "运维", "其他"]; // ;
let industryData = [56, 76, 72, 37, 55, 29];
var option1 = {
yAxis: {
type: "category",
data: industryDataAxis,
axisLabel: {
interval: 0, // 0:强制显示所有标签 1:隔一个标签显示一个
color: "#ddd",
},
axisTick: {
show: false, // 不显示坐标轴刻度线
},
inverse: true,
},
xAxis: {
type: "value",
axisLabel: {
color: "#ddd",
},
splitLine: {
show: true, //去除网格线
lineStyle: {
color: ["rgba(255,255,255,0.1)"],
height: 1,
type: "solid",
},
},
},
series: [
{
data: industryData,
type: "bar",
// showBackground: true,
barWidth: 12,
itemStyle: {
normal: {
color: function (params) {
// build a color map as your need.
var colorList = [
// 四个数字分别对应 数组中颜色的开始位置,分别为 右,下,左,上。例如(1,0,0,0 )代表从右边开始渐变。
// offset取值为0~1,0代表开始时的颜色,1代表结束时的颜色,柱子表现为这两种颜色的渐变。
new echarts.graphic.LinearGradient(1, 0, 0, 0, [
{
offset: 1,
color: "rgba(18,203,184)",
},
{
offset: 0,
color: "rgba(95,252,216)",
},
]),
new echarts.graphic.LinearGradient(1, 0, 0, 0, [
{
offset: 1,
color: "rgba(62,134,255)",
},
{
offset: 0,
color: "rgba(95,220,255)",
},
]),
new echarts.graphic.LinearGradient(1, 0, 0, 0, [
{
offset: 1,
color: "rgba(67,244,255)",
},
{
offset: 0,
color: "rgba(141,252,255)",
},
]),
new echarts.graphic.LinearGradient(1, 0, 0, 0, [
{
offset: 1,
color: "rgba(180,65,12)",
},
{
offset: 0,
color: "rgba(234,89,5)",
},
]),
new echarts.graphic.LinearGradient(1, 0, 0, 0, [
{
offset: 1,
color: "rgba(255,166,6)",
},
{
offset: 0,
color: "rgba(255,213,81)",
},
]),
new echarts.graphic.LinearGradient(1, 0, 0, 0, [
{
offset: 1,
color: "rgba(127,126,125)",
},
{
offset: 0,
color: "rgba(176,176,176)",
},
]),
];
return colorList[params.dataIndex];
},
label: {
show: true,
position: "right",
textStyle: {
fontSize: 12,
color: "rgba(255,255,255)",
},
},
},
},
},
// 创建第二条y轴做背景
{
data: [100, 100, 100, 100, 100, 100],
type: "bar",
// showBackground: true,
barWidth: 25,
barGap: "-150%", //柱状图偏移量
itemStyle: {
normal: {
color: "rgba(56,128,255,0.14)",
// borderColor: 'rgba(56,145,255,0.20)',
borderWidth: 4,
},
},
},
],
};
// echarts1 渲染
var charts1 = echarts.init(dom1);
charts1.setOption(option1);
// echarts2 初始化
var dom2 = document.getElementById("echarts2");
var option2 = {
title: {
text: "",
subtext: "",
},
tooltip: {
trigger: "axis",
},
legend: {
data: ["走访", "试点", "签约"],
right: 20,
top: 25,
itemHeight: 10, //图标大小
itemWidth: 10,
icon: "rect",
textStyle: {
color: "#fff",
},
},
xAxis: {
type: "category",
boundaryGap: true, //左右不留白
axisLabel: {
color: "#fff",
},
axisTick: {
show: false, // 不显示坐标轴刻度线
},
data: ["2018", "2019", "2020", "2021", "2022"],
},
yAxis: {
type: "value",
axisLabel: {
color: "#ddd",
},
splitLine: {
show: true, //去除网格线
lineStyle: {
color: ["rgba(255,255,255,0.1)"],
height: 1,
type: "solid",
},
},
},
series: [
{
name: "走访",
type: "line",
smooth: true,
// symbol:'arrow', //拐点样式
data: [13, 17, 19, 24, 21],
itemStyle: {
normal: {
color: "#fff",
shadowBlur: 8,
shadowColor: "#25d5f0",
borderColor: "#00c1de",
borderWidth: 2,
backgroundColor: "transparent",
},
},
},
{
name: "试点",
type: "line",
smooth: true,
data: [21, 26, 31, 39, 47],
itemStyle: {
normal: {
color: "#00c1de",
shadowBlur: 8,
shadowColor: "#25d5f0",
borderColor: "#1DD7FF",
borderWidth: 2,
backgroundColor: "transparent",
},
},
areaStyle: {
normal: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: "#1DD7FF",
},
{
offset: 1,
color: "rgba(0,0,0,0)",
},
]),
},
},
},
{
name: "签约",
type: "line",
smooth: true,
data: [9, 19, 24, 28, 29],
itemStyle: {
normal: {
color: "#1D61FF",
shadowBlur: 8,
shadowColor: "#25d5f0",
borderColor: "#00c1de",
borderWidth: 2,
backgroundColor: "transparent",
},
},
areaStyle: {
normal: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: "#1D61FF",
},
{
offset: 1,
color: "rgba(0,0,0,0)",
},
]),
},
},
},
],
};
// echarts2 渲染
var charts2 = echarts.init(dom2);
charts2.setOption(option2);
// echarts3 初始化
var dom3 = document.getElementById("echarts3");
var option3 = {
tooltip: {
trigger: "axis",
axisPointer: {
type: "shadow",
},
//这里改变样式
formatter:
' {b0}月
{c0}台
{c1}万元',
},
legend: {
data: ["设备数量", "项目金额"],
right: 20,
top: 5,
itemHeight: 10, //图标大小
itemWidth: 10,
icon: "rect",
textStyle: {
color: "#fff",
},
},
grid: {
left: "3%",
right: "4%",
bottom: "3%",
containLabel: true,
},
yAxis: [
{
name: "单位:万元",
type: "value",
boundaryGap: [0, 0.01],
axisLabel: {
color: "#ddd",
},
splitLine: {
show: true, //去除网格线
lineStyle: {
color: ["rgba(255,255,255,0.1)"],
height: 1,
type: "solid",
},
},
},
{
name: "单位:万元",
type: "value",
boundaryGap: [0, 0.01],
axisLabel: {
color: "#ddd",
},
splitLine: {
show: true, //去除网格线
lineStyle: {
color: ["rgba(255,255,255,0.1)"],
height: 1,
type: "solid",
},
},
},
],
xAxis: {
type: "category",
axisLabel: {
//在y轴的数据的值旁边拼接单位
color: "#ddd",
formatter: "{value} 月",
},
axisTick: {
show: false, // 不显示坐标轴刻度线
},
data: ["1", "2", "3", "4", "5", "6", "7", "8", "9"],
},
series: [
{
name: "设备数量",
color: "#fff",
type: "bar",
yAxisIndex: 0,
barWidth: 6,
data: [256, 304, 328, 350, 358, 389, 421, 452, 486],
},
{
name: "项目金额",
type: "bar",
yAxisIndex: 1,
color: "#458AF4",
barWidth: 6,
data: [26, 48, 57, 65, 76, 79, 84, 90, 94],
},
],
};
// echarts3 渲染
var charts3 = echarts.init(dom3);
charts3.setOption(option3);
// 3D 圆环图
// 生成模拟 3D 饼图的配置项
// pieData(object):饼图数据
// internalDiameterRatio(0~1之间的浮点数):内径/外径的值(默认值 1/2),当该值等于 0 时,
// heigth配置每个数据生成的高度
function getPie3D(pieData, internalDiameterRatio, height) {
let series = [];
let sumValue = 0;
let startValue = 0;
let endValue = 0;
let legendData = [];
let k =
typeof internalDiameterRatio !== "undefined" ? (1 - internalDiameterRatio) / (1 + internalDiameterRatio) : 1 / 3;
//console.log(k) //internalDiameterRatio==>2时,k=-0.3333333333333333
// 计算比例
let total = 0;
for (let i = 0; i < pieData.length; i++) {
pieData[i].value = Number(pieData[i].value);
// console.log(Number(pieData[i].value))
total += Number(pieData[i].value);
}
// console.log(total)
for (let i = 0; i < pieData.length; i++) {
pieData[i].proportion = parseFloat(pieData[i].value / total).toFixed(4);
// console.log(pieData[i].proportion)
}
// 为每一个饼图数据,生成一个 series-surface 配置
for (let i = 0; i < pieData.length; i++) {
sumValue += pieData[i].value;
let seriesItem = {
name: typeof pieData[i].name === "undefined" ? `series${i}` : pieData[i].name,
type: "surface",
parametric: true,
wireframe: {
show: false,
},
pieData: pieData[i],
pieStatus: {
selected: pieData[i].selected ? pieData[i].selected : false,
hovered: pieData[i].hovered ? pieData[i].hovered : false,
k: k,
},
};
if (typeof pieData[i].itemStyle != "undefined") {
let itemStyle = {};
typeof pieData[i].itemStyle.color != "undefined" ? (itemStyle.color = pieData[i].itemStyle.color) : null;
typeof pieData[i].itemStyle.opacity != "undefined" ? (itemStyle.opacity = pieData[i].itemStyle.opacity) : null;
seriesItem.itemStyle = itemStyle;
}
series.push(seriesItem);
}
// 使用上一次遍历时,计算出的数据和 sumValue,调用 getParametricEquation 函数,
// 向每个 series-surface 传入不同的参数方程 series-surface.parametricEquation,也就是实现每一个扇形。
for (let i = 0; i < series.length; i++) {
height = pieData[i].height; //给每个扇区都添加独立高度
endValue = startValue + series[i].pieData.value;
// console.log(series[i]);
series[i].pieData.startRatio = startValue / sumValue;
series[i].pieData.endRatio = endValue / sumValue;
series[i].parametricEquation = getParametricEquation(
series[i].pieData.startRatio,
series[i].pieData.endRatio,
series[i].pieStatus.selected,
series[i].pieStatus.hovered,
k,
height ? series[i].pieData.proportion * height : 1 //自己自定义传入高度,每个类型按比例生成高度
// series[i].pieData.value ==>这个是饼图默认自己生成高度
// 1 设置为1所有的扇形高度都一样高
);
startValue = endValue;
legendData.push(series[i].name);
}
// console.log(series);
return series;
}
// startRatio(浮点数): 当前扇形起始比例,取值区间[0, endRatio)
// endRatio(浮点数): 当前扇形结束比例,取值区间(startRatio, 1]
// isSelected(布尔值): 是否选中,效果参照二维饼图选中效果(单选)
// isHovered(布尔值): 是否放大,效果接近二维饼图高亮(放大)效果(未能实现阴影)
// k(0~1之间的浮点数):用于参数方程的一个参数,取值 0~1 之间,通过「内径 / 外径」的值换算而来。
//height配置3d扇形高度
function getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, height) {
// 计算
let midRatio = (startRatio + endRatio) / 2;
let startRadian = startRatio * Math.PI * 2;
let endRadian = endRatio * Math.PI * 2;
let midRadian = midRatio * Math.PI * 2;
// 如果只有一个扇形,则不实现选中效果。
if (startRatio === 0 && endRatio === 1) {
isSelected = false;
}
// 通过扇形内径/外径的值,换算出辅助参数 k(默认值 1/3)
k = typeof k !== "undefined" ? k : 1 / 3;
// 计算选中效果分别在 x 轴、y 轴方向上的位移(未选中,则位移均为 0)
let offsetX = isSelected ? Math.cos(midRadian) * 0.2 : 0;
let offsetY = isSelected ? Math.sin(midRadian) * 0.2 : 0;
// 计算高亮效果的放大比例(未高亮,则比例为 1)
let hoverRate = isHovered ? 1.05 : 1;
// 返回曲面参数方程
return {
u: {
min: -Math.PI,
max: Math.PI * 3,
step: Math.PI / 32,
},
v: {
min: 0,
max: Math.PI * 2,
step: Math.PI / 20,
},
x: function (u, v) {
if (u < startRadian) {
return offsetX + Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate;
}
if (u > endRadian) {
return offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate;
}
return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate;
},
y: function (u, v) {
if (u < startRadian) {
return offsetY + Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate;
}
if (u > endRadian) {
return offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate;
}
return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate;
},
z: function (u, v) {
if (u < -Math.PI * 0.5) {
return Math.sin(u);
}
if (u > Math.PI * 2.5) {
return Math.sin(u);
}
return Math.sin(v) > 0 ? 1 * height : -1;
},
};
}
var optionData = [
{
name: "机电",
value: 10,
height: 13,
itemStyle: {
opacity: 0.2,
color: "#199BD7",
},
},
{
name: "注塑",
value: 13,
height: 7,
itemStyle: {
opacity: 0.2,
color: "#F7CD6F",
},
},
{
name: "卫浴",
value: 8,
height: 0,
itemStyle: {
opacity: 0.2,
color: "#EF5A05",
},
},
{
name: "汽摩配",
value: 7,
height: 5,
itemStyle: {
opacity: 0.2,
color: "#09C3A1",
},
},
{
name: "装备制造",
value: 6,
height: 7,
itemStyle: {
opacity: 0.2,
color: "#A7A7A7",
},
},
];
// 基于准备好的dom,初始化echarts实例
let dataList = []; //名称列表
let chartPanel = echarts.init(document.getElementById("chart-panel"));
for (let i = 0; i < optionData.length; i++) {
dataList.push(optionData[i].name);
delete optionData[i].itemStyle.opacity;
}
// 传入数据生成 option
let series = getPie3D(optionData, 2);
let option = {
tooltip: {
formatter: (params) => {
// console.log(params)
if (params.seriesName !== "mouseoutSeries" && params.seriesName !== "pie2d") {
return `