0
votes

I'm trying to construct bar chart, with variable number of y-values for x-values (for example, for x=1 I want to have 3 data entries, for x=2 1 data entry etc).

Using ios-charts, problem is that chart for some reason places bars incorrectly. Using following data source for my graph:

    BarChartDataEntry *entry = [[BarChartDataEntry alloc] initWithValues:@[@1] xIndex:0];
    BarChartDataEntry *entry2 = [[BarChartDataEntry alloc] initWithValues:@[@4] xIndex:0];
    BarChartDataSet *set = [[BarChartDataSet alloc] initWithYVals:@[entry,entry2] label:@"test1"];

    BarChartDataEntry *entry3 = [[BarChartDataEntry alloc] initWithValues:@[@6] xIndex:1];
    BarChartDataEntry *entry4 = [[BarChartDataEntry alloc] initWithValues:@[@7] xIndex:1];
    BarChartDataSet *set2 = [[BarChartDataSet alloc] initWithYVals:@[entry3,entry4] label:@"test2"];

    BarChartDataEntry *entry5 = [[BarChartDataEntry alloc] initWithValues:@[@3] xIndex:2];
    BarChartDataSet *set3 = [[BarChartDataSet alloc] initWithYVals:@[entry5] label:@"test3"];

    NSArray *tstArr = @[set, set2, set3];

   //x-values
   <__NSArrayM 0x7a897960>(
   02.júla.15,
   23.júla.15,
   24.júla.15
   )

    BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:tstArr];

produces following HorizontalBarChart:

graph

Looking at the documentation, I'm not even sure if this is possible to do with ios-charts. Maybe auto-layout has something to do with it. If someone with experience using this library can contribute, I would be glad. Alternatively, I will probably have to use Core plot.

EDIT: I noticed that my logic was wrong and I created stacked bars instead of multiple. I changed my code to

    BarChartDataEntry *entry = [[BarChartDataEntry alloc] initWithValues:@[@1] xIndex:0];
    BarChartDataEntry *entry2 = [[BarChartDataEntry alloc] initWithValues:@[@4] xIndex:0];
    BarChartDataSet *set = [[BarChartDataSet alloc] initWithYVals:@[entry] label:@"test1"];
    BarChartDataSet *set2 = [[BarChartDataSet alloc] initWithYVals:@[entry2] label:@"test2"];

    BarChartDataEntry *entry3 = [[BarChartDataEntry alloc] initWithValues:@[@6] xIndex:1];
    BarChartDataEntry *entry4 = [[BarChartDataEntry alloc] initWithValues:@[@7] xIndex:1];
    BarChartDataSet *set3 = [[BarChartDataSet alloc] initWithYVals:@[entry3] label:@"test3"];
    BarChartDataSet *set4 = [[BarChartDataSet alloc] initWithYVals:@[entry4] label:@"test4"];

So I'm having multiple data sets for single x-point. This works a bit better, but chart still does not understand my data source and puts everything into single x-point (notice the black-selected column)

chart2

1

1 Answers

1
votes

It is a bug, and, I think you have opened this issue on github, right? Anyway, you should be able to proceed with the fix: https://github.com/danielgindi/ios-charts/pull/248.

if there are two bars in one group, there should be only two dataSets, not 4, and each dataSet has two dataEntries. What you are using has 4 dataSets, and it leads to more space left for each x axis zone, that's why it is not centered.