I have troubles with the library and i can't find a solution, hope you can help.
When I put data on the chart everything about drawing axis and shapes goes well but the labels on my xAxis multiplies itself and i don't know why, look i let you my code and a problem's picture, maybe it sounds weird but in order to show the problem i created a barChart with just one data.
func setChart(values: [Double]) {
let formato:BarChartFormatter = BarChartFormatter()
formato.setArray(self.listadoLabels)
let xaxis:XAxis = XAxis()
max = values.maxElement()
barChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = []
for i in 0..<values.count {
let dataEntry = BarChartDataEntry(x: Double(i), y:values[i])
dataEntries.append(dataEntry)
formato.stringForValue(Double(i), axis: xaxis)
}
xaxis.valueFormatter = formato
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
chartDataSet.colors = ChartColorTemplates.joyful()
barChartView.fitBars = true //Para que las barras inicial y final se muestren completas
barChartView.xAxis.valueFormatter = xaxis.valueFormatter
barChartView.xAxis.labelPosition = .Bottom
barChartView.leftAxis.axisMinimum = 0
barChartView.leftAxis.axisMaximum = max + 2
barChartView.rightAxis.enabled = false
barChartView.drawValueAboveBarEnabled = false
barChartView.descriptionText = ""
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)
let chartData = BarChartData()
chartData.addDataSet(chartDataSet)
barChartView.data = chartData
}
Most of code is just styling, and i have one class for the xAxis format because you need one to put labels in the xAxis, this is the code:
public func stringForValue(value: Double, axis: AxisBase?) -> String {
return "Label1"
}
I'm just sending one value of 5512, and the result is, don't pay attention to segmentTab and button:
I was in the same situation you were, i couldn't find an exact solution, but this one worked for me:
barChart?.xAxis.wordWrapEnabled = true
barChart?.xAxis.setLabelCount(5, force: true)
I also centered my bars to match the labels
let dataEntry = BarChartDataEntry(x: Double(i) + 0.5, y: Double(count))
In my case my chart can't have more than 5 bars so i used 5, but you could pass a variable with the number of bars and set the "LabelCount" to that number, when you zoom in it will still repeat the labels, but they won't be overlapped and won't look as bad.
I know it is not exactly the answer you were looking for but hope it helps
Related
Hi using cocoapods charts. Trying to pivot bar-chart from right to left. with hidden bottom x-axis values.
Charts
https://cocoapods.org/pods/Charts
Below is code which I tried.
totalExpenseAmount = 33 -> Double
timesheetAmount = 300 -> Double
self.setBarChart(expenseAmount: totalExpenseAmount, timesheetAmount: totalTimesheetAmount)
// MARK: - DRAW CHART
func setBarChart(expenseAmount: Double, timesheetAmount: Double){
var dataEntries: [BarChartDataEntry] = []
barChartView.backgroundColor = UIColor.white
for i in 0..<2 {
let dataEntry = BarChartDataEntry(x: Double(i), y: i == 0 ? expenseAmount : timesheetAmount)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(entries: dataEntries)
chartDataSet.colors = [UIColor.init(hexString: "#008CA1") , UIColor.init(hexString: "#EF776C")]
let chartData = BarChartData()
chartData.barWidth = Double(0.25)
chartData.append(chartDataSet)
chartData.setDrawValues(true)
barChartView.xAxis.labelPosition = .bottom
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.chartDescription.enabled = false
barChartView.legend.enabled = false
barChartView.rightAxis.enabled = false
barChartView.leftAxis.drawGridLinesEnabled = true
barChartView.leftAxis.drawLabelsEnabled = true
barChartView.data = chartData
barChartView.leftAxis.spaceBottom = 0.0
barChartView.notifyDataSetChanged()
}
I tried to pivot bar chart from right to left. with Hidden bottom x-axis values.
Issue is second bar chart for 33 value not shown.
Unable to drawn bar chart from right to left.
Unable to hide the x-axis labels values ( -0.3 0.0 0.3 0.6 0.9 1.2 1.5)
Any one worked on charts please provide your inputs, Its deeply appreciated your input's.
I am using the iOS-charts library to integrate LineChartView and I have to create below chart :
So far I have achieved this :
I am facing three issues here :
Feb and Jun month text is missing in xAxis.
Have to add Left and right space in the chart.
Remove "Description Label" text visible in the chart.
Is it possible to add small mark above months as shown in the image for indicating months in xAxis ?
Implemented code :
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let unitsSold = [50.0, 25.0, 50.0, 75.0, 100.0, 75.0]
setChart(dataPoints: months, values: unitsSold)
and the setChart method:
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x: Double(i), y: values[i], data: dataPoints[i] as AnyObject)
dataEntries.append(dataEntry)
}
let chartDataSet = LineChartDataSet(values: dataEntries, label: nil)
chartDataSet.circleRadius = 5
chartDataSet.circleHoleRadius = 2
chartDataSet.drawValuesEnabled = false
let chartData = LineChartData(dataSets: [chartDataSet])
lineChartView.data = chartData
lineChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
lineChartView.xAxis.labelPosition = .bottom
lineChartView.xAxis.drawGridLinesEnabled = false
lineChartView.xAxis.avoidFirstLastClippingEnabled = true
lineChartView.rightAxis.drawAxisLineEnabled = false
lineChartView.rightAxis.drawLabelsEnabled = false
lineChartView.leftAxis.drawAxisLineEnabled = false
lineChartView.pinchZoomEnabled = false
lineChartView.doubleTapToZoomEnabled = false
lineChartView.legend.enabled = false
}
Please help if anyone has worked on charts in swift.
1) To avoid crowd Chart shows limited data in graph axis by zooming user can see the entire data
2) For left and Right spacing: Give left and right constraints from interface builder
3) To hide description you can use below code
lineChartView.chartDescription?.text = " "
Hope this help you
I had the same problem. I did this to solve it:
ineChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
ineChartView.xAxis.setLabelCount(months.count, force: true)
Hope it helps.
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.
I can not get an iOS-charts bar chart to put the bars zero (the bottom of the bars) on the x-axis. They seem to float just above the x-axis:
The bars in orange are set to 100 which is the max height of the y-axis for illustration. The only thing that might be doing it is at about 3PM there is a blank space.
I have rightAxis.axisMinValue = 0 set.
In your sample project, you forgot to set
dataSet.axisDependency = .Right
barChartView.leftAxis.spaceBottom = 0.0
works for me, for you it is .rightAxis
I have just work around, This works for me.
Just refer this code.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var barChartView: BarChartView!
override func viewDidLoad() {
super.viewDidLoad()
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 0.0, 16.0]
setChart(months, values: unitsSold)
}
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Units Sold")
let chartData = BarChartData(xVals: dataPoints, dataSet: chartDataSet)
barChartView.data = chartData
chartDataSet.colors = ChartColorTemplates.colorful()
}
}
Have you tried setting the right y-axis minimum to zero (rightAxisY.axisMinimum = 0)? That should set the y-axis minimum to the zero line and shift the entire chart downward.
You might also try enabling the zero line (rightAxisY.drawZeroLineEnabled = true) and eliminate the zero value from your data since you are setting zero as the minimum by enabling the zero line.