echarts显示百分比

一、echarts显示表格

在很多时候,我们需要将数据以表格的形式展示给用户,而echarts提供了表格组件,方便我们实现这个需求。下面是一个示例:


// 引入echarts和table组件
import echarts from 'echarts';
import 'echarts/lib/component/legendScroll';
import 'echarts/lib/component/toolbox';
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';
import 'echarts/lib/component/grid';
import 'echarts/lib/component/visualMap';
import 'echarts/lib/component/markPoint';
import 'echarts/lib/component/markLine';
import 'echarts/lib/component/dataZoom';
import 'echarts/lib/component/calendar';
import 'echarts/lib/component/graphic';
import 'echarts/lib/component/dataset';
import 'echarts/lib/component/axisPointer';
import 'echarts/lib/component/polar';
import 'echarts/lib/component/geo';
import 'echarts/lib/component/singleAxis';
import 'echarts/lib/component/parallel';
import 'echarts/lib/component/sankey';
import 'echarts/lib/component/tree';
import 'echarts/lib/component/timeline';
import 'echarts/lib/component/gauge';
import 'echarts/lib/component/funnel';
import 'echarts/lib/component/heatmap';
import 'echarts/lib/component/treeMap';
import 'echarts/lib/component/bmap';
import 'echarts/lib/component/boxplot';
import 'echarts/lib/component/custom';
import 'echarts/lib/component/graph';
import 'echarts/lib/component/lines';
import 'echarts/lib/component/map';

import 'echarts/lib/chart/bar';
import 'echarts/lib/chart/candlestick';
import 'echarts/lib/chart/chord';
import 'echarts/lib/chart/custom';
import 'echarts/lib/chart/effectScatter';
import 'echarts/lib/chart/funnel';
import 'echarts/lib/chart/gauge';
import 'echarts/lib/chart/graph';
import 'echarts/lib/chart/heatmap';
import 'echarts/lib/chart/line';
import 'echarts/lib/chart/map';
import 'echarts/lib/chart/pie';
import 'echarts/lib/chart/radar';
import 'echarts/lib/chart/sankey';
import 'echarts/lib/chart/scatter';
import 'echarts/lib/chart/themeRiver';
import 'echarts/lib/chart/treemap';

import 'echarts/lib/component/dataset';

import 'echarts/lib/chart/line';

import 'echarts/lib/component/grid';

import 'echarts/lib/component/title';

import 'echarts/lib/component/tooltip';

import 'echarts/lib/component/legend';

import 'echarts/lib/chart/pie';

import 'echarts/lib/chart/bar';

import 'echarts/lib/chart/map';

import 'echarts/lib/component/visualMap';

// 设置图表的数据和配置项
var tabledata = [
  [ {value:'Name' , rowspan:2} , {value:'Math' , rowspan:2} , {value:'English' , rowspan:2} , {value:'Science' , colspan:2} , {value:'Total' , rowspan:2} ] ,
  [ {value:'物理'} , {value:'化学'} ]
  ,
  [ 'Tom' , 80 , 70 , 90 , 85 , 325 ] ,
  [ 'Jack' , 90 , 60 , 80 , 70 , 300 ] ,
  [ 'Tim' , 87 , 63 , 92 , 78 , 320 ] ,
  [ 'Green' , 77 , 93 , 78 , 82 , 330 ] ,
  [ 'Mike' , 75 , 65 , 58 , 87 , 285 ]
];

var option = {
  title: {
    text: '成绩表'
  },
  tooltip: {},
  legend: {
    data: ['姓名','数学','物理','化学','英语','总分']
  },
  xAxis: {
    type: 'category',
    axisLabel: {
      interval:0,
      rotate:40
    },
    data: ['Name', 'Tom', 'Jack', 'Tim', 'Green', 'Mike']
  },
  yAxis: {},
  series: [
    {
      name: '姓名',
      type: 'bar',
      stack: '总量',
      label: {
        show: true
      },
      data: tabledata[0]
    },
    {
      name: '数学',
      type: 'bar',
      stack: '总量',
      label: {
        show: true,
      },
      data: tabledata.slice(2, 7).map(function (row) {
        return row[1];
      })
    },
    {
      name: '物理',
      type: 'bar',
      stack: '总量',
      label: {
        show: true
      },
      data: tabledata.slice(2, 7).map(function (row) {
        return row[2];
      })
    },
    {
      name: '化学',
      type: 'bar',
      stack: '总量',
      label: {
        show: true,
      },
      data: tabledata.slice(2, 7).map(function (row) {
        return row[3];
      })
    },
    {
      name: '英语',
      type: 'bar',
      stack: '总量',
      label: {
        show: true,
      },
      data: tabledata.slice(2, 7).map(function (row) {
        return row[4];
      })
    },
    {
      name: '总分',
      type: 'bar',
      stack: '总量',
      label: {
        show: true,
        position: 'inside'
      },
      data: tabledata.slice(2, 7).map(function (row) {
        return row[5];
      })
    }
  ]
};

// 获得具体的DOM元素
var chartDom = document.getElementById('chart');
var myChart = echarts.init(chartDom);
// 渲染图表
myChart.setOption(option);

二、echarts百分比柱状图

百分比柱状图是我们经常会用到的图形之一,它可以很直观地展示数据的比例关系。echarts提供了显示百分比柱状图的方法,在数据中加入小数点再设置如下配置项即可:


var option = {
  title: {
    text: '百分比柱状图'
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    }
  },
  legend: {
    data: ['比例']
  },
  grid: {

  },
  xAxis: {
    type: 'category',
    data: ['A', 'B', 'C', 'D', 'E', 'F', 'G']
  },
  yAxis: {
    type: 'value'
  },
  series: [{
    name: '比例',
    data: [0.2, 0.3, 0.5, 0.7, 0.4, 0.8, 1],
    type: 'bar',
    barWidth: '40%',
    label: {
      show: true,
      formatter: function(params) {
        return params.data * 100 + '%'
      }
    }
  }]
};

// 获得具体的DOM元素
var chartDom = document.getElementById('chart');
var myChart = echarts.init(chartDom);
// 渲染图表
myChart.setOption(option);

三、echart调整百分比大小

我们可以调整数据和label的百分比大小。具体方法可以通过调节以下两个配置项实现:


label: {
  show: true,
  fontSize: 14, //改变字体大小
  formatter: function(params) {
    return params.data * 100 + '%'
  }
}


yAxis: {
  type: 'value',
  axisLabel: {
    show: true,
    fontSize: 14, //改变轴上标记的字体大小
    formatter: function(params) {
      return params * 100 + '%'
    }
  },
},

通过调节这两个属性,我们可以很方便地调整百分比的大小。

原创文章,作者:DVEUZ,如若转载,请注明出处:https://www.506064.com/n/317569.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
DVEUZ的头像DVEUZ
上一篇 2025-01-11 16:27
下一篇 2025-01-11 16:27

相关推荐

  • Echarts 地图 Label 增加背景图

    本文将从多个方面对 Echarts 地图 Label 增加背景图进行详细的阐述。 一、背景图的作用 为 Echarts 地图添加背景图可以使 Label 更加直观、美观,提升视觉效…

    编程 2025-04-29
  • Python如何写百分比

    在python中,我们可以使用各种方法来计算和表示百分比。在本文中,我们将会从字符串格式化、数学计算、列表推导式和函数等多个方面来详细阐述Python如何计算和表示百分比。 一、字…

    编程 2025-04-27
  • Python怎么把数变成百分比中心

    如果需要将数变成百分比显示,Python是一个强大的语言可以帮助您高效地完成这个任务。Python提供了内置的格式化方法和标准库来处理百分比的计算和输出。那么,下面将从以下几个方面…

    编程 2025-04-27
  • ECharts地图轮播

    本文将从插件基础、数据准备及处理、地图呈现、轮播控制等方面,对ECharts地图轮播进行详细阐述。 一、插件基础 ECharts官方提供了一个名为“echarts-gl”的插件,它…

    编程 2025-04-27
  • Echarts 双 Y 轴详解

    一、什么是双 Y 轴 Echarts 双 Y 轴是一种常见的图表类型,它可以在同一张图表中展示多个指标,每个指标都有自己的 Y 轴。这种图表类型主要用于展示异构指标之间的关系,可以…

    编程 2025-04-25
  • ECharts柱状图间隔完全指南

    ECharts是一个非常强大的JavaScript图表库,它提供了丰富的可定制化配置选项以及良好的兼容性。其中,柱状图是一种非常常见的图表类型,同时也是ECharts中功能齐全的图…

    编程 2025-04-24
  • 使用Vue3引入Echarts

    一、概述 Echarts是一个数据可视化库,支持多种图表类型,包括折线图、柱状图、饼图、散点图等等。Vue3作为前端开发的主流框架之一,也可以方便地集成Echarts来实现数据可视…

    编程 2025-04-23
  • ECharts水球图的详细阐述

    ECharts是一个基于JavaScript的开源可视化库,可用于Web应用程序开发。ECharts官方提供的全要素的多种类型数据图表支持,其中水球图是一种较为特殊的图表类型。可以…

    编程 2025-04-23
  • echarts横向柱状图

    一、概览 ECharts是百度前端开发部开发的一个开源可视化库,它可以帮助开发者轻松的实现各种数据可视化。 横向柱状图是ECharts中一个非常常见的图表类型,可以用于展示一组数据…

    编程 2025-04-23
  • 深入探究echarts柱状图堆叠

    一、echarts柱状图堆叠混合 echarts柱状图堆叠是一种常用的可视化方式,通过堆叠不同数据项的数值,可以直观地比较不同数据之间的关系。而在实际应用中,很多情况下我们需要在同…

    编程 2025-04-23

发表回复

登录后才能评论