iOS Charts library and LineChart xAxis - ios

I'm trying to create a LineChartView with values representing expenses for the current quarter. I need to print months and days with a custom number of labels for the xAxis. Unfortunately the LineChart is generating a set of entries that doesn't suit my needs and I cannot change them. I need the grid lines at my custom positions that are the first day of the month and a set of days for the month and I want them at specific positions not where the LineChart wants. I've checked the code and seen that the entry values for the xAxis are calculate in XAxisRenderer.computeAxisValues and it looks like I have no way to specify their number and positions. The number of labels that I set it is not taken into account ... and I do not have time to customise it.

There are 2 ways might achieve what you needed:
Set the number of labels for x-axis, for example,
let xAxis = chartView.xAxis
xAxis.labelCount = 7
//xAxis.drawLabelsEnabled = true
Through this, exactly 7 grid lines as will be drawn, if you set
drawLabelsEnabled as false, the min and max lines will be added in
additional to those 7
Use limit lines
You can disable the drawing of grid lines, and use limit lines
instead. In this way, you can draw line for whatever values you
need, for example,
let limitLine = ChartLimitLine(limit: xValue, label:
labelForThisLine)
limitLine.lineWidth = 0.5
limitLine.lineColor = .black
limitLine.valueTextColor = .black
chartView.xAxis.addLimitLine(limitLine)

Related

X-axis action strange - Charts iOS

I'm using the Charts library on iOS, but I'm having troubles with one of my charts in my TableView. The X-axis looks strange, and I loose resolution in my data. Here is how it currently looks:
My data is typically between 900 and 1100.
I configured the chart like this:
let dataSet = LineChartDataSet(values: entries, label: "")
dataSet.drawCirclesEnabled = false
dataSet.lineWidth = 2
dataSet.mode = .horizontalBezier
dataSet.colors = UIColor.gray
dataSet.drawValuesEnabled = false
let lineChartData = LineChartData(dataSets: [dataSet])
self.chartEntries.append(lineChartData)
Any ideas on why my X-axis looks like this? My others charts look fine and dandy.
The chart looks fine, since it's trying to squeeze your data that's around value 1000 into a very small space. If you have a look at the MPAndroidChart lib wiki, you can see that you can adjust the min and max range per axis, so you can effectively zoom in closer to your values to better see the distribution:
setAxisMaximum(float max): Set a custom maximum value for this axis.
If set, this value will not be calculated automatically depending on
the provided data.
setAxisMinimum(float min): Set a custom minimum value for this axis.
If set, this value will not be calculated automatically depending on
the provided data.
It would look something like:
graph.yAxis.axisMinimum = 900
graph.yAxis.axisMaximum = 1100

How to set the label count for X axis in LineChartView?

In my data, I have hundreds of values for the x-axis, but I want to show only 6 or 7 of them at equal intervals so that my whole range of x-axis values are covered, but I am facing an issue as I am not able to show a limited number of values on the x-axis . I tried these
lineChartView.xAxis.spaceMin = 4
lineChartView.xAxis.spaceMax = 7
lineChartView.xAxis.xOffset = 6
lineChartView.xAxis.labelCount = 6
, but it is not working .Here is my screenshot for the display
The axis property granularity is used to specify the minimum interval between axis values. The default value is 1. Currently in my app i have it set to 0.5 to force all values to be displayed, but I only have 10 or so on the chart at once.
chartView.xAxis.granularity = 0.5
Perhaps setting this to a higher value will help you.

iOS-charts radar chart removing labels and filling the web with color

Probably a very basic question.
I am using iOS-charts and am working with a radar chart.
I am struggling to figure out how to remove the labels from the graph and I am really struggling to FILL the web. I can change the outer lines but I want to fill the web/data in a specific colour.
any help would be much appreciated.
Kind regards
Wayne
So, there are two types of labels on a radar chart, the axis label that shows you the list of possible values on the chart from min to max and the labels that correspond to each point on your graph.
REMOVING AXIS LABELS
Grab either the x or y axis on your radar chart view and set drawLabelsEnabled to false depending on the axis of the labels that you wish to hide
let yAxis = radarChartView.yAxis
let xAxis = radarChartView.xAxis
yAxis.drawLabelsEnabled = false
xAxis.drawLabelsEnabled = false
REMOVING LABELS FOR VALUE OF EACH POINT
Go to the location that you create your RadarChartDataSet
Before using it to create your RadarChartData, set the drawValuesEnabled property to false
let radarChartDataSet = RadarChartDataSet(yVals: dataEntries, label: "label")
radarChartDataSet.drawValuesEnabled = false
Also, here's a link to the Charts documentation page which can also be helpful (although it is not fully filled-in): http://cocoadocs.org/docsets/Charts/2.0.9/index.html

setting yAxis in iOS-charts swift iOS 8

I'm learning ios-charts and I was wondering how to manipulare the yAxis since there is no method related to that. On the contrary xAxis can be customized.
A small example, by default I have two set of labels for the yAxis as the figure shows.
I managed to move the xAxis labels using
lineChartView.xAxis.labelPosition = .Bottom
but there is no such thing for the yAxis.
Is it possible to leave just the labels on the left clearing the labels on the right?
use rightAxis.drawLabelsEnabled = false or even rightAxis.enabled = false.
The xAxis is rendered by xIndex, which means the distance between each data point on xAxis is equal. But yAxis is rendered by the data value and the yaxis range.
So be careful, when you are setting the dataSet, you got a chance to set the axis dependency, like dataSet.axisDependency = axisDependencyRight. Then it will use rightAxis to render your line chart, not left axis. By default, it is axisDependencyLeft
You can choose which yAxis you would like to have, and just disable the other one. rightAxis.drawLabelsEnabled = false just don't render the label, but still calculated and rendererd. Watch out for this.
I think that the only way to do it is the following
let yaxis = lineChartView.getAxis(ChartYAxis.AxisDependency.Right)
yaxis.drawLabelsEnabled = false

More efficient way to plot x-axis points?

I have been working on a project that requires a bar graph to be populated with price results. The chart displays the number of items within a given price range. For instance, if on amazon there are 9 items within the price range of $0-$10 the x-axis would display $0-$10 and the y-axis would be populated with a value of 9.
My bar graph has 8 bars, all with similar price ranges. $0-$10, $10-$20, $20-$30... etc.
My question is this: What is the best way to define those individual points? There is no common price range between these items, so the x-axis cannot be static numbers. They must be dynamically calculated within the range of results.
As such, currently I am creating the x-axis points as follows:
I take the lowest result:
#numbers[0] = results[0];
And I take the highest result:
#numbers[8] = results[-1];
Then I find the median of the two:
#numbers[4] = (#numbers[0]+#numbers[8])/2;
I then repeat the process 6 more times
#numbers[2] = (#numbers[0]+#numbers[4])/2; #numbers[6] = (#numbers[4]+#numbers[8])/2; #numbers[1] = (#numbers[0]+#numbers[2])/2; #numbers[3] = (#numbers[2]+#numbers[4])/2; #numbers[5] = (#numbers[4]+#numbers[6])/2; #numbers[7] = (#numbers[6]+#numbers[8])/2;
This gives me the results I need, but it seems awfully repetitive and I would imagine there is a better way.
I tried creating a loop, but I could not write it in a less verbose manner.
Is there a quicker way to do this, or perhaps something more along the lines of DRY?
Are your bins always of equal size? In your example, all share range=10. If so, then you could do:
binspacing = overall range / (numberofbins-1);
and the position of bin n would be the x-axis position of numbers[0] plus n times the binspacing.

Resources