双Y轴 Axis
    Tooltip
    format
    https://bizcharts.net/product/BizCharts4/demo/474
    image.png

    1. import React from "react";
    2. import {
    3. G2,
    4. Chart,
    5. Geom,
    6. Axis,
    7. Tooltip,
    8. Coord,
    9. Label,
    10. Legend,
    11. View,
    12. Guide,
    13. Shape,
    14. Facet,
    15. Util,
    16. Point,
    17. Line,
    18. Interval,
    19. } from "bizcharts";
    20. import moment from "moment";
    21. class LineChart extends React.Component {
    22. render() {
    23. const data = [
    24. {
    25. time: "2020-07-02 10:10",
    26. call: 4,
    27. waiting: 20,
    28. people: 2,
    29. },
    30. {
    31. time: "2020-07-02 10:15",
    32. call: 2,
    33. waiting: 30,
    34. people: 3,
    35. },
    36. {
    37. time: "2020-07-02 10:20",
    38. call: 13,
    39. waiting: 18,
    40. people: 5,
    41. },
    42. {
    43. time: "2020-07-02 10:25",
    44. call: 9,
    45. waiting: 19,
    46. people: 1,
    47. },
    48. {
    49. time: "2020-07-02 10:30",
    50. call: 5,
    51. waiting: 2,
    52. people: 3,
    53. },
    54. {
    55. time: "2020-07-02 10:35",
    56. call: 8,
    57. waiting: 2,
    58. people: 1,
    59. },
    60. {
    61. time: "2020-07-02 10:40",
    62. call: 13,
    63. waiting: 1,
    64. people: 2,
    65. },
    66. ];
    67. let chartIns = null;
    68. const scale = {
    69. // tickCount控制双轴的对齐
    70. people: {
    71. alias: "处理中客户(个)",
    72. tickCount: 5,
    73. min: 0,
    74. type: 'linear-strict'// (⚠️需要更新至4.1.x版本才能使用)
    75. },
    76. waiting: {
    77. alias: "等待中客户(个)",
    78. tickCount: 5,
    79. min: 0,
    80. type: 'linear-strict'
    81. },
    82. time: {
    83. alias: "时间(秒)",
    84. // type: 'timeCat',
    85. // mask: 'YYYY-MM-DD HH:mm:ss'
    86. },
    87. };
    88. const colors = ["#6394f9", "#62daaa"];
    89. const axisLabel = {
    90. formatter(text, item, index) {
    91. // return moment(text).format('YYYY-MM-DD HH:mm:ss')
    92. console.log("axisLabel", text);
    93. return moment(text).format("HH:mm");
    94. },
    95. };
    96. return (
    97. <Chart
    98. scale={scale}
    99. autoFit
    100. height={400}
    101. data={data}
    102. onGetG2Instance={(chart) => {
    103. chartIns = chart;
    104. }}
    105. >
    106. <Axis name="waiting" title />
    107. <Axis name="people" title />
    108. <Axis name="time" title label={axisLabel} />
    109. <Legend
    110. custom={true}
    111. allowAllCanceled={true}
    112. items={[
    113. {
    114. value: "waiting",
    115. name: "等待",
    116. marker: {
    117. symbol: "circle",
    118. style: { fill: colors[0], r: 5 },
    119. },
    120. },
    121. {
    122. value: "people",
    123. name: "人数",
    124. marker: {
    125. symbol: "hyphen",
    126. style: { stroke: colors[1], r: 5, lineWidth: 3 },
    127. },
    128. },
    129. ]}
    130. onChange={(ev) => {
    131. console.log("ev", ev);
    132. const item = ev.item;
    133. const value = item.value;
    134. const checked = !item.unchecked;
    135. const geoms = chartIns.geometries;
    136. for (let i = 0; i < geoms.length; i++) {
    137. const geom = geoms[i];
    138. if (geom.getYScale().field === value) {
    139. if (checked) {
    140. geom.show();
    141. } else {
    142. geom.hide();
    143. }
    144. }
    145. }
    146. }}
    147. />
    148. <Tooltip shared showCrosshairs />
    149. {/*API参考文档 https://pre-bxs.alibaba-inc.com/product/bizcharts/category/7/page/27#tooltip*/}
    150. <Line
    151. position="time*waiting"
    152. color={colors[0]}
    153. tooltip={[
    154. "time*waiting",
    155. (time, waiting) => {
    156. console.log("waiting", waiting);
    157. const myTitle = moment(time).format("MM-DD HH:mm");
    158. return {
    159. name: "等待中客户",
    160. value: `${waiting} 人`,
    161. title: myTitle,
    162. };
    163. },
    164. ]}
    165. />
    166. <Point
    167. position="time*waiting"
    168. color={colors[0]}
    169. size={3}
    170. shape="circle"
    171. tooltip={false}
    172. />
    173. <Line
    174. position="time*people"
    175. color={colors[1]}
    176. size={3}
    177. shape="smooth"
    178. tooltip={[
    179. "time*people",
    180. (time, people) => {
    181. console.log("people", people);
    182. const myTitle = moment(time).format("MM-DD HH:mm");
    183. return {
    184. name: "处理中客户",
    185. value: `${people} 人`,
    186. title: myTitle,
    187. };
    188. },
    189. ]}
    190. />
    191. <Point
    192. position="time*people"
    193. color={colors[1]}
    194. size={3}
    195. shape="circle"
    196. tooltip={false}
    197. />
    198. </Chart>
    199. );
    200. }
    201. }
    202. export default LineChart