#import <CorePlot/CorePlot.h>
@interface YourViewController : UIViewController <CPTPlotDataSource>
@property (nonatomic, strong) CPTGraphHostingView *hostingView;
@property (nonatomic, strong) CPTXYGraph *graph;
@property (nonatomic, strong) NSMutableArray *data;
@end
@implementation YourViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建并设置图表视图
self.hostingView = [[CPTGraphHostingView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.hostingView];
// 创建图表对象
self.graph = [[CPTXYGraph alloc] initWithFrame:self.hostingView.bounds];
self.hostingView.hostedGraph = self.graph;
// 设置图表样式
self.graph.paddingLeft = 10.0;
self.graph.paddingTop = 10.0;
self.graph.paddingRight = 10.0;
self.graph.paddingBottom = 10.0;
// 创建曲线图绘制区域
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
// 设置曲线图的坐标范围
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:@0 length:@10];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:@0 length:@100];
// 创建曲线图对象
CPTScatterPlot *scatterPlot = [[CPTScatterPlot alloc] init];
scatterPlot.dataSource = self;
// 添加曲线图到图表中
[self.graph addPlot:scatterPlot];
}
#pragma mark - CPTPlotDataSource
- (NSUInteger)numberOfRecordsForPlot:(nonnull CPTPlot *)plot {
// 返回曲线图的数据点数量
return self.data.count;
}
- (nullable id)numberForPlot:(nonnull CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
// 返回曲线图的数据点值,fieldEnum 可以是 CPTScatterPlotFieldX 或 CPTScatterPlotFieldY
// index 是数据点的索引
// 实现此方法以获取曲线图的数据点
if (fieldEnum == CPTScatterPlotFieldX) {
// 返回 x 坐标值
return @(index);
} else if (fieldEnum == CPTScatterPlotFieldY) {
// 返回 y 坐标值
NSNumber *value = self.data[index];
return value;
}
return nil;
}
@end
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My First Flutter App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home Page'),
),
body: Center(
child: Text(
'Hello, World!',
style: TextStyle(fontSize: 24.0),
),
),
);
}
}