iOS-charts slow down my app - ios

I'm using ios-chart to present a calendar I've built. I'm currently using LineChart to plot my data, and I plot 1 point for each day of the year in one chart. So I have 365 points plotted in one chart. And it takes like 1 second to draw it. This isn't a huge issue, except that I have my calendar as a TableViewCell, which will result in a very hacky scroll once the TableViewCell is scrolled outside the ContentView and then scrolled back again (so the cell gets redrawn). It feels weird that it takes so long to draw around 400 points, even on an iPhone 6. I might be doing something wrong here?
My setup code for the chart:
lineChart.descriptionText = ""
lineChart.drawGridBackgroundEnabled = false
lineChart.userInteractionEnabled = false
lineChart.xAxis.drawAxisLineEnabled = false
lineChart.xAxis.drawGridLinesEnabled = false
lineChart.xAxis.drawLabelsEnabled = false
lineChart.drawBordersEnabled = false
lineChart.leftAxis.enabled = false
lineChart.rightAxis.enabled = false
lineChart.legend.enabled = false
lineChart.contentMode = .ScaleAspectFill
var xVals = [String]()
var dataSet = LineChartDataSet(yVals: [ChartDataEntry]())
for (index, value) in enumerate(plotData){
dataSet.addEntry(ChartDataEntry(value: Float(value), xIndex: index))
xVals.append("\(index)")
}
dataSet.setColor(Colors.whiteColor())
dataSet.lineWidth = 1.0
dataSet.circleRadius = 0.0
dataSet.drawCirclesEnabled = false
dataSet.drawValuesEnabled = false
dataSet.drawFilledEnabled = true
dataSet.fillColor = Colors.whiteColor()
dataSet.fillAlpha = 0.1
dataSet.valueTextColor = Colors.whiteColor()
lineChart.data = LineChartData(xVals: xVals, dataSet: dataSet)
The code above is done each time a cell is created (or reused). Any ideas?

The issue was in data that was being setup each time I reused the cell. Data creation should obviously not be in a cell, but somewhere else. Should be solved once I move my data initiation somewhere else.

Related

How to fix iOS Charts Bar Chart X Axis labels Duplicating and bleeding

I am trying to display a bar chart using this library with months as the X-label. However, the x labels seem to be duplicating and "bleeding" to the next columns especially when I scroll. How do I fix this?
Code:
let xAxis = barChartView.xAxis
xAxis.drawGridLinesEnabled = false
xAxis.drawLabelsEnabled = true
xAxis.drawAxisLineEnabled = false
xAxis.labelPosition = .bottom
xAxis.enabled = true
xAxis.axisLineColor = white
xAxis.labelTextColor = white
xAxis.decimals = 0
xAxis.granularity = 1
xAxis.granularityEnabled = true
After grabbing data
// indexCounter = # of data points
xAxis.valueFormatter = IndexAxisValueFormatter(values: xAxisLabelValues)
xAxis.setLabelCount(Int(indexCounter), force: true)
notifyDataSetChanged()
No idea how it works.. but removing the setLabelCount fixed the problem for me...

iOS Charts empty center space

When displaying a line chart I'm trying to move the data points from the edges and into the center but cannot seem to stop them from hugging the edges of the graph.
//Set data
let dataSet = retrieveScoreDataForGraphing()
dataSet.label = "Scores"
uiView.data = LineChartData(dataSet: dataSet)
uiView.rightAxis.enabled = false
uiView.setScaleEnabled(true)
uiView.doubleTapToZoomEnabled = false
uiView.pinchZoomEnabled = false
uiView.xAxis.labelCount = dataSet.count
uiView.xAxis.forceLabelsEnabled = true
uiView.xAxis.granularityEnabled = true
uiView.xAxis.granularity = 1
//Format chart
uiView.xAxis.valueFormatter = CustomChartFormatter()
uiView.xAxis.labelPosition = .bottom
uiView.xAxis.labelTextColor = accentColor
uiView.xAxis.labelFont = UIFont.systemFont(ofSize: 16)
formatDataSet(dataSet: dataSet)
formatLeftAxis(leftAxis: uiView.leftAxis)
formatLegend(legend: uiView.legend)
uiView.notifyDataSetChanged()

Remove values above data line iOS Charts

I have built a small application to measure Heart Rate (HR) and currently trying to implement a chart using iOS Charts. It is working as intended (getting HR from watch and displaying it), but I have a small problem with the design of the chart.
On the image below you can see that I have overlapping numbers above the data line (70, and the later 90). I do not know how to remove them.
Chart Image
Here is my setup for the chart and its' update counter function:
//chart set up
self.chtChart.delegate = self as? ChartViewDelegate
let set_a: LineChartDataSet = LineChartDataSet(entries:[ChartDataEntry(x: Double(0), y: self.valueHR)], label: "HR")
set_a.drawCirclesEnabled = false
set_a.setColor(UIColor.systemPink)
self.chtChart.xAxis.drawGridLinesEnabled = false
self.chtChart.rightAxis.drawLabelsEnabled = false
self.chtChart.xAxis.drawLabelsEnabled = false
self.chtChart.data = LineChartData(dataSets: [set_a])
// update counter
var i = 1
#objc func updateCounter() {
self.chtChart.data?.addEntry(ChartDataEntry(x: Double(i), y: valueHR), dataSetIndex: 0)
self.chtChart.setVisibleXRange(minXRange: Double(0), maxXRange: Double(1000))
self.chtChart.notifyDataSetChanged()
self.chtChart.moveViewToX(Double(i))
i = i + 1
}
I know that there is a duplicate question, but the solution with the formatter did not help me: nothing was changed after the implementation of the solution.
formatter solution

How to show the Y grid lines without the Y-Axis in iOS-charts

I am working on a chart, with the "danielgindi/Charts" ( iOS-charts ), I want to be able to show the grid lines for the Y axis, but without the base.
This should be the result:
But I am stuck and can't get the lines without having to enable the left axis.
This is what I am getting, I want to get rid of the line to the left. ( The one the red arrow is pointing to )
Here is some of my code:
func setupChart(data: LineChartData ) {
chartView.delegate = self
chartView.backgroundColor = .white
chartView.chartDescription?.enabled = false
chartView.dragEnabled = false
chartView.setScaleEnabled(false)
chartView.pinchZoomEnabled = false
chartView.setViewPortOffsets(left: 10, top: 0, right: 10, bottom: 0)
chartView.legend.enabled = false
chartView.leftAxis.enabled = true
chartView.leftAxis.labelCount = 3
chartView.leftAxis.spaceTop = 0.70
chartView.leftAxis.spaceBottom = 0.20
chartView.rightAxis.enabled = false
chartView.xAxis.enabled = false
chartView.data = data
}
Any help is appreciated !!
I was able to find the answer myself, and I decided not to delete this question so that if anyone runs into the same issue they can find the answer here.
When setting your left axis chart, and you do not want the first line to show, set this property to "false".
chartView.leftAxis.drawAxisLineEnabled = false
This will give you this result.

Hide bottom x axis in horizontal bar chart?

I want to render a (stacked) horizontal bar chart using ios-charts that does not render any x axis or grid lines. I've disabled every setting I can find, but the bottom x axis still renders.
func setUpBarChart() {
// General bar chart settings
barChart.pinchZoomEnabled = false
barChart.drawGridBackgroundEnabled = false
barChart.drawBarShadowEnabled = false
barChart.drawValueAboveBarEnabled = false
barChart.drawBordersEnabled = false
barChart.drawMarkers = false
barChart.legend.enabled = false
barChart.descriptionText = ""
barChart.drawBordersEnabled = false
// Left-axis settings
barChart.leftAxis.drawLabelsEnabled = false
barChart.leftAxis.drawTopYLabelEntryEnabled = false
barChart.leftAxis.drawAxisLineEnabled = false
// x-axis settings
barChart.xAxis.drawAxisLineEnabled = false
barChart.xAxis.drawGridLinesEnabled = false
barChart.xAxis.drawLabelsEnabled = false
barChart.xAxis.enabled = false
// add some dummy data
let entry = BarChartDataEntry(values: [10,2,5], xIndex: 0)
let set = BarChartDataSet(yVals: [entry], label: nil)
set.colors = [UIColor.greenColor(), UIColor.yellowColor(), UIColor.redColor()]
set.drawValuesEnabled = false
let data = BarChartData(xVals: [""], dataSet: set)
barChart.data = data
}
this code results in the following rendering:
How do I remove the bottom axis rendering and vertical value lines?
Finally figured it out. Since this is a HorizontalBarChart the names of the axis are a little off. The axes that renders on the bottom of this graph is actually the rightAxis. Therefore this code does the trick:
barChart.rightAxis.enabled = false
enabling rightAxis will lead to space calcuation. Disable if you don't want it. However, if you do want it take some space, but just not draw values, axis lines, grid lines, checkout drawLabelsEnabled, drawGridLineEnabled, drawAxisLineEnabled

Resources