问题
I am generating a multiple line chart using ios-charts library. (danielgindi/Charts). I am creating an array to for the x-axsis which is retrieved from Core Data and if there is no value for x week, i appended a 0.
I have an example array of (for the x values):
[[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]]
and for the y:
[Jan,Feb,March,April,May,June,July,August,Sept,Oct,Nov,Dec]
I have the following code to display these 3 lines within a line chart:
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
}
How can I avoid plotting the 0 values?
Thanks for any help
回答1:
Are you looking to skip the 0.0 values entirely? You can check before appending them to the array.
Perhaps you can do:
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))
}
}
And similarly with the other for loops
回答2:
The only way you can do that is overriding the default behavior.
It's rather simple actually, create a subclass of LineChartRenderer
and override drawLinear
(lines), drawValues
(value rendering) and drawExtras
(circles).
Just copy the implementation of the superclass methods (LineChartRenderer) there and add the required condition to skip zero values.
Then assign the custom renderer to your chart view.
Another option that could probably work without overriding the renderer is to provide a color for every value. Then you can just set clear color for zero values.
来源:https://stackoverflow.com/questions/36445428/multiple-line-chart-dont-want-to-plot-0-values-swift-2