scale属性

ticks是固定的值,如果值域有上限,就用 ticks
max-min是区间值,不固定,如果值的上限不确定,就用 max-min

ticks

ticks是固定的值

  1. // 双 Y轴,左侧百分比,右侧动态适应
  2. const ticks = [0, 8, 16, 24, 32];
  3. const scale = {
  4. percent: {
  5. alias: '百分比',
  6. type: 'linear',
  7. formatter: val => `${val} %`,
  8. tickCount: 5,
  9. ticks: [0, 25, 50, 75, 100],
  10. },
  11. total: {
  12. alias: '总量',
  13. type: 'linear',
  14. formatter: val => `${val} GB`,
  15. tickCount: 5,
  16. ticks,
  17. },
  18. free: {
  19. alias: '空闲',
  20. type: 'linear',
  21. formatter: val => `${val} GB`,
  22. tickCount: 5,
  23. ticks,
  24. },
  25. used: {
  26. alias: '已使用',
  27. type: 'linear',
  28. formatter: val => `${val} GB`,
  29. tickCount: 5,
  30. ticks,
  31. },
  32. time: {
  33. alias: '时间',
  34. type: timeCat ? 'timeCat' : 'time',
  35. formatter: val => formatDate(val),
  36. tickCount: 6,
  37. nice: false,
  38. },
  39. };
  40. <Chart
  41. forceFit
  42. data={data}
  43. padding={padding}
  44. scale={scale}
  45. height={320}
  46. />

max-min自适应

  1. // 获取最大值
  2. function getMaxValue(data) {
  3. if (!Array.isArray(data)) return 100;
  4. const total = data.map(it => it.total);
  5. return Math.max(...total);
  6. }
  7. const scale = {
  8. percent: {
  9. alias: '百分比',
  10. type: 'linear',
  11. formatter: val => `${val} %`,
  12. tickCount: 5,
  13. ticks: [0, 25, 50, 75, 100], // 百分比
  14. },
  15. total: {
  16. alias: '总量',
  17. type: 'linear',
  18. formatter: val => `${val} GB`,
  19. tickCount: 5,
  20. min: 0,
  21. max,
  22. },
  23. used: {
  24. alias: '已使用',
  25. type: 'linear',
  26. formatter: val => `${val} GB`,
  27. tickCount: 5,
  28. min: 0,
  29. max,
  30. },
  31. time: {
  32. alias: '时间',
  33. type: timeCat ? 'timeCat' : 'time',
  34. formatter: val => formatDate(val),
  35. tickCount: 6,
  36. nice: false,
  37. },
  38. };
  39. <Chart
  40. forceFit
  41. data={data}
  42. padding={padding}
  43. scale={scale}
  44. height={320}
  45. />