f2ExtendCharts.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <div class="nt-f2-charts">
  3. <div v-if="chart.chartOptions.title" class="nt-f2-header" :class="chart.chartOptions.title.line ? 'line-flex': ''">
  4. <div
  5. v-if="chart.chartOptions.title.text"
  6. class="title"
  7. :class="chart.chartOptions.title.px == 'center' ? 'center' : (chart.chartOptions.title.px == 'left' ? 'left' : 'right')"
  8. >
  9. <div class="title__icon">
  10. <span v-if="chart.chartOptions.title.textIcon" :class="chart.chartOptions.title.textIcon"></span>
  11. <span :class="chart.chartOptions.title.textStyle">{{chart.chartOptions.title.text}}</span>
  12. </div>
  13. <div v-if="chart.chartOptions.dropdown" class="nt-dropdown-menu">
  14. <van-dropdown-menu>
  15. <van-dropdown-item
  16. @change="dropdownChange()"
  17. v-model="chart.chartOptions.dropdown.valueDef"
  18. :options="chart.chartOptions.dropdown.list"
  19. />
  20. </van-dropdown-menu>
  21. </div>
  22. </div>
  23. <div
  24. v-if="chart.chartOptions.title.subtext"
  25. class="sub-title"
  26. :class="chart.chartOptions.title.px == 'center' ? 'center' : (chart.chartOptions.title.px == 'left' ? 'left' : 'right')"
  27. >{{chart.chartOptions.title.subtext}}</div>
  28. </div>
  29. <div class="f2-chart-wrapper" :class="[chart.chartOptions.class, chart.chartOptions.id]">
  30. <canvas :id="chart.chartOptions.id"></canvas>
  31. </div>
  32. </div>
  33. </template>
  34. <script>
  35. import F2 from "@antv/f2/lib/index-all";
  36. import { chartsInitData, drawCharts } from "./f2chart";
  37. export default {
  38. name: "F2",
  39. props: {},
  40. inject: ['chart'],
  41. data() {
  42. return chartsInitData();
  43. },
  44. watch: {
  45. chartData: {
  46. deep: true,
  47. handler() {
  48. this.initData();
  49. }
  50. }
  51. },
  52. created() {},
  53. mounted() {
  54. this.initData();
  55. },
  56. methods: {
  57. initData() {
  58. if (!this.elChart) {
  59. let chartClass = "f2-chart-wrapper",
  60. el = document.getElementsByClassName(chartClass)[0],
  61. chartWidth = el.clientWidth || el.offsetWidth,
  62. chartHeight = el.clientHeight || el.offsetHeight,
  63. chartDom = document.getElementById(this.chart.chartOptions.id);
  64. chartDom.addEventListener("touchstart", function(e) {
  65. if (e.preventDefault) e.preventDefault();
  66. });
  67. this.elChart = new F2.Chart({
  68. id: this.chart.chartOptions.id,
  69. width: chartWidth,
  70. height: chartHeight,
  71. appendPadding: [10, 0, 20, 0],
  72. pixelRatio: window.devicePixelRatio
  73. });
  74. }
  75. drawCharts(this);
  76. },
  77. dropdownChange() {
  78. let selected = null,
  79. dList = this.dropdown.list;
  80. for (let i = 0; i < dList.length; i++) {
  81. if (dList[i].value == this.dropdown.valueDef) {
  82. selected = dList[i];
  83. }
  84. }
  85. if (selected && this.chart.chartOptions.hasOwnProperty("series")) {
  86. let list = [];
  87. this.chart.chartOptions.series.forEach(item => {
  88. if (selected.select.indexOf(item[selected.name]) !== -1) {
  89. list.push(item);
  90. }
  91. });
  92. this.downList = list;
  93. this.elChart.clear();
  94. drawCharts(this);
  95. }
  96. }
  97. }
  98. };
  99. </script>
  100. <style lang="scss">
  101. .f2-chart-wrapper, .nt-f2-charts{
  102. height: 260px;
  103. width: 100%;
  104. }
  105. </style>