-
-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to set multiple StackBar ? #1801
Comments
It's nothing about swift. Because you add it in a wrong way. You array should have only 1 data set and the stacked data has size of 3. Take a look at ChartDemo. |
Sorry, I can't figure out how to fix this. I try to simplify my code so you may can tell me what is wrong.
|
sorry, I misunderstand your q. Just too many sceenshots to get clear. You want to show 1 normal bar and 1 stacked bar together? If so, I think you missed one line: Take below code into multiple bar chart to replace, works like a charm: - (void)setDataCount:(int)count range:(double)range
{
float groupSpace = 0.08f;
float barSpace = 0.03f;
float barWidth = 0.2f;
// (0.2 + 0.03) * 4 + 0.08 = 1.00 -> interval per "group"
NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
NSMutableArray *yVals2 = [[NSMutableArray alloc] init];
NSMutableArray *yVals3 = [[NSMutableArray alloc] init];
NSMutableArray *yVals4 = [[NSMutableArray alloc] init];
double randomMultiplier = range * 100000.f;
int groupCount = count + 1;
int startYear = 1980;
int endYear = startYear + groupCount;
for (int i = startYear; i < endYear; i++)
{
[yVals1 addObject:[[BarChartDataEntry alloc]
initWithX:i
y:(double) (arc4random_uniform(randomMultiplier))]];
[yVals2 addObject:[[BarChartDataEntry alloc]
initWithX:i
y:(double) (arc4random_uniform(randomMultiplier))]];
[yVals3 addObject:[[BarChartDataEntry alloc]
initWithX:i
y:(double) (arc4random_uniform(randomMultiplier))]];
[yVals4 addObject:[[BarChartDataEntry alloc] initWithX:i yValues:@[@((arc4random_uniform(randomMultiplier))), @((arc4random_uniform(randomMultiplier))), @((arc4random_uniform(randomMultiplier)))]]];
}
BarChartDataSet *set1 = nil, *set2 = nil, *set3 = nil, *set4 = nil;
if (_chartView.data.dataSetCount > 0)
{
set1 = (BarChartDataSet *)_chartView.data.dataSets[0];
set2 = (BarChartDataSet *)_chartView.data.dataSets[1];
set3 = (BarChartDataSet *)_chartView.data.dataSets[2];
set4 = (BarChartDataSet *)_chartView.data.dataSets[3];
set1.values = yVals1;
set2.values = yVals2;
set3.values = yVals3;
set4.values = yVals4;
BarChartData *data = _chartView.barData;
_chartView.xAxis.axisMinimum = startYear;
_chartView.xAxis.axisMaximum = [data groupWidthWithGroupSpace:groupSpace barSpace: barSpace] * _sliderX.value + startYear;
[data groupBarsFromX: startYear groupSpace: groupSpace barSpace: barSpace];
[_chartView.data notifyDataChanged];
[_chartView notifyDataSetChanged];
}
else
{
set1 = [[BarChartDataSet alloc] initWithValues:yVals1 label:@"Company A"];
[set1 setColor:[UIColor colorWithRed:104/255.f green:241/255.f blue:175/255.f alpha:1.f]];
set2 = [[BarChartDataSet alloc] initWithValues:yVals2 label:@"Company B"];
[set2 setColor:[UIColor colorWithRed:164/255.f green:228/255.f blue:251/255.f alpha:1.f]];
set3 = [[BarChartDataSet alloc] initWithValues:yVals3 label:@"Company C"];
[set3 setColor:[UIColor colorWithRed:242/255.f green:247/255.f blue:158/255.f alpha:1.f]];
set4 = [[BarChartDataSet alloc] initWithValues:yVals4 label:@"Company D"];
set4.colors = @[ChartColorTemplates.material[0], ChartColorTemplates.material[1], ChartColorTemplates.material[2]];
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
[dataSets addObject:set2];
[dataSets addObject:set3];
[dataSets addObject:set4];
BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
[data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
[data setValueFormatter:[[LargeValueFormatter alloc] init]];
// specify the width each bar should have
data.barWidth = barWidth;
// restrict the x-axis range
_chartView.xAxis.axisMinimum = startYear;
// groupWidthWithGroupSpace(...) is a helper that calculates the width each group needs based on the provided parameters
_chartView.xAxis.axisMaximum = startYear + [data groupWidthWithGroupSpace:groupSpace barSpace: barSpace] * groupCount;
[data groupBarsFromX: startYear groupSpace: groupSpace barSpace: barSpace];
_chartView.data = data;
}
} |
Thanks for your advice. Are there any methods to calculate the barWidth, barSpace, and groupWidth which their values make groupBars size equal to xAxis Label spacing ?
|
Below is my code after remove 2 datasets. You may use it to verify that whether or not my understanding and conclusion is correct. - (void)setDataCount:(int)count range:(double)range
{
float groupSpace = 0.08f;
float barSpace = 0.03f;
float barWidth = 0.2f;
// (0.2 + 0.03) * 4 + 0.08 = 1.00 -> interval per "group"
NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
NSMutableArray *yVals2 = [[NSMutableArray alloc] init];
NSMutableArray *yVals3 = [[NSMutableArray alloc] init];
NSMutableArray *yVals4 = [[NSMutableArray alloc] init];
double randomMultiplier = range * 100000.f;
int groupCount = count + 1;
// int groupCount = 12;
int startYear = 1980;
int endYear = startYear + groupCount;
for (int i = startYear; i < endYear; i++)
{
double val1 = (double) (arc4random_uniform(randomMultiplier) / 3);
double val2 = (double) (arc4random_uniform(randomMultiplier) / 3);
double val3 = (double) (arc4random_uniform(randomMultiplier) / 3);
[yVals1 addObject:[[BarChartDataEntry alloc] initWithX:i yValues:@[@(val1), @(val2), @(val3)]]];
[yVals2 addObject:[[BarChartDataEntry alloc]
initWithX:i
y:(double) (arc4random_uniform(randomMultiplier))]];
[yVals3 addObject:[[BarChartDataEntry alloc]
initWithX:i
y:(double) (arc4random_uniform(randomMultiplier))]];
[yVals4 addObject:[[BarChartDataEntry alloc]
initWithX:i
y:(double) (arc4random_uniform(randomMultiplier))]];
}
BarChartDataSet *set1 = nil, *set2 = nil, *set3 = nil, *set4 = nil;
if (_chartView.data.dataSetCount > 0)
{
set1 = (BarChartDataSet *)_chartView.data.dataSets[0];
set2 = (BarChartDataSet *)_chartView.data.dataSets[1];
set3 = (BarChartDataSet *)_chartView.data.dataSets[2];
set4 = (BarChartDataSet *)_chartView.data.dataSets[3];
set1.values = yVals1;
set2.values = yVals2;
set3.values = yVals3;
set4.values = yVals4;
BarChartData *data = _chartView.barData;
_chartView.xAxis.axisMinimum = startYear;
_chartView.xAxis.axisMaximum = [data groupWidthWithGroupSpace:groupSpace barSpace: barSpace] * _sliderX.value + startYear;
[data groupBarsFromX: startYear groupSpace: groupSpace barSpace: barSpace];
[_chartView.data notifyDataChanged];
[_chartView notifyDataSetChanged];
}
else
{
set1 = [[BarChartDataSet alloc] initWithValues:yVals1 label:@"Company A"];
// [set1 setColor:[UIColor colorWithRed:104/255.f green:241/255.f blue:175/255.f alpha:1.f]];
set1.colors = @[[UIColor colorWithRed:255/255.f green:0/255.f blue:0/255.f alpha:1.f], [UIColor colorWithRed:0/255.f green:255/255.f blue:0/255.f alpha:1.f], [UIColor colorWithRed:0/255.f green:0/255.f blue:255/255.f alpha:1.f]];
set2 = [[BarChartDataSet alloc] initWithValues:yVals2 label:@"Company B"];
[set2 setColor:[UIColor colorWithRed:164/255.f green:228/255.f blue:251/255.f alpha:1.f]];
set3 = [[BarChartDataSet alloc] initWithValues:yVals3 label:@"Company C"];
[set3 setColor:[UIColor colorWithRed:242/255.f green:247/255.f blue:158/255.f alpha:1.f]];
set4 = [[BarChartDataSet alloc] initWithValues:yVals4 label:@"Company D"];
[set4 setColor:[UIColor colorWithRed:255/255.f green:102/255.f blue:0/255.f alpha:1.f]];
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
[dataSets addObject:set2];
// [dataSets addObject:set3];
// [dataSets addObject:set4];
BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
[data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
[data setValueFormatter:[[LargeValueFormatter alloc] init]];
// specify the width each bar should have
data.barWidth = barWidth;
// restrict the x-axis range
_chartView.xAxis.axisMinimum = startYear;
// groupWidthWithGroupSpace(...) is a helper that calculates the width each group needs based on the provided parameters
_chartView.xAxis.axisMaximum = startYear + [data groupWidthWithGroupSpace:groupSpace barSpace: barSpace] * groupCount;
[data groupBarsFromX: startYear groupSpace: groupSpace barSpace: barSpace];
_chartView.data = data;
}
}
|
hmm, you may want to summerize if it's too long. Problem solved, right? |
Yes. Summary is already on the top.
The later is samples supporting my conclusion. The next comment is code modified from ChartDemo and used to show samples which you may want to verify and confirm before sharing to other developers. |
hello, please help me i have only one array for the stack bar values and i have to create stack bar chart using it. but i'm not geeing proper so help me please. hear is my code. print(arrValue)
multiple values in arrValue. and i have to set that values in same index that's why i put count += 1. |
I have 2 StackBar, each one likes this :
let barData = BarChartData(dataSets: [attemptDataSet])
let barData = BarChartData(dataSets: [resultDataSet])
This is what I get when trying to put them together into the same chart :
let barData = BarChartData(dataSets: [attemptDataSet, resultDataSet])
And this is the result I got with swift 2
I don't know how to make it work as before with swift 3.
The text was updated successfully, but these errors were encountered: