图表示例

区域图基本用法 - 图1

标准数据格式

  1. Area: {
  2. categories: ['2012', '2013', '2014', '2015', '2016', '2017'],
  3. series: [{
  4. name: '成交量A',
  5. data: [100, 80, 95, 150, 112, 132],
  6. color: '#facc14'
  7. }, {
  8. name: '成交量B',
  9. data: [70, 40, 65, 100, 44, 68],
  10. color: '#2fc25b'
  11. }, {
  12. name: '成交量C',
  13. data: [35, 20, 25, 37, 4, 20],
  14. color: '#1890ff'
  15. }]
  16. }

调用方法

  1. canvaArea=new uCharts({
  2. $this:_self,
  3. canvasId: canvasId,
  4. type: 'area',
  5. fontSize:11,
  6. legend:true,
  7. dataLabel:false,
  8. dataPointShape:true,
  9. background:'#FFFFFF',
  10. pixelRatio:_self.pixelRatio,
  11. categories: chartData.categories,
  12. series: chartData.series,
  13. animation: true,
  14. xAxis: {
  15. type:'grid',
  16. gridColor:'#CCCCCC',
  17. gridType:'dash',
  18. dashLength:8
  19. },
  20. yAxis: {
  21. gridType:'dash',
  22. gridColor:'#CCCCCC',
  23. dashLength:8,
  24. splitNumber:5,
  25. min:10,
  26. max:180,
  27. },
  28. width: _self.cWidth*_self.pixelRatio,
  29. height: _self.cHeight*_self.pixelRatio,
  30. extra: {
  31. area:{
  32. type: 'straight',
  33. opacity:0.2,
  34. addLine:true,
  35. width:2
  36. }
  37. }
  38. });

完整代码示例

  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="canvasArea" id="canvasArea" class="charts" @touchstart="touchArea"></canvas>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import uCharts from '@/components/u-charts/u-charts.js';
  13. var _self;
  14. var canvaArea=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 Area={categories:[],series:[]};
  38. //这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去
  39. Area.categories=res.data.data.Area.categories;
  40. Area.series=res.data.data.Area.series;
  41. _self.showArea("canvasArea",Area);
  42. },
  43. fail: () => {
  44. _self.tips="网络错误,小程序端请检查合法域名";
  45. },
  46. });
  47. },
  48. showArea(canvasId,chartData){
  49. canvaArea=new uCharts({
  50. $this:_self,
  51. canvasId: canvasId,
  52. type: 'area',
  53. fontSize:11,
  54. legend:true,
  55. dataLabel:false,
  56. dataPointShape:true,
  57. background:'#FFFFFF',
  58. pixelRatio:_self.pixelRatio,
  59. categories: chartData.categories,
  60. series: chartData.series,
  61. animation: true,
  62. xAxis: {
  63. type:'grid',
  64. gridColor:'#CCCCCC',
  65. gridType:'dash',
  66. dashLength:8
  67. },
  68. yAxis: {
  69. gridType:'dash',
  70. gridColor:'#CCCCCC',
  71. dashLength:8,
  72. splitNumber:5,
  73. min:10,
  74. max:180,
  75. },
  76. width: _self.cWidth*_self.pixelRatio,
  77. height: _self.cHeight*_self.pixelRatio,
  78. extra: {
  79. area:{
  80. type: 'straight',
  81. opacity:0.2,
  82. addLine:true,
  83. width:2
  84. }
  85. }
  86. });
  87. },
  88. touchArea(e) {
  89. canvaArea.showToolTip(e, {
  90. format: function (item, category) {
  91. return category + ' ' + item.name + ':' + item.data
  92. }
  93. });
  94. }
  95. }
  96. }
  97. </script>
  98. <style>
  99. /*样式的width和height一定要与定义的cWidth和cHeight相对应*/
  100. .qiun-charts {
  101. width: 750upx;
  102. height: 500upx;
  103. background-color: #FFFFFF;
  104. }
  105. .charts {
  106. width: 750upx;
  107. height: 500upx;
  108. background-color: #FFFFFF;
  109. }
  110. </style>