多个条形图组间距问题

时间:2017-06-05 11:52:54

标签: ios ios-charts

我正在使用一个应用程序,其中图表用于显示报告。 我使用https://github.com/danielgindi/Charts。这里我能够绑定数据,但这些组相对于x轴放置在错误的位置。请参阅下面显示的图像。The bars placed in the charts are not grouped properly

请帮我正确分组吧。

1 个答案:

答案 0 :(得分:0)

    - (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];
    NSMutableArray *yVals5 = [[NSMutableArray alloc] init];
    NSMutableArray *yVals6 = [[NSMutableArray alloc] init];
    NSMutableArray *yVals7 = [[NSMutableArray alloc] init];
    NSMutableArray *yVals8 = [[NSMutableArray alloc] init];
    NSMutableArray *yVals9 = [[NSMutableArray alloc] init];

    NSMutableArray *xVals = [[NSMutableArray alloc] init];

   // double randomMultiplier = range * 100000.f;

    int groupCount = count+1;
    int startYear = 1980;
    //int endYear = startYear + groupCount;

    //NSArray *getFloatingVal = [[NSUserDefaults standardUserDefaults] objectForKey:@"selectedFloatValTrend"];

    for (int i = 0; i < count; i++)
    {


        if ([arrForChartsDisplay containsObject:@"Total Production"] && flagTotal==true) {
        [yVals1 addObject:[[BarChartDataEntry alloc]
                           initWithX:[[[getDataToLoadChart valueForKey:@"date"] objectAtIndex:i] doubleValue]
                           y:[[[getDataToLoadChart valueForKey:@"totalProd"] objectAtIndex:i]doubleValue]]];
        }

         if ([arrForChartsDisplay containsObject:@"eb/100sh"] && flageb==true) {
        [yVals2 addObject:[[BarChartDataEntry alloc]
                           initWithX:[[[getDataToLoadChart valueForKey:@"date"] objectAtIndex:i] doubleValue]
                           y:[[[getDataToLoadChart valueForKey:@"ebPerHunderedSh"] objectAtIndex:i] doubleValue]]];
         }

         if ([arrForChartsDisplay containsObject:@"Production Efficiency"] && flagProduction==true) {
        [yVals3 addObject:[[BarChartDataEntry alloc]
                           initWithX:[[[getDataToLoadChart valueForKey:@"date"] objectAtIndex:i] doubleValue]
                           y:[[[getDataToLoadChart valueForKey:@"productionEfficiency"] objectAtIndex:i] doubleValue]]];
         }

        if ([arrForChartsDisplay containsObject:@"End Break count"] && flagEnd==true) {
        [yVals4 addObject:[[BarChartDataEntry alloc]
                           initWithX:[[[getDataToLoadChart valueForKey:@"date"] objectAtIndex:i] doubleValue]
                           y:[[[getDataToLoadChart valueForKey:@"endBreakCount"] objectAtIndex:i] doubleValue]]];
         }

         if ([arrForChartsDisplay containsObject:@"Rouge Spindle Count"] && flagRouge==true) {
        [yVals5 addObject:[[BarChartDataEntry alloc]
                           initWithX:[[[getDataToLoadChart valueForKey:@"date"] objectAtIndex:i] doubleValue]
                           y:[[[getDataToLoadChart valueForKey:@"rougeCount"] objectAtIndex:i] doubleValue]]];
         }

         if ([arrForChartsDisplay containsObject:@"No of Doff"] && flagNoDoff==true) {
        [yVals6 addObject:[[BarChartDataEntry alloc]
                           initWithX:[[[getDataToLoadChart valueForKey:@"date"] objectAtIndex:i] doubleValue]
                           y:[[[getDataToLoadChart valueForKey:@"numberofDoffs"] objectAtIndex:i] doubleValue]]];
         }

         if ([arrForChartsDisplay containsObject:@"Startup Breaks"] && flagStartup==true) {
        [yVals7 addObject:[[BarChartDataEntry alloc]
                           initWithX:[[[getDataToLoadChart valueForKey:@"date"] objectAtIndex:i] doubleValue]
                           y:[[[getDataToLoadChart valueForKey:@"startupBreak"] objectAtIndex:i] doubleValue]]];
         }

         if ([arrForChartsDisplay containsObject:@"Slip Count"] && flagSlip==true) {
        [yVals8 addObject:[[BarChartDataEntry alloc]
                           initWithX:[[[getDataToLoadChart valueForKey:@"date"] objectAtIndex:i] doubleValue]
                           y:[[[getDataToLoadChart valueForKey:@"slipCount"] objectAtIndex:i] doubleValue]]];
         }

         if ([arrForChartsDisplay containsObject:@"Idle Count"] && flagIdle==true) {
        [yVals9 addObject:[[BarChartDataEntry alloc]
                           initWithX:[[[getDataToLoadChart valueForKey:@"date"] objectAtIndex:i] doubleValue]
                           y:[[[getDataToLoadChart valueForKey:@"idleCount"] objectAtIndex:i] doubleValue]]];
         }



        NSString *dateStr = [[getDataToLoadChart valueForKey:@"date"] objectAtIndex:i];

        // Convert string to date object
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"yyyyMMdd"];
        NSDate *date = [dateFormat dateFromString:dateStr];
        [xVals addObject:date];
    }

    BarChartDataSet *set1 = nil, *set2 = nil, *set3 = nil, *set4 = nil ,*set5 = nil, *set6 = nil, *set7 = nil, *set8 = nil,*set9 = nil;
    _chartView.data=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];
        set5 = (BarChartDataSet *)_chartView.data.dataSets[4];
        set6 = (BarChartDataSet *)_chartView.data.dataSets[5];
        set7 = (BarChartDataSet *)_chartView.data.dataSets[6];
        set8 = (BarChartDataSet *)_chartView.data.dataSets[7];
        set9 = (BarChartDataSet *)_chartView.data.dataSets[8];
        set1.values = yVals1;
        set2.values = yVals2;
        set3.values = yVals3;
        set4.values = yVals4;
        set5.values = yVals5;
        set6.values = yVals6;
        set7.values = yVals7;
        set8.values = yVals8;
        set9.values = yVals9;



        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
    {
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    if (count>0) {



    if ([arrForChartsDisplay containsObject:@"Total Production"] && flagTotal==true) {
        set1 = [[BarChartDataSet alloc] initWithValues:yVals1 label:@"Total Production"];
        [set1 setColor:[UIColor colorWithRed:104/255.f green:241/255.f blue:175/255.f alpha:1.f]];
        [dataSets addObject:set1];
    }

    if ([arrForChartsDisplay containsObject:@"eb/100sh"] && flageb==true) {
        set2 = [[BarChartDataSet alloc] initWithValues:yVals2 label:@"eb/100sh"];
        [set2 setColor:[UIColor colorWithRed:164/255.f green:228/255.f blue:251/255.f alpha:1.f]];
        [dataSets addObject:set2];
    }

    if ([arrForChartsDisplay containsObject:@"Production Efficiency"] && flagProduction==true) {
        set3 = [[BarChartDataSet alloc] initWithValues:yVals3 label:@"Production Efficiency"];
        [set3 setColor:[UIColor colorWithRed:242/255.f green:247/255.f blue:158/255.f alpha:1.f]];
        [dataSets addObject:set3];
    }

    if ([arrForChartsDisplay containsObject:@"End Break count"] && flagEnd==true) {
        set4 = [[BarChartDataSet alloc] initWithValues:yVals4 label:@"End Break count"];
        [set4 setColor:[UIColor colorWithRed:255/255.f green:102/255.f blue:0/255.f alpha:1.f]];
         [dataSets addObject:set4];
    }

    if ([arrForChartsDisplay containsObject:@"Rouge Spindle Count"] && flagRouge==true) {
        set5 = [[BarChartDataSet alloc] initWithValues:yVals5 label:@"Rouge Spindle Count"];
        [set5 setColor:[UIColor colorWithRed:255/255.f green:102/255.f blue:204/255.f alpha:1.f]];
        [dataSets addObject:set5];
    }

    if ([arrForChartsDisplay containsObject:@"No of Doff"] && flagNoDoff==true) {
        set6 = [[BarChartDataSet alloc] initWithValues:yVals6 label:@"No of Doff"];
        [set6 setColor:[UIColor colorWithRed:204/255.f green:0/255.f blue:0/255.f alpha:1.f]];
        [dataSets addObject:set6];
    }

    if ([arrForChartsDisplay containsObject:@"Startup Breaks"] && flagStartup==true) {
        set7 = [[BarChartDataSet alloc] initWithValues:yVals7 label:@"Startup Breaks"];
        [set7 setColor:[UIColor colorWithRed:0/255.f green:255/255.f blue:0/255.f alpha:1.f]];
         [dataSets addObject:set7];
    }

    if ([arrForChartsDisplay containsObject:@"Slip Count"] && flagSlip==true) {
        set8 = [[BarChartDataSet alloc] initWithValues:yVals8 label:@"Slip Count"];
        [set8 setColor:[UIColor colorWithRed:153/255.f green:153/255.f blue:102/255.f alpha:1.f]];
         [dataSets addObject:set8];
    }

    if ([arrForChartsDisplay containsObject:@"Idle Count"] && flagIdle==true) {
        set9 = [[BarChartDataSet alloc]initWithValues:yVals9 label:@"Idle Count"];
        [set9 setColor:[UIColor colorWithRed:0/255.f green:102/255.f blue:102/255.f alpha:1.f]];
        [dataSets addObject:set9];
    }

    }


     if (count>0) {

        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;

         _chartView.xAxis.valueFormatter=self;

        [data groupBarsFromX: startYear groupSpace: groupSpace barSpace: barSpace];
         //(0.2 + 0.03) * 4 + 0.08
    _chartView.data = data;
     }
    }
}