npm install echarts --save / cnpm i -S echarts
复制代码
<div id="pie"></div>
复制代码
import echarts from "echarts";
复制代码
let myChart = echarts.init(document.getElementById("pie"));
复制代码
let option = {
tooltip: { // 提示
trigger: "item", // 触发方式
formatter: "{a} <br/>{b}: {c} ({d}%)" // 提示的格式
},
legend: { // 图例
orient: "vertical",
x: "left",
data: ["直接访问", "邮件营销", "联盟广告", "视频广告", "搜索引擎"]
},
series: [
{
name: "访问来源",
type: "pie", // 图标的类型
radius: ["50%", "70%"], // 饼图的范围
avoidLabelOverlap: false,
label: {
normal: {
show: false,
position: "center"
},
emphasis: {
show: true,
textStyle: {
fontSize: "30",
fontWeight: "bold"
}
}
},
labelLine: {
normal: {
show: false
}
},
data: [
{ value: 335, name: "直接访问" },
{ value: 310, name: "邮件营销" },
{ value: 234, name: "联盟广告" },
{ value: 135, name: "视频广告" },
{ value: 1548, name: "搜索引擎" }
]
}
]
};
复制代码
myChart.setOption(option)
复制代码
修改x,y的值
legend: { // 图例
orient: "vertical",
x: "left", // left/right
y: 'top', // top/bottom
data: ["直接访问", "邮件营销", "联盟广告", "视频广告", "搜索引擎"]
},
复制代码
效果以下 html
修改orient的值改变legend的方向。
legend: { // 图例
orient: "horizontal", // horizontal/vertical,默认horizontal
x: "left",
y: 'top',
data: ["直接访问", "邮件营销", "联盟广告", "视频广告", "搜索引擎"]
},
复制代码
修改left、right、top、bottom的值,调整legend位置,值能够是'left', 'center', 'right',或者具体的数值
legend: {
orient: "horizontal",
// x: "left",
// y: 'bottom',
left: 'center',
// right: 10,
// top: 10,
bottom: 30,
data: ["直接访问", "邮件营销", "联盟广告", "视频广告", "搜索引擎"]
}
复制代码
legend: {
orient: "horizontal", // horizontal/vertical,默认horizontal
// x: "left", // x方向位置,left/right
// y: 'bottom', // y方向位置,top/bottom
left: 'center', // 距离左边的位置、距离 (值能够是'left', 'center', 'right',或者具体的数值)
// right: 10,
// top: 10,
bottom: 30,
itemGap: 10, // 间隔。横向布局时为水平间隔,纵向布局时为纵向间隔
itemWidth: 50, //图形宽度
itemHeight: 10, //图形高度
padding: 10, // 图例内边距。
textStyle: {
fontSize: 16, // 字体大小
color: "#999EE0" // 字体颜色
},
formatter: function (params) { // 格式化图例,支持字符串模板和回调函数
console.log(params) // 多个参数时能够格式化格式
return params
},
data: ["直接访问", "邮件营销", "联盟广告", "视频广告", "搜索引擎"]
}
复制代码
tooltip: {
show: true,// 是否显示提示,true/false,默认true
trigger: "item",// 触发类型, item/axis/none
backgroundColor: 'rgba(0,0,0,.5)',// 提示框背景
borderWidth: 1, // 提示框边框大小
padding: 10,// 提示框内边距
borderColor: '#ff0000',// 提示框边框颜色
formatter: "{a} <br/>{b}: {c} ({d}%)",// 提示格式,支持回调函数
textStyle: {
color: '#0DB9DF', // 提示文字样式
fontStyle: 'normal', // 提示文字风格,normal,italic,oblique
fontWeight: 'normal', // 提示文字粗细, normal,bold,bolder,lighter,100~900
fontFamily: 'sans-serif', //提示文字字体, 'serif' , 'monospace', 'Arial', 'Courier New', 'Microsoft YaHei', ...
fontSize: 14, //字体大小
lineHeight: 28, //字体行高
rich: {
a: {
lineHeight: 28 // 没有设置则继承textStyle的 `lineHeight`,
}
}
}
},
复制代码
这里再主要讲一下formatter,能够给自定义风格样式。vue
formatter: function(params) {
var res = "";
res = `<span style="color:#fff;padding: 10px;line-height: 28px;">${params.name}</span><br />
<span style="color:#0DB9DF;padding: 10px;line-height: 28px;">${ params.percent}%</span>
<span style="color:#FC6961;padding: 10px;line-height: 28px;">${params.value} 元</span>`;
return res;
}
复制代码
series: [
{
name: "访问来源",
type: "pie",
radius: ["50%", "70%"], // 饼图的范围
center: ["50%", "42%"], // 中心位置。默认都是50%
avoidLabelOverlap: false,
color: [ // 颜色,按循序使用
"#faa41b",
"#fc6961",
"#fc4190",
"#6a01d7",
"#3a02d7",
"#0309d9",
"#065cfd",
"#06c3fd",
"#0cffbf"
],
label: { // 视觉引导的文本
normal: {
show: false, // 是否显示视觉引导线
position: "center", // 标签的位置。outside/inside/center
// color: '#fff',
fontStyle: 'normal',
fontWeight: 'normal',
fontFamily: 'sans-serif',
fontSize: 12,
align: 'left'
formatter: '{b}: {d}' // 格式,支持回调
},
emphasis: {
show: true,
textStyle: {
fontSize: "30",
fontWeight: "bold"
}
}
},
labelLine: { // 视觉引导线的配置
normal: {
show: true
}
},
data: [ // 数据源,支持多参数
{ value: 335, name: "直接访问" },
{ value: 310, name: "邮件营销" },
{ value: 234, name: "联盟广告" },
{ value: 135, name: "视频广告" },
{ value: 1548, name: "搜索引擎" }
]
}
]
复制代码