环形图

image.png
具体实现看代码中的注释

  1. import React, { useEffect, useRef } from 'react'
  2. import * as echarts from 'echarts'
  3. import { createEchartsOptions } from '../shared/create-echart-options'
  4. import { px } from '../shared/px'
  5. export const Chart7 = () => {
  6. const divRef = useRef(null)
  7. useEffect(() => {
  8. console.log(divRef.current)
  9. var myChart = echarts.init(divRef.current)
  10. const color = ['#8D70F8', '#33A4FA']
  11. myChart.setOption(
  12. createEchartsOptions({
  13. color: color,
  14. xAxis: { show: false },
  15. yAxis: { show: false },
  16. legend: {
  17. bottom: px(10),
  18. itemWidth: px(20),
  19. itemHeight: px(10),
  20. itemGap: px(20),
  21. textStyle: {
  22. fontSize: px(16),
  23. lineHeight: px(16),
  24. color: '#a1d1f1',
  25. },
  26. },
  27. series: [
  28. {
  29. name: '访问来源',
  30. type: 'pie',
  31. radius: ['50%', '60%'],
  32. center: ['50%', '45%'], // 设置图例的位置
  33. avoidLabelOverlap: false,
  34. labelLine: { show: false },
  35. label: {
  36. distanceToLabelLine: -px(20), // label距离引导线的距离
  37. show: true,
  38. position: 'outside',
  39. textStyle: { color: '#a1d1f1', fontSize: px(16) },
  40. formatter(options) {
  41. return options.value * 100 + '%'
  42. },
  43. },
  44. itemStyle: {
  45. borderColor: '#0F113A',
  46. borderWidth: px(4),
  47. },
  48. data: [
  49. { value: 0.2, name: '女' },
  50. { value: 0.8, name: '男' },
  51. ].map((item, index) => {
  52. return { ...item, label: { color: color[index] } } // 设置label的颜色
  53. }),
  54. },
  55. ],
  56. })
  57. )
  58. }, [])
  59. return (
  60. <div className="年龄段-图1">
  61. <div className="chart">
  62. <div className="main" ref={divRef} />
  63. <div className="text">性别</div>
  64. </div>
  65. </div>
  66. )
  67. }

自定义环形图

image.png

  1. option = {
  2. color: color,
  3. xAxis: { show: false },
  4. yAxis: { show: false },
  5. legend: {
  6. bottom: px(4),
  7. itemWidth: px(20),
  8. itemHeight: px(10),
  9. itemGap: px(20),
  10. textStyle: {
  11. fontSize: px(16),
  12. lineHeight: px(16),
  13. color: '#a1d1f1',
  14. },
  15. data: ['经济','民事', '刑事', '其他']
  16. },
  17. series: [
  18. {
  19. name: '访问来源',
  20. type: 'pie',
  21. radius: ['25%', '65%'],
  22. // 图例位置
  23. center: ['50%', '42%'],
  24. avoidLabelOverlap: false,
  25. labelLine: {
  26. show: true,
  27. length: px(15) // 设置指示线的长度
  28. },
  29. label: {
  30. show: true,
  31. position: 'outside',
  32. textStyle: { color: '#a1d1f1', fontSize: px(16) },
  33. formatter(options) {
  34. return options.value * 100 + '%'
  35. },
  36. },
  37. itemStyle: {
  38. shadowBlur: 200,
  39. shadowColor: 'rgba(0, 0, 0, 0.5)'
  40. },
  41. data: [
  42. { value: 0.24, name: '刑事' },
  43. { value: 0.36, name: '民事' },
  44. {value: 0.18, name: '经济'},
  45. {value: 0.20, name: '其他'}
  46. ].sort((a, b) => a.value - b.value).map((item, index) => {
  47. return { ...item, label: { color: color[index] } }
  48. }),
  49. roseType: 'radius', // 半径按数值大小成锯齿状
  50. },
  51. ],
  52. }

饼图

饼图和环形图的区别是饼图的 radius 只有一个值, 环形图的 radius 有2个值
image.png

  1. import React, { useEffect, useRef } from 'react'
  2. import * as echarts from 'echarts'
  3. import { createEchartsOptions } from '../shared/create-echart-options'
  4. import { px } from '../shared/px'
  5. export const Chart12 = () => {
  6. const divRef = useRef(null)
  7. useEffect(() => {
  8. const data = [
  9. { value: 0.08, name: '东岗路' },
  10. { value: 0.06, name: '段家滩' },
  11. { value: 0.26, name: '雁北' },
  12. { value: 0.09, name: '五泉山' },
  13. { value: 0.15, name: '中山路' },
  14. { value: 0.06, name: '庆阳路' },
  15. { value: 0.08, name: '武都路' },
  16. { value: 0.19, name: '酒泉路' },
  17. { value: 0.03, name: '天水路' },
  18. ]
  19. var myChart = echarts.init(divRef.current)
  20. const color = [
  21. '#8d70f8',
  22. '#f46064',
  23. '#f38e1c',
  24. '#1adc7d',
  25. '#e8e800',
  26. '#34a3f9',
  27. '#40b27d',
  28. '#ea7ccc',
  29. '#ff33ef',
  30. ]
  31. myChart.setOption(
  32. createEchartsOptions({
  33. color: color,
  34. xAxis: { show: false },
  35. yAxis: { show: false },
  36. grid: { x: 0, x2: 0, y: 0, y2: 0, containLabel: true },
  37. legend: {
  38. orient: 'vertical',
  39. left: px(20),
  40. top: 'center',
  41. icon: 'line', // 形状
  42. itemWidth: px(10),
  43. itemGap: px(10),
  44. textStyle: {
  45. fontSize: px(12),
  46. lineHeight: px(12),
  47. color: '#6ca8ed',
  48. },
  49. formatter(name) {
  50. const value =
  51. data.filter((i) => i.name === name)[0]?.value * 100 + '%'
  52. return name + ' ' + value
  53. },
  54. },
  55. series: [
  56. {
  57. name: '访问来源',
  58. type: 'pie',
  59. radius: '75%',
  60. // 图例位置
  61. center: ['60%', '50%'],
  62. avoidLabelOverlap: false,
  63. labelLine: {
  64. show: false,
  65. },
  66. label: {
  67. show: false,
  68. },
  69. data: data.map((item, index) => {
  70. return { ...item, label: { color: color[index] } }
  71. }),
  72. },
  73. ],
  74. })
  75. )
  76. }, [])
  77. return <div className="chart" ref={divRef} />
  78. }