Bar Graphs in iOS App

Bar Graphs in iOS app

Use this buddy. :)

EDIT

Installing Core Plot might be some headache for you, if you need help in that, let me know.

If you need very simple graphs you can go for ECGraph

iOS: Two Bar Charts together

Now that you've changed your question, this requires a different solution.

As I mentioned in the comments before, you need to manually calculate each bar's position. Then, don't use the grouping feature, because you've already grouped them how you want.

// Determine bar sizing and spacing parameters.
int barsPerGroup = 2;
double targetGroupWidth = 1;
double barSpacing = 0.2;
double groupSpacing = 0.3;
double barWidth = (targetGroupWidth - groupSpacing - barSpacing * barsPerGroup) / barsPerGroup;
double compareBarOffset = barWidth / 3;

for (int i = 0; i < entryCount; i++) {

// Determine X position for each bar
// NOTE: This is the most important step!
double groupStartPosition = targetGroupWidth * i;

double group1X = groupStartPosition + barWidth / 2;
double group1CompareX = group1X + compareBarOffset;

double group2X = group1X + barWidth + barSpacing;
double group2CompareX = group2X + compareBarOffset;

// Create data entries positioned at values calculated by previous step
NSNumber *group1Value = group1[i];
NSNumber *group1CompareValue = group1Compare[i];

NSNumber *group2Value = group2[i];
NSNumber *group2CompareValue = group2Compare[i];

BarChartDataEntry *group1DataEntry = [[BarChartDataEntry alloc] initWithX:group1X
y:[group1Value doubleValue]];
BarChartDataEntry *group1CompareDataEntry = [[BarChartDataEntry alloc] initWithX:group1CompareX
y:[group1CompareValue doubleValue]];

BarChartDataEntry *group2DataEntry = [[BarChartDataEntry alloc] initWithX:group2X
y:[group2Value doubleValue]];
BarChartDataEntry *group2CompareEntry = [[BarChartDataEntry alloc] initWithX:group2CompareX
y:[group2CompareValue doubleValue]];

// ...
}

// Create Data Sets, set styles, etc.
// ...

// Do NOT use this method because bars are already grouped.
//[barChartView groupBarsFromX:0 groupSpace:groupSpacing barSpace:barSpacing];

RESULT:

Note: I can't find the property isDashedBorder, so I don't know what version of the library you're using. Instead, I just set clear bars with a solid gray border.

Chart with orange and blue bars with overlapping bars for comparison

How to create a grouped bar chart in Swift?

[Swift 4]You need to use groupBars and valueFormatter form BarChart API.

This is just an example of code, you need to fine tuning as per your requirement.

[Edit 1]

Formula: (0.2 + 0.03) * countGourp + 0.08 = 1.00 -> interval per "group"

 let data = BarChartData(dataSets: datasets)
// (0.2 + 0.03) * 2 + 0.54 = 1.00
let groupSpace = 0.54
let barSpace = 0.03
let barWidth = 0.2
data.barWidth = barWidth

barView.xAxis.axisMinimum = Double(0)
barView.xAxis.axisMaximum = Double(0) + data.groupWidth(groupSpace: groupSpace, barSpace: barSpace) * Double(2) // group count : 2
data.groupBars(fromX: Double(0), groupSpace: groupSpace, barSpace: barSpace)

barView.data = data

let x_Axis = barView.xAxis
x_Axis.valueFormatter = IndexAxisValueFormatter(values:xVals)
x_Axis.centerAxisLabelsEnabled = true
x_Axis.granularity = 1
barView.xAxis.labelPosition = .top

Change Bar Chart Design [iOS Charts]

Currently, there's no way to change the bar design in the way you want.

However, someone did try a similar thing by changing the grid line to draw over the bars but it wasn't added.

See: Added drawGridLinesOnTopEnabled boolean flag to draw grid lines on top of Bar Charts.

You can check out his work here.

Another manual workaround, although not the best but should work if you absolutely need it, is by putting a transparent view with non-transparent lines on top of the chartView (or any other UIView for that matter)

Example:

class HorizontalLines: UIView {        
override func draw(_ rect: CGRect) {

//number of parts to divide the height of view by
let segments = 20

//number of lines ignoring the bottom most line
let numberOfLines = segments - 1

//draw the lines from left to right
for i in (1...numberOfLines).reversed() {
let multiplier = CGFloat(i)/CGFloat(segments)
let y = rect.size.height * multiplier
let startPoint = CGPoint(x:0, y:y)
let endPoint = CGPoint(x:rect.size.width, y:y)

let aPath = UIBezierPath()
aPath.move(to: startPoint)
aPath.addLine(to: endPoint)
aPath.close()

//line width
aPath.lineWidth = 1

aPath.stroke()
aPath.fill()
}
}
}

Usage:

let lineView = HorizontalLines()
lineView.isUserInteractionEnabled = false
lineView.backgroundColor = .clear

lineView.translatesAutoresizingMaskIntoConstraints = false
chartView.addSubview(lineView)

lineView.leadingAnchor.constraint(equalTo: chartView.leadingAnchor).isActive = true
lineView.trailingAnchor.constraint(equalTo: chartView.trailingAnchor).isActive = true
lineView.topAnchor.constraint(equalTo: chartView.topAnchor).isActive = true
lineView.bottomAnchor.constraint(equalTo: chartView.bottomAnchor).isActive = true

Be aware that this will draw a line over anything shown on the chartView.

So if you decide to show bar data label, then the lines would draw over that as well.

I hope this helps you in some way :)

How to display bar values on the top of bar?

I don't think that this is possible at the moment using Charts. The values are rendered in BarChartRender.swift in the drawValues(context: CGContext) function but always above the bar. They are not rendered below if the left axis is inverted. If you want this behavior you should implement it yourself changing this part of code of the function:

drawValue(
context: context,
value: formatter.stringForValue(
val,
entry: e,
dataSetIndex: dataSetIndex,
viewPortHandler: viewPortHandler),
xPos: x,
yPos: val >= 0.0
? (rect.origin.y + rect.size.height + posOffset)
: (rect.origin.y + negOffset), // Those lines should do what you're trying to achieve
font: valueFont,
align: .center,
color: dataSet.valueTextColorAt(j))

Create BarChart application in ios

There are various ways of achieving this.

You can use API's such as Core Plot to achieve your requirement. At the same time you can use Core Graphics & quartz Core to achieve it.

You should also check this out Bar Graphs in iOS app

ios charts not displaying line/bar in charts

I am struggling with Version 3.0 and Swift 3 and the lack of documentation... May I point you to the best text on the subject which can be found here

Xvals as Parameters are gone, instead you use two-dimensional data on axis

Regarding your problem with timevalues.. you could use the "timeIntervalSince1970" of the date-object and format things with an extension

return dateFormatter.string(from: Date(timeIntervalSince1970: value))

all of which is to be found at the link mentioned above

How to show value of bar on the top of the bar in Charts iOS?

My values were their on the top of bar but what i did wrong was that i was clearing their text color on line: "dataSet.valueColors = [.clear]".



Related Topics



Leave a reply



Submit