Costello Costello - 2 months ago 23
Swift Question

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 = 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;
}];

Answer

You don't have to set the fillFormatter, unless you need to adjust the fill-behaviour. As far as I am concerned the static withBlock: does not exist in the Swift interface. Instead you have to create you own ChartFillFormatter implementation and implement the public func getFillLinePosition(dataSet dataSet: ILineChartDataSet, dataProvider: LineChartDataProvider) -> CGFloat function.

You should not have to do anything else than setting the drawFilledEnabled and fillColor on the LineChartDataSet and then of course use the 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)
Comments