图表示例

仪表盘基本用法 - 图1

标准数据格式

  1. Gauge: {
  2. categories: [{
  3. value: 0.2,
  4. color: '#2fc25b'
  5. }, {
  6. value: 0.8,
  7. color: '#f04864'
  8. }, {
  9. value: 1,
  10. color: '#1890ff'
  11. }],
  12. series: [{
  13. name: '完成率',
  14. data: 0.85
  15. }]
  16. }

调用方法

  1. canvaGauge = new uCharts({
  2. $this:_self,
  3. canvasId: canvasId,
  4. type: 'gauge',
  5. fontSize:11,
  6. legend:false,
  7. title: {
  8. name: Math.round(chartData.series[0].data*100)+'%',
  9. color: chartData.categories[1].color,
  10. fontSize: 25*_self.pixelRatio,
  11. offsetY:50*_self.pixelRatio,//新增参数,自定义调整Y轴文案距离
  12. },
  13. subtitle: {
  14. name: chartData.series[0].name,
  15. color: '#666666',
  16. fontSize: 15*_self.pixelRatio,
  17. offsetY:-50*_self.pixelRatio,//新增参数,自定义调整Y轴文案距离
  18. },
  19. extra: {
  20. gauge:{
  21. type:'default',
  22. width: _self.gaugeWidth*_self.pixelRatio,//仪表盘背景的宽度
  23. startAngle:0.75,
  24. endAngle:0.25,
  25. startNumber:0,
  26. endNumber:100,
  27. splitLine:{
  28. fixRadius:0,
  29. splitNumber:10,
  30. width: _self.gaugeWidth*_self.pixelRatio,//仪表盘背景的宽度
  31. color:'#FFFFFF',
  32. childNumber:5,
  33. childWidth:_self.gaugeWidth*0.4*_self.pixelRatio,//仪表盘背景的宽度
  34. },
  35. pointer:{
  36. width: _self.gaugeWidth*0.8*_self.pixelRatio,//指针宽度
  37. color:'auto'
  38. }
  39. }
  40. },
  41. background:'#FFFFFF',
  42. pixelRatio:_self.pixelRatio,
  43. categories: chartData.categories,
  44. series: chartData.series,
  45. animation: true,
  46. width: _self.cWidth*_self.pixelRatio,
  47. height: _self.cHeight*_self.pixelRatio,
  48. dataLabel: true,
  49. });

完整代码示例

  1. <template>
  2. <view class="qiun-columns">
  3. <view class="qiun-bg-white qiun-title-bar qiun-common-mt" >
  4. <view class="qiun-title-dot-light">仪表盘</view>
  5. </view>
  6. <view class="qiun-charts" >
  7. <canvas canvas-id="canvasGauge" id="canvasGauge" class="charts"></canvas>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import uCharts from '@/components/u-charts/u-charts.js';
  13. var _self;
  14. var canvaGauge=null;
  15. export default {
  16. data() {
  17. return {
  18. cWidth:'',
  19. cHeight:'',
  20. pixelRatio:1,
  21. }
  22. },
  23. onLoad() {
  24. _self = this;
  25. this.cWidth=uni.upx2px(750);
  26. this.cHeight=uni.upx2px(500);
  27. this.getServerData();
  28. },
  29. methods: {
  30. getServerData(){
  31. uni.request({
  32. url: 'https://www.easy-mock.com/mock/5cc586b64fc5576cba3d647b/uni-wx-charts/chartsdata2',
  33. data:{
  34. },
  35. success: function(res) {
  36. console.log(res.data.data)
  37. let Gauge={categories:[],series:[]};
  38. //这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去
  39. Gauge.categories=res.data.data.Gauge.categories;
  40. Gauge.series=res.data.data.Gauge.series;
  41. _self.showGauge("canvasGauge",Gauge);
  42. },
  43. fail: () => {
  44. _self.tips="网络错误,小程序端请检查合法域名";
  45. },
  46. });
  47. },
  48. showGauge(canvasId,chartData){
  49. canvaGauge = new uCharts({
  50. $this:_self,
  51. canvasId: canvasId,
  52. type: 'gauge',
  53. fontSize:11,
  54. legend:false,
  55. title: {
  56. name: Math.round(chartData.series[0].data*100)+'%',
  57. color: chartData.categories[1].color,
  58. fontSize: 25*_self.pixelRatio,
  59. offsetY:50*_self.pixelRatio,//新增参数,自定义调整Y轴文案距离
  60. },
  61. subtitle: {
  62. name: chartData.series[0].name,
  63. color: '#666666',
  64. fontSize: 15*_self.pixelRatio,
  65. offsetY:-50*_self.pixelRatio,//新增参数,自定义调整Y轴文案距离
  66. },
  67. extra: {
  68. gauge:{
  69. type:'default',
  70. width: _self.gaugeWidth*_self.pixelRatio,//仪表盘背景的宽度
  71. startAngle:0.75,
  72. endAngle:0.25,
  73. startNumber:0,
  74. endNumber:100,
  75. splitLine:{
  76. fixRadius:0,
  77. splitNumber:10,
  78. width: _self.gaugeWidth*_self.pixelRatio,//仪表盘背景的宽度
  79. color:'#FFFFFF',
  80. childNumber:5,
  81. childWidth:_self.gaugeWidth*0.4*_self.pixelRatio,//仪表盘背景的宽度
  82. },
  83. pointer:{
  84. width: _self.gaugeWidth*0.8*_self.pixelRatio,//指针宽度
  85. color:'auto'
  86. }
  87. }
  88. },
  89. background:'#FFFFFF',
  90. pixelRatio:_self.pixelRatio,
  91. categories: chartData.categories,
  92. series: chartData.series,
  93. animation: true,
  94. width: _self.cWidth*_self.pixelRatio,
  95. height: _self.cHeight*_self.pixelRatio,
  96. dataLabel: true,
  97. });
  98. }
  99. }
  100. }
  101. </script>
  102. <style>
  103. /*样式的width和height一定要与定义的cWidth和cHeight相对应*/
  104. .qiun-charts {
  105. width: 750upx;
  106. height: 500upx;
  107. background-color: #FFFFFF;
  108. }
  109. .charts {
  110. width: 750upx;
  111. height: 500upx;
  112. background-color: #FFFFFF;
  113. }
  114. </style>