图表示例

:-:K线图基本用法 - 图1

标准数据格式

  1. Candle: {
  2. categories: ['2019/5/5', '2019/5/6', '2019/5/6', '2019/5/8', '2019/5/9', '2019/5/10'],
  3. series: [{
  4. name: '上证指数',
  5. data: [
  6. [2320.26, 2302.6, 2287.3, 2362.94],
  7. [2300, 2291.3, 2288.26, 2308.38],
  8. [2295.35, 2346.5, 2295.35, 2346.92],
  9. [2347.22, 2358.98, 2337.35, 2363.8],
  10. [2360.75, 2382.48, 2347.89, 2383.76],
  11. [2383.43, 2385.42, 2371.23, 2391.82],
  12. [2377.41, 2419.02, 2369.57, 2421.15]
  13. ]
  14. }]
  15. }

K线图data[n]传入顺序:开盘,收盘,最低,最高

调用方法

  1. canvaCandle=new uCharts({
  2. $this:_self,
  3. canvasId: canvasId,
  4. type: 'candle',
  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:true,
  15. itemCount:_self.itemCount,
  16. scrollShow:true,
  17. scrollAlign:'right',
  18. },
  19. yAxis: {
  20. gridType:'dash',
  21. splitNumber:5,
  22. format:(val)=>{return val.toFixed(0)}
  23. },
  24. width: _self.cWidth*_self.pixelRatio,
  25. height: _self.cHeight*_self.pixelRatio,
  26. dataLabel: false,
  27. dataPointShape: true,
  28. extra: {
  29. candle:{
  30. color:{
  31. upLine:'#f04864',
  32. upFill:'#f04864',
  33. downLine:'#2fc25b',
  34. downFill:'#2fc25b'
  35. },
  36. average:{
  37. show:true,
  38. name:['MA5','MA10','MA30'],
  39. day:[5,10,30],
  40. color:['#1890ff', '#2fc25b', '#facc14']
  41. }
  42. },
  43. tooltip:{
  44. bgColor:'#000000',
  45. bgOpacity:0.7,
  46. gridType:'dash',
  47. dashLength:5,
  48. gridColor:'#1890ff',
  49. fontColor:'#FFFFFF',
  50. horizentalLine:true,
  51. xAxisLabel:true,
  52. yAxisLabel:true,
  53. labelBgColor:'#DFE8FF',
  54. labelBgOpacity:0.95,
  55. labelAlign:'left',
  56. labelFontColor:'#666666'
  57. }
  58. },
  59. });

完整代码示例

  1. <template>
  2. <view class="qiun-columns">
  3. <view class="qiun-bg-white qiun-title-bar qiun-common-mt qiun-rows" >
  4. <view class="qiun-title-dot-light">基本K线图(完善中)</view>
  5. <view style="flex: 1;qiun-rows;text-align: right;">
  6. <button type="default" size="mini" @tap="tapButton('in')">放大</button>
  7. <button type="default" size="mini" style="margin-left: 20upx;" @tap="tapButton('out')">缩小</button>
  8. </view>
  9. </view>
  10. <view class="qiun-charts">
  11. <canvas canvas-id="canvasCandle" id="canvasCandle" class="charts" disable-scroll=true @touchstart="touchCandle" @touchmove="moveCandle" @touchend="touchEndCandle"></canvas>
  12. </view>
  13. <view class="qiun-padding qiun-bg-white ">
  14. <slider :value="itemCount" min="5" :max="sliderMax" block-color="#f8f8f8" block-size="18" @changing="sliderMove" @change="sliderMove"/>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. import uCharts from '@/components/u-charts/u-charts.js';
  20. var _self;
  21. var canvaCandle=null;
  22. export default {
  23. data() {
  24. return {
  25. cWidth:'',
  26. cHeight:'',
  27. pixelRatio:1,
  28. itemCount:20,//x轴单屏数据密度
  29. sliderMax:50,
  30. }
  31. },
  32. onLoad() {
  33. _self = this;
  34. this.cWidth=uni.upx2px(750);
  35. this.cHeight=uni.upx2px(500);
  36. this.getServerData();
  37. },
  38. methods: {
  39. getServerData(){
  40. uni.request({
  41. url: 'https://www.easy-mock.com/mock/5cc586b64fc5576cba3d647b/uni-wx-charts/chartsdata2',
  42. data:{
  43. },
  44. success: function(res) {
  45. console.log(res.data.data)
  46. let Candle={categories:[],series:[]};
  47. //这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去
  48. Candle.categories=res.data.data.Candle.categories;
  49. Candle.series=res.data.data.Candle.series;
  50. _self.showCandle("canvasCandle",Candle);
  51. },
  52. fail: () => {
  53. _self.tips="网络错误,小程序端请检查合法域名";
  54. },
  55. });
  56. },
  57. showCandle(canvasId,chartData){
  58. canvaCandle=new uCharts({
  59. $this:_self,
  60. canvasId: canvasId,
  61. type: 'candle',
  62. fontSize:11,
  63. legend:true,
  64. background:'#FFFFFF',
  65. pixelRatio:_self.pixelRatio,
  66. categories: chartData.categories,
  67. series: chartData.series,
  68. animation: true,
  69. enableScroll: true,
  70. xAxis: {
  71. disableGrid:true,
  72. itemCount:_self.itemCount,
  73. scrollShow:true,
  74. scrollAlign:'right',
  75. },
  76. yAxis: {
  77. //disabled:true
  78. gridType:'dash',
  79. splitNumber:5,
  80. format:(val)=>{return val.toFixed(0)}
  81. },
  82. width: _self.cWidth*_self.pixelRatio,
  83. height: _self.cHeight*_self.pixelRatio,
  84. dataLabel: false,
  85. dataPointShape: true,
  86. extra: {
  87. candle:{
  88. color:{
  89. upLine:'#f04864',
  90. upFill:'#f04864',
  91. downLine:'#2fc25b',
  92. downFill:'#2fc25b'
  93. },
  94. average:{
  95. show:true,
  96. name:['MA5','MA10','MA30'],
  97. day:[5,10,30],
  98. color:['#1890ff', '#2fc25b', '#facc14']
  99. }
  100. },
  101. tooltip:{
  102. bgColor:'#000000',
  103. bgOpacity:0.7,
  104. gridType:'dash',
  105. dashLength:5,
  106. gridColor:'#1890ff',
  107. fontColor:'#FFFFFF',
  108. horizentalLine:true,
  109. xAxisLabel:true,
  110. yAxisLabel:true,
  111. labelBgColor:'#DFE8FF',
  112. labelBgOpacity:0.95,
  113. labelAlign:'left',
  114. labelFontColor:'#666666'
  115. }
  116. },
  117. });
  118. },
  119. touchCandle(e){
  120. canvaCandle.scrollStart(e);
  121. },
  122. moveCandle(e) {
  123. canvaCandle.scroll(e);
  124. },
  125. touchEndCandle(e) {
  126. canvaCandle.scrollEnd(e);
  127. //下面是toolTip事件,如果滚动后不需要显示,可不填写
  128. canvaCandle.showToolTip(e, {
  129. format: function (item, category) {
  130. return category + ' ' + item.name + ':' + item.data
  131. }
  132. });
  133. },
  134. tapButton(type){
  135. let step=5;
  136. if(type=='in'){
  137. _self.itemCount -= step;
  138. if(_self.itemCount<=5){
  139. _self.itemCount=5;
  140. }
  141. }else{
  142. _self.itemCount += step;
  143. if(_self.itemCount>=_self.sliderMax){
  144. _self.itemCount=_self.sliderMax;
  145. }
  146. }
  147. _self.zoomCandle(_self.itemCount);
  148. },
  149. sliderMove(e){
  150. _self.itemCount=e.detail.value;
  151. _self.zoomCandle(e.detail.value);
  152. },
  153. zoomCandle(val) {
  154. canvaCandle.zoom({
  155. itemCount: val
  156. });
  157. }
  158. }
  159. }
  160. </script>
  161. <style>
  162. /*样式的width和height一定要与定义的cWidth和cHeight相对应*/
  163. .qiun-charts {
  164. width: 750upx;
  165. height: 500upx;
  166. background-color: #FFFFFF;
  167. }
  168. .charts {
  169. width: 750upx;
  170. height: 500upx;
  171. background-color: #FFFFFF;
  172. }
  173. .qiun-textarea{height: 300upx;}
  174. </style>