图表示例

线图基本用法 - 图1

标准数据格式

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

调用方法

  1. canvaLineA=new uCharts({
  2. $this:_self,
  3. canvasId: canvasId,
  4. type: 'line',
  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. format:(val)=>{return val.toFixed(0)+'元'}
  28. },
  29. width: _self.cWidth*_self.pixelRatio,
  30. height: _self.cHeight*_self.pixelRatio,
  31. extra: {
  32. line:{
  33. type: 'straight'
  34. }
  35. }
  36. });

完整代码示例

  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="canvasLineA" id="canvasLineA" class="charts" @touchstart="touchLineA"></canvas>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import uCharts from '@/components/u-charts/u-charts.js';
  13. var _self;
  14. var canvaLineA=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 LineA={categories:[],series:[]};
  38. //这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去
  39. LineA.categories=res.data.data.LineA.categories;
  40. LineA.series=res.data.data.LineA.series;
  41. _self.showLineA("canvasLineA",LineA);
  42. },
  43. fail: () => {
  44. _self.tips="网络错误,小程序端请检查合法域名";
  45. },
  46. });
  47. },
  48. showLineA(canvasId,chartData){
  49. canvaLineA=new uCharts({
  50. $this:_self,
  51. canvasId: canvasId,
  52. type: 'line',
  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. format:(val)=>{return val.toFixed(0)+'元'}
  76. },
  77. width: _self.cWidth*_self.pixelRatio,
  78. height: _self.cHeight*_self.pixelRatio,
  79. extra: {
  80. line:{
  81. type: 'straight'
  82. }
  83. }
  84. });
  85. },
  86. touchLineA(e) {
  87. canvaLineA.showToolTip(e, {
  88. format: function (item, category) {
  89. return category + ' ' + item.name + ':' + item.data
  90. }
  91. });
  92. }
  93. }
  94. }
  95. </script>
  96. <style>
  97. /*样式的width和height一定要与定义的cWidth和cHeight相对应*/
  98. .qiun-charts {
  99. width: 750upx;
  100. height: 500upx;
  101. background-color: #FFFFFF;
  102. }
  103. .charts {
  104. width: 750upx;
  105. height: 500upx;
  106. background-color: #FFFFFF;
  107. }
  108. </style>