I'm using Coreplot and I need help plotting data on a graph.
I've populated an array called results with dictionary data. Output of it's contents are as follows (I've shortened data for simplicity purposes) -
{
temperature = "5.0832667,5.2916833,4.6418246";
windspeed = "2.333,2.521,2.021,1.833,1.292,1.167";
}
I'm having trouble getting this data to plot using the method numberForPlot. Here is what I've done up until now. Below is an attempt to only plot the 'temperature' data -
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index;
return [results valueForKey:#"temperature"];
{
I'm guessing it's to do with the way I create my dictionary, as they are strings, and NSNumbers are needed? As an additional note, here is the code that created the dictionary -
for(counter = 0; counter < [node childCount]; counter++) {
[item setObject:[[node childAtIndex:counter] stringValue] forKey:[[node childAtIndex:counter] name]];
}
[results addObject:item];
Any help would be greatly appreciated as I've been trying to get this working for several days now! Thanks
Each plot type uses different "fields" to request the plot data. For example, a scatterplot uses CPTScatterPlotFieldX to request x-values and CPTScatterPlotFieldY to request y-values.
The -numberOfRecordsForPlot: method tells Core Plot how many data points are available for the plot. The -numberForPlot:field:recordIndex: method will be called once with each field value for each index in the range 0 through (n - 1).
There are a lot of example apps included with Core Plot. Look at any of them to see how to set up the datasource.
Related
I want to calculate the area inside an object which is any closed object like rectangle ,square ,polygon etc.
I can not get exact solution to do this.Please help to find it.
Thanks.
For arbitrary polygons with known vertice coordinates you can use Shoelace formula
A = 1/2 * Abs(Sum{i=0..n-1} (X[i] * Y[i+1] - X[i+1] * Y[i]) )
where for i=n-1 take i+1=0
I am using this method which will give me closed object's area.It may be helpful for you.
-(double)areaWithXpts :(NSArray *)xpts withYpts :(NSArray *)ypts totalPoints :(int)numPoints
{
double area=0;
int j=numPoints-1;
for(int i=0 ; i < numPoints;i++)
{
area=area+([[xpts objectAtIndex:j] floatValue]+[[xpts objectAtIndex:i] floatValue])*([[ypts objectAtIndex:j] floatValue]-[[ypts objectAtIndex:i] floatValue]);
j=i;
}
double areaVal=area/2;
NSLog(#"Area is :%f",areaVal);
return areaVal;
}
Usage :
Array of X points
NSArray *xArray =[NSArray arrayWithObjects:#"0", #"10", #"10", #"29",nil];
Array of Y points
NSArray *yArray =[NSArray arrayWithObjects:#"4", #"26", #"26", #"4",nil];
Total points = Array count.
Note: x array and y array both have same count.
double objectArea = [self areaWithXpts:xArray withYpts:yArray totalPoints:4];
Or you can check this answer also - Area calculation in objective c
And about area with curve shape object : you can find reference from this link - Curve shape object area reference
I already have an chart like this Chart 1
The day 9/28 and 9/30 plot are displayed continuously. I want to display 9/29 plot with no data, and expect the red line go from 9/28 to 9/30.
So I add 9/29 plot, with x = 9/29 and return value nil for 9/29 in this fuction
- (NSNumber *)numberForPlot:(CPTPlot *)plot
field:(NSUInteger)fieldEnum
recordIndex:(NSUInteger)index
But no line shows up, like this: Chart 2
Is there any way to achieve the chart I want? Thanks
Don't return values for the dates you want to skip. For the second chart, it only shows three data points (9/28, 9/30, and 10/3). Return three (3) from -numberOfRecordsForPlot: and only return data values for those dates.
I'm using the Shinobi Controls charting package on iOS and I cannot fathom how to implement a crosshair tooltip showing multiple y values. In my case I have a candlestick chart showing standard financial OHLC data values using the SChartCandlestickSeries class. The documentation seems to imply that I need to subclass an SChartSeries in order to implement some SChartData protocol methods, but I can't believe it's that involved.
I'm still struggling through the documentation here, but if anyone has some example code it would be worth its weight in gold right now!
Note: I've tried simply assigning an instance of SChartCrosshairMultiValueTooltip to the chart's crosshair.tooltip property but that doesn't seem to do very much - I just see a normal tooltip displaying a single x and y value.
It sounds like you're very much along the right lines. You need a multi-valued series (supplied by the appropriate datasource method):
- (SChartSeries *)sChart:(ShinobiChart *)chart seriesAtIndex:(int)index
{
SChartCandleStickSeries *series = [SChartCandlestickSeries new];
series.crosshairEnabled = YES;
return series;
}
And then the chart needs to have a tooltip set to an instance of the type you mentioned (SChartCrosshairMultiValueTooltip):
ShinobiChart *chart = [[ShinobiChart alloc] initWithFrame:self.view.bounds
withPrimaryXAxisType:SChartAxisTypeNumber
withPrimaryYAxisType:SChartAxisTypeNumber];
chart.datasource = self;
[self.view addSubview:chart];
chart.delegate = self;
chart.crosshair.tooltip = [SChartCrosshairMultiValueTooltip new];
For completeness, the following is the data point method of the datasource:
- (id<SChartData>)sChart:(ShinobiChart *)chart
dataPointAtIndex:(int)dataIndex
forSeriesAtIndex:(int)seriesIndex
{
SChartMultiYDataPoint *d = [SChartMultiYDataPoint new];
d.xValue = #(dataIndex);
[d.yValues setValue:_data[dataIndex] forKey:SChartCandlestickKeyOpen];
[d.yValues setValue:#([_data[dataIndex] doubleValue] * 1.3) forKey:SChartCandlestickKeyHigh];
[d.yValues setValue:#([_data[dataIndex] doubleValue] * 0.8) forKey:SChartCandlestickKeyLow];
[d.yValues setValue:#([_data[dataIndex] doubleValue] * 1.1) forKey:SChartCandlestickKeyClose];
return d;
}
(Note that the values here are just samples)
I have searched through the internet but I haven't find any straightforward answer. What I am trying to develop is basically a three same-object matching game. I have 3 UIButtons in one row. (each of these 3 buttons have a black hat icon).There are gonna be 3 unique types of hats. There are 3 rows of three items each. I want to touch the first hat and reveal a number from 0 to 2 (Let's say 1). After choosing the first hat, I want the second hat to generate a number between the 2 remained numbers( the choices are 0 and 2, let's say 2). And finally when I touch the third hat, it will generate the last remainder (number 0 in this example).The main reason for picking the numbers is because I want a certain number to represent a unique "Hat", so when I pick a hat with the number 1, a blue hat will pop out, with the number 0 a red hat etc... I have implemented the whole animation and stuff. I am just struggling in the "Unique random number picking". I am sure arrays will be a part of the "Random logic" but I haven't managed to implement it correctly... Any help would be HIGHLY appreciated :) Thank you all!
You can write a method that does this really simply with arc4random and a mutable array property that stores the numbers that have been shown as NSNumber objects.
-(NSInteger) randomNumberZeroToTwo {
NSInteger randomNumber = (NSInteger) arc4random_uniform(3); // picks between 0 and n-1 where n is 3 in this case, so it will return a result between 0 and 2
if ([self.mutableArrayContainingNumbers containsObject: [NSNumber numberWithInteger:randomNumber]])
[self randomNumberZeroToTwo] // call the method again and get a new object
} else {
// end case, it doesn't contain it so you have a number you can use
[self.mutableArrayContainingNumbers addObject: [NSNumber numberWithInteger:randomNumber]];
return randomNumber;
}
}
arc4random returns an NSUInteger so you have to cast it to avoid warnings with NSNumber.
Also make sure to instantiate your mutable array by adding this code so it does so automatically when self.mutableArrayContainingNumbers is called (i.e. lazy instantiation).
-(NSMutableArray *) mutableArrayContainingNumbers
{
if (!_mutableArrayContainingNumbers)
_mutableArrayContainingNumbers = [[NSMutableArray alloc] init];
return _mutableArrayContainingNumbers;
}
Have an array of pickable numbers.
pickable = #[0,1,2]; //Use NSNumbers but you get the idea.
/*Code to generate random number (rNum) with the range of 0-([pickable count]-1)*/
/*
Assign the number to your hat
and then remove that object from pickable
*/
[pickable removeObjectAtIndex:rNum]
and loop over that until [pickable count] == 0;
I hope that gives you a start good luck.
I am constructing an application that plots a barchart.
when i wrote this line
num=(NSDecimalNumber *)[NSDecimalNumber numberWithInt:index];
return num;
in the method
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
it works properly
but i need to pass a float number to plot
for that i wrote
num=(NSDecimalNumber *)[NSDecimalNumber numberWithFloat:f1];
return num;
then all of the bars appears together
i tried to adjust the barofset and barwidth but no solution
can anyone help me
thanks in advance
Assuming these numbers are for the CPTBarPlotFieldBarLocation field, you need to check the corresponding plot space range (xRange for vertical bars and yRange for horizontal). Make sure the location and length of the plot space range are appropriate for the range of numbers you're returning for this field. It sounds like the length is too large.