Show loading indicator while graph is loading in Highcharts - ios

I am generating a graph using Highcharts framework in one of the iOS projects using Swift language.
The function for generating graph is as follows.
private func createBUGraph()
{
let chartView = HIChartView(frame: self.chartHolderViewOutlet.bounds)
let options = HIOptions()
let title = HITitle()
title.text = "UTS"
options.title = title
let subtitle = HISubtitle()
subtitle.text = "Sales & Margin"
options.subtitle = subtitle
let xAxis = HIXAxis()
xAxis.categories = ["A", "B", "C", "D"]//
options.xAxis = [xAxis]
let yAxis = HIYAxis()
//yAxis.min = 0
yAxis.title = HITitle()
yAxis.title.text = "Sales"
yAxis.lineWidth = 1
let y2Axis = HIYAxis()
y2Axis.title = HITitle()
y2Axis.title.text = "Margin"
y2Axis.lineWidth = 1
y2Axis.tickPositions = [5,10,15,20]
y2Axis.opposite = true
options.yAxis = [yAxis,y2Axis]
let Sales = HIColumn()
Sales.name = "Sales"
Sales.data = [3, 2, 1, 3]
let SalesForecast = HIColumn()
SalesForecast.name = "Sales Forecast"
SalesForecast.data = 2, 3, 5, 7]
let Margin = HISeries()
Margin.name = "Margin"
Margin.data = [2.5, 1.2, 8.5, 5.5]
Margin.marker = HIMarker()
Margin.marker.lineWidth = 2
Margin.marker.symbol = "circle"
let MarginForeast = HISeries()
MarginForeast.name = "Margin Foreast"
MarginForeast.data = [3, 2.67, 3, 6.33] //
MarginForeast.marker = HIMarker()
MarginForeast.marker.lineWidth = 2
MarginForeast.marker.symbol = "circle"
options.series = [Sales, SalesForecast, Margin, MarginForeast]
chartView.options = options
self.chartHolderViewOutlet.addSubview(chartView)
}
While the graph is being loaded, I need to show a loading indicator in the graph. Is there any way to show that?
Or is there any way to know whether the graph has completed loading or not?
Thanks in advance

Add any loader when ever you call Chart method and stop loader when your charts load.
These are library for loader you can use any other or by default loader by apple.
1.https://github.com/ninjaprox/NVActivityIndicatorView
2.https://github.com/relatedcode/ProgressHUD

You may try this
func createBUGraph(completion: (Bool) -> ()) {
// do stuff here
completion(true)
}
createBUGraph(completion: { (success) -> Void in
// add loader
if success {
print("true")
// hide loader
}
})

You might as well use your own loader and show it when the chart is loading.
Then you can hide it using the chartViewDidLoad function from HIChartViewDelegate
API: https://api.highcharts.com/ios/highcharts/Protocols/HIChartViewDelegate.html#//api/name/chartViewDidLoad

Related

Highcharts y-axis tick interval

I am trying to create a combined graph using HighCharts in a swift iOS project. The function for creating the graph is as follows.
private func createBUGraph()
{
let chartView = HIChartView(frame: self.chartHolderViewOutlet.bounds)
let options = HIOptions()
let title = HITitle()
title.text = "UTS"
options.title = title
let subtitle = HISubtitle()
subtitle.text = "Sales & Margin"
options.subtitle = subtitle
let xAxis = HIXAxis()
xAxis.categories = ["A", "B", "C", "D"]//
options.xAxis = [xAxis]
let yAxis = HIYAxis()
//yAxis.min = 0
yAxis.title = HITitle()
yAxis.title.text = "Sales"
yAxis.lineWidth = 1
let y2Axis = HIYAxis()
y2Axis.title = HITitle()
y2Axis.title.text = "Margin"
y2Axis.lineWidth = 1
y2Axis.tickPositions = [5,10,15,20]
y2Axis.opposite = true
options.yAxis = [yAxis,y2Axis]
let Sales = HIColumn()
Sales.name = "Sales"
Sales.data = [3, 2, 1, 3]
let SalesForecast = HIColumn()
SalesForecast.name = "Sales Forecast"
SalesForecast.data = 2, 3, 5, 7]
let Margin = HISeries()
Margin.name = "Margin"
Margin.data = [2.5, 1.2, 8.5, 5.5]
Margin.marker = HIMarker()
Margin.marker.lineWidth = 2
Margin.marker.symbol = "circle"
let MarginForeast = HISeries()
MarginForeast.name = "Margin Foreast"
MarginForeast.data = [3, 2.67, 3, 6.33] //
MarginForeast.marker = HIMarker()
MarginForeast.marker.lineWidth = 2
MarginForeast.marker.symbol = "circle"
options.series = [Sales, SalesForecast, Margin, MarginForeast]
chartView.options = options
self.chartHolderViewOutlet.addSubview(chartView)
}
The graph is drawing but I am stuck at the following issues
How to set automatic tick intervals in y2Axis according to the data[Opposite axis]?
How to set the series data to the y2Axis?
I tried to look for solution in highcharts documentation but I could not find any which will give me a solution.
Remove y2Axis.tickPositions = [5,10,15,20]
Set series.yAxis = 1 (API)
How to set automatic tick intervals in y2Axis according to the
data[Opposite axis]?
Remove tickPositions and use HIYAxis.linkedTo property.
How to set the series data to the y2Axis?
Use HISeries.yAxis property
API: https://api.highcharts.com/ios/highcharts/

Highcharts categories with many curves is not working in iOS

I'm new using Highcharts. Using it in iOS (which is pretty much the same as JS) I have this piece of code for testing:
private func initChart(){
let chart = HIChart()
let chartOptions = HIOptions()
chartOptions.chart = chart
chart.type = "line"
chartOptions.title = HITitle()
chartOptions.title.text = "Auction Details"
chartOptions.subtitle = HISubtitle()
chartOptions.subtitle.text = "P&L Curves"
let xAxis = HIXAxis()
xAxis.title = HITitle()
xAxis.title.text = "Time"
xAxis.categories = ["10-01-2018", "23-03-2018", "10-04-2018", "30-04-2018", "01-05-2018", "14-09-2018", "10-011-2018", "22-11-2018"]
chartOptions.xAxis = [xAxis]
let yAxis = HIYAxis()
yAxis.title = HITitle()
yAxis.title.text = "Money"
yAxis.min = 0.0
chartOptions.yAxis = [yAxis]
let seriesRewards = HISeries()
seriesRewards.name = "Rewards"
seriesRewards.data = [["10-01-2018", 23.4], ["23-03-2018", 34.2], ["22-11-2018",7.9]]
let seriesPrice = HISeries()
seriesPrice.name = "Price"
seriesPrice.data = [["23-03-2018",42.4], ["23-03-2018",34.2], ["10-04-2018",61.1], ["01-05-2018",87.0], ["14-09-2018",25.9], ["22-11-2018",100.1], ["01-12-2018",95.9], ["22-12-2018",81.9]]
chartOptions.series = [seriesRewards, seriesPrice]
chartView.options = chartOptions
}
This runs perfectly but it I the points for the 1st series do not match with what I said... pls take a look to the following image:
My question is, seriesRewards is defined like this:
seriesRewards.data = [["10-01-2018", 23.4], ["23-03-2018", 34.2],
["22-11-2018",7.9]]
Why the last point is not shown in the correct position in the chart?
Regards

Read out specific JSON from NSArray

This is the JSON object I'm getting from the openweathermap - API:
["main": {
humidity = 12;
pressure = 922;
temp = "271.13";
"temp_max" = "171.15";
"temp_min" = "291.15";
}, "name": mycity, "id": 299129219, "coord": {
lat = "92.1211";
lon = "182.1211";
}, "weather": <__NSArrayI 0x1c042e820>(
{
description = "light snow";
icon = 13n;
id = 120;
main = Snow;
},
{
description = mist;
icon = 50n;
id = 722;
main = Mist;
}
)
, "clouds": {
all = 12;
}, "dt": 211, "base": stations, "sys": {
country = XXX;
id = 4891;
message = "0.02221";
sunrise = 1221122112;
sunset = 4343344343;
type = 1;
}, "cod": 100, "visibility": 3200, "wind": {
speed = 3;
}]
Because I like to readout some information (like the current temperature, the weather description, etc.) I tried to use this few lines:
let temperature = (result["main"] as! [String:Double])["temp"]!
The code above is working fine but I got massive problems reading out the description of the first Weather element (called "light snow"):
let description = (result["weather"] as! [String:Any]).first["description"]! //(result should be : "light snow")
... doesn't seems working at all.
So how can I fix this issue?
Thanks a million in advance.
Also used this API :)
This worked for me:
guard let weathersArray = json["weather"] as? [[String: Any]],
let weatherJson = weathersArray.first,
let description = weatherJson["description"] as? String
else { return }
Update: in case you want all the array elements just loop over the weathersArray and get all the descriptions.

DotNet.HighCharts StepLine

I'd start to use Dotnet.HighCharts controls to draw charts on my page in ASP.NET MVC app.
I used Google Charts, but they don't have enough functionality.
The task is to make charts with area ranges, step line and syncronized crosshair on multiple charts. Does DotNet.HighCharts do it?
By this time i did draw only area ranges.
If not, please give me a hint, which controls can do it.
Highcharts chart = new Highcharts("chart")
.InitChart(new Chart())
.SetTitle(new Title { Text = "July temperatures" })
.SetXAxis(new XAxis { Type = AxisTypes.Datetime })
.SetYAxis(new YAxis { Title = new YAxisTitle { Text = string.Empty } })
.SetLegend(new Legend { Enabled = false })
.SetTooltip(new Tooltip
{
Crosshairs = new Crosshairs(true),
Shared = true,
ValueSuffix = "°C"
})
.SetLegend(new Legend())
.SetSeries(new[]
{
new Series
{
Name = "Temperature",
Data = ChartsData.TemperatureVariationAverages,
ZIndex = 1,
Type = ChartTypes.Line,
PlotOptionsLine = new PlotOptionsLine
{
Marker = new PlotOptionsLineMarker
{
FillColor = Color.White,
LineWidth = 2,
LineColor = Color.Black
}
}
},
new Series
{
Name = "Range",
Data = ChartsData.TemperatureVariationRanges,
Type = ChartTypes.Arearange,
ZIndex = 0,
PlotOptionsArearange = new PlotOptionsArearange
{
LineWidth = 0,
LinkedTo = ":previous",
Color = Color.Green,
FillOpacity = 0.1,
}
},
new Series
{
Name = "Range3",
Data = ChartsData.TemperatureVariationRanges1,
Type = ChartTypes.Arearange,
ZIndex = 0,
PlotOptionsArearange = new PlotOptionsArearange
{
LineWidth = 0,
LinkedTo = ":previous",
Color = Color.Red,
FillOpacity = 0.1,
}
},
new Series
{
Name = "Range2",
Data = ChartsData.TemperatureVariationRanges2,
Type = ChartTypes.Arearange,
ZIndex = 0,
PlotOptionsArearange = new PlotOptionsArearange
{
LineWidth = 0,
LinkedTo = ":previous",
Color = Color.Yellow,
FillOpacity = 0.1,
}
}
});

Setup the maximum value, and the width of a pie chart using charts

Hi there I'm working with Charts library and I've build some graphs, but I want to do something special with a PieChart and I can't do it yet I don't know which could be the properties to setup my pie graph like y want. Ok then I want to setup a maximum value to 100 for the graph, by setting up this, I guess that my graph will show just the number that I'm trying to show in this case es 50 and the other part of the graph should look empty. I hope yo can help me.
This is what I have now the graph has just one Double value that value is 50
This is the way that the graph should look like
func createPieGraphRedemptions() {
var chart = PieChartView()
chart = PieChartView(frame: CGRect(x: 229, y: 40, width: 165, height: 165))
let yVals: [Double] = [50]
var entries = [ ChartDataEntry]()
for (i, v) in yVals.enumerated() {
let entry = ChartDataEntry()
entry.x = Double( i)
entry.y = v
entries.append( entry)
}
let set = PieChartDataSet( values: entries, label: "Pie Chart")
set.colors = [UIColor.red]
let data = PieChartData( dataSet: set)
//data.setDrawValues(false)
chart.data = data
chart.noDataText = "No hay datos disponibles"
chart.isUserInteractionEnabled = false
chart.legend.enabled = false
chart.chartDescription?.text = ""
chart.highlighter = nil
self.view.addSubview(chart)
}

Resources