更改 iOS 个图表中组合图表的绘制顺序

Change Drawing Order for Combined Charts in iOS Charts

我正在使用 Danielgindi 中的图表框架向用户展示数据。我想要做的是使用 CombinedChartView 在彼此之上显示两个图表。这两个图表是 BarChartDataSetLineChartDataSet。我可以成功显示两组数据,但我似乎无法控制绘图顺序。柱线 BarChartDataSet 总是绘制在 LineChartDataSet 后面。我希望将此订单反转并在后面添加 LineChartDataSet(包括我对其应用的任何填充)。

我的问题

如何更改 CombinedChartView 的绘制顺序以使折线图在后面而不是前面?

这是我的代码:

// barDataEntries and lineDataEntries are generated in a different section and are not relevant to this question. Any data will do.
// barDataEntries is of type [BarChartDataEntry]()
// lineDataEntries is of type [ChartDataEntry]()
let barChartSet = BarChartDataSet(yVals: barDataEntries, label: "The Bars")
let lineChartSet = LineChartDataSet(yVals: lineDataEntries, label: "The Line")
barChartSet.barShadowColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0)  // If alpha is > 0, then grey bars will appear.

// Cosmetic changes for the line chart
lineChartSet.mode = .CubicBezier
lineChartSet.lineWidth = 5.0
lineChartSet.drawCirclesEnabled = false
lineChartSet.drawFilledEnabled = true
lineChartSet.colors = [UIColor(red: 0, green: 0, blue: 1, alpha: 0.0)]
lineChartSet.fillColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1.0)
lineChartSet.fillAlpha = 0.8

// Organizing data for statsChartViewController
let data = CombinedChartData(xVals: xAxis)
data.lineData = LineChartData(xVals: xAxis, dataSets: [lineChartSet])
data.barData = BarChartData(xVals: xAxis, dataSets: [barChartSet])

// statsChartViewController is of type CombinedChartView
statsChartViewController.data = data
statsChartViewController.leftAxis.axisMinValue = 0

这是我得到的:

在查看 source code 之后,我找到了一个允许您更改绘制顺序的解决方案。 class CombinedChartView 有一个 public 枚举负责绘制顺序:

public enum DrawOrder: Int
{
    case Bar       // 0
    case Bubble    // 1
    case Line      // 2
    case Candle    // 3
    case Scatter   // 4
}

您可以按照您希望绘制对象的顺序为 drawOrder 设置 Int 数组来更改绘制顺序。在下面的示例中(取自上面的代码),条形图将位于行的前面:

// 2 is the line and 0 is the bar. 2 is drawn first, then 0 follows.
statsChartViewController.drawOrder = [2, 0]
statsChartViewController.data = data

这是唯一需要进行的修改。需要记住的重要一件事是,在设置数据之前 设置绘制顺序 。如果您不这样做,抽奖顺序将与以前相同。以下是新结果: