Swift Charts: Labels on Y-Axis is missing after zooming - ios

I'm very new dealing charts in swift. Using LineChartView to plot my data on graph. On zooming,values on Y-axis min and max change accordingly based on values of viewport min and max. For that implemented the following delegate.
func chartScaled(_ chartView: ChartViewBase, scaleX: CGFloat, scaleY: CGFloat) {
self.mapView.leftAxis.axisMaximum = axisY[Int(self.mapView.highestVisibleX)]
self.mapView.leftAxis.axisMinimum = axisY[Int(self.mapView.lowestVisibleX)]
}
Sometimes working fine, but after scaling the graph,the labels are not visible and graph is not scaling to normal position also.
Snippet for leftaxis
self.mapView!.leftAxis.enabled = true
self.mapView!.leftAxis.drawAxisLineEnabled = false
self.mapView!.leftAxis.spaceTop = 0.5
self.mapView!.leftAxis.spaceBottom = 0.4
self.mapView.leftAxis.granularity = 5.0
self.mapView.leftAxis.granularityEnabled = true
Please help me out where am doing wrong.
Thanks in advance.

Related

Remove values above data line iOS Charts

I have built a small application to measure Heart Rate (HR) and currently trying to implement a chart using iOS Charts. It is working as intended (getting HR from watch and displaying it), but I have a small problem with the design of the chart.
On the image below you can see that I have overlapping numbers above the data line (70, and the later 90). I do not know how to remove them.
Chart Image
Here is my setup for the chart and its' update counter function:
//chart set up
self.chtChart.delegate = self as? ChartViewDelegate
let set_a: LineChartDataSet = LineChartDataSet(entries:[ChartDataEntry(x: Double(0), y: self.valueHR)], label: "HR")
set_a.drawCirclesEnabled = false
set_a.setColor(UIColor.systemPink)
self.chtChart.xAxis.drawGridLinesEnabled = false
self.chtChart.rightAxis.drawLabelsEnabled = false
self.chtChart.xAxis.drawLabelsEnabled = false
self.chtChart.data = LineChartData(dataSets: [set_a])
// update counter
var i = 1
#objc func updateCounter() {
self.chtChart.data?.addEntry(ChartDataEntry(x: Double(i), y: valueHR), dataSetIndex: 0)
self.chtChart.setVisibleXRange(minXRange: Double(0), maxXRange: Double(1000))
self.chtChart.notifyDataSetChanged()
self.chtChart.moveViewToX(Double(i))
i = i + 1
}
I know that there is a duplicate question, but the solution with the formatter did not help me: nothing was changed after the implementation of the solution.
formatter solution

CorePlot - Annotations swinging in the Scatter Plot

In my app I've a CorePlot which hosts three scatterplots, I've also configured as follows,
let borderStyle = CPTMutableLineStyle()
borderStyle.lineColor = maxPlotColor
let textLayer = CPTTextLayer(text: "MAX")
textLayer.fill = CPTFill(color: .white())
textLayer.cornerRadius = 10.0
textLayer.borderLineStyle = borderStyle
maxLabelAnnotation = CPTPlotSpaceAnnotation(plotSpace: maxLinePlot.plotSpace!, anchorPlotPoint: [5, NSNumber(value: kMaxLineValue)])
maxLabelAnnotation.contentLayer = textLayer
maxLinePlot.graph?.plotAreaFrame?.plotArea?.addAnnotation(maxLabelAnnotation)
The annotations are updated dynamically when the plot data is updated. The code snippet is shown below,
func newData(_ theTimer: Timer) {
// MARK: Dynmic annotations
maxLabelAnnotation.anchorPlotPoint = [NSNumber(value: currentIndex - 18), NSNumber(value: kMaxLineValue)]
riskLabelAnnotation.anchorPlotPoint = [NSNumber(value: currentIndex - 18), NSNumber(value: kRiskLineValue)] }
The problem is shown in the figure, where the annotation is moving back and froth as the graph moving continuously
I just want to stick the annotation in a position with out any swinging. any help would be much appreciated. Thanks in advance.
The annotations are locked to the plot space as it scrolls, but you update their anchor positions with each new data point, causing them to jump to the next position. Use layer annotations instead of plot space annotations to lock them in place. You'll need to recalculate the y-positions if the scale of the y-axis changes but the labels will stay in place as the graph scrolls below them.
Do this after the view hierarchy is build and everything is laid out so the view calculations are correct.
maxLinePlot.layoutIfNeeded()
if let plotArea = maxLinePlot.graph?.plotAreaFrame?.plotArea {
let plotSpace = maxLinePlot.plotSpace!
let viewPoint = plotSpace.plotAreaViewPoint(forPlotPoint: [0.0, kMaxLineValue])
let maxLabelAnnotation = CPTLayerAnnotation(anchorLayer: plotArea)
maxLabelAnnotation.contentLayer = textLayer
maxLabelAnnotation.xConstraints = CPTConstraints(relativeOffset: 0.25)
maxLabelAnnotation.yConstraints = CPTConstraints(lowerOffset: viewPoint.y)
plotArea.addAnnotation(maxLabelAnnotation)
}

SwiftCharts graph not in bounds

So I have am using the SwiftCharts graph, and have it setup like this.
func roundToTens(_ x : Double) -> Int {
return 10 * Int((x / 10.0).rounded(.awayFromZero))
}
func createChart(){
let chartConfig = BarsChartConfig(
valsAxisConfig: ChartAxisConfig(from: 0, to: Double(roundToTens(maximum)), by: Double(roundToTens(maximum)/6))
)
let frame = view2.bounds
let chart = BarsChart(
frame: frame,
chartConfig: chartConfig,
xTitle: "Categories",
yTitle: "Y axis",
bars: test,
color: UIColor.red,
barWidth: 20
)
self.chart = chart
view2.addSubview(chart.view)
self.view.addSubview(view2)
}
However, the graph ends up looking like this.
I tried fixing the bounds, by adding Margins to the bounds but all it did was make the graph look wrong. Is anyone familiar with the SwiftCharts framework and how I can centre the graph to the middle and make it overall smaller so that the top y-axis number is visible

How to animate only x Axis in core plot?

I am animating x Axis value in core plot but with that my xAxis , yAxis is also animating and disappearing . My set up for graph is shown in here as I have some issue related to graph data I have asked question and here as u can see in code graph set up is correct : Graph set up code. What is missing? MY code for animation is as below:
let plotSpace = graph.defaultPlotSpace as! CPTXYPlotSpace!
let location = currentIndex
let oldRange = CPTPlotRange(location: location , length: kMaxDataPoints - 2)
let newRange = CPTPlotRange(location: location + 1, length: kMaxDataPoints - 2)
CPTAnimation.animate(plotSpace, property: "xRange", fromPlotRange: oldRange, toPlotRange: newRange, duration: 1.0)
currentIndex += 1
(moved from the comments into an answer)
You can use axis constraints to lock the y-axis in position. Look at the "Real Time Plot" in the Plot Gallery example app.

How to create a Legend with Core Plot in Swift

I have created a CPTXYGraph using Core Plot in Swift. The graph works just fine. Now I am trying to add a legend. I have looked at several examples and come up with the following to create the legend:
var theLegend=CPTLegend(graph: graph)
var legendFill=CPTFill(color: CPTColor.blueColor())
theLegend.fill = legendFill
var legendLineStyle = CPTMutableLineStyle()
legendLineStyle.lineColor = CPTColor.whiteColor()
theLegend.borderLineStyle = legendLineStyle
theLegend.cornerRadius = 2.0
theLegend.swatchSize = CGSizeMake(15.0, 15.0)
graph.legend = theLegend
graph.legendAnchor = CPTRectAnchor.TopLeft
graph.legendDisplacement = CGPointMake(100.0, -100.0)
When I run this, I get a blue square with slightly rounded corners that appears to be about 10 X 10 pixels.
I tried setting the frame just to see if it would make a difference:
theLegend.frame = CGRectMake(0, 0, 200, 200)
This did not change anything.
Has anyone had any success with this in Swift?
Make sure all of your plots are already in the graph before using that legend initializer. You can also initialize a legend with an array of individual plots.
I found a solution here : Problems with legend in Core-Plot (pieChart)
You need to add this method
func legendTitleForPieChart(pieChart:CPTPieChart,recordIndex index:Int) -> NSString{
return "the title"
}
You need 3 things:
this delegate:
CPTPieChartDelegate
You need set the delegate to self on piechart:
pieChart.delegate = self
and feed the legend:
func legendTitleForPieChart(pieChart: CPTPieChart!, recordIndex idx: UInt) -> String! {
return "titulo"
}

Resources