DGCharts使用
lazy var chartView: LineChartView = {
let chartLineView = LineChartView.init()
chartLineView.delegate = nhtChartDele;
self.addSubview(chartLineView);
chartLineView.backgroundColor = UIColor.clear;
chartLineView.legend.enabled = false;
chartLineView.frame = self.bounds
let leftAxis = chartLineView.leftAxis;
self.setXYLineInfo(axisBase: leftAxis);
#warning("必须设置leftAxis.drawTopYLabelEntryEnabled = true;leftAxis.drawBottomYLabelEntryEnabled = true; ,否则上下放大到一定程度会导致崩溃 ")
leftAxis.drawTopYLabelEntryEnabled = true;
leftAxis.drawBottomYLabelEntryEnabled = true;
leftAxis.labelPosition = YAxis.LabelPosition.outsideChart;
chartLineView.rightAxis.enabled = false;
let xAxis = chartLineView.xAxis;
self.setXYLineInfo(axisBase: xAxis);
xAxis.labelPosition = XAxis.LabelPosition.bottom;
if (self.showXCustomValue){
xAxis.valueFormatter = nhtChartDele;
}
xAxis.labelRotationAngle = self.labelRotationAngle;
leftAxis.drawGridLinesEnabled = true;
xAxis.drawGridLinesEnabled = true;
leftAxis.axisLineColor = UIColor.gray.withAlphaComponent(0.3)
leftAxis.gridColor = UIColor.gray.withAlphaComponent(0.3)
xAxis.gridColor = UIColor.gray.withAlphaComponent(0.3)
leftAxis.gridLineWidth = leftAxis.axisLineWidth;
xAxis.gridLineWidth = leftAxis.axisLineWidth;
if(self.showMarkerView){
let marker = BalloonMarker.init(color: self.markerColorShow, font: UIFont.systemFont(ofSize: 12), textColor: UIColor.white, insets: UIEdgeInsets(top: 8, left: 8, bottom: 20, right: 8));
marker.chartView = chartLineView;
marker.minimumSize = CGSize(width: 30, height: 30);
chartLineView.marker = marker;
}
return chartLineView
}()
private func setXYLineInfo(axisBase:AxisBase) -> Void {
axisBase.removeAllLimitLines()
#warning ("注意 如果不设置axisMaximum axisMinimum,系统会自动根据数据进行显示")
axisBase.gridLineDashLengths = [3.0, 3.0];
axisBase.gridLineWidth = 6;
axisBase.labelTextColor = UIColor.gray.withAlphaComponent(0.5);
axisBase.gridColor = UIColor.gray.withAlphaComponent(0.5);
axisBase.granularityEnabled = true;
axisBase.axisLineColor = self.axisLineColor;
axisBase.labelTextColor = self.axisLineColor;
axisBase.labelFont = UIFont.systemFont(ofSize: 12);
axisBase.axisLineWidth = 1;
axisBase.drawGridLinesEnabled = false;
axisBase.gridLineWidth = 1.0;
axisBase.drawLimitLinesBehindDataEnabled = true;
axisBase.gridAntialiasEnabled = false;
#warning ("注意 如果drawBottomYLabelEntryEnabled = NO 可能导致最下面的刻度不显示")
}
private func setDataSet(values:[ChartDataEntry],key:String) -> LineChartDataSet {
let set1 = LineChartDataSet.init(entries: values, label: key);
set1.drawIconsEnabled = false;
set1.setColor((self.lineColorAll[key] as? UIColor ?? LineColorDefault))
set1.setCircleColor(UIColor.black) ;
set1.valueFont = UIFont.systemFont(ofSize: 12);
set1.valueTextColor = UIColor.black;
set1.drawValuesEnabled = false;
set1.highlightColor = UIColor.gray;
set1.highlightEnabled = true;
set1.drawCirclesEnabled = false;
set1.mode = .cubicBezier;
return set1;
}
Charts使用
#pragma mark---折线图 遵循代理 <ChartViewDelegate,IChartAxisValueFormatter,IChartFillFormatter,IChartValueFormatter>
#pragma mark---折线图
-(ChartLimitLine *)createLilitLineLine:(CGFloat)limit label:(NSString *)label{
ChartLimitLine *limitLine = [[ChartLimitLine alloc] initWithLimit:limit label:label];
limitLine.labelPosition = ChartLimitLabelPositionTopLeft;
limitLine.valueFont = [UIFont systemFontOfSize:9];
limitLine.valueTextColor = rgba(90, 107, 200, 1);
limitLine.lineWidth = 1.0;
limitLine.lineColor = rgba(236, 105, 65, 1);
limitLine.lineDashLengths = @[@5.f, @5.f];
return limitLine;
}
-(void)showChart{
_chartView = [[LineChartView alloc] init];
_chartView.delegate = self;
_chartView.frame = CGRectMake(WMAKENEW(7), WMAKENEW(25), SCREEN_WIDTH-WMAKENEW(12)-WMAKENEW(12)-WMAKENEW(12), self.oneSpaceWidth*5);
[self.backView addSubview:_chartView];
_chartView.chartDescription.enabled = NO;
_chartView.dragEnabled = YES;
[_chartView setScaleEnabled:YES];
_chartView.pinchZoomEnabled = YES;
_chartView.drawGridBackgroundEnabled = NO;
ChartYAxis *leftAxis = _chartView.leftAxis;
[self setXYLineInfo:leftAxis];
#pragma mark---特殊部分
[leftAxis addLimitLine:[self createLilitLineLine:90.0 label:@"上面的线"]];
leftAxis.drawTopYLabelEntryEnabled = YES;
#warning 注意 如果drawBottomYLabelEntryEnabled = NO 可能导致最下面的刻度不显示
leftAxis.drawBottomYLabelEntryEnabled = YES;
leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
_chartView.rightAxis.enabled = NO;
BalloonMarker *marker = [[BalloonMarker alloc]
initWithColor: rgba(87, 174, 255, 1)
font: FONT(12)
textColor: UIColor.whiteColor
insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0)];
marker.chartView = _chartView;
marker.minimumSize = CGSizeMake(40.f, 40.f);
_chartView.marker = marker;
_chartView.legend.form = ChartLegendFormNone;
_chartView.legend.enabled = NO;
[_chartView animateWithXAxisDuration:1];
ChartXAxis *xAxis = _chartView.xAxis;
[self setXYLineInfo:xAxis];
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.valueFormatter = self;
xAxis.labelFont = FONT(10);
xAxis.labelRotationAngle = 25;
xAxis.avoidFirstLastClippingEnabled = YES;
_chartView.extraRightOffset = WMAKENEW(20);
leftAxis.drawGridLinesEnabled = xAxis.drawGridLinesEnabled = YES;
leftAxis.gridColor = xAxis.gridColor = leftAxis.axisLineColor;
leftAxis.gridLineWidth = xAxis.gridLineWidth = leftAxis.axisLineWidth;
_chartView.noDataText = @"暂无数据";
_chartView.noDataTextColor = UIColor.redColor;
_chartView.noDataFont = FONT(14);
}
-(void)setXYLineInfo:(ChartAxisBase *)axisBase{
[axisBase removeAllLimitLines];
#warning 注意 如果不设置axisMaximum axisMinimum,系统会自动根据数据进行显示
axisBase.gridLineDashLengths = @[@5.f, @5.f];
axisBase.gridLineWidth = 6;
axisBase.gridColor = rgba(90, 107, 200, 1);
axisBase.labelTextColor = rgba(90, 107, 200, 1);
axisBase.granularityEnabled = YES;
axisBase.granularity = 1;
axisBase.axisLineColor = rgba(90, 107, 200, 1);
axisBase.axisLineWidth = 1;
axisBase.drawGridLinesEnabled = NO;
axisBase.gridLineWidth = 1;
axisBase.gridLineCap = 10;
axisBase.drawLimitLinesBehindDataEnabled = YES;
axisBase.gridAntialiasEnabled = NO;
axisBase.valueFormatter = self;
#warning 注意 如果drawBottomYLabelEntryEnabled = NO 可能导致最下面的刻度不显示
}
#pragma mark - ChartViewDelegate
- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
{
NSLog(@"chartValueSelected");
}
- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
NSLog(@"chartValueNothingSelected");
}
#pragma mark - IChartValueFormatter delegate (折线值) 自定义显示所画的每个点的值
- (NSString *)stringForValue:(double)value entry:(ChartDataEntry *)entry dataSetIndex:(NSInteger)dataSetIndex viewPortHandler:(ChartViewPortHandler *)viewPortHandler {
if (value>60 || value<20) {
return nil;
}else{
return nil;
}
}
#pragma mark - IChartAxisValueFormatter delegate (y轴值) (x轴的值写在DateValueFormatter类里, 都是这个协议方法)
- (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis {
if ([axis isKindOfClass:[ChartYAxis class]]) {
return [NSString stringWithFormat:@"%.f", value];
}else{
return [NSString stringWithFormat:@"%.f", value];
}
}
-(CGFloat)getFillLinePositionWithDataSet:(id<ILineChartDataSet>)dataSet dataProvider:(id<LineChartDataProvider>)dataProvider{
return 1;
}
-(void)showNeedData:(NSArray *)values{
NSArray *oldDataSet = [_chartView.data.dataSets mutableCopy];
if (oldDataSet.count>0) {
for (LineChartDataSet *subData in oldDataSet) {
[_chartView.data removeDataSet:subData];
}
[_chartView setNeedsDisplay];
}
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
for (NSArray *subArr in values) {
[dataSets addObject:[self setDataSet:subArr]];
}
LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets];
_chartView.data = data;
if (self.isFirstShow) {
self.oneTimer.fireDate = [NSDate distantPast];
}
[_chartView setNeedsDisplay];
}
-(LineChartDataSet *)setDataSet:(NSArray *)values{
LineChartDataSet *set1 = nil;
set1.mode = LineChartModeCubicBezier;
set1 = [[LineChartDataSet alloc] initWithEntries:values label:@""];
set1.drawIconsEnabled = NO;
[set1 setColor:rgba(87, 174, 255, 1)];
[set1 setCircleColor:rgba(87, 174, 255, 1)];
set1.drawValuesEnabled = false;
set1.valueFont = FONT(9);
set1.valueTextColor = rgba(87, 174, 255, 1);
set1.valueFormatter = self;
set1.lineWidth = 1.0;
set1.circleRadius = 3;
set1.drawCircleHoleEnabled = NO;
set1.circleHoleRadius = 1.0f;
set1.circleHoleColor = rgba(87, 174, 255, 1);
set1.drawFilledEnabled = NO;
set1.highlightColor = [UIColor yellowColor];
set1.highlightLineWidth = 1.00f;
if (values.count==1) {
set1.drawCirclesEnabled = YES;
}else{
set1.drawCirclesEnabled = NO;
}
return set1;
}
- (void)setDataCount:(int)count range:(double)range
{
NSMutableArray *values = [[NSMutableArray alloc] init];
for (int i = 0; i < 0+count; i++){
#warning 如果可能是负数,不能直接将Y或者X的值写到初始化方法上
NSInteger nowX = i*3;
if (i%10) {
[values addObject:[[ChartDataEntry alloc] initWithX:nowX y:-i icon: [UIImage imageNamed:@"icon"]]];
}else{
[values addObject:[[ChartDataEntry alloc] initWithX:nowX y:i+10 icon: [UIImage imageNamed:@"icon"]]];
}
}
self.chartView.xAxis.axisMinimum = 0;
self.chartView.leftAxis.axisMinimum = -100;
self.chartView.leftAxis.axisMaximum = 100;
[self showNeedData:@[values]];
}