Highlights on multiple lines LineChart - swift

I am using danielgindi/Charts library.
I have a LineChart with two data sets, I want only one highlight line to highlight both data sets instead of individual highlights for each.
For example, as the picture shows, there are two highlight lines for each line(dataset), but I want only one highlight line can highlight both lines(datasets) and select values similar to this.
Here is the code I have for reference:
let set1 = LineChartDataSet(entries: set1Values, label: "first")
set1.highlightLineWidth = 1
set1.highlightEnabled = true
let set2 = LineChartDataSet(entries: set2Values, label: "second")
set2.highlightLineWidth = 1
set2.highlightEnabled = true
let data = LineChartData(dataSets: [set1, set2])
myChartView.data = data
Thank you for your help.

Why you persist in highlight a line?
I'm not sure but I think you should highlight your chart's container so that checking every line.

Related

How to edit NSAttributedstring background color size and position with Swift

in my app I have a feature where user can add background color to text
So, I added .backgroundColor attribute and it works but background position is different for different fonts. Is there a way to edit string background size and position or should I try to edit ttf fonts instead?
Thanks
Edit - Code:
I tried 2 methods
First one excluding spaces:
var startIndex = self.text.startIndex
while let r = self.text.range(of: "\\S+", options: .regularExpression, range: startIndex..<self.text.endIndex) {
customizedText.addAttribute(.backgroundColor, value: highlightColor, range: NSRange(r, in: self.text))
startIndex = r.upperBound
}
Result excluding spaces
Another one is simply add background color for every char:
customizedText.addAttribute(.backgroundColor, value: highlightColor, range: range)
Result: whole string
Ass you can see, it's doesn't look good with any alignment but Left.
We could probably live with this but problem is what it looks horribly wrong with some other fonts
Examples:
Gayatri Anton
Another example of what is now(red) and what I want(white):
Desired result
Any ideas?
Thanks

Formatting bar chart x-axis values, bars, and label

I have this bar chart, it has some formatting issues:
I'd like to address these:
there's strange numbers inside the bars. They could disappear, that would be quite happy.
there's also strange, but different numbers above the bars. These could be the actual number.
the chart label "recent Apps Data" could look more primary (bigger, bold-italic for example)
the dates in the x-axis are .. kinda close. Not sure how to handle that one.
here's how I'm drawing these:
func setBarChart (data: [(String,[Double])], label: String, xLabels: [String]) {
var entries: [BarChartDataEntry] = []
for (index, tuple) in data.enumerated() {
entries.append(BarChartDataEntry(x: Double(index), yValues: tuple.1))
}
let set = BarChartDataSet(values: entries, label: DataViewController.constants.labelDate)
set.setColor(UIColor.orange)
let data = BarChartData(dataSets: [set])
barChart.data = data
barChart.chartDescription?.text = label
barChart.xAxis.valueFormatter = IndexAxisValueFormatter(values: xLabels)
barChart.xAxis.granularity = 1
let legendEntry = LegendEntry()
legendEntry.formColor = UIColor.orange
legendEntry.label = "mean performance"
barChart.legend.setCustom(entries: [legendEntry]);
barChart.notifyDataSetChanged()
}
I do not know the answer to your first three questions, however, I had a similar problem to the one you describe in your fourth question. I discovered two options to overcome the problem of overlaying labels:
You could show labels' texts in two lines by adding \n in the middle of the string (for example, "2018\n07-23"). This would reduce the width of the label and thus they would not cover each other.
The second option could be to reduce the amount of bars shown in the chart. You could achieve this by using setVisibleXRangeMaximum (more on this here).
Good luck!

Remove Border and Margin around iOS Charts in Swift

I am using iOS Charts with Swift 3, and I can't figure out how to do a couple things:
I want to remove the margin around the chart. I know the chart goes edge-to-edge in my UI because if I change the chart's background color, it goes all the way to the edge. How do I remove the gap indicated by the red arrows below?
How do I remove the border around the whole graph (note the black arrow)? I already have totalsGraph.drawBordersEnabled = false and it doesn't work. Is there a different option for that?
Thank you!
It is minOffset.
/**
Sets the minimum offset (padding) around the chart, defaults to 10
*/
You can change it as this:
chartView.minOffset = 0
that line is actually axis line.
To hide the all the lines, you can use
totalsGraph.rightAxis.enabled = false
totalsGraph.legend.enabled = false
totalsGraph.leftAxis.enabled = false
totalsGraph.xAxis.labelPosition = .bottom
totalsGraph.xAxis.drawGridLinesEnabled = false
totalsGraph.xAxis.drawAxisLineEnabled = false
I am looking for solution to remove margins as well. I will update my answer when I find it.
Actually the way to do it is like this:
chartView.xAxis.enabled = false
chartView.leftAxis.enabled = false
chartView.rightAxis.enabled = false
chartView.drawBordersEnabled = false
chartView.minOffset = 0

IOS-Charts set maximum visible x axis values

I'm using ios-charts (https://github.com/danielgindi/Charts). I have a LineChartView with 12 values in the x axis.
This however is far too many to see at the same time, so I want to display only 5 and then let the user drag to the right to see the next.
I've tried this:
let chart = LineChartView()
chart.dragEnabled = true
chart.setVisibleXRangeMaximum(5)
let xAxis = chart.xAxis
xAxis.axisMinValue = 0
xAxis.axisMaxValue = 5.0
xAxis.setLabelsToSkip(0)
But still see all 11 values at the time. How can I only see 5?
I finally got it!
The correct answer is:
chart.setVisibleXRangeMaximum(5)
This however needs to be set after the data has been set in the chart (not in a configure before)
This did the trick for me
You should set the X axis's labelCount property of the chart view.
In objc,like this
_chartView.xAxis.labelCount = 5;
Swift
chartView.xAxis.labelCount = 5
Here is my finding!!
you don't need to really use label count
if you are using DefaultAxisValueFormatter, NEVER use this. a lot of errors pop ! just use no2.
chart.setVisibleXRangeMaximum(number) will do.
please put this after chart data setting here you can see detail
combinedChartView.data = combineData. //this need to come first
combinedChartView.setVisibleXRangeMaximum(2) //after data setting

how can a label display string in multiple lines

I have a label which displays a string triggered by a button and some int values entered in some textfields...
However, how can I make the label's string to be in multiple lines?
I can see a lot of people have asked this question, but for some reason, when I try to follow the answers it deletes the rest of my sentence after the use of "\n", as I have just tried earlier...
If the label is created in storyboard, go to your storyboard and select it, then in the attribute inspector there is a field 'Lines' with default value of 1. change that 0 and that should work.
Try using the following code
var label:UILabel = UILabel(frame: CGRectMake(10
,100, 300, 40));
label.textAlignment = NSTextAlignment.Center;
label.numberOfLines = 0;
label.font = UIFont.systemFontOfSize(16.0);
label.text = "First label\nsecond line";
Also make sure the height of label is enough to handle the string when presented in multiline else it won't show the next line in label.
To calculate height of label according to String this link can be helpful