1. /**
  2. * User: lulongwen
  3. * Date: 2019-10-24 10:09
  4. * Description: 折线图封装
  5. */
  6. import React, { useEffect, useState } from 'react';
  7. import { array, bool, number, oneOf } from 'prop-types';
  8. import {
  9. TooltipComponent,
  10. GridComponent,
  11. LegendComponent,
  12. } from 'echarts/components';
  13. import { LineChart } from 'echarts/charts';
  14. import { UniversalTransition } from 'echarts/features';
  15. import Echarts, {
  16. grid, xAxis, yAxis, chartHeight, mapLabels, getColor, darkColor,
  17. } from '@/components/Echarts';
  18. import { readLabeledMetricsValues } from './readLabel.json';
  19. Chart.propTypes = {
  20. dataSource: array,
  21. xAxisData: array,
  22. height: number,
  23. loading: bool,
  24. theme: oneOf(['light', 'dark'])
  25. };
  26. Chart.defaultProps = {
  27. dataSource: [],
  28. xAxisData: [],
  29. loading: false,
  30. height: chartHeight,
  31. theme: 'light',
  32. };
  33. function Chart({ dataSource, xAxisData, loading, theme, height }) {
  34. const [options, setOptions] = useState({});
  35. useEffect(update, [dataSource]);
  36. function update() {
  37. const OPTIONS = getOptions();
  38. setOptions(OPTIONS);
  39. }
  40. function getOptions() {
  41. const _dataSource = mapLabels(readLabeledMetricsValues, "P50, P75, P90, P95, P99");
  42. const legendData = _dataSource.map(it => it.label);
  43. const seriesData = _dataSource.map(({ label, value }) => {
  44. return {
  45. name: label,
  46. type: 'line',
  47. symbol: 'none',
  48. data: value,
  49. lineStyle: {
  50. width: 1.5,
  51. type: 'solid',
  52. },
  53. // stack: 'Total', // 堆叠折线图
  54. };
  55. });
  56. return {
  57. color: getColor(legendData),
  58. grid: { ...grid, top: 40, left: 48 },
  59. tooltip: {
  60. trigger: 'axis',
  61. backgroundColor: 'rgb(50,50,50)',
  62. textStyle: {
  63. fontSize: 13,
  64. color: '#ccc',
  65. },
  66. enterable: true,
  67. extraCssText: 'max-height: 300px; overflow: auto;',
  68. },
  69. legend: {
  70. type: 'scroll',
  71. icon: 'circle',
  72. itemHeight: 10, // 图例大小
  73. data: legendData,
  74. show: legendData.length > 1,
  75. textStyle: { color: darkColor(theme) },
  76. },
  77. xAxis: {
  78. ...xAxis,
  79. data: xAxisData,
  80. },
  81. yAxis,
  82. series: seriesData,
  83. };
  84. }
  85. const attrs = {
  86. height,
  87. options,
  88. loading,
  89. components: [
  90. TooltipComponent,
  91. LegendComponent,
  92. GridComponent,
  93. LineChart,
  94. UniversalTransition,
  95. ],
  96. renderType: 'svg'
  97. };
  98. return (
  99. <Echarts {...attrs} />
  100. );
  101. }
  102. export default Chart;

series折线图数据

一个折线图的数据

  1. {
  2. series: [
  3. {
  4. data: [150, 230, 224, 218, 135, 147, 260],
  5. type: "line",
  6. smooth: true,
  7. },
  8. ];
  9. }