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.
Related
I am using danielgindi/Charts library. In my main view controller I have table view with 3 prototype cell. One of them is for bar chart, second one is for line chart and third one is for pie chart. There are also 3 dequeue reusable cell method and charts configure methods are inside of them. When I first execute application all my bar charts are drawn without any problem. However, when I add new charts or when I scroll (I am not sure but I think one of them lead weird problem) bar chart view looks weird as it shown below image.
It is shown like second one when I first execute program but after some scrolling or adding new charts it looks first one. Every data set cover x axis. Following is my configureBarChart method.
func configureBarChart(xTitle: IndexAxisValueFormatter, index: Int, chartData: BarChartData, chartType: String) {
barChartView.data?.setDrawValues(false)
barChartView.doubleTapToZoomEnabled = false
barChartView.chartDescription?.enabled = false
barChartView.animate(xAxisDuration: 1)
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.xAxis.valueFormatter = xTitle
barChartView.xAxis.labelCount = index
barChartView.rightAxis.enabled = false
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.xAxis.labelPosition = .bottom
barChartView.xAxis.centerAxisLabelsEnabled = true
barChartView.xAxis.granularity = 1
let leftAxisFormatter = NumberFormatter()
leftAxisFormatter.maximumFractionDigits = 1
barChartView.leftAxis.spaceTop = 0.35
barChartView.leftAxis.axisMinimum = 0
barChartView.leftAxis.drawGridLinesEnabled = false
barChartView.rightAxis.enabled = false
var groupSpace = 0.0
var barSpace = 0.0
var barWidth = 0.0
if (chartData.dataSetCount == 1) {
groupSpace = 0.3
barSpace = 0.0
barWidth = 0.7
} else if (chartData.dataSetCount == 2) {
groupSpace = 0.3
barSpace = 0.05
barWidth = 0.3
} else if (chartData.dataSetCount == 3) {
groupSpace = 0.25
barSpace = 0.05
barWidth = 0.2
} else if (chartData.dataSetCount == 4) {
groupSpace = 0.2
barSpace = 0.05
barWidth = 0.15
}
let groupCount = index
let startYear = 0
chartData.barWidth = barWidth;
barChartView.xAxis.axisMinimum = Double(startYear)
let gg = chartData.groupWidth(groupSpace: groupSpace, barSpace: barSpace)
if (chartType == "clustered") {
barChartView.xAxis.axisMaximum = Double(startYear) + gg * Double(groupCount)
chartData.groupBars(fromX: Double(startYear), groupSpace: groupSpace, barSpace: barSpace)
} else {
barChartView.xAxis.axisMaximum = Double(startYear) + 1 * Double(groupCount)
}
barChartView.setScaleEnabled(false)
if xTitle.values.count >= 10 {
barChartView.setVisibleXRangeMaximum(12)
} else {
barChartView.setVisibleXRangeMaximum(Double(xTitle.values.count)) // When too many data comes I limit it so data's are not covering each other.
}
}
I am also using that bar chart for grouped bar chart that is why there is some barSpace and barWidth code. What could be the reason of changing behavior of chart and how can I fix it ?
Edit: Here is my cellForRowAt method and getting data of bar chart
if MainGrafikVC.barChartDataArray[indexPath.row].dataSetCount != 0 {
cell.barChartView.data = MainGrafikVC.barChartDataArray[indexPath.row]
cell.configureBarChart(xTitle: MainGrafikVC.xTitleArray[indexPath.row], index: MainGrafikVC.barChartDataArray[indexPath.row].getDataSetByIndex(0).entryCount, chartData: MainGrafikVC.barChartDataArray[indexPath.row], chartType: Servisler.servisListesi[MainGrafikVC.indexler[indexPath.row]].chartType)
}
if x10.count > 0 {
var barChartEntry10 = [BarChartDataEntry]()
for i in 0..<x10.count {
let value = BarChartDataEntry(x: Double(i), y: Double(x10[i]) ?? 0.0)
barChartEntry10.append(value)
}
let bar10 = BarChartDataSet(values: barChartEntry10, label: "10")
bar10.colors = ChartColorTemplates.vordiplom()
dataSets.append(bar10)
}
let data = BarChartData(dataSets: dataSets)
MainGrafikVC.barChartDataArray.append(data)
I think the problem is about setVisibleXRangeMaximum because when I comment it everything looks same. However, I need to set visible x axis what could be alternative of it?
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 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