Today i finally got my autolayout constrainst animation to work, and after that i noticed, that the Horizontal bar chart isnt behaving like it used to be.
I have a Horizontal Bar chart populated with data in Descending order (the most should be on top) but the problem is, whenever i try to move the view back to the first entry (the biggest value) using moveViewToX the chart just doesn't do anything it always shows the last added entry, so its scrolled to the end.
I'd like to have it start from the top, i mean from the first entry.
I have tried the moveViewToX function, but it still has no effect.
Here's the code for setting the data:
let sortedData = chartData.sorted(by: { $0.money < $1.money })
for i in 0..<sortedData.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: Double(sortedData[i].money))
dataEntries.append(dataEntry)
labels2.append(sortedData[i].Name)
}
Here is the setup for the chart:
let chartDataSet = BarChartDataSet(entries: dataEntries, label: "kategoriak")
chartDataSet.colors = [UIColor.red]
let kerekitöFormatter = KerekitöNumberFormatter(showZeros: false, showMoney: true)
chartDataSet.valueFormatter = kerekitöFormatter
chartDataSet.valueFont = chartDataSet.valueFont.withSize(10)
chartDataSet.valueTextColor = .white
let horChartData = BarChartData(dataSet: chartDataSet)
horizontalChartView.xAxis.valueFormatter = BarChartXaxisFormatter(labels: labels2, truncMode: true)
horizontalChartView.data = horChartData
horizontalChartView.leftAxis.axisMinimum = 0
horizontalChartView.leftAxis.valueFormatter = YAxisValueFormatter()
horizontalChartView.legend.enabled = false
horizontalChartView.xAxis.labelPosition = .bottom
horizontalChartView.setVisibleXRangeMaximum(5)
horizontalChartView.moveViewToX(Double(sortedData.count))
horizontalChartView.drawValueAboveBarEnabled = false
horizontalChartView.xAxis.granularityEnabled = true
//horizontalChartView.setExtraOffsets (left: 0.0, top: 0.0, right:0.0, bottom: 0.0)
horizontalChartView.xAxis.granularity = 1
horizontalChartView.doubleTapToZoomEnabled = false
if #available(iOS 13.0, *) {
horizontalChartView.backgroundColor = .systemBackground
horizontalChartView.xAxis.labelTextColor = .label
horizontalChartView.leftAxis.labelTextColor = .label
horizontalChartView.gridBackgroundColor = .label
}
let rightAxis = horizontalChartView.rightAxis
rightAxis.drawGridLinesEnabled = false
rightAxis.enabled = false
I hope someone can help.
Looking forward to your answers.
After hours of research and trying i figured it out. For some strange reason if you use moveViewToX it does not respond, but when you use moveViewTo(float xValue, float yValue, AxisDependency axis) it works.
So, to be short, i added this line of code at the end of setting up the chart.
horizontalChartView.moveViewTo(xValue: Double(sortedData.count), yValue: sortedData.first!.money, axis: .left)
Hope this helps you in the future!
Related
I'm using charts 3.2.2 iOS version. I a trying to plot a line chart. In the line chart whenever user taps on the value points a highlight line would show up. Is there any way I can hide or change this highlight line color. Another issue I'm facing is the bubble won't show up for the first entry. I found a lot of solutions online but nothing works for me. Any help will be appreciated.
scoreLineChart.legend.form = .none
scoreLineChart.rightAxis.enabled = false
scoreLineChart.xAxis.labelPosition = .bottom
scoreLineChart.xAxis.drawGridLinesEnabled = false
scoreLineChart.dragEnabled = false
scoreLineChart.doubleTapToZoomEnabled = false
scoreLineChart.xAxis.granularityEnabled = true
scoreLineChart.xAxis.granularity = 1.0
scoreLineChart.leftAxis.axisMinimum = 0
scoreLineChart.leftAxis.axisMaximum = 100.0
scoreLineChart.leftAxis.setLabelCount(5, force: true)
scoreLineChart.xAxis.valueFormatter = valueFormatter
scoreLineChart.xAxis.avoidFirstLastClippingEnabled = true
scoreLineChart.pinchZoomEnabled = false
let marker = BalloonMarker(color: UIColor(hex: "#58595b"),
font: UIFont(name: "Avenir-Heavy", size: 14)!,
textColor: .white,
insets: UIEdgeInsets(top: 8, left: 8, bottom: 20, right: 8))
marker.chartView = scoreLineChart
marker.minimumSize = CGSize(width: 80, height: 40)
scoreLineChart.marker = marker
After adding data entries
let setOne = LineChartDataSet(values: lineChartEntry, label: "") //Here we convert lineChartEntry to a LineChartDataSet
setOne.mode = .cubicBezier
setOne.drawValuesEnabled = true
setOne.lineWidth = 1
setOne.circleRadius = 3
setOne.drawCircleHoleEnabled = false
setOne.valueFont = .systemFont(ofSize: 9)
setOne.formLineWidth = 1
setOne.formSize = 15
setOne.setCircleColor(ChartColorTemplates.colorFromString("#ffcc1a29"))
if !isSingelValue {
setOne.setColor(ChartColorTemplates.colorFromString("#ffcc1a29"))
}else {
setOne.setColor(UIColor.clear)
}
let data = LineChartData(dataSet: setOne)
data.addDataSet(setOne) //Adds the line to the dataSet
scoreLineChart.xAxis.axisMinimum = 0
scoreLineChart.data = data //finally - it adds the chart data to the chart and causes an update
// scoreLineChart.data?.setValueFormatter(valueFormatter)
scoreLineChart.chartDescription?.text = "" // Here we set the description for the graph
scoreLineChart.notifyDataSetChanged()
You just need to set drawHorizontalHighlightIndicatorEnabled drawVerticalHighlightIndicatorEnabled to false to hide grid lines.
let set1 = ScatterChartDataSet(entries: values1, label: "DS 1")
set1.drawHorizontalHighlightIndicatorEnabled = false
set1.drawVerticalHighlightIndicatorEnabled = false
Is there any way I can hide or change this highlight line color.
To show/hide or change the highlight line color, edit the properties of your ChartDateSet
let setOne = LineChartDataSet(values: lineChartEntry, label: "")
setOne.highlightColor = .red // color of the line
setOne.highlightWidht = 2.0 // width of the line
setOne.drawHorizontalHighlightIndicatorEnabled = false // hide horizontal line
setOne.drawVerticalHighlightIndicatorEnabled = false // hide vertical line
Another issue I'm facing is the bubble won't show up for the first entry.
And sadly I cannot reproduce this issue with the BallonMarker.swift from Charts github repository. And It is not clear which one is the first entry of your data from the image you provided.
I tried add a "first entry" to my chart but the BallonMarker works prefectly.
I am having very hard time while implementing line chart. I am using latest Charts pod. When I zoom chart its not work properly. X-axis missing its coordinate.
I have attached both images.
// Make sure that only 1 x-label per index is shown
updatedCampaignEndDateArray = ["", "20-11", "28-11", "03-12", "04-12", "31-12"]
self.chartView.xAxis.granularityEnabled = true
self.chartView.xAxis.granularity = 1.0
self.chartView.xAxis.labelPosition = .top
self.chartView.xAxis.labelCount = self.updatedCampaignEndDateArray.count
self.chartView.xAxis.drawLabelsEnabled = true
self.chartView.xAxis.drawLimitLinesBehindDataEnabled = true
self.chartView.xAxis.avoidFirstLastClippingEnabled = true
self.chartView.xAxis.labelFont = UIFont.systemFont(ofSize: 8.0)
self.chartView.xAxis.setLabelCount(self.updatedCampaignEndDateArray.count, force: true)
self.chartView.xAxis.valueFormatter = DefaultAxisValueFormatter(block: {(index, _) in
return self.updatedCampaignEndDateArray[Int(index)]
})
self.chartView.xAxis.wordWrapEnabled = false
self.chartView.legend.form = .line
self.chartView.animate(xAxisDuration: 0.5)
acceptedArrayWithAddition = [0, 8, 0, 503, 551]
let values = (0..<self.pointCount + 1).map { (i) -> ChartDataEntry in
return ChartDataEntry(x: Double(i), y: Double(acceptedArrayWithAddition[Int(i)]))
let set = LineChartDataSet(values: values, label: "Leads")
I have created piechart with the help of following library:
https://github.com/danielgindi/Charts
and currently using pod 'Charts', '~> 3.0.4'.
I have created half pie chart and also setup label on it but it keeps on rotating and sometimes it's going outside the slice or overlap on another label. Please use following images for reference.
When we setup label at the first time
When we rotate the screen then it gets overlapped
Also, code for implementation:
pieChartLV1 = PieChartView();
pieChartLV1.chartDescription?.text = ""
pieChartLV1.legend.enabled = false
pieChartLV1.holeRadiusPercent = 0.3
pieChartLV1.translatesAutoresizingMaskIntoConstraints = false
pieChartLV1.transparentCircleColor = UIColor.clear
pieChartLV1.usePercentValuesEnabled = true
pieChartLV1.entryLabelColor = UIColor.black
//adding data
let Level1Data = ["Eligibilty": 10, "Enrollment": 10, "Benefit Design": 10, "Biology": 10, "Behaviour": 10,"Environment": 10, "Outcomes": 10, "workflow": 10, "Errors": 10, "Billings & \nPayments": 10, "Fraud & \nAbuse": 10, "Medical \nPolicy": 10]
var dataEntries = [PieChartDataEntry]()
for (key, val) in Level1Data {
let entry = PieChartDataEntry(value: Double(val), label: key)
entry.label = key
dataEntries.append(entry)
}
let chartDataSet = PieChartDataSet(values: dataEntries, label: "")
chartDataSet.colors = ChartColorTemplates.material()
chartDataSet.sliceSpace = 2
chartDataSet.selectionShift = 0
chartDataSet.valueTextColor = UIColor.black
let chartData = PieChartData(dataSet: chartDataSet)
chartData.setDrawValues(false)
pieChartLV3.data = chartData2
I want a fixed label on pie chart with some angle Or need to remove rotational animation of a label.
Thanks in advance.
chartDataSet.rotationEnabled = false
I'm currently using the iOS Charts library, and I'm trying to scale the width of the bar when using a BarChartView, but I'm having a difficult ensuring the width remains almost the same regardless of how many entries there are in the chart.
According to an answer provided here: Changing width of the bar in bar chart
one of the developers stated that the bar's width will shrink with an increasing number of entries.
I have calculated a zoom level factor that I am satisfied with based on the number of entries using a zoomMultiplier like so:
func createBarChart(dataPoints: [Date], values: [Double])
{
var dataEntries: [BarChartDataEntry] = []
for index in 0..<dataPoints.count
{
let dataEntry = BarChartDataEntry(x: Double(index),
y: values[index])
dataEntries.append(dataEntry)
}
barChartView.xAxis.valueFormatter = self
barChartView.xAxis.granularity = 1
barChartView.xAxis.setLabelCount(dataPoints.count, force: false)
barChartView.animate(yAxisDuration: 1.0, easingOption: .easeInOutQuad)
switch dataPoints.count
{
case 1...6:
zoomXMultiplier = 0.0
barWidthMultiplier = 0.3
break
default:
zoomXMultiplier = CGFloat(dataPoints.count) * 0.168
barWidthMultiplier = Double(dataPoints.count) * 0.0014 - Double(zoomXMultiplier) * 0.02
break
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "")
let chartData = BarChartData(dataSet: chartDataSet)
barChartView.data = chartData
barChartView.zoom(scaleX: zoomXMultiplier, scaleY: 0.0, x: 0, y: 0)
barChartView.barData?.barWidth = barWidthMultiplier
}
However, I can't seem to get the barWidthMultiplier just right, the width of the bar noticeable scales down/up with decreasing/increasing number of data point entries.
Can someone provide any suggestions to help me resolve this issue if possible?
Thanks!
I experience a number of issues with the horizontal bar chart since I updated to the last version of ios-chart (3.0.0).
First thing is that I had labels for the x axis on the charts. With the new version of ios-charts I had to write my own formatter nevertheless only every second label get displayed now. The second problem is that the bar does not reach the value indicated on the y-axis. What I mean by that is that if I give the bar a value of 4 the height of the bar is actually not long enough to reach the 4.0 label on the y-axis. Those 2 issues are illustrated in this picture.
picture of the problems
Here is my code for the value formatter:
#objc(BarChartFormatter)
public class BarChartFormatter: NSObject, IAxisValueFormatter{
var labelArray : [String] = ["text1", "text2", "text3", "text4", "text5", "text6", "text7", "text8", "text9", "text10"]
public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return labelArray[Int(value)]
}
}
And here is the code for the bar chart
horizontalBarChartView.noDataText = "You need to provide data for the chart."
horizontalBarChartView.isUserInteractionEnabled = false
horizontalBarChartView.drawGridBackgroundEnabled = false
horizontalBarChartView.drawValueAboveBarEnabled = false
horizontalBarChartView.legend.enabled = false
horizontalBarChartView.drawBarShadowEnabled = false
horizontalBarChartView.chartDescription?.text = ""
horizontalBarChartView.setExtraOffsets(left: 0, top: 0, right: 0, bottom: 0)
// Right y axis
let barRightAxis = horizontalBarChartView.leftAxis
barRightAxis.drawGridLinesEnabled = false
barRightAxis.enabled = false
let formato:BarChartFormatter = BarChartFormatter()
let xaxis:XAxis = XAxis()
// Left y axis
let barLeftAxis = horizontalBarChartView.rightAxis
let count = 5
barLeftAxis.setLabelCount(count, force: true)
barLeftAxis.drawGridLinesEnabled = false
barLeftAxis.axisLineWidth = 2
barLeftAxis.axisLineColor = UIColor.white
barLeftAxis.labelTextColor = UIColor.white
barLeftAxis.axisMinimum = 0.0
barLeftAxis.axisMaximum = 4.0
barLeftAxis.granularityEnabled = true
barLeftAxis.granularity = 1.0
barLeftAxis.decimals = 0
// x axis
let barXaXis = horizontalBarChartView.xAxis
barXaXis.drawGridLinesEnabled = false
barXaXis.axisLineColor = UIColor.white
barXaXis.axisLineWidth = 2
barXaXis.labelTextColor = UIColor.white
barXaXis.labelPosition = .bottomInside
barXaXis.granularityEnabled = true
barXaXis.granularity = 1.0
barXaXis.decimals = 0
var dataEntries: [BarChartDataEntry] = []
var colors: [UIColor] = []
let theScoreVec = [1.0, 2.0, 3.0, 4.0, 3.0, 0.0, 2.0, 0.0, 1.0, 4.0]
for i in 0..<10 {
let theScore = theScoreVec[i]
if (theScore == 0.0){
colors.append(UIColor.clear)
let dataEntry = BarChartDataEntry(x: Double(i), y : Double(4.0))
dataEntries.append(dataEntry)
} else {
colors.append(UIColor.red)
let dataEntry = BarChartDataEntry(x: Double(i), y: Double(theScore))
dataEntries.append(dataEntry)
}
}
xaxis.valueFormatter = formato
barXaXis.valueFormatter = xaxis.valueFormatter
for i in 0..<10{
formato.stringForValue(Double(i), axis: xaxis)
}
xaxis.valueFormatter = formato
barXaXis.valueFormatter = xaxis.valueFormatter
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Completion")
chartDataSet.colors = colors
let chartData = BarChartData(dataSet: chartDataSet)
chartData.setDrawValues(false)
horizontalBarChartView.data = chartData
Here I also use some kind of hack because when one enters a value of 0.0 for a BarChartDataEntry the bars and the x axis have a significant offset. Therefore I create bars with a fake value and give them a transparent color.
Do one you had a similar issue with the library and know where it comes from ? Or maybe has even solved it ?
Thanks in advance for the help.