How can I show first and end values in Chart objective c? - ios

I'm using ios-chart (ios Charts). I have some values to show xAxis, but when I load a type of graph, I only want show 2 or 3 values. The first, the mid and the last value.
But i only can show 1 value at first if the values are more than 30 aprox.
I think I need override the stringForValue function, how can override this to show almost one value at finish and at begining?
- (NSString *)stringForValue:(double)value
axis:(ChartAxisBase *)axis
I set values array in format like:
xValuess: (
"1",
"",
"",
"",
"65",
"",
"",
"",
"140"
)
But the values received in the function override islike format: 0, 50, 100. I don't know how to get the concret values of my xValues array.

1- First you need to add <IChartValueFormatter>
2- Then you should set your LineChartDataSet's valueFormatter to self
set1.valueFormatter = self;
3- And finally you can reach your data from;
-(NSString *)stringForValue:(double)value entry:(ChartDataEntry *)entry dataSetIndex:(NSInteger)dataSetIndex viewPortHandler:(ChartViewPortHandler *)viewPortHandler{
if (entry.x == 0 || entry.x == myArray.count-1) {
NSInteger index = [NSNumber numberWithDouble:value].integerValue;
return [NSString stringWithFormat:#"%ld",(long)index];
} else {
return #"";
}
}
I hope it helps.

Related

Parsing JSON with NSDictionary

I am working on an app which fetches data from a server. I convert this data to XML and then parse it using XMLDictionary. My problem is counting the number of objects inside the dictionary.
"finance_fee_collection" = (
{
amount = "790.00";
"due_date" = "2015-06-04";
name = "Third Payment";
},
{
amount = "790.00";
"due_date" = "2014-12-11";
name = "First Payment";
},
{
amount = "740.00";
"due_date" = "2015-07-06";
name = "third payment";
}
);
Counting the above number of objects yields 3, which is true. But counting the following also results 3.
"finance_fee_collection" = {
amount = "740.00";
"due_date" = "2015-07-06";
name = "third payment";
};
What I want is to count the number of "finance_fee_collection" items, such that the first one results 3 and the second one results 1. Is there anyway I can approach this goal?
The first one seems to be an array, the second a dictionary:
if([finance_fee_collection iskindOfClass:[NSDictionary class]]){
count = 1;
} else {
count = result.count;
}
something similar to this should work. :) hope it is helpful.
This doesn't make sense really, since both NSArray and NSDictionary implement the count method, which should return 3 for both structures:
id obj = #[#1 ,#2, #3]; // This is an NSArray
NSLog(#"%lu", (unsigned long)[obj count]); // Outputs 3
obj = #{ #1:#1,
#2:#2,
#3:#3
}; // now it's an NSDictionary
NSLog(#"%lu", (unsigned long)[obj count]); // Outputs 3
Were you using [object count] to get the count?
Either way, I wouldn't assume that count would be 1 simply because the data structure is a dictionary (you didn't count it)

How to format an XLForm row value

I'm using XLForm in my project and want to display the value of one of the rows in a specific format.
The row is "Annual turnover" and I want to display the value entered by the user as a currency - for example, if the user enters 1000000 into the form field, the field should be updated to display this: $ 1,000,000.00
How do I do that?
This is how the form field is initialised in initializeForm():
-(void)initializeForm
{
// Other fields in the form...
// Annual turnover Section
self.section = [XLFormSectionDescriptor formSectionWithTitle:formLabel8AnnualTurnover];
//section.footerTitle = #"Describe new products";
[self.formDescriptor addFormSection:self.section];
// Annual turnover
self.row = [XLFormRowDescriptor formRowDescriptorWithTag:formField8AnnualTurnover rowType:XLFormRowDescriptorTypeText title:nil];
self.row.required = NO;
rowDescriptor8AnnualTurnover = self.row;
[self.section addFormRow:self.row];
// Other fields in the form...
self.form = self.formDescriptor;
}
According to the documentation, I should catch that the user has interacted with the field and then change the value of the field and update the row.
This is how I catch that the user has entered something into the field:
-(void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
{
[super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
// Get the current form values
formValues = [self.form formValues];
int i = 0;
if ([rowDescriptor.tag hasPrefix:formField8AnnualTurnover]){
NSString *annualTurnoverValue = [(XLFormOptionsObject*)formValues[formField8AnnualTurnover] displayText];
rowDescriptor8AnnualTurnover.value = [NSString stringWithFormat:#"R %#", annualTurnoverValue]; //This breaks.
[self reloadFormRow:rowDescriptor8AnnualTurnover];
}
}
An endless loop was being created by resetting the value of the rowDescriptor in formRowDescriptorValueHasChanged.
I resolved this issue by changing formRowDescriptorValueHasChanged to first check whether or not the rowDescriptor's value already starts with the currency symbol, and only add it if it's missing:
-(void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
{
[super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
// Get the current form values
formValues = [self.form formValues];
if ([rowDescriptor.tag hasPrefix:formField8AnnualTurnover]){
NSString *annualTurnoverValue = [(XLFormOptionsObject*)formValues[formField8AnnualTurnover] displayText];
if([annualTurnoverValue hasPrefix:#"R "]){
// Do nothing - the value will simply be increased.
}else{
rowDescriptor8AnnualTurnover.value = [NSString stringWithFormat:#"R %#", annualTurnoverValue];
[self reloadFormRow:rowDescriptor8AnnualTurnover];
}
}
}

Convert NSString to Int and then use that value to determine which text to display in a UILabel

I have my weather station app I am working on, it all works but the one thing I want to do is with the UV rating there is usually and word rating associated with the number. (Ex - UV rating is less then 3, word accompniing it "Low", between 3-6, "Medium" and etc...)
How do I convert my NSString, which displays the value into and int and then assign a word based on the range in which that value falls in.
I know how I would do the comparing in Java but I do not know how to in swift.
This is what I was thinking for it ( sudocode, Java basedish):
METHOD()
if(this.UVrating() < 3)
println("Low")
First of course I need to convert the NSString to an int in order to do this. The NSString will always be an integer.
TL;DR -
1: Convert NSString to int
2: Depending on int value, update a UILabel with "Low" "Medium" "High" and "Extreme"
Low -> 0-3
Medium -> 4-6
High -> 7-10
Extreme -> 11+
you can try like below.
NSString *str = #"1"; // Here your string value which contains int value as string
int data = str.intValue;
then continue your code
if (data>=11) {
NSLog(#"Extreme"); // here instead of nslog you can write code to change lable values
}else if (data<=10 && data>=7){
NSLog(#"High");
}else if (data<=6 && data>=4){
NSLog(#"Medium");
}else{
NSLog(#"Low");
}
Update : SWIFT Version
var strData:NSString = "1"
var intValueOfString:Int = strData.integerValue
if (intValueOfString>=11) {
NSLog("Extreme") // here instead of nslog you can write code to change lable values
}else if (intValueOfString<=10 && intValueOfString>=7){
NSLog("High")
}else if (intValueOfString<=6 && intValueOfString>=4){
NSLog("Medium")
}else{
NSLog("Low")
}
Hope this helps you.

Compare two hour strings Objective C

I have an string with an hour in it, for example "15:15", and an array of other hours in strings ex: #["15:00","16:00","17:00"] I should compare the single string with the array ones in order to get the ETA in a bus station, I tried this code but it keeps iterating and gives me the last greater value in the array, not the first greater value as I need.
int i = 0;
horaArribada = [[[objects objectAtIndex:0]objectForKey:#"Horaris"]objectAtIndex:i];
while ([hora compare:horaArribada]) {
i++;
if (i >= [[[objects objectAtIndex:0]objectForKey:#"Horaris"]count]) {
break;
}else{
horaArribada = [[[objects objectAtIndex:0]objectForKey:#"Horaris"]objectAtIndex:i];
}
}
self.tfHoraArribada.text = horaArribada;
}
}
Where objects is a query from Parse and hora the single string with an hour in it.
You appear to be doing a lot of extra work to iterate over your array. Instead, try a different format for your loop:
for (NSString *horaArribada in [[objects objectAtIndex:0] objectForKey:#"Horaris"]) {
if ([hora compare:horaArribada] == NSOrderedAscending) {
self.tfHoraArribada.text = horaArribada;
break;
}
}
This assumes that your Horaris array is already sorted such from smallest to largest. Also, the logic will not work for the midnight rollover, so you'll probably want to account for that.

creating default vars (objects) in objective c

dumb question: lets say I'm assigning a var in a conditional statement. I don't know if the condition will be satisfied and i still want the var to be defined.. whats the correct way of writing this
example:
NSDecimalNumber *number = [[NSDecimalNumber alloc]init]; // this is pointless right?
if(x == z){
number = [whatevernum1 decimalNumberByMultiplyingBy: whatevernum2];
} else {
number = [whatevernum2 decimalNumberByDividingBy: whatevernum3];
}
// do something with number variable.
There is no need to initialize number since it will be set. Just do this:
NSDecimalNumber *number;
if(x == z){
number = [whatevernum1 decimalNumberByMultiplying: whatevernum2];
} else {
number = [whatevernum2 decimalNumberByDividing: whatevernum3];
}
// do something with number variable.
In your case number will be assigned a value one way or another. But you might have a situation like this:
if (someCondition) {
// set number to value A
} else if (anotherCondition) {
// set number to value B
}
Here, it is possible that neither condition is met. In this case you need to deal with this properly by initializing number to nil.
NSDecimalNumber *number = nil;
if (someCondition) {
// set number to value A
} else if (anotherCondition) {
// set number to value B
}
if (number) {
// process result
}
You need to declare the variable but not assign it, like this:
NSDecimalNumber *number;
if(x == z){
number = [whatevernum1 decimalNumberByMultiplying: whatevernum2];
} else {
number = [whatevernum2 decimalNumberByDividing: whatevernum3];
}
This tells the compiler that you want to use a variable named number, but don't have a value for it yet. In some cases, you may find it convenient to initialise the variable to nil rather than leaving it as a null pointer.
Normally, as others have pointed out, you would either not initialise (if you can guarantee that you will set a value, eg through an if/else pair), or you would initialise to nil.
In this simple case, a ternary statement would make your code much clearer:
NSDecimalNumber *number = x == z ? [whatevernum1 decimalNumberByMultiplyingBy:whatevernum2] : [whatevernum2 decimalNumberByDividingBy:whatevernum3];

Resources