多折线图,不想绘制 0 值。 Swift 2
Multiple Line Chart, don't want to plot 0 values. Swift 2
我正在使用 ios-charts 库生成多折线图。 (danielgindi/Charts)。我正在为从核心数据中检索到的 x 轴创建一个数组,如果 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.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循环类似
唯一可以做到这一点的方法是覆盖默认行为。
其实很简单,创建一个LineChartRenderer
的子类并覆盖drawLinear
(线),drawValues
(值渲染)和drawExtras
(圆圈)。
只需将超类方法 (LineChartRenderer) 的实现复制到那里并添加跳过零值所需的条件。
然后将自定义渲染器分配给您的图表视图。
另一个可能在不覆盖渲染器的情况下工作的选项是为每个值提供颜色。然后你可以为零值设置清晰的颜色。
我正在使用 ios-charts 库生成多折线图。 (danielgindi/Charts)。我正在为从核心数据中检索到的 x 轴创建一个数组,如果 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.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循环类似
唯一可以做到这一点的方法是覆盖默认行为。
其实很简单,创建一个LineChartRenderer
的子类并覆盖drawLinear
(线),drawValues
(值渲染)和drawExtras
(圆圈)。
只需将超类方法 (LineChartRenderer) 的实现复制到那里并添加跳过零值所需的条件。
然后将自定义渲染器分配给您的图表视图。
另一个可能在不覆盖渲染器的情况下工作的选项是为每个值提供颜色。然后你可以为零值设置清晰的颜色。