图表示例

温度计式图表 - 图1

标准数据格式

  1. "ColumnMeter": {
  2. "categories": ["2013", "2014", "2015", "2016", "2017", "2018"],
  3. "series": [{
  4. "name": "目标值",
  5. "data": [35, 36, 31, 33, 13, 34],
  6. "color": "#2fc25b"
  7. }, {
  8. "name": "完成量",
  9. "data": [18, {"value": 27,"color": "#f04864"}, 21, 24, 6, 28],
  10. "color": "#1890ff"
  11. }]
  12. }

注意:series数据中第一位即series[0](目标值)中数值需要比series[1](完成量)中大。否则进度条会超出边框。

调用方法

  1. new uCharts({
  2. $this: _self,
  3. canvasId: canvasId,
  4. type: 'column',
  5. legend: true,
  6. fontSize: 11,
  7. background: '#FFFFFF',
  8. pixelRatio: _self.pixelRatio,
  9. animation: true,
  10. categories: chartData.categories,
  11. series: chartData.series,
  12. xAxis: {
  13. disableGrid: true,
  14. },
  15. yAxis: {
  16. //disabled:true
  17. },
  18. dataLabel: true,
  19. width: _self.cWidth * _self.pixelRatio,
  20. height: _self.cHeight * _self.pixelRatio,
  21. extra: {
  22. column: {
  23. //meter参数为“温度计式图表”
  24. type: 'meter',
  25. //width为每个柱子的宽度
  26. width: _self.cWidth * _self.pixelRatio * 0.45 / chartData.categories.length,
  27. meter: {
  28. //这个是外层边框(即目标值)的宽度
  29. border: 4,
  30. //这个是内部填充颜色
  31. fillColor: '#E5FDC3'
  32. }
  33. }
  34. }
  35. });

完整代码示例

  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="canvasColumnMeter" id="canvasColumnMeter" class="charts" @touchstart="touchColumn"></canvas>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import uCharts from '../../components/u-charts/u-charts.js';
  13. var _self;
  14. var canvaColumn=null;
  15. export default {
  16. data() {
  17. return {
  18. cWidth:'',
  19. cHeight:'',
  20. pixelRatio:1,
  21. serverData:'',
  22. }
  23. },
  24. onLoad() {
  25. _self = this;
  26. this.cWidth=uni.upx2px(750);
  27. this.cHeight=uni.upx2px(500);
  28. this.getServerData();
  29. },
  30. methods: {
  31. getServerData(){
  32. uni.request({
  33. url: 'https://www.easy-mock.com/mock/5cc586b64fc5576cba3d647b/uni-wx-charts/chartsdata2',
  34. data:{
  35. },
  36. success: function(res) {
  37. console.log(res.data.data)
  38. //下面这个根据需要保存后台数据,我是为了模拟更新柱状图,所以存下来了
  39. _self.serverData=res.data.data;
  40. let ColumnMeter={categories:[],series:[]};
  41. //这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去
  42. ColumnMeter.categories=res.data.data.ColumnMeter.categories;
  43. //这里的series数据,如果为Meter,series[0]定义为外层数据,series[1]定义为内层数据
  44. ColumnMeter.series=res.data.data.ColumnMeter.series;
  45. _self.showColumnMeter("canvasColumnMeter",ColumnMeter);
  46. },
  47. fail: () => {
  48. _self.tips="网络错误,小程序端请检查合法域名";
  49. },
  50. });
  51. },
  52. showColumnMeter(canvasId,chartData){
  53. canvaColumn=new uCharts({
  54. $this:_self,
  55. canvasId: canvasId,
  56. type: 'column',
  57. legend:true,
  58. fontSize:11,
  59. background:'#FFFFFF',
  60. pixelRatio:_self.pixelRatio,
  61. animation: true,
  62. categories: chartData.categories,
  63. series: chartData.series,
  64. xAxis: {
  65. disableGrid:true,
  66. },
  67. yAxis: {
  68. },
  69. dataLabel: true,
  70. width: _self.cWidth*_self.pixelRatio,
  71. height: _self.cHeight*_self.pixelRatio,
  72. extra: {
  73. column: {
  74. type:'meter',
  75. width: _self.cWidth*_self.pixelRatio*0.45/chartData.categories.length,
  76. meter:{
  77. //这个是外层边框的宽度
  78. border:4,
  79. //这个是内部填充颜色
  80. fillColor:'#E5FDC3'
  81. }
  82. }
  83. }
  84. });
  85. },
  86. touchColumn(e){
  87. canvaColumn.showToolTip(e, {
  88. format: function (item, category) {
  89. if(typeof item.data === 'object'){
  90. return category + ' ' + item.name + ':' + item.data.value
  91. }else{
  92. return category + ' ' + item.name + ':' + item.data
  93. }
  94. }
  95. });
  96. },
  97. }
  98. }
  99. </script>
  100. <style>
  101. page{background:#F2F2F2;width: 750upx;overflow-x: hidden;}
  102. .qiun-padding{padding:2%; width:96%;}
  103. .qiun-wrap{display:flex; flex-wrap:wrap;}
  104. .qiun-rows{display:flex; flex-direction:row !important;}
  105. .qiun-columns{display:flex; flex-direction:column !important;}
  106. .qiun-common-mt{margin-top:10upx;}
  107. .qiun-bg-white{background:#FFFFFF;}
  108. .qiun-title-bar{width:96%; padding:10upx 2%; flex-wrap:nowrap;}
  109. .qiun-title-dot-light{border-left: 10upx solid #0ea391; padding-left: 10upx; font-size: 32upx;color: #000000}
  110. .qiun-charts{width: 750upx; height:500upx;background-color: #FFFFFF;}
  111. .charts{width: 750upx; height:500upx;background-color: #FFFFFF;}
  112. </style>