I am using ios-charts library (Ios Charts) for graphs (line , bar).my issue is that few cases y axis labels are not showing.
please help me to resolve this.
here what my code is
ChartYAxis *leftAxis = _barChartView.leftAxis;
leftAxis.startAtZeroEnabled = YES;
leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
leftAxis.labelCount = 8;
leftAxis.valueFormatter = [[NSNumberFormatter alloc] init];
leftAxis.valueFormatter.maximumFractionDigits = 1;
leftAxis.axisLineColor = [UIColor blackColor];
leftAxis.axisLineWidth = 1.0;
leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
leftAxis.spaceTop = 0.15;
BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:#"Legend"];
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
BarChartData *data = [[BarChartData alloc] initWithXVals:xvalues dataSets:dataSets];
[data setValueFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:10.f]];
_barChartView.leftAxis.startAtZeroEnabled = NO;
_barChartView.data = data;
The below solved my problem.
leftAxis.labelCount = 5;
leftAxis.forceLabelsEnabled = YES;
Related
I'm using this library to make charts. Everything works fine if there are several bars but when I only have one, it seems that the value cannot be shown. This is the chart if there is only one bar (image)
But when I put two bars the values are visible.
My code is this:
-(void)setupChart{
myQuota = [[DataManager shared] myQuota];
[self setupBarLineChartView:_chartView];
_chartView.delegate = self;
_chartView.drawBarShadowEnabled = NO;
_chartView.drawValueAboveBarEnabled = YES;
_chartView.maxVisibleValueCount = [myQuota.quota doubleValue] * 2;
ChartXAxis *xAxis = _chartView.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.labelFont = [UIFont systemFontOfSize:10.f];
xAxis.drawAxisLineEnabled = YES;
xAxis.drawGridLinesEnabled = YES;
xAxis.gridLineWidth = .3;
ChartYAxis *leftAxis = _chartView.leftAxis;
leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
leftAxis.drawAxisLineEnabled = YES;
leftAxis.drawGridLinesEnabled = YES;
leftAxis.drawLabelsEnabled = NO; // don't write label values
leftAxis.gridLineWidth = .3;
leftAxis.axisMinValue = 0.0; // this replaces startAtZero = YES
ChartYAxis *rightAxis = _chartView.rightAxis;
rightAxis.enabled = YES;
rightAxis.labelFont = [UIFont systemFontOfSize:10.f];
rightAxis.drawAxisLineEnabled = YES;
rightAxis.drawGridLinesEnabled = NO;
rightAxis.axisMinValue = 0.0; // this replaces startAtZero = YES
rightAxis.yOffset = 1.0;
rightAxis.axisMaxValue = [myQuota.quota doubleValue] * 2;
[rightAxis removeAllLimitLines];
ChartLimitLine *limit = [[ChartLimitLine alloc] initWithLimit:[myQuota.quota doubleValue] label:[NSString stringWithFormat:#"%d",[myQuota.quota integerValue] ]];
[rightAxis addLimitLine:limit];
rightAxis.valueFormatter = [[NSNumberFormatter alloc] init];
rightAxis.valueFormatter.minimumFractionDigits = 0;
_chartView.doubleTapToZoomEnabled = NO;
_chartView.highlightPerTapEnabled = NO;
_chartView.legend.enabled = NO;
[_chartView animateWithYAxisDuration:1.5];
[self setDataCount:(1) range: ([myQuota.quota doubleValue]*2)];
}
- (void)setDataCount:(int)count range:(double)range
{
NSMutableArray *xVals = [[NSMutableArray alloc] init];
[xVals addObject:myQuota.rowName];
[xVals addObject:myQuota.rowName];
NSMutableArray *yVals = [[NSMutableArray alloc] init];
double val = [myQuota.quotaReached doubleValue];
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:([myQuota.quota doubleValue]*2) xIndex:0]];
BarChartDataSet *set1 = nil;
if (_chartView.data.dataSetCount > 0)
{
set1 = (BarChartDataSet *)_chartView.data.dataSets[0];
set1.yVals = yVals;
_chartView.data.xValsObjc = xVals;
[_chartView notifyDataSetChanged];
}
else
{
set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:#""];
set1.barSpace = 0.35;
set1.colors = [NSArray arrayWithObjects:[UIColor greenColor], nil]; // set color
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];
[data setValueFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:10.f]];
//_chartView.xAxis.labelPosition = Top
_chartView.data = data;
}
}
I had an iPhone application in which i have to calculate the values for x axis and y axis with some intervals as xaxis array and Yaxis array
I had fixed values.
Then i need to draw 3 line graph with their on x values and Y
values.then i need to plot scatter points on that graph itself with
different (x,y) coordinates.
I am using IOs charts library.But when giving different x values to
the line graph the X axis values are also changing, I am trying in
this way
` chartview.descriptionText = #"";
chartview.noDataTextDescription = #"You need to provide data for the chart.";
chartview.drawGridBackgroundEnabled = NO;
chartview.drawBarShadowEnabled = NO;
chartview.highlightFullBarEnabled = NO;
chartview.rightAxis.enabled=NO;
chartview.drawOrder = #[#(CombinedChartDrawOrderLine),
#(CombinedChartDrawOrderScatter),
];
ChartLegend *l = chartview.legend;
l.enabled = false;
//chartview.xAxisRenderer
ChartYAxis *leftAxis = chartview.leftAxis;
leftAxis.axisLineWidth=2.0f;
leftAxis.axisLineColor =[UIColor whiteColor];
leftAxis.drawGridLinesEnabled = YES;
leftAxis.gridColor=[UIColor whiteColor];
leftAxis.gridLineDashLengths = #[#5.f, #5.f];
NSLog(#"%#",yaxispoints);
NSLog(#"%f",[[yaxispoints firstObject] floatValue]);
NSLog(#"%f",[[yaxispoints lastObject] floatValue]);
leftAxis.axisMinimum = [[yaxispoints firstObject] floatValue]; // this replaces startAtZero = YES
leftAxis.axisMaximum = [[yaxispoints lastObject] floatValue];
leftAxis.labelCount = [yaxispoints count];
leftAxis.spaceTop = 40.0;
leftAxis.labelTextColor=[UIColor whiteColor];
ChartXAxis *xAxis = chartview.xAxis;
[xAxis setDrawAxisLineEnabled:YES];
xAxis.axisLineWidth=2.0f;
xAxis.granularityEnabled=YES;
xAxis.axisLineColor =[UIColor whiteColor];
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.axisMinimum = [[months firstObject] floatValue]; // this replaces startAtZero = YES
xAxis.axisMaximum = [[months lastObject] floatValue];
xAxis.labelCount = [months count];
xAxis.forceLabelsEnabled=YES;
xAxis.drawGridLinesEnabled = YES;
xAxis.gridColor=[UIColor whiteColor];
xAxis.gridLineDashLengths = #[#5.f, #5.f];
xAxis.valueFormatter = self;
xAxis.labelTextColor=[UIColor whiteColor];
[self updateChartData];
[chartview animateWithXAxisDuration:1.5 yAxisDuration:1.5];`
- (void)updateChartData
{
[self setChartData];
}
- (void)setChartData
{
CombinedChartData *data = [[CombinedChartData alloc] init];
data.lineData = [self generateLineData];
data.scatterData = [self generateScatterData];
chartview.xAxis.axisMaximum = data.xMax + 5.0;
chartview.data = data;
}
- (LineChartData *)generateLineData
{
LineChartData *d = [[LineChartData alloc] init];
NSMutableArray *entries = [[NSMutableArray alloc] init];
NSArray *scxaxis = #[#"55.987", #"75.976", #"85.976"
];
NSArray *scyaxis = #[#"85.987", #"25.976", #"95.976"
];
for (int index = 0; index < [scxaxis count]; index++)
{
[entries addObject:[[ChartDataEntry alloc] initWithX:[[scxaxis objectAtIndex:index] doubleValue] y:[[scyaxis objectAtIndex:index] doubleValue]]];
}
LineChartDataSet *set = [[LineChartDataSet alloc] initWithValues:entries label:#"Line DataSet"];
[set setColor:[UIColor greenColor]];
set.lineWidth = 3.0;
// [set setCircleColor:[UIColor redColor]];
//set.circleRadius = 0.0;
// set.circleHoleRadius = 0.0;
set .drawCirclesEnabled=NO;
set.fillColor = [UIColor clearColor];
set.mode = LineChartModeLinear;
set.drawValuesEnabled = NO;
set.valueFont = [UIFont systemFontOfSize:14.f];
set.valueTextColor = [UIColor whiteColor];
set.axisDependency = AxisDependencyLeft;
[d addDataSet:set];
return d;
}
I am trying like this,But when giving different x values to each data set in line graph the x axis is completely going wrong according to it. i want the x axis to be fetched from different array and x,y points of line graph to be from different array, Can anybody help me on achieving this ?
Maybe the problem is
chartview.xAxis.axisMaximum = data.xMax + 5.0;
Cause you are using CombinedChartData,if the you should call calcMinMax() at first.
And accord to your code ,the x axis should be in [0,90.976].What your graph shows?
I've used the ios-charts project on github and used the HorizontalCharts example. It works good in the example but when I try to set the horizontal bar value, the bar just gets too long. Example visible here:
My code is this:
-(void)setupChart{
myQuota = [[DataManager shared] myQuota];
[self setupBarLineChartView:_chartView];
_chartView.delegate = self;
_chartView.drawBarShadowEnabled = NO;
_chartView.drawValueAboveBarEnabled = YES;
_chartView.maxVisibleValueCount = 60;
ChartXAxis *xAxis = _chartView.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.labelFont = [UIFont systemFontOfSize:10.f];
xAxis.drawAxisLineEnabled = YES;
xAxis.drawGridLinesEnabled = YES;
xAxis.gridLineWidth = .3;
ChartYAxis *leftAxis = _chartView.leftAxis;
leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
leftAxis.drawAxisLineEnabled = YES;
leftAxis.drawGridLinesEnabled = YES;
leftAxis.drawLabelsEnabled = NO;
leftAxis.gridLineWidth = .3;
leftAxis.axisMinValue = 0.0;
ChartYAxis *rightAxis = _chartView.rightAxis;
rightAxis.enabled = YES;
rightAxis.labelFont = [UIFont systemFontOfSize:10.f];
rightAxis.drawAxisLineEnabled = YES;
rightAxis.drawGridLinesEnabled = NO;
rightAxis.axisMinValue = 0.0;
rightAxis.yOffset = 1.0;
rightAxis.axisMaxValue = [myQuota.quota doubleValue]; // here is 10
_chartView.doubleTapToZoomEnabled = NO;
_chartView.highlightPerTapEnabled = NO;
_chartView.legend.enabled = NO;
[_chartView animateWithYAxisDuration:2.5];
[self updateChartData];
}
- (void)updateChartData
{
if (self.shouldHideData)
{
_chartView.data = nil;
return;
}
if(myQuota != nil && myQuota.quota != nil)
{
[self setDataCount:(1) range: [myQuota.quota doubleValue]];
}
}
- (void)setDataCount:(int)count range:(double)range
{
NSMutableArray *xVals = [[NSMutableArray alloc] init];
[xVals addObject:myQuota.rowName];
NSMutableArray *yVals = [[NSMutableArray alloc] init];
double val = [myQuota.quotaReached doubleValue]; // here is 2
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:0]];
BarChartDataSet *set1 = nil;
if (_chartView.data.dataSetCount > 0)
{
set1 = (BarChartDataSet *)_chartView.data.dataSets[0];
set1.yVals = yVals;
_chartView.data.xValsObjc = xVals;
[_chartView notifyDataSetChanged];
}
else
{
set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:#""];
set1.barSpace = 0.35;
set1.colors = [NSArray arrayWithObjects:[UIColor greenColor], nil];
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];
[data setValueFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:10.f]];
_chartView.data = data;
}
}
Why is the bar so long?
Okay, since nobody offered an answer I tried a HACK. I've added an invisible bar/row for which I set the value to the maximum of the bar chart. This way the axis shows the maximum even though the bar is invisible.
I welcome anybody who knows a better way but until then I'll do it like this:
- (void)setDataCount:(int)count range:(double)range{
MY_MAX_VALUE = 10; // put HERE your MAX value for the axis
NSMutableArray *xVals = [[NSMutableArray alloc] init];
[xVals addObject:myQuota.rowName];
NSMutableArray *yVals = [[NSMutableArray alloc] init];
double val = [myQuota.quotaReached doubleValue]; // here is 2
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:0]];
[yVals addObject:[[BarChartDataEntry alloc] initWithValue: MY_MAX_VALUE xIndex:1]];
}
I have a view controller that's just a full screen ios-chart view nested in a nav controller. This was working in October and I've looked through my git repo and nothing has changed about this file. However I did pull in the latest version from the repo and I achieved the same results.
So first I have a setup function that sets up chart:
if (vertical)
{
self.barChart = [[BarChartView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
}
else
{
self.barChart = [[HorizontalBarChartView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
}
self.barChart.backgroundColor = [UIColor clearColor];
self.barChart.delegate = self;
self.barChart.descriptionText = #"";
self.barChart.noDataTextDescription = #"";
self.barChart.drawBarShadowEnabled = NO;
self.barChart.drawValueAboveBarEnabled = YES;
self.barChart.rightAxis.drawLabelsEnabled = NO;
self.barChart.rightAxis.drawGridLinesEnabled = NO;
self.barChart.maxVisibleValueCount = 60;
self.barChart.pinchZoomEnabled = NO;
self.barChart.drawGridBackgroundEnabled = NO;
self.barChart.backgroundColor = [UIColor whiteColor];
ChartXAxis *xAxis = self.barChart.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.labelFont = [UIFont systemFontOfSize:10.f];
xAxis.drawGridLinesEnabled = NO;
xAxis.spaceBetweenLabels = 0;
ChartYAxis *leftAxis = self.barChart.leftAxis;
leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
leftAxis.labelCount = 3;
leftAxis.drawGridLinesEnabled = YES;
leftAxis.valueFormatter = [[NSNumberFormatter alloc] init];
leftAxis.valueFormatter.maximumFractionDigits = 0;
leftAxis.valueFormatter.positivePrefix = #"$";
leftAxis.valueFormatter.negativePrefix = #"-$";
leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
leftAxis.spaceTop = 0.25;
[self.barChart.legend setEnabled:false];
[self refresh]; //refreshes data for charting
Refresh on self grabs the JSON off my website and forms it into an array of dictionaries and then calls this:
NSMutableArray *xAxis = [[NSMutableArray alloc] init];
NSMutableArray *yAxis = [[NSMutableArray alloc] init];
for (int x = 0; x < data.count; x++) {
[xAxis addObject:data[x][#"title"]];
[yAxis addObject:[[BarChartDataEntry alloc] initWithValue:floor([data[x][#"value"] doubleValue]) xIndex:x]];
}
BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yAxis label:self.barTitle];
set1.barSpace = 0.35;
set1.colors = #[[[UIColor alloc] initWithRed:0.007843137255 green:0.2117647059 blue:0.4470588235 alpha:1]];
set1.valueFormatter = [[NSNumberFormatter alloc] init];
set1.valueFormatter.positivePrefix = #"$";
set1.valueFormatter.negativePrefix = #"-$";
[set1.valueFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
set1.valueFormatter.maximumFractionDigits = 0;
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
BarChartData *barData = [[BarChartData alloc] initWithXVals:xAxis dataSets:dataSets];
[barData setValueFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:10.f]];
self.barChart.data = barData;
The dataset is correct and gets all 24 values and appears to setup the xAxis and yAxis correctly but after all this, my chart just displays "No chart data available"
I've searched around and didn't find many articles on this but one suggestion was use dispatch_after to run setNeedsDisplay on the bar chart:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.barChart setNeedsDisplay];
});
I've tried that code with 30 seconds as my timer to be absolutely sure the chart library finished doing what it needs to with that data and still the chart shows no data.
Any and all help would be appreciated. Thanks in advance!
I've got a pie chart being generated through Coreplot. It's put a black line all the way around the chart and through all the segments like so:
I generate the chart with the following code:
-(CPTFill *)sliceFillForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index
{
CPTFill *areaGradientFill ;
if (index==0)
return areaGradientFill= [CPTFill fillWithColor:[CPTColor colorWithComponentRed:0.0f/255.0f green:111.0f/255.0f blue:115.0f/255.0f alpha:1.0f]];
else if (index==1)
return areaGradientFill= [CPTFill fillWithColor:[CPTColor colorWithComponentRed:171.0f/255.0f green:213.0f/255.0f blue:199.0f/255.0f alpha:1.0f]];
else if (index==2)
return areaGradientFill= [CPTFill fillWithColor:[CPTColor yellowColor]];
return areaGradientFill;
}
-(void)constructPieChart:(NSString *)datain;
{
NSArray *strings = [datain componentsSeparatedByString:#","];
dataforcharts = [datain componentsSeparatedByString:#","];
NSLog(#"dataforcharts: %#", dataforcharts);
// Create pieChart from theme
pieGraph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
[pieGraph applyTheme:theme];
pieChartView.hostedGraph = pieGraph;
pieGraph.plotAreaFrame.borderLineStyle = nil;
pieGraph.plotAreaFrame.masksToBorder = NO;
pieGraph.paddingLeft = 20.0;
pieGraph.paddingTop = 20.0;
pieGraph.paddingRight = 20.0;
pieGraph.paddingBottom = 20.0;
pieGraph.axisSet = nil;
// Prepare a radial overlay gradient for shading/gloss
CPTGradient *overlayGradient = [[CPTGradient alloc] init];
overlayGradient.gradientType = CPTGradientTypeRadial;
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.0] atPosition:0.0];
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.3] atPosition:0.9];
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.7] atPosition:1.0];
// Add pie chart
piePlot = [[CPTPieChart alloc] init];
piePlot.dataSource = self;
piePlot.pieRadius = 100.0;
piePlot.pieInnerRadius = 50.0;
piePlot.identifier = #"Pie Chart 1";
piePlot.startAngle = M_PI_4;
// piePlot.sliceDirection = CPTPieDirectionCounterClockwise;
piePlot.borderLineStyle = [CPTLineStyle lineStyle];
piePlot.labelOffset = -40.0;
piePlot.overlayFill = [CPTFill fillWithGradient:overlayGradient];
[pieGraph addPlot:piePlot];
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.color = [CPTColor blackColor];
textStyle.fontName = #"Helvetica-Bold";
textStyle.fontSize = 16.0f;
pieGraph.title=#"Chart1";
pieGraph.titleTextStyle = textStyle;
pieGraph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
pieGraph.titleDisplacement = CGPointMake(0.0f, -1.0f);
int i = [[strings objectAtIndex:0] intValue];
int f = [[strings objectAtIndex:1] intValue];
NSLog(#"Int1: %i", i);
NSLog(#"Int2: %i", f);
// Add some initial data
NSMutableArray *contentArray = [NSMutableArray arrayWithObjects:[NSNumber numberWithDouble:i], [NSNumber numberWithDouble:f], nil];
self.dataForChart = contentArray;
CPTLegend *theLegend = [CPTLegend legendWithGraph:pieGraph];
theLegend.numberOfColumns = 2;
theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
theLegend.borderLineStyle = [CPTLineStyle lineStyle];
theLegend.cornerRadius = 5.0;
pieGraph.legend = theLegend;
pieGraph.legendAnchor = CPTRectAnchorBottom;
pieGraph.legendDisplacement = CGPointMake(0.0, -0.0);
}
and I'd like to remove the black lines around the chart. I've already tried setting the borderLineStyle to nil but this doesn't seem to have any effect.
Any help is appreciated.
Thanks!
Adam
Use this and debug again, it's working.
piePlot.borderLineStyle = nil;