图表符号和图例符号在SwiftUI中不同。

huangapple go评论98阅读模式
英文:

Chart Symbols and Legend Symbols Are Diferent in SwiftUI

问题

图表中使用.symbol(by:)修饰符生成的图例符号与图表中使用的符号不同。我特别在图表中使用了三角形和加号符号,但它们被忽略了,自动在图例部分生成了圆圈和方形符号。如何使它们一致?

以下是修复代码:

  1. struct Line: Identifiable {
  2. let xyzipped: [(Double, Double)]
  3. let symbol: BasicChartSymbolShape
  4. let legendStr: String
  5. let id = UUID()
  6. init(x: [Double],
  7. y: [Double],
  8. symbol: BasicChartSymbolShape,
  9. legendStr: String)
  10. {
  11. self.xyzipped = Array(zip(x, y))
  12. self.symbol = symbol
  13. self.legendStr = legendStr
  14. }
  15. }
  16. struct ChartOfLines: View {
  17. let lines: [Line]
  18. init() {
  19. let line1 = Line(x: [0, 1, 2, 3, 4],
  20. y: [3, 5, 8, -1, 0],
  21. symbol: BasicChartSymbolShape.triangle,
  22. legendStr: "line1")
  23. let line2 = Line(x: [0, 1, 2, 3, 4],
  24. y: [5, 4, 3, 7, 1],
  25. symbol: BasicChartSymbolShape.plus,
  26. legendStr: "line2")
  27. lines = [line1, line2]
  28. }
  29. var body: some View {
  30. Chart(lines, id: \.id) { line in
  31. ForEach(line.xyzipped, id:\.0) { xy in
  32. LineMark(
  33. x: .value("X values", xy.0),
  34. y: .value("Y values", xy.1)
  35. )
  36. .lineStyle(StrokeStyle(lineWidth: 0.5))
  37. PointMark(
  38. x: .value("X values", xy.0),
  39. y: .value("Y values", xy.1)
  40. )
  41. }
  42. .symbol(line.symbol)
  43. .symbol(by: .value("", line.legendStr))
  44. .foregroundStyle(by: .value("", line.legendStr))
  45. }
  46. .frame(width: 500, height: 500)
  47. .padding()
  48. }
  49. }

这将确保图例中的符号与图表中使用的一致。

英文:

Legend symbols produced by the .symbol(by:) modifier are different than the ones used in the chart. I specifically used the triangle and plus symbols in the chart, but they are disregarded and the circle and square symbols are produced automatically in the legend part. How can I make them agree?

  1. struct Line: Identifiable {
  2. let xyzipped: [(Double, Double)]
  3. let symbol: BasicChartSymbolShape
  4. let legendStr: String
  5. let id = UUID()
  6. init(x: [Double],
  7. y: [Double],
  8. symbol: BasicChartSymbolShape,
  9. legendStr: String)
  10. {
  11. self.xyzipped = Array(zip(x, y))
  12. self.symbol = symbol
  13. self.legendStr = legendStr
  14. }
  15. }
  16. struct ChartOfLines: View {
  17. let lines: [Line]
  18. init() {
  19. let line1 = Line(x: [0, 1, 2, 3, 4],
  20. y: [3, 5, 8, -1, 0],
  21. symbol: BasicChartSymbolShape.triangle,
  22. legendStr: "line1")
  23. let line2 = Line(x: [0, 1, 2, 3, 4],
  24. y: [5, 4, 3, 7, 1],
  25. symbol: BasicChartSymbolShape.plus,
  26. legendStr: "line2")
  27. lines = [line1, line2]
  28. }
  29. var body: some View {
  30. Chart(lines, id: \.id) { line in
  31. ForEach(line.xyzipped, id:\.0) { xy in
  32. LineMark(
  33. x: .value("X values", xy.0),
  34. y: .value("Y values", xy.1)
  35. )
  36. .lineStyle(StrokeStyle(lineWidth: 0.5))
  37. PointMark(
  38. x: .value("X values", xy.0),
  39. y: .value("Y values", xy.1)
  40. )
  41. }
  42. .symbol(line.symbol)
  43. .symbol(by: .value("", line.legendStr))
  44. .foregroundStyle(by: .value("", line.legendStr))
  45. }
  46. .frame(width: 500, height: 500)
  47. .padding()
  48. }
  49. }

Here is the produced chart:
图表符号和图例符号在SwiftUI中不同。

答案1

得分: 2

创建一个自定义的 chartLegend,如果你希望 ChartSymbolShape 出现在图例字符串的左侧:

测试示例,根据需要调整 UI:

  1. struct Line: Identifiable {
  2. let xyzipped: [(Double, Double)]
  3. let symbol: BasicChartSymbolShape
  4. let legendStr: String
  5. let lineColor: Color
  6. let id = UUID()
  7. init(x: [Double],
  8. y: [Double],
  9. symbol: BasicChartSymbolShape,
  10. legendStr: String,
  11. lineColor: Color)
  12. {
  13. self.xyzipped = Array(zip(x, y))
  14. self.symbol = symbol
  15. self.legendStr = legendStr
  16. self.lineColor = lineColor
  17. }
  18. }
  19. struct ContentView: View {
  20. let lines: [Line]
  21. init() {
  22. let line1 = Line(x: [0, 1, 2, 3, 4],
  23. y: [3, 5, 8, -1, 0],
  24. symbol: BasicChartSymbolShape.triangle,
  25. legendStr: "line1", lineColor: .green)
  26. let line2 = Line(x: [0, 1, 2, 3, 4],
  27. y: [5, 4, 3, 7, 1],
  28. symbol: BasicChartSymbolShape.plus,
  29. legendStr: "line2", lineColor: .blue)
  30. lines = [line1, line2]
  31. }
  32. var body: some View {
  33. Chart(lines, id: \.id) { line in
  34. ForEach(line.xyzipped, id:\.0) { xy in
  35. LineMark(
  36. x: .value("X values", xy.0),
  37. y: .value("Y values", xy.1)
  38. )
  39. .lineStyle(StrokeStyle(lineWidth: 0.5))
  40. .foregroundStyle(line.lineColor)
  41. PointMark(
  42. x: .value("X values", xy.0),
  43. y: .value("Y values", xy.1)
  44. )
  45. .foregroundStyle(line.lineColor)
  46. }
  47. .symbol(line.symbol)
  48. .foregroundStyle(by: .value("", line.legendStr))
  49. }
  50. .chartLegend(position: .bottomLeading, alignment: .bottomLeading) {
  51. HStack() {
  52. ForEach(lines) { xy in
  53. HStack {
  54. xy.symbol
  55. .frame(width: 10, height: 10)
  56. .foregroundColor(xy.lineColor)
  57. Text(xy.legendStr).foregroundColor(.black)
  58. }
  59. }
  60. }
  61. }
  62. .frame(width: 500, height: 500)
  63. .padding()
  64. }
  65. }
英文:

Create a custom chartLegend if you want the ChartSymbolShape to appear to the left of the legend string:

Example to test, adjust UI as necessary:

  1. struct Line: Identifiable {
  2. let xyzipped: [(Double, Double)]
  3. let symbol: BasicChartSymbolShape
  4. let legendStr: String
  5. let lineColor: Color
  6. let id = UUID()
  7. init(x: [Double],
  8. y: [Double],
  9. symbol: BasicChartSymbolShape,
  10. legendStr: String,
  11. lineColor: Color)
  12. {
  13. self.xyzipped = Array(zip(x, y))
  14. self.symbol = symbol
  15. self.legendStr = legendStr
  16. self.lineColor = lineColor
  17. }
  18. }
  19. struct ContentView: View {
  20. let lines: [Line]
  21. init() {
  22. let line1 = Line(x: [0, 1, 2, 3, 4],
  23. y: [3, 5, 8, -1, 0],
  24. symbol: BasicChartSymbolShape.triangle,
  25. legendStr: "line1", lineColor: .green)
  26. let line2 = Line(x: [0, 1, 2, 3, 4],
  27. y: [5, 4, 3, 7, 1],
  28. symbol: BasicChartSymbolShape.plus,
  29. legendStr: "line2", lineColor: .blue)
  30. lines = [line1, line2]
  31. }
  32. var body: some View {
  33. Chart(lines, id: \.id) { line in
  34. ForEach(line.xyzipped, id:\.0) { xy in
  35. LineMark(
  36. x: .value("X values", xy.0),
  37. y: .value("Y values", xy.1)
  38. )
  39. .lineStyle(StrokeStyle(lineWidth: 0.5))
  40. .foregroundStyle(line.lineColor)
  41. PointMark(
  42. x: .value("X values", xy.0),
  43. y: .value("Y values", xy.1)
  44. )
  45. .foregroundStyle(line.lineColor)
  46. }
  47. .symbol(line.symbol)
  48. .foregroundStyle(by: .value("", line.legendStr))
  49. }
  50. .chartLegend(position: .bottomLeading, alignment: .bottomLeading) {
  51. HStack() {
  52. ForEach(lines) { xy in
  53. HStack {
  54. xy.symbol
  55. .frame(width: 10, height: 10)
  56. .foregroundColor(xy.lineColor)
  57. Text(xy.legendStr).foregroundColor(.black)
  58. }
  59. }
  60. }
  61. }
  62. .frame(width: 500, height: 500)
  63. .padding()
  64. }
  65. }

huangapple
  • 本文由 发表于 2023年2月6日 07:48:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定