图表示例

混合图基本用法 - 图1

标准数据格式

  1. Mix: {
  2. "categories": ["2012", "2013", "2014", "2015", "2016", "2017"],
  3. "series": [{
  4. "name": "曲面",
  5. "data": [70, 50, 85, 130, 64, 88],
  6. "type": "area",
  7. "style": "curve"
  8. }, {
  9. "name": "柱1",
  10. "data": [40, 30, 55, 110, 24, 58],
  11. "type": "column"
  12. }, {
  13. "name": "柱2",
  14. "data": [50, 20, 75, 60, 34, 38],
  15. "type": "column"
  16. }, {
  17. "name": "曲线",
  18. "data": [70, 50, 85, 130, 64, 88],
  19. "type": "line",
  20. "style": "curve",
  21. "color": "#1890ff"
  22. }, {
  23. "name": "折线",
  24. "data": [120, 140, 105, 170, 95, 160],
  25. "type": "line",
  26. "color": "#2fc25b"
  27. }, {
  28. "name": "点",
  29. "data": [100, 80, 125, 150, 112, 132],
  30. "type": "point",
  31. "color": "#f04864"
  32. }]
  33. }

调用方法

  1. canvaMix=new uCharts({
  2. $this:_self,
  3. canvasId: canvasId,
  4. type: 'mix',
  5. fontSize:11,
  6. legend:true,
  7. background:'#FFFFFF',
  8. pixelRatio:_self.pixelRatio,
  9. categories: chartData.categories,
  10. series: chartData.series,
  11. animation: true,
  12. enableScroll: true,//开启图表拖拽功能
  13. xAxis: {
  14. disableGrid:false,
  15. type:'grid',
  16. gridType:'dash',
  17. itemCount:4,
  18. scrollShow:true,
  19. scrollAlign:'left',
  20. },
  21. yAxis: {
  22. gridType:'dash',
  23. splitNumber:5,
  24. min:10,
  25. max:180,
  26. format:(val)=>{return val.toFixed(0)}
  27. },
  28. width: _self.cWidth*_self.pixelRatio,
  29. height: _self.cHeight*_self.pixelRatio,
  30. dataLabel: true,
  31. dataPointShape: true,
  32. extra: {
  33. tooltip:{
  34. bgColor:'#000000',
  35. bgOpacity:0.7,
  36. gridType:'dash',
  37. dashLength:8,
  38. gridColor:'#1890ff',
  39. fontColor:'#FFFFFF',
  40. horizentalLine:true,
  41. xAxisLabel:true,
  42. yAxisLabel:true,
  43. labelBgColor:'#DFE8FF',
  44. labelBgOpacity:0.95,
  45. labelAlign:'left',
  46. labelFontColor:'#666666'
  47. }
  48. },
  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="canvasMix" id="canvasMix" class="charts" disable-scroll=true @touchstart="touchMix" @touchmove="moveMix" @touchend="touchEndMix"></canvas>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import uCharts from '@/components/u-charts/u-charts.js';
  13. var _self;
  14. var canvaMix=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 Mix={categories:[],series:[]};
  38. //这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去
  39. Mix.categories=res.data.data.Mix.categories;
  40. Mix.series=res.data.data.Mix.series;
  41. _self.showMix("canvasMix",Mix);
  42. },
  43. fail: () => {
  44. _self.tips="网络错误,小程序端请检查合法域名";
  45. },
  46. });
  47. },
  48. showMix(canvasId,chartData){
  49. canvaMix=new uCharts({
  50. $this:_self,
  51. canvasId: canvasId,
  52. type: 'mix',
  53. fontSize:11,
  54. legend:true,
  55. background:'#FFFFFF',
  56. pixelRatio:_self.pixelRatio,
  57. categories: chartData.categories,
  58. series: chartData.series,
  59. animation: true,
  60. enableScroll: true,//开启图表拖拽功能
  61. xAxis: {
  62. disableGrid:false,
  63. type:'grid',
  64. gridType:'dash',
  65. itemCount:4,
  66. scrollShow:true,
  67. scrollAlign:'left',
  68. },
  69. yAxis: {
  70. gridType:'dash',
  71. splitNumber:5,
  72. min:10,
  73. max:180,
  74. format:(val)=>{return val.toFixed(0)}
  75. },
  76. width: _self.cWidth*_self.pixelRatio,
  77. height: _self.cHeight*_self.pixelRatio,
  78. dataLabel: true,
  79. dataPointShape: true,
  80. extra: {
  81. tooltip:{
  82. bgColor:'#000000',
  83. bgOpacity:0.7,
  84. gridType:'dash',
  85. dashLength:8,
  86. gridColor:'#1890ff',
  87. fontColor:'#FFFFFF',
  88. horizentalLine:true,
  89. xAxisLabel:true,
  90. yAxisLabel:true,
  91. labelBgColor:'#DFE8FF',
  92. labelBgOpacity:0.95,
  93. labelAlign:'left',
  94. labelFontColor:'#666666'
  95. }
  96. },
  97. });
  98. },
  99. touchMix(e){
  100. canvaMix.scrollStart(e);
  101. },
  102. moveMix(e) {
  103. canvaMix.scroll(e);
  104. },
  105. touchEndMix(e) {
  106. canvaMix.scrollEnd(e);
  107. //下面是toolTip事件,如果滚动后不需要显示,可不填写
  108. canvaMix.showToolTip(e, {
  109. format: function (item, category) {
  110. return category + ' ' + item.name + ':' + item.data
  111. }
  112. });
  113. }
  114. }
  115. }
  116. </script>
  117. <style>
  118. /*样式的width和height一定要与定义的cWidth和cHeight相对应*/
  119. .qiun-charts {
  120. width: 750upx;
  121. height: 500upx;
  122. background-color: #FFFFFF;
  123. }
  124. .charts {
  125. width: 750upx;
  126. height: 500upx;
  127. background-color: #FFFFFF;
  128. }
  129. </style>