页面引入柱状图图表并且进行相关属性的配置(两大步骤)
- 官网找到类似实例, 适当分析,并且引入到HTML页面中
- 根据需求定制图表
1. 引入到html页面中
~~~javascript
(function() {
let myChart = echarts.init(document.querySelector(".bar .chart"));
let option = {
color: ["#3398DB"],
tooltip: {
trigger: "axis",
axisPointer: {
type: "shadow"
}
},
grid: {
left: "3%",
right: "4%",
bottom: "3%",
containLabel: true
},
xAxis: [
{
type: "category",
data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
axisTick: {
alignWithLabel: true
}
}
],
yAxis: [
{
type: "value"
}
],
series: [
{
name: "直接访问",
type: "bar",
barWidth: "60%",
data: [10, 52, 200, 334, 390, 330, 220]
}
]
};
myChart.setOption(option);
})();
~~~
2. 根据需求定制
- 修改图表柱形颜色 #2f89cf
- 修改图表大小 top 为 10px bottom 为 4% grid决定我们的柱状图的大小
~~~JavaScript
color: ["#2f89cf"],
grid: {
left: "0%",
top: "10px",
right: "0%",
bottom: "4%",
containLabel: true
},
~~~
- X轴相关设置 xAxis
- 文本颜色设置为 rgba(255,255,255,.6) 字体大小为 12px
- X轴线的样式 不显示
~~~JavaScript
axisLabel: {
color: "rgba(255,255,255,.6)",
fontSize: "12"
},
axisLine: {
show: false
// 如果想要设置单独的线条样式
// lineStyle: {
// color: "rgba(255,255,255,.1)",
}
}
~~~
- Y 轴相关定制
- 文本颜色设置为 rgba(255,255,255,.6) 字体大小为 12px
- Y 轴线条样式 更改为 1像素的 rgba(255,255,255,.1) 边框
- 分隔线的颜色修饰为 1像素的 rgba(255,255,255,.1)
~~~JavaScript
axisLabel: {
color: "rgba(255,255,255,.6)",
fontSize: "12"
},
axisLine: {
lineStyle: {
color: "rgba(255,255,255,.1)",
}
5232},
splitLine: {
lineStyle: {
color: "rgba(255,255,255,.1)"
}
}
~~~
- 修改柱形为圆角以及柱子宽度 series 里面设置
~~~JavaScript
series: [ { name: "直接访问", type: "bar", // 修改柱子宽度 barWidth: "35%", data: [10, 52, 200, 334, 390, 330, 220],
itemStyle: {
barBorderRadius: 5
}
}
]
};
~~~
- 更换对应数据
~~~JavaScript
data: [ "旅游行业","教育培训", "游戏行业", "医疗行业", "电商行业", "社交行业", "金融行业" ],
data: [200, 300, 300, 900, 1500, 1200, 600]
~~~
- 让图表跟随屏幕自适应
~~~javascript
window.addEventListener("resize", function() {
myChart.resize();
});
~~~
### 柱状图2定制
- 官网找到类似实例, 适当分析,并且引入到HTML页面中
- 根据需求定制图表
需求1: 修改图形大小 grid
~~~javascript
grid: {
top: "10%",
left: "22%",
bottom: "10%"
},
~~~
需求2: 不显示x轴
~~~javascript
xAxis: {
show: false
},
~~~
需求3: y轴相关定制
- 不显示y轴线和相关刻度
~~~javascript
axisLine: {
show: false
},
axisTick: {
show: false
},
~~~
- y轴文字的颜色设置为白色
~~~javascript
axisLabel: {
color: "#fff"
},
~~~
需求4: 修改第一组柱子相关样式(条状)
~~~javascript
name: "条",
barCategoryGap: 50,
barWidth: 10,
itemStyle: {
normal: {
barBorderRadius: 20,
}
},
~~~
- 设置第一组柱子内百分比显示数据
~~~javascript
label: {
normal: {
show: true,
position: "inside",
formatter: "{c}%"
}
},
~~~
- 设置第一组柱子不同颜色
~~~javascript
var myColor = ["#1089E7", "#F57474", "#56D0E3", "#F8B448", "#8B78F6"];
itemStyle: {
normal: {
barBorderRadius: 20,
console.log(params);
return myColor[params.dataIndex];
}
},
~~~
需求5: 修改第二组柱子的相关配置(框状)
~~~javascript
name: "框",
type: "bar",
barCategoryGap: 50,
barWidth: 15,
itemStyle: {
color: "none",
borderColor: "#00c1de",
borderWidth: 3,
barBorderRadius: 15
},
data: [19325, 23438, 31000, 121594, 134141, 681807]
}
~~~
需求6: 给y轴添加第二组数据
~~~javascript
yAxis: [ { type: "category", data: ["印尼", "美国", "印度", "中国", "世界人口(万)"],
axisLine: {
show: false
},
axisTick: {
show: false
},
axisLabel: {
color: "#fff"
}
},
{
show: true,
data: [702, 350, 610, 793, 664],
// 不显示y轴的线
axisLine: {
show: false
},
axisTick: {
show: false
},
axisLabel: {
textStyle: {
fontSize: 12,
color: "#fff"
}
}
}
],
~~~
需求7: 设置两组柱子层叠以及更换数据
~~~javascript
yAxisIndex: 0,
yAxisIndex: 1,
data: [70, 34, 60, 78, 69],
data: [100, 100, 100, 100, 100],
data: ["HTML5", "CSS3", "javascript", "VUE", "NODE"],
data:[702, 350, 610, 793, 664],
~~~
完整代码:
~~~javascript
(function() {
var myColor = ["#1089E7", "#F57474", "#56D0E3", "#F8B448", "#8B78F6"];
var myChart = echarts.init(document.querySelector(".bar2 .chart"));
var option = {
grid: {
top: "10%",
left: "22%",
bottom: "10%"
},
xAxis: {
show: false
},
yAxis: [
{
type: "category",
inverse: true,
data: ["HTML5", "CSS3", "javascript", "VUE", "NODE"],
axisLine: {
show: false
},
axisTick: {
show: false
},
axisLabel: {
color: "#fff"
}
},
{
data: [702, 350, 610, 793, 664],
inverse: true,
axisLine: {
show: false
},
axisTick: {
show: false
},
axisLabel: {
color: "#fff"
}
}
],
series: [
{
name: "条",
type: "bar",
data: [70, 34, 60, 78, 69],
yAxisIndex: 0,
itemStyle: {
barBorderRadius: 20,
color: function(params) {
console.log(params);
return myColor[params.dataIndex];
}
},
barCategoryGap: 50,
barWidth: 10,
label: {
show: true,
position: "inside",
formatter: "{c}%"
}
},
{
name: "框",
type: "bar",
barCategoryGap: 50,
barWidth: 15,
yAxisIndex: 1,
data: [100, 100, 100, 100, 100],
itemStyle: {
color: "none",
borderColor: "#00c1de",
borderWidth: 3,
barBorderRadius: 15
}
}
]
};
myChart.setOption(option);
})();