echarts的安装与基本使用

1、通过npm install echarts --save安装。

npm install echarts --save

2、创建一个带有宽高的盒子。

<div id="main" style="width: 100%; height: 400px"></div>

3、在组件中全局引入 。

// 引入echarts
import * as echarts from "echarts";

4、在组件中使用模板(如柱状图)

<script setup lang="ts">
// 引入echarts
import * as echarts from "echarts";
import { onMounted } from "vue";
onMounted(() => {
  onEcharts();
});
function onEcharts() {
  // 基于准备好的dom,初始化echarts实例
  var myChart = echarts.init(document.getElementById("main"));
  // 指定图表的配置项和数据
  var option = {
    // 标题
    title: {
      text: "巡查上报记录数量",
      //   标题样式
      textStyle: {
        fontSize: 18,
        color: "#FFFFFF", // 主标题文字颜色
      },
    },
    // 提示信息
    tooltip: {},
    // 包括了坐标轴标签在内的所有内容所形成的矩形的位置。作用可以用来使坐标轴文字完全显示出来
    grid: {
      containLabel: true,
      bottom: "5%",
    },
    // x轴
    xAxis: {
      // 数据类型
      type: "value",
      //坐标轴刻度标签的相关设置
      axisLabel: {
        color: "#fff",
      },
    },
    // y轴
    yAxis: {
      // 类目类型
      type: "category",
      //坐标轴刻度标签的相关设置
      axisLabel: {
        color: "#fff", //x轴文字颜色
      },
      data: ["交通事故", "路面", "桥面", "日常养护", "路基", "交安设施"],
    },
    // 系列列表(用来设置柱状图)
    series: [
      {
        name: "数量",
        // 柱状图bar
        type: "bar",
        data: [
          {
            value: 5,
            // 设置单个柱子的样式
            itemStyle: {
              color: "#f0768b",
            },
          },
          {
            value: 5,
            // 设置单个柱子的样式
            itemStyle: {
              color: "#91cc75",
            },
          },
          {
            value: 20,
            // 设置单个柱子的样式
            itemStyle: {
              color: "#f0768b",
            },
          },
          {
            value: 16,
            // 设置单个柱子的样式
            itemStyle: {
              color: "#91cc75",
            },
          },
          {
            value: 10,
            // 设置单个柱子的样式
            itemStyle: {
              color: "#f0768b",
            },
          },
          {
            value: 10,
            // 设置单个柱子的样式
            itemStyle: {
              color: "#91cc75",
            },
          },
          {
            value: 20,
            // 设置单个柱子的样式
            itemStyle: {
              color: "#f0768b",
            },
          },
        ],
      },
      // {
      //   name: "数量",
      //   // 柱状图bar
      //   type: "line",
      //   data: [5,5,20,16,10,10,20],
      // },
    ],
    // 工具栏
    toolbox: {
      // 是否显示
      show: true,
      //   工具栏 icon 每项之间的间隔。横向布局时为水平间隔,纵向布局时为纵向间隔。
      itemGap: 4,
      iconStyle: {
        // 工具栏边框颜色
        borderColor: "#fff",
      },
      //   工具栏配置项
      feature: {
        // 区域缩放
        dataZoom: {
          yAxisIndex: "none",
          title: {
            //缩放和还原的标题文本
            zoom: "区域缩放",
            back: "区域缩放还原",
          },
        },
        // 动态类型切换
        magicType: {
          type: ["line", "bar"],
          title: {
            line: "切换为折线图",
            bar: "切换为柱状图",
          },
        },
        // 配置项还原(刷新)
        restore: {
          title: "配置项还原",
        },
        // 保存为图片
        saveAsImage: {
          title: "保存为图片",
        },
      },
    },
  };
  // 使用刚指定的配置项和数据显示图表。
  myChart.setOption(option);
}
</script>

5、效果图。

注:相关属性可查看echarts的文档配置项手册。

6、配置项介绍。

// 标题
title: {
  text: "巡查上报记录数量",
   link:'http://www.baidu.com',//超链接
   target:'self',//超链接打开方式,当前页self
   backgroundColor:'pink',//背景色
   borderColor:'pink',//边框色
   borderWidth:5,//边框粗细
   x:'center',//水平居中
   subtext:'副标题',
   sublink:'http://www.baidu.com',//副标题超链接
   //主标题文字样式
   textStyle: {
     fontSize: 18,//文字大小
     color: "#FFFFFF", // 主标题文字颜色
   },
   //副标题文字样式
   subtextStyle: {
     fontSize: 18,//文字大小
     color: "#FFFFFF", // 主标题文字颜色
   },
},
// 提示信息
tooltip: {
    trigger:'axis',//触发类型,默认是item图形触发,axis是坐标轴触发
    //坐标轴指示器
    axisPointer:{
       type:'shadow' //shadow阴影效果 line实线 cross 十字准星
    },
    showContent:false,//提示悬浮框是否显示
    backgroundColor:'pink',//悬浮框背景色
    //自定义提示框信息
    formatter(re){
        console.log(re)
        for(let i ,i<=re.length,i++){
            return '数量:'+re.data.value
        }
    }
},
//图例
legend:{
    show:true,//设置图例的显示与隐藏
    icon:'circle',图例形状设置,这里是圆形
    top:'10%',//图例位置设置,通过方位名词设置
    itemWidth:10,//图例宽度
    itemHeight:20,//图例高度
    textStyle:{//文字样式设置
        color:'pink',
    }
}
//柱状图
series:[
    type: "bar",
    //设置柱状图宽度
    barWidth:20,
    //设置全部轴颜色
    color:'pink',
    //单独设置每个轴的颜色
    itemStyle:{
        normal:{
            color:function (params){
                let colorList = [
                    'pink',
                    'red',
                    '#c23531',
                    '#c23532',
                ]
                return colorList[parmas.dataIndex]
            }
        }
    },
//设置最大值最小值
    markPoint:{
        data:[
            {
                type:'max',
                name:'最大值'
            },
            {
                type:'min',
                name:'最小值'
            }
        ]
    },
    markLine:{
        data:[
            {
                type:'average',
                name:'平均值'
            }
        ]
    }
]
//饼状图
legend:{
        left:'left',
        //图例的布局朝向
        orient:'verical'
}
series:[
    type: "pie",
    

]

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 C# 和 echarts 绘制图表需要从数据库中获取数据,然后将数据转换成 echarts 所需的格式。以下是使用 C# 和 echarts 绘制图表的基本步骤: 1. 连接数据库:使用 C# 的 ADO.NET 连接数据库,获取数据库中的数据。 2. 处理数据:将从数据库中获取的数据进行处理,转换成 echarts 所需的格式。比如将数据转换成 JSON 格式。 3. 引入 echarts 库:在 HTML 页面中引入 echarts 库,以便能够使用 echarts 绘制图表。 4. 绘制图表:使用 echarts 的 API 绘制图表,将数据传入 echarts 中。 下面是一个简单的例子,演示如何使用 C# 和 echarts 绘制柱状图: ```csharp using System.Data.SqlClient; using Newtonsoft.Json; public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True"); SqlCommand cmd = new SqlCommand("SELECT * FROM Table1", con); con.Open(); SqlDataReader reader = cmd.ExecuteReader(); string data = "["; while (reader.Read()) { data += "['" + reader["Name"].ToString() + "'," + reader["Value"].ToString() + "],"; } data = data.TrimEnd(',') + "]"; con.Close(); string option = @" { title: { text: '柱状图' }, tooltip: {}, xAxis: { type: 'category', data: ['名称'] }, yAxis: { type: 'value' }, series: [{ name: '数值', type: 'bar', data: " + data + @" }] }"; Page.ClientScript.RegisterStartupScript(this.GetType(), "chart", "var chart = echarts.init(document.getElementById('chart')); chart.setOption(" + option + ");", true); } } ``` 在上面的例子中,我们使用 ADO.NET 连接数据库,获取了 Table1 中的数据。然后将数据转换成 echarts 所需的格式,使用 echarts 的 API 绘制柱状图。最后将绘制好的图表通过 JavaScript 注入到 HTML 页面中。 注意:本例子使用了 Newtonsoft.Json 库来将数据转换成 JSON 格式。如果您没有安装这个库,请先安装它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值