我正在使用ios-charts库生成多线图。 (danielgindi /图表)。我正在为从Core Data检索的x-axsis创建一个数组,如果没有x周的值,我附加了一个0。
我有一个示例数组(对于x值):
[[0.0,0.0,5.6,3.0,6.0,5.4,2.1,0.0,0.0,1.3,4.1,0.0],[5.2,0.0,3.2,7.0,8.9,0.0,5.0,0.0,4.6,0.0 ,0.0,0.0],[3.6,1.5,3.6,0.0,0.0,3.5,0.0,4.6,5.6,2.8,8.4,0.0]]
和y:
[一月,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月]
我有以下代码在折线图中显示这3行:
func setChart(xValues: [NSDate], valuesLineChart: [[Double]]) {
chartView.descriptionText = ""
chartView.noDataText = "You need to provide data for the chart."
var dataSets : [LineChartDataSet] = [LineChartDataSet]()
var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
for i in 0..<xValues.count {
yVals1.append(ChartDataEntry(value: valuesLineChart[0][i], xIndex: i))
}
var yVals2 : [ChartDataEntry] = [ChartDataEntry]()
for i in 0..<xValues.count {
yVals2.append(ChartDataEntry(value: valuesLineChart[1][i], xIndex: i))
}
var yVals3 : [ChartDataEntry] = [ChartDataEntry]()
for i in 0..<xValues.count {
yVals3.append(ChartDataEntry(value: valuesLineChart[2][i], xIndex: i))
}
let set1 = LineChartDataSet(yVals: yVals1, label: nil)
let set2 = LineChartDataSet(yVals: yVals2, label: nil)
let set3 = LineChartDataSet(yVals: yVals3, label: nil)
set1.fillColor = UniversalStatic.data.colourArrayForGraph[0]
set2.fillColor = UniversalStatic.data.colourArrayForGraph[1]
set3.fillColor = UniversalStatic.data.colourArrayForGraph[2]
dataSets.append(set1)
dataSets.append(set2)
dataSets.append(set3)
set1.setColor(UniversalStatic.data.colourArrayForGraph[0])
set2.setColor(UniversalStatic.data.colourArrayForGraph[1])
set3.setColor(UniversalStatic.data.colourArrayForGraph[2])
set1.lineWidth = 2
set2.lineWidth = 2
set3.lineWidth = 2
set1.setCircleColor(UniversalStatic.data.colourArrayForGraph[0])
set2.setCircleColor(UniversalStatic.data.colourArrayForGraph[1])
set3.setCircleColor(UniversalStatic.data.colourArrayForGraph[2])
set1.drawCircleHoleEnabled = false
set2.drawCircleHoleEnabled = false
set3.drawCircleHoleEnabled = false
set1.circleRadius = 5.0
set2.circleRadius = 5.0
set3.circleRadius = 5.0
let data: CombinedChartData = CombinedChartData(xVals: xValues)
data.lineData = LineChartData(xVals: xValues, dataSets: dataSets)
chartView.data = data
}
如何避免绘制0值?
感谢您的帮助
答案 0 :(得分:1)
您是否希望完全跳过0.0值?您可以在将它们附加到阵列之前进行检查。
也许你可以这样做:
var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
for i in 0..<xValues.count {
if valuesLineChart[0][i] != 0.0 {
yVals1.append(ChartDataEntry(value: valuesLineChart[0][i], xIndex: i))
}
}
与其他for循环类似
答案 1 :(得分:1)
您可以做到的唯一方法是覆盖默认行为。
实际上相当简单,创建LineChartRenderer
的子类并覆盖drawLinear
(行),drawValues
(值呈现)和drawExtras
(圆圈)。
只需复制超类方法(LineChartRenderer)的实现,并添加所需条件以跳过零值。
然后将自定义渲染器指定给图表视图。
另一个可能无法覆盖渲染器的选项是为每个值提供颜色。然后你可以为零值设置清晰的颜色。