Core Plot - Bar plot automatic initial zooming - ios

How do I set an initial zoom based the maximum value of the x-axis and y-axis?
Example:
Plot 1:
Maximum value:
x-axis = 100;
y-axis = 110;
Plot 2:
Maximum value:
x-axis = 180;
y-axis = 230;
I need to the initial zoom be equal to two plots.
Regards

This will adjust the plot space to fit the plot data for two plots:
[plotSpace scaleToFitPlots:#[plot1, plot2]];
You can use this shortcut to scale the plot space to fit all of the plots in a graph:
[plotSpace scaleToFitPlots:[graph allPlots]];

Related

iOS Chart: How to set bar width to a fixed size?

I would like bar width equal to 30 pixels.
barData.barWidth let me change it but this is proportional to chart width and number of bars to display, which display a big bar on iPad for only one element.
Do you have an idea?
Thanks.
You Can not fix BarSize directly in ios-charts but you can change the default ratio width of the bar.
By default barWidth ratio is 0.85 so based on this it will cover 85% area of the chart if you have only 1 Bar on the chart and if you have 2 it will calculate the ratio based on Bar count so you can set approximate bar width with this property.
Default value:
/// **default**: 0.85
open var barWidth = Double(0.85)
You can set:
let chartData = BarChartData(dataSet: chartDataSet)
chartData.barWidth = Double(0.01)
chartData.barWidth = Double(0.10)
chartData.barWidth = Double(0.25)
chartData.barWidth = Double(0.30)
chartData.barWidth = Double(0.50)
like that you will achieve your fix width between 0.01 to 1.00.
Hope this will helps!
Assuming chart width is fixed:
step 1: find the ratio for required width in scenario where there is only one element on x-axis (for me it was 0.05)
step 2: set the bar width to this ratio multiplied by element count on x-axis
data.barWidth = 0.05 * Double(xArray.count)
This will work because max width for a single bar is inversely proportional to number of elements on x-axis
For variable chart width you will need an additional factor for bar width correction: (original chart width/current chart width)
You can add a chartView to the scrollView, then set the chartView.widthand scrollView.contentsize
width = values.count * fixedWidth
chartView.width = width
and
scrollView.contentSize = {width,scrollView.height}

How to get coordinate on the chart

I have used following way to get x-axis and y-axis co-ordinate. But I'm unable to get y axis co-ordinates.
**/*X axis max*/**
double xMaxAxisBottom = m_Chart.GetAxis().GetBottom().GetMaximum();// try to get x axis
double xMaxAxisBottomPixelPos = m_Chart.GetAxis().GetBottom().CalcXPosValue(xMaxAxisBottom); // Here trying to position based on x-axis co-ordinate
**/*X axis min*/**
double xMinAxisBottom = m_reschedChart.GetAxis().GetBottom().GetMinimum();//// try to get x axis minimum
double xMinAxisBottomPixelPos = m_Chart.GetAxis().GetBottom().MinXValue();
**/*Y axis max*/**
double xMaxAxisLeft = m_reschedChart.GetAxis().GetLeft().GetMaximum();
double xMaxAxisLeftPixelPos = m_reschedChart.GetAxis().GetLeft().MaxXValue();
**/*Y axis min*/**
double xMinAxisLeft = m_Chart.GetAxis().GetLeft().GetMinimum();
double xMinAxisLeftPixelPos = m_Chart.GetAxis().GetLeft().MinXValue();
**/*X axis length*/**
double xAxisBottomLen = m_Chart.GetAxis().GetBottom().GetEndPosition() - m_Chart.GetAxis().GetBottom().GetStartPosition();
double xAxisBottomLenPixelPos = m_Chart.GetAxis().GetBottom().CalcXPosValue(xAxisBottomLen);
**/*Y axis length*/**
double yAxisLeftLen = m_Chart.GetAxis().GetLeft().GetEndPosition() - m_Chart.GetAxis().GetLeft().GetStartPosition();
double xAxisLeftLenPixelPos = m_Chart.GetAxis().GetBottom().CalcXPosValue(yAxisLeftLen);
**/*X origin*/**
double dXstartPos = m_Chart.GetAxis().GetBottom().GetStartPosition();
double dXstartPixelPos = m_reschedChart.GetAxis().GetBottom().CalcXPosValue(dXstartPos);
**/*Y origin*/**
double dYStartPos = m_Chart.GetAxis().GetLeft().GetStartPosition();
double dYStartPixelPos = m_Chart.GetAxis().GetLeft().CalcXPosValue(dYStartPos);
let me know if i am making any mistake to find the co-ordinates.
I wanted to find below mention co-ordinated using above code.
1 X axis max
2 X axis min
3 Y axis max
4 Y axis min
5 X axis length
6 Y axis length
7 X origin
8 Y origin
9 Label font size
Please let me know your view.
Thanks
The chart needs to be drawn to use these methods. They need some internal properties to be initialized to work as expected.
You can force a chart repaint before calling them with:
m_Chart.GetEnvironment().InternalRepaint();
EDIT:
Since you seem to be calling these functions at OnAfterDraw event, you don't need to force a chart repaint. However, I'd suggest you some modifications in your code.
I see you are using m_Chart and also m_reschedChart. Make sure you are using the correct TChart variable.
Your variables start with x and y but thay also include Bottom or Left depending on the axis they refer. This is redundant and increments the chances to write a mistake (ie xMaxAxisLeft).
CalcXPosValue has to be used with Horizontal axes and CalcYPosValue with Vertical axes. So you shouldn't call GetLeft().CalcXPosValue.
CalcXPosValue and CalcYPosValue are functions to convert axis values to screen pixels.
MinXValue and MaxXValue are to be used with Horizontal Axes while MinYValue and MaxYValue are to be used with Vertical Axes.
GetMinimum returns the same than MinXValue/MinYValue, and GetMaximum returns the same than MaxXValue/MaxYValue. All these functions return Axis values, not screen pixels.
GetStartPosition and EndStartPosition are thought to modify the Axis length and by default they use percentages as explained here, so GetStartPosition - EndStartPosition is always zero. And I think CalcXPosValue(GetStartPosition - EndStartPosition) is conceptually wrong too. Note IStartPos and IEndPos give you the Start and End positions in pixels. See the TeeChart ActiveX Tutorials here.
Find below the modified code I suggest you:
**/*X axis max*/**
double maxAxisBottom = m_Chart.GetAxis().GetBottom().GetMaximum();// try to get x axis
double maxAxisBottomPixelPos = m_Chart.GetAxis().GetBottom().CalcXPosValue(xMaxAxisBottom); // Here trying to position based on x-axis co-ordinate
**/*X axis min*/**
double minAxisBottom = m_Chart.GetAxis().GetBottom().GetMinimum();//// try to get x axis minimum
double minAxisBottomPixelPos = m_Chart.GetAxis().GetBottom().CalcXPosValue(minAxisBottom);
**/*Y axis max*/**
double maxAxisLeft = m_Chart.GetAxis().GetLeft().GetMaximum();
double maxAxisLeftPixelPos = m_Chart.GetAxis().GetLeft().CalcYPosValue(maxAxisLeft);
**/*Y axis min*/**
double minAxisLeft = m_Chart.GetAxis().GetLeft().GetMinimum();
double minAxisLeftPixelPos = m_Chart.GetAxis().GetLeft().CalcYPosValue(minAxisLeft);
Now you already know the position of the four squares in screen pixels so you can ie draw a rectangle using them to check it:
m_Chart.getCanvas().Rectangle(minAxisBottomPixelPos, minAxisLeftPixelPos, maxAxisBottomPixelPos, maxAxisLeftPixelPos);
If you also want or need the sizes of the axes in pixels, you can do:
**/*X axis length*/**
double axisBottomLenPixelPos = m_Chart.GetAxis().GetBottom().GetIEndPos() - m_Chart.GetAxis().GetBottom().GetIStartPos();
**/*Y axis length*/**
double axisLeftLenPixelPos = m_Chart.GetAxis().GetLeft().GetIEndPos() - m_Chart.GetAxis().GetLeft().GetIStartPos();
And you can check they are correctly calculated:
m_Chart.getCanvas().Rectangle minAxisBottomPixelPos, maxAxisLeftPixelPos, minAxisBottomPixelPos + axisBottomLenPixelPos, maxAxisLeftPixelPos + axisLeftLenPixelPos

How to fix a gap between the x and y axes in core plot

I am using [_graph.defaultPlotSpace scaleToFitPlots:[_graph allPlots]];
and if where none of the plots has a y value of 0, there is a gap between the bottom of the Y axis and the X axis.
I've tried using _graph.axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromDouble(0.0);
as well as a calculated yMin instead of 0.0.
Yet, I still get this:
I've also tried to rescale the plot space, thus:
[_graph.defaultPlotSpace setPlotRange:r forCoordinate:CPTCoordinateY];
but couldn't figure out how to set a new minLimit for r.
Use the axisConstraints to position the x-axis at the lower edge of the plot area:
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];

Core-Plot : y Axis labels should start from 60

I need to draw a scatter plot, in which the x axis should intersect with y axis at (0,60).
I also need interval of 20 on the y Axis.
The problems I am facing right now are:
y axis is starting from 0
The gap between y axis labels example, from 60 - 80 is too much.
What I have done so far:
To set the intersection points:
x.orthogonalCoordinateDecimal = CPTDecimalFromString(#"60");
To set the major tick locations:
NSSet *majorTickLocations = [NSSet setWithObjects:[NSDecimalNumber zero],
[NSDecimalNumber numberWithUnsignedInteger:60],
[NSDecimalNumber numberWithUnsignedInteger:80],
[NSDecimalNumber numberWithUnsignedInteger:100],
[NSDecimalNumber numberWithUnsignedInteger:120],
nil];
y.majorTickLocations = majorTickLocations;
Any pointer in the direction will be helpful.
Regards,
Ishan
To make the axes cross at (0, 60), set the orthogonalCoordinateDecimal of each axis:
x.orthogonalCoordinateDecimal = CPTDecimalFromDouble(60.0);
y.orthogonalCoordinateDecimal = CPTDecimalFromDouble(0.0);
The axis labeling policy determines how the tick marks and labels are positioned. If you are providing the tick and label positions yourself (CPTAxisLabelingPolicyNone), you can put them anywhere you want. See the "Axis Labeling Policies" demo in the Plot Gallery example app for samples of each available labeling policy.

Core Plot : Plotting percentage in pie chart

Am trying out a simple Pie Chart with Core Plot in an iPhone App. I want to plot a value, which is the percentage of CPU used. In the method 'numberForPlot', here is what I have written
-(NSNumber *) numberForPlot:(CPTPlot *)plot
field:(NSUInteger)fieldEnum
recordIndex:(NSUInteger)index {
Health_Cpu *healthData = (Health_Cpu *) [dictHealth objectForKey:[self.arrayHealthKeys objectAtIndex:0]];
if (CPTPieChartFieldSliceWidth == fieldEnum) {
return [NSNumber numberWithFloat:healthData.cpuUsage]; // In Percentage
}
}
How do I tell Core Plot that this is a percentage value (i.e. if the value is 25.0, then it has to plot it as 25% i.e. color 1/4th of the pie chart)? Now, it is taking it as an absolute number. So what I have done is to return two values from numberForPlot, one being the value, and the other being (100-value).
To make a slice width of 25.0 take up 1/4 of the pie, the other slices should total 75.0. Each slice takes up a fraction of the full circle computed by x / sum(x) where x is the slice width value and sum(x) is the sum of the widths of all pie slices.
Alternatively, set the startAngle and endAngle of the pie chart. All of the slices will be drawn between those two angles, sized proportional to their values. This should work with only one slice which can have any value.

Resources