uniapp利用ucharts组件

[复制链接]
发表于 2025-11-18 19:08:16 | 显示全部楼层 |阅读模式
 1.ucharts预备

有两种利用方式:一种是在uni的插件市场下载(组件化开发)。一种是手动引入ucharts包。官方都封装好组件了,我们不消岂不是浪费。
直接去dcloud插件市场(DCloud 插件市场)找,第一个是ucharts,第二个是echarts。
不思量小步伐的话,echarts更好,功能丰富,文档工具免费。
ucharts体积要小非常多,但是工具要会员,差点意思,而且网传bug会多一点,但我没碰到。
ucharts文档:uCharts官网 - 秋云uCharts跨平台图表库

2.体积标题

ucharts的里static下放了echarts的文件,导致体积比力大,可以删了。忘了要不要改qiun-data-charts里的引入地点,如果要改的话项目会有报错提示。
以下酌情优化:
由于我的uni_modules里包比力多,导致主包体积有点大,以是我又将ucharts给提取到分包里,而且删除了不必要的文件。同时把qiun-data-charts里的引入地点改一下,注意有两处引入要改。
如果开发中不必要改u-charts源码的话,可以用u-charts.min.js,同样改一下上述引入地点,再把u-charts.js删了,体积会更小。


 3.利用

完备代码在下面,根据本身的明白写了点表明。
跟寻常利用组件一样,如果没有像我一样把ucharts的提到分包,就不必要引入和界说,直接用。
ucharts把图表设置和渲染数据分开了,chartData里放渲染数据,opts里放图表设置。
type就是界说这是什么图标,柱状图,条形图之类的。
  1. import qiunDataCharts from './components/qiun-data-charts/qiun-data-charts.vue';
  2. components:{qiunDataCharts},
  3. //==========================================
  4. <view style="width:100%; height:280rpx;">
  5.         <qiun-data-charts :chartData="chartData"
  6.                 background="none" type="bar"
  7.                 :opts="opts"/>
  8. </view>
复制代码
 

  1. <template>
  2.         <view>
  3.                 <view class="chart">
  4.                         <view class="chart-item">
  5.                                 <view class="form-title">
  6.                                         车间在制品
  7.                                 </view>
  8.                                 <!-- 车间在制品 -->
  9.                                 <view class="board">
  10.                                         <view style="width:100%; height:280rpx;">
  11.                                                  <qiun-data-charts :chartData="chartData"
  12.                                                         background="none" type="bar"
  13.                                                         :opts="opts"/>
  14.                                         </view>
  15.                                 </view>
  16.                         </view>
  17.                         <view class="chart-item">
  18.                                 <view class="form-title">
  19.                                         各工步在制品
  20.                                 </view>
  21.                                 <!-- 各工步在制品 -->
  22.                                 <view class="board">
  23.                                         <view style="width:100%; height:520rpx;">
  24.                                                 <qiun-data-charts :chartData="workData"
  25.                                                         background="none" type="column"
  26.                                                         :opts="workopt"/>
  27.                                         </view>
  28.                                 </view>
  29.                         </view>
  30.                 </view>
  31.         </view>
  32. </template>
  33. <script>
  34.         import qiunDataCharts from './components/qiun-data-charts/qiun-data-charts.vue';
  35.         export default {
  36.                 data() {
  37.                         return {
  38.                                 chartData: {},
  39.                                 opts: {},
  40.                                 workData: {},
  41.                                 workopt: {}
  42.                         };
  43.                 },
  44.                 components:{qiunDataCharts},
  45.                 onShow() {
  46.                         this.initWork()
  47.                         this.initCar()
  48.                 },
  49.                 methods: {
  50.                         // 工步
  51.                         initWork(){
  52.                                 this.workData = {
  53.                                         categories: ["自动焊接","系统返修","层压","层压测试","层压外观检","包装","装机"],
  54.                                         series: [
  55.                                                 {
  56.                                                         name: "1车间",
  57.                                                         data: [35,36,31,33,13,34,14]
  58.                                                 },
  59.                                                 {
  60.                                                         name: "2车间",
  61.                                                         data: [18,27,21,24,6,28,37]
  62.                                                 },
  63.                                                 {
  64.                                                         name: "3车间",
  65.                                                         data: [18,27,21,24,6,28,23]
  66.                                                 },
  67.                                                 {
  68.                                                         name: "4车间",
  69.                                                         data: [18,27,21,24,6,28,19]
  70.                                                 }
  71.                                         ]
  72.                                 },
  73.                                 this.workopt = {
  74.                                         color: ["#105fa3","#62bf8f","#c56d15","#c62d32","#73C0DE","#3CA272","#FC8452","#9A60B4","#ea7ccc"],
  75.                                         padding: [15,15,0,5],
  76.                                         enableScroll: false,
  77.                                         legend: {
  78.                                                 show: true,//显示说明文本块
  79.                                                 fontColor: "#ffffff",//说明文本块颜色
  80.                                         },
  81.                                         dataLabel: false,//柱状体上方不显示数字
  82.                                         xAxis: {
  83.                                                 rotateLabel: true,
  84.                                                 rotateAngle: 50,
  85.                                                 lineHeight: 50,
  86.                                                 axisLineColor: '#ffffff',//轴线颜色
  87.                                                 fontColor: "#ffffff",//x轴刻字颜色
  88.                                                 disableGrid: true,
  89.                                                 // format: 'xAxisDemo3'
  90.                                         },
  91.                                         yAxis: {
  92.                                                 gridColor: "#ffffff",//横向网格线颜色
  93.                                                 data: [{
  94.                                                         axisLineColor: "#ffffff",//轴线颜色
  95.                                                         fontColor: "#ffffff",//y轴刻字颜色
  96.                                                         min: 0
  97.                                                 }]
  98.                                         },
  99.                                         extra: {
  100.                                                 column: {
  101.                                                         type: "group",
  102.                                                         width: 6,
  103.                                                         seriesGap: 2,//一个刻度中多个柱的间距
  104.                                                         activeBgColor: "#000000",
  105.                                                         activeBgOpacity: 0.08,
  106.                                                 }
  107.                                         }
  108.                                 }
  109.                         },
  110.                         // 车间
  111.                         initCar(){
  112.                                 this.chartData = {
  113.                                         categories: ["1车间","2车间","3车间","4车间"],
  114.                                         series: [
  115.                                                 {
  116.                                                         name: "完成量",
  117.                                                         data: [38,47,51,24]
  118.                                                 }
  119.                                         ]
  120.                                 },
  121.                                 this.opts = {
  122.                                         color: ["#1890FF","#91CB74"],
  123.                                         padding: [15,30,0,5],
  124.                                         enableScroll: false,
  125.                                         legend: {
  126.                                                 fontColor: "#ffffff",//说明文本块颜色
  127.                                         },
  128.                                         dataLabel: false,//柱状体上方不显示数字
  129.                                         xAxis: {
  130.                                                 axisLineColor: "#ffffff",//轴线颜色
  131.                                                 gridColor: "#ffffff",//纵坐标颜色
  132.                                                 fontColor: "#ffffff",//x轴刻字颜色
  133.                                                 boundaryGap: "justify",
  134.                                                 disableGrid: false,
  135.                                                 min: 0,
  136.                                                 axisLine: false,
  137.                                                 max: 80
  138.                                         },
  139.                                         yAxis: {
  140.                                                 data: [{
  141.                                                         type: 'categories',
  142.                                                         axisLineColor: "#ffffff",//轴线颜色
  143.                                                         fontColor: "#ffffff",//y轴刻字颜色
  144.                                                 }]
  145.                                         },
  146.                                         extra: {
  147.                                                 bar: {
  148.                                                         type: "group",
  149.                                                         width: 9,
  150.                                                         meterBorde: 1,
  151.                                                         meterFillColor: "#FFFFFF",
  152.                                                         activeBgColor: "#000000",
  153.                                                         activeBgOpacity: 0.08,
  154.                                                         linearType: "custom",
  155.                                                         barBorderCircle: true,
  156.                                                         seriesGap: 2,
  157.                                                         categoryGap: 2
  158.                                                 }
  159.                                         }
  160.                                 }
  161.                         },
  162.                 },
  163.         }
  164. </script>
  165. <style lang="scss">
  166. .chart{
  167.         padding: 0 10rpx;
  168.         background: #3a3d5c;
  169.         .chart-item{
  170.                 margin: 8rpx 0;
  171.                 padding: 20rpx;
  172.                 background: #202437;
  173.         }
  174.         .form-title{
  175.                 margin: 10rpx 0 26rpx;
  176.                 font-size: 30rpx;
  177.                 color: #fff;
  178.                 text-align: center;
  179.         }
  180.        
  181. }
  182. </style>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表