经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Echarts » 查看文章
Echart Bar双柱状图样式最全详解
来源:jb51  时间:2021/8/26 8:53:49  对本文有异议

前言

在最近的项目中,有可视化图表的需求,第一时间就想到了Echarts和Hightcharts。

要用到的可视化图表都是比较常见的,Echarts文档和实例都比较全面,而且还是中文的,方便阅读,于是选择了Echarts。

Echarts的图表样式如果是自用,肯定是没啥问题的,但是 UI 肯定是不满意的,于是进行了一系列的样式调整...

安装及配置

前端框架为easywebpack-vue,使用的Echarts版本为^5.0.1

Echarts 官方文档: echarts.apache.org/zh/index.ht

1. 安装 Echarts

  1. npm install echarts --save

2. 全局引入 Echarts

在 main.js 加入如下代码:

  1. import * as echarts from "echarts";
  2. Vue.prototype.$echarts = echarts;

3. 按需引入 Echarts

(1)新增 echarts.js 文件

  1. // 引入 echarts 核心模块,核心模块提供了 echarts 使用必须要的接口
  2. import * as echarts from "echarts/core";
  3.  
  4. // 引入各种图表,图表后缀都为 Chart
  5. import { BarChart, LineChart, PieChart } from "echarts/charts";
  6.  
  7. // 引入提示框,标题,直角坐标系等组件,组件后缀都为 Component
  8. import {
  9. TitleComponent,
  10. TooltipComponent,
  11. ToolboxComponent,
  12. GridComponent,
  13. LegendComponent,
  14. AxisPointerComponent,
  15. DatasetComponent,
  16. } from "echarts/components";
  17.  
  18. // 引入 Canvas 渲染器,注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
  19. import { SVGRenderer } from "echarts/renderers";
  20.  
  21. // 注册必须的组件
  22. echarts.use([
  23. BarChart,
  24. LineChart,
  25. PieChart,
  26.  
  27. TitleComponent,
  28. TooltipComponent,
  29. ToolboxComponent,
  30. GridComponent,
  31. LegendComponent,
  32. AxisPointerComponent,
  33. DatasetComponent,
  34.  
  35. SVGRenderer,
  36. ]);
  37.  
  38. export default echarts;

(2)在 main.js 文件中引入

  1. import echarts from "./utils/echarts";
  2. Vue.prototype.$echarts = echarts;

使用举例

  1. <template>
  2. <div id="charts" style="width: 600px; height: 400px"></div>
  3. </template>
  4.  
  5. <script>
  6. import * as R from "ramda";
  7.  
  8. export default {
  9. mounted() {
  10. this.initCharts();
  11. },
  12. methods: {
  13. initCharts() {
  14. let charts = this.$echarts.init(document.getElementById("charts"));
  15. let option = {
  16. title: {
  17. text: "逐月消费趋势", // 标题
  18. subtext: "柱状图", // 副标题
  19. },
  20. xAxis: {
  21. type: "category",
  22. },
  23. yAxis: {
  24. type: "value",
  25. },
  26. color: ["#1890ff", "#52c41a", " #faad14"], // 柱状图颜色
  27. dataset: {
  28. source: [
  29. // 数据源
  30. ["1月", 1330, 666, 560],
  31. ["2月", 820, 760, 660],
  32. ["3月", 1290, 1230, 780],
  33. ["4月", 832, 450, 890],
  34. ["5月", 901, 880, 360],
  35. ["6月", 934, 600, 700],
  36. ],
  37. },
  38. series: [
  39. // 图标列设置
  40. { type: "bar", stack: "total", name: "苹果" },
  41. { type: "bar", stack: "total", name: "梨子" },
  42. { type: "bar", stack: "total", name: "桃子" },
  43. ],
  44. tooltip: {
  45. // 提示框组件
  46. }
  47. };
  48. charts.setOption(option);
  49. },
  50. },
  51. };
  52. </script>
  53.  
  54. <style lang="scss" scoped></style>

原始效果展示:

改造后目标效果展示:

样式优化

x 轴基础样式

基础设置如下所示,可设置刻度和轴线相关的属性

  1. xAxis: {
  2. type: "category",
  3. boundaryGap: true, // 坐标轴两边留白策略,默认为true
  4. axisTick: { // 刻度
  5. show: false,
  6. },
  7. axisLabel: { // 刻度标签
  8. color: "#808080",
  9. fontSize: 12,
  10. margin: 8, // 刻度标签与轴线之间的距离
  11. interval: "auto", // x轴标签显示间隔,自动
  12. },
  13. axisLine: { // 轴线
  14. lineStyle: {
  15. color: "#c3c3c3",
  16. width: 0.5,
  17. },
  18. },
  19. splitLine: { // 分割线
  20. show: false,
  21. interval: "auto",
  22. },
  23. splitArea: { // 分割区域
  24. show: false,
  25. areaStyle: {},
  26. },
  27. },

最大和最小刻度标签

主要属性是interval,要设置的足够大,比正常展示的刻度个数大一些,就能实现只展示最大和最小刻度标签

  1. xAxis: {
  2. axisLabel: {
  3. // interval: "auto",
  4. interval: 50, // 只显示最大和最小坐标
  5. showMinLabel: true, // 显示最小刻度标签
  6. showMaxLabel: true, // 显示最大刻度标签
  7. }
  8. }

series 数据列悬浮高亮

  1. const stackBarSeries = {
  2. type: "bar", // 柱状图
  3. barWidth: 32, // 柱体宽度
  4. stack: "total", // 数据堆叠
  5. showBackground: false, // 是否显示柱条背景色
  6. // 高亮的图形样式和标签样式
  7. emphasis: {
  8. // 鼠标hover时,同业务项高亮,其他项淡出图形
  9. // focus: "series",
  10. // 默认配置,仅当前hover数据淡出
  11. focus: "none",
  12. },
  13. };
  14.  
  15. let option = {
  16. series: R.map(
  17. (o) =>
  18. R.merge(stackBarSeries, {
  19. name: o,
  20. }),
  21. ["苹果", "梨子", "桃子"]
  22. ),
  23. };

坐标指示器背景渐变色

主要是设置tooltip提示框组件的trigger,让 x 轴悬浮触发;然后设置xAxis的坐标指示器axisPointer,指示器遮罩样式shadowStyle可以设置渐变色

  1. let option = {
  2. tooltip: {
  3. // 提示框组件
  4. trigger: "axis", // 坐标轴触发
  5. },
  6. xAxis: {
  7. // 坐标轴指示器
  8. axisPointer: {
  9. type: "shadow",
  10. // 坐标轴指示器的 z 值,控制图形的前后顺序
  11. z: 1,
  12. // 指示器遮罩样式
  13. shadowStyle: {
  14. // 解决hover背景色渐变问题
  15. color: {
  16. type: "linear",
  17. x: 0,
  18. y: 0,
  19. x2: 0,
  20. y2: 1,
  21. colorStops: [
  22. {
  23. offset: 0,
  24. color: "rgba(234,244,255,1)", // 0% 处的颜色
  25. },
  26. {
  27. offset: 1,
  28. color: "rgba(234,244,255,0.3)", // 100% 处的颜色
  29. },
  30. ],
  31. global: false, // 缺省为 false
  32. },
  33. // 设置背景色及阴影
  34. // color: "rgba(234,244,255,1)",
  35. // opacity: 1,
  36. // shadowColor: "rgba(0, 0, 0, 0.5)",
  37. // shadowBlur: 10,
  38. // shadowOffsetX: 10,
  39. // shadowOffsetY: 10,
  40. },
  41. },
  42. },
  43. };

tooltip 提示框自定义样式

tooltip默认的样式或者值可能不符合开发的要求,可以使用formatter函数自定义处理

  1. let option = {
  2. tooltip: {
  3. // 提示框组件
  4. trigger: "axis", // 坐标轴触发
  5. padding: [20, 16, 12, 16],
  6. backgroundColor: "#fff",
  7. alwaysShowContent: false,
  8. formatter: function(params) {
  9. let html = `<div style="height:auto;width: 163px;">
  10. <div style="font-size:14px;font-weight:bold;color:#333;margin-bottom:16px;line-height:1;">
  11. ${params[0].axisValue}
  12. </div>
  13. ${params
  14. .map(
  15. (
  16. item
  17. ) => `<div style="font-size:12px;color:#808080;margin-bottom:8px;display:flex;align-items:center;line-height:1;">
  18. <span style="display:inline-block;margin-right:8px;border-radius:6px;width:6px;height:6px;background-color:${
  19. item.color
  20. };"></span>
  21. ${item.seriesName}
  22. <span style="flex:1;text-align:right;"${item.value[
  23. item.encode.y[0]
  24. ] || 0}</span>
  25. </div>`
  26. )
  27. .join("")}
  28. <div style="display:flex;align-items:center;justify-content:space-between;font-size:12px;color:#333;padding-top:4px;margin-bottom:8px;line-height:1;">
  29. <span>总计</span>
  30. <span>¥${R.reduceRight(
  31. R.add,
  32. 0,
  33. R.drop(1, params[0].value || [])
  34. )}</span>
  35. </div>
  36. </div>`;
  37. return html;
  38. },
  39. },
  40. };

y 轴基础样式

  1. let option = {
  2. yAxis: {
  3. type: "value",
  4. minInterval: 100,
  5. nameGap: 8,
  6. axisLabel: {
  7. color: "#808080",
  8. fontSize: 10,
  9. // formatter: (value) => {
  10. // return moneyFormatValue(value);
  11. // },
  12. },
  13. splitLine: {
  14. lineStyle: {
  15. type: "dashed",
  16. color: "#ebebeb",
  17. width: 0.5,
  18. },
  19. },
  20. },
  21. };

legend 图例样式自定义

  1. let option = {
  2. grid: {
  3. left: 0,
  4. right: 12,
  5. bottom: 0,
  6. top: 68,
  7. containLabel: true,
  8. },
  9. // 图例设置
  10. legend: {
  11. top: 32,
  12. left: -5,
  13. icon: "circle",
  14. itemHeight: 6, // 修改icon图形大小
  15. itemGap: 24,
  16. textStyle: {
  17. fontSize: 12,
  18. color: "#333",
  19. padding: [0, 0, 0, -8], // 修改文字和图标距离
  20. },
  21. },
  22. };

总结

到此这篇关于Echart Bar双柱状图样式的文章就介绍到这了,更多相关Echart Bar双柱状图样式内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号