F2 可视化方案 折线图 和 曲线图

F2 文档

  1. ui 组件中集成了 F2 移动端可视化方案
  2. 文档中写了俩个简单的 折线图和曲线图的 demo
  3. 具体需要使用查看F2 的官方文档

效果展示

F2 可视化方案 折线图 和 曲线图 - 图1

代码示例

  1. <template>
  2. <div>
  3. <canvas id="brokenLine" height="260"></canvas>
  4. <br/><br/><br/>
  5. <canvas id="brokenLine2" height="260"></canvas>
  6. </div>
  7. </template>
  8. <script>
  9. import F2 from '@antv/f2'
  10. export default {
  11. created() {
  12. this.$nextTick(() => {
  13. this.setNavbar(); // 设置topbar
  14. // 画折线图
  15. this.playBrokenLine();
  16. // 画曲线图
  17. this.playCurve();
  18. });
  19. },
  20. data () {
  21. return {
  22. list: [{
  23. day: '周一',
  24. value: 300
  25. }, {
  26. day: '周二',
  27. value: 400
  28. }, {
  29. day: '周三',
  30. value: 350
  31. }, {
  32. day: '周四',
  33. value: 500
  34. }, {
  35. day: '周五',
  36. value: 490
  37. }, {
  38. day: '周六',
  39. value: 600
  40. }, {
  41. day: '周日',
  42. value: 900
  43. }],
  44. list2: [{
  45. time: '2016-08-08 00:00:00',
  46. tem: 10
  47. }, {
  48. time: '2016-08-08 00:10:00',
  49. tem: 22
  50. }, {
  51. time: '2016-08-08 00:30:00',
  52. tem: 20
  53. }, {
  54. time: '2016-08-09 00:35:00',
  55. tem: 26
  56. }, {
  57. time: '2016-08-09 01:00:00',
  58. tem: 20
  59. }, {
  60. time: '2016-08-09 01:20:00',
  61. tem: 26
  62. }, {
  63. time: '2016-08-10 01:40:00',
  64. tem: 28
  65. }, {
  66. time: '2016-08-10 02:00:00',
  67. tem: 20
  68. }, {
  69. time: '2016-08-10 02:20:00',
  70. tem: 18
  71. }]
  72. }
  73. },
  74. methods: {
  75. setNavbar(){
  76. AI.setNavbar({
  77. title: 'F2折线图',
  78. })
  79. },
  80. // 画折线图
  81. playBrokenLine() {
  82. var chart = new F2.Chart({
  83. id: 'brokenLine',
  84. pixelRatio: window.devicePixelRatio
  85. });
  86. chart.source(this.list, {
  87. value: {
  88. tickCount: 5,
  89. min: 0
  90. },
  91. day: {
  92. range: [0, 1]
  93. }
  94. });
  95. chart.tooltip({
  96. showCrosshairs: true,
  97. showItemMarker: false,
  98. onShow: function onShow(ev) {
  99. var items = ev.items;
  100. items[0].name = null;
  101. items[0].value = '$ ' + items[0].value;
  102. }
  103. });
  104. chart.axis('day', {
  105. label: function label(text, index, total) {
  106. var textCfg = {};
  107. if (index === 0) {
  108. textCfg.textAlign = 'left';
  109. } else if (index === total - 1) {
  110. textCfg.textAlign = 'right';
  111. }
  112. return textCfg;
  113. }
  114. });
  115. chart.line().position('day*value');
  116. chart.point().position('day*value').style({
  117. stroke: '#fff',
  118. lineWidth: 1
  119. });
  120. chart.render();
  121. },
  122. // 绘制曲线图
  123. playCurve() {
  124. var chart = new F2.Chart({
  125. id: 'brokenLine2',
  126. pixelRatio: window.devicePixelRatio
  127. });
  128. var defs = {
  129. time: {
  130. type: 'timeCat',
  131. mask: 'MM/DD',
  132. tickCount: 3,
  133. range: [0, 1]
  134. },
  135. tem: {
  136. tickCount: 5,
  137. min: 0,
  138. alias: '日均温度'
  139. }
  140. };
  141. chart.source(this.list2, defs);
  142. chart.axis('time', {
  143. label: function label(text, index, total) {
  144. var textCfg = {};
  145. if (index === 0) {
  146. textCfg.textAlign = 'left';
  147. } else if (index === total - 1) {
  148. textCfg.textAlign = 'right';
  149. }
  150. return textCfg;
  151. }
  152. });
  153. chart.tooltip({
  154. showCrosshairs: true
  155. });
  156. chart.line().position('time*tem').shape('smooth');
  157. chart.point().position('time*tem').shape('smooth').style({
  158. stroke: '#fff',
  159. lineWidth: 1
  160. });
  161. chart.render();
  162. }
  163. }
  164. }
  165. </script>
  166. <style>
  167. #brokenLine, #brokenLine2{
  168. width: 100%;
  169. }
  170. </style>