iOS中具有不同X,Y值的IOS图表?

时间:2016-08-21 10:29:55

标签: ios iphone ipad ios-charts

我有一个iPhone应用程序,我必须计算x轴和y轴的值,一些间隔为xaxis数组和Yaxis数组 我有固定的价值观。

  

然后我需要用它们的x值和Y绘制3线图   然后,我需要在该图表上绘制散点图   不同的(x,y)坐标。

     

我正在使用IOs图表库。但是在给出不同的x值时   X轴值也在变化的线图,我正在尝试   这样

`  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;
}

我是这样想的,但是当给线图中的每个数据集赋予不同的x值时,x轴根据它完全出错。我希望从不同的数组中获取x轴,并且从不同的数组中获取线图的x,y点,任何人都可以帮我实现这个目标吗?

1 个答案:

答案 0 :(得分:0)

也许问题是

chartview.xAxis.axisMaximum = data.xMax + 5.0;

因为您使用CombinedChartData,如果您应该首先致电calcMinMax()

根据你的代码,x轴应该是[0,90.976]。你的图表显示了什么?