iOS Charts empty center space - ios

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()

Related

Line chart remove extra line in Y-axis vertical lines

I am using this library for chat Chart
There is one extra line above the final y-axis value is appearing. Same for all vertical line is exceeding for all x-axis value.
Here is the image :
Here after y-axis 30 value, extra line are going up. Same for all x-axis values as well. Any way to disable the exceeding vertical lines ?
Some code snippet :
lineChart.xAxis.labelPosition = .bottom
lineChart.drawGridBackgroundEnabled = false
lineChart.leftAxis.drawGridLinesEnabled = true
lineChart.rightAxis.drawGridLinesEnabled = true
lineChart.drawGridBackgroundEnabled = false
lineChart.setScaleEnabled(true)
lineChart.rightAxis.enabled = false
lineChart.leftAxis.axisMinimum = 0.0
lineChart.xAxis.axisMinimum = 0.0
lineChart.scaleYEnabled = false
lineChart.scaleXEnabled = true
lineChart.leftAxis.enabled = true
lineChartrightAxis.drawZeroLineEnabled = true
lineChart.leftAxis.drawZeroLineEnabled = true
lineChart.moveViewToX(Double(pushedCount.count))
lineChart.xAxis.granularityEnabled = true
lineChartxAxis.axisMaximum = Double(pushedCount.count)

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...

How to give dot in X axis and Y axis in line chart?

I am implementing Charts in my project to show line chart. I want to add dot in x axis and y axis as shown in the image below. Here is the image :
Here is my code so far:
let yAxis = lineChart.leftAxis
yAxis.labelFont = .boldSystemFont(ofSize: 12)
yAxis.setLabelCount(5, force: false)
yAxis.labelTextColor = .darkGray
yAxis.axisLineColor = .darkGray
yAxis.gridLineWidth.round()
yAxis.drawGridLinesEnabled = false
let xAxis = lineChart.xAxis
xAxis.labelPosition = .bottom
xAxis.labelFont = .boldSystemFont(ofSize: 12)
xAxis.setLabelCount(5, force: false)
xAxis.labelTextColor = .darkGray
xAxis.axisLineColor = .darkGray
lineChart.leftAxis.axisLineDashPhase = .nan
I think that this feature is not supported yet, so I would suggest to:
either enhance that library or do it by yourself:
Subclass XAxisRenderer and YAxisRenderer
In that subclasses, overwrite drawLabels, call the base function, and add the dots graphic (check the original drawLabels implementation).
Create the subclass renderer instances and set it as leftYAxisRenderer / rightYAxisRenderer / xAxisRenderer in your chart.

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

iOS-charts slow down my app

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.

Resources