Set an Horizontal Scroll to My Barchart in Swift

Set an horizontal scroll to my Barchart in swift

the scroll will only happen if you zoomed in. By default, all data are plotted on the canvas, so nothing to scroll.

You can try increase scaleX by code, to see if your code works.

Trouble getting the chart to scroll, instead of scaling whole chart to fit in space

I solved the problem by calling

chart.setVisibleXRangeMaximum(30)

AFTER the data has been set, and calling

chart.scaleYEnabled = false & chart.scaleXEnabled = true

when I set the other chart settings.

iOS_Charts HorizontalBarChartView Vertical scrolling

It already supports so. you can set maxScaleX/Y to limit the scale,

also you can try setVisibleYRangeMaximum. horizontal bar chart's x axis is the same as normal bar's x axis position. It's not reverted with y axis

How to add marker to center position in ios charts while scrolling horizontally with increasing scaleX and enabling drag

Unfortunately ChartViewBase.swift is full of private function and internal vars and you can't extend some method or obtain some var to get the values you searching for.

Anyway, you can always improve sources adding some other method you want to use inside the public protocol ChartViewDelegate:

ChartViewBase.swift

under the line:

@objc optional func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat)

add this:

@objc optional func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat, entry: ChartDataEntry?, highlight: Highlight?, centerIndices:Highlight?)

Then , you can easily call this new delegate method to the only source part of the code where chartTranslated is called:

BarLineChartViewBase.swift

search the part of the code where you see these lines:

if delegate !== nil
{
delegate?.chartTranslated?(self, dX: translation.x, dY: translation.y)
}

and change it with:

if delegate !== nil
{
delegate?.chartTranslated?(self, dX: translation.x, dY: translation.y)
var entry: ChartDataEntry?
var h = self.lastHighlighted

if h == nil
{
_indicesToHighlight.removeAll(keepingCapacity: false)
}
else
{
// set the indices to highlight
entry = _data?.entryForHighlight(h!)
if entry == nil
{
h = nil
_indicesToHighlight.removeAll(keepingCapacity: false)
}
else
{
_indicesToHighlight = [h!]
}
}
let centerH = getHighlightByTouchPoint(self.center)
if centerH === nil || centerH!.isEqual(self.lastHighlighted)
{
//self.highlightValue(nil, callDelegate: true)
//self.lastHighlighted = nil
}
else
{
print("\n in center we have: \(centerH!)")
self.highlightValue(centerH, callDelegate: true)
self.lastHighlighted = centerH
// please comment these lines if you don't want to automatic highlight the center indices..
}
delegate?.chartTranslated?(self, dX: translation.x, dY: translation.y, entry: entry, highlight: h, centerIndices:centerH)
}

Usage:

import Foundation
import UIKit
import Charts

class test: UIViewController, ChartViewDelegate {

func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat, entry: ChartDataEntry?, highlight: Highlight?, centerIndices:Highlight?) {
if let entry = entry, let highlight = highlight {
print("chartTranslated info:\n\(self)\ndX and dY:\(dX)-\(dY)\nentry:\(entry)\nhightlight:\(highlight)")
if let centerIndices = centerIndices {
print("\n center indices is:\n\(centerIndices)")
}
}
}

Now, with this new delegate method you can make your marker to the center indices.

A gif to show a little demonstration:

Sample Image

Horizontal Bar chart not moving to X

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 Topics



Leave a reply



Submit