简体   繁体   English

如何使用iOS图表创建面积图?

[英]How to create area graph with ios charts?

I am trying to create an area graph with the min and max value for each points.But did not found anywhere .I guess setting the background color in the area between two line graph should solve this. 我正在尝试使用每个点的最小值和最大值创建一个面积图。但是在任何地方都找不到。我想在两个折线图之间的区域中设置背景色应该可以解决这个问题。

Tried setting fillColor for two data sets as. 尝试将两个数据集的fillColor设置为。

.fillColor = UIColor.lightGrayColor()
.drawFilledEnabled = true

I have seen from demo project.It can be done as 我从演示项目中看到过,它可以完成

    set1 = [[LineChartDataSet alloc] initWithValues:yVals1 label:@"DataSet 1"];
    set1.axisDependency = AxisDependencyLeft;
    [set1 setColor:[UIColor colorWithRed:255/255.0 green:241/255.0 blue:46/255.0 alpha:1.0]];
    set1.drawCirclesEnabled = NO;
    set1.lineWidth = 2.0;
    set1.circleRadius = 3.0;
    set1.fillAlpha = 1.0;
    set1.drawFilledEnabled = YES;
    set1.fillColor = UIColor.whiteColor;
    set1.highlightColor = [UIColor colorWithRed:244/255.0 green:117/255.0 blue:117/255.0 alpha:1.0];
    set1.drawCircleHoleEnabled = NO;
    set1.fillFormatter = [ChartDefaultFillFormatter withBlock:^CGFloat(id<ILineChartDataSet>  _Nonnull dataSet, id<LineChartDataProvider>  _Nonnull dataProvider) {
        return _chartView.leftAxis.axisMinimum;
    }];

    set2 = [[LineChartDataSet alloc] initWithValues:yVals2 label:@"DataSet 2"];
    set2.axisDependency = AxisDependencyLeft;
    [set2 setColor:[UIColor colorWithRed:255/255.0 green:241/255.0 blue:46/255.0 alpha:1.0]];
    set2.drawCirclesEnabled = NO;
    set2.lineWidth = 2.0;
    set2.circleRadius = 3.0;
    set2.fillAlpha = 1.0;
    set2.drawFilledEnabled = YES;
    set2.fillColor = UIColor.whiteColor;
    set2.highlightColor = [UIColor colorWithRed:244/255.0 green:117/255.0 blue:117/255.0 alpha:1.0];
    set2.drawCircleHoleEnabled = NO;
    set2.fillFormatter = [ChartDefaultFillFormatter withBlock:^CGFloat(id<ILineChartDataSet>  _Nonnull dataSet, id<LineChartDataProvider>  _Nonnull dataProvider) {
        return _chartView.leftAxis.axisMaximum;
    }];

But i cannot convert the syntax in swift. 但是我不能迅速转换语法。

  set2.fillFormatter = [ChartDefaultFillFormatter withBlock:^CGFloat(id<ILineChartDataSet>  _Nonnull dataSet, id<LineChartDataProvider>  _Nonnull dataProvider) {
            return _chartView.leftAxis.axisMaximum;
        }];

You don't have to set the fillFormatter , unless you need to adjust the fill-behaviour. 除非需要调整填充行为,否则不必设置fillFormatter As far as I am concerned the static withBlock: does not exist in the Swift interface. 就我而言,静态withBlock:在Swift接口中不存在。 Instead you have to create you own ChartFillFormatter implementation and implement the public func getFillLinePosition(dataSet dataSet: ILineChartDataSet, dataProvider: LineChartDataProvider) -> CGFloat function. 相反,您必须创建自己的ChartFillFormatter实现并实现public func getFillLinePosition(dataSet dataSet: ILineChartDataSet, dataProvider: LineChartDataProvider) -> CGFloat函数。

You should not have to do anything else than setting the drawFilledEnabled and fillColor on the LineChartDataSet and then of course use the LineChartView . 除了在LineChartDataSet上设置drawFilledEnabledfillColor之外,您无需执行其他任何操作,然后当然使用LineChartView

So if you really need the fill formatter you will have to do something like this: 因此,如果您确实需要填充格式化程序,则必须执行以下操作:

public class MyFillFormatter: NSObject, ChartFillFormatter
{
    private let _chartView: LineChartView
    public override init(chartView: LineChartView)
    {
        super.init()
        _chartView = chartView
    }

    public func getFillLinePosition(dataSet dataSet: ILineChartDataSet, dataProvider: LineChartDataProvider) -> CGFloat
    {
        return _chartView.leftAxis.axisMaximum
    }
}

And then set it as: 然后将其设置为:

set1.fillFormatter = MyFillFormatter(chartView: lineChartView)

There is now swift example for what you asked: 现在有您所要求的快速示例:

https://github.com/danielgindi/Charts/blob/master/ChartsDemo-iOS/Swift/Demos/LineChartFilledViewController.swift https://github.com/danielgindi/Charts/blob/master/ChartsDemo-iOS/Swift/Demos/LineChartFilledViewController.swift

Looks like this in the demo app: 在演示应用程序中看起来像这样:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM