This commit is contained in:
toly
2023-12-21 22:06:28 +08:00
parent 8b846f86d3
commit 2f4e8d3564
62 changed files with 6651 additions and 92 deletions

View File

@@ -0,0 +1 @@
export 'p17_page.dart';

View File

@@ -0,0 +1,21 @@
import 'package:flutter/material.dart';
import 'package:components/components.dart';
import 's01.dart' as s1;
import 's02.dart' as s2;
class P17Page extends StatelessWidget {
const P17Page({super.key});
@override
Widget build(BuildContext context) {
return const DemoShower(
srcCodeDir: 'draw/p17',
demos: [
s1.Paper(),
// s2.Paper(),
],
);
}
}

View File

@@ -0,0 +1,271 @@
import 'dart:math';
import 'dart:ui';
// import 'dart:ui' as ui;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
/// create by 张风捷特烈 on 2020/11/5
/// contact me by email 1981462002@qq.com
/// 说明:
class Paper extends StatelessWidget {
const Paper({super.key});
@override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
alignment: Alignment.center,
child: const ICharts(),
);
}
}
class ICharts extends StatefulWidget {
const ICharts({super.key});
@override
_IChartsState createState() => _IChartsState();
}
class _IChartsState extends State<ICharts> with SingleTickerProviderStateMixin {
late AnimationController _controller;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(milliseconds: 1500),
vsync: this,
)
..forward();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Stack(alignment: Alignment.topCenter, children: [
Container(
width: 500,
height: 380,
padding: EdgeInsets.only(top: 20, right: 20, bottom: 20, left: 20),
child: CustomPaint(
painter: ChartPainter(repaint: _controller),
),
),
// Padding(
// padding: const EdgeInsets.only(top: 8.0),
// child: Text(
// "捷特 3 月支出统计图 - 2040 年",
// style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold),
// ),
// )
]);
}
}
const double _kPiePadding = 20; // 圆边距
const double _kStrokeWidth = 12; // 圆弧宽
const double _kAngle = 270; // 圆弧角度
const int _kMax = 220; // 最大刻度值
const int _kMin = 0; // 最小刻度值
const double _kColorStopRate = 0.2; // 颜色变化分率
const double _kScaleHeightLever1 = 14; // 短刻度线
const double _kScaleHeightLever2 = 18; // 逢5线
const double _kScaleHeightLever3 = 20; // 逢10线
const double _kScaleDensity = 0.5; // 密度
const double _kScaleTextStep = 10; // 刻度文字步长
const List<Color> _kColors = [
// 颜色列表
Colors.green,
Colors.blue,
Colors.red,
];
class ChartPainter extends CustomPainter {
final TextPainter _textPainter =
TextPainter(textDirection: TextDirection.ltr);
double value = 150; //指针数值
double radius = 0; // 圆半径
final Animation<double> repaint;
Paint fillPaint = Paint();
Paint stokePaint = Paint()
..strokeWidth = 1
..style = PaintingStyle.stroke;
double get initAngle => (360 - _kAngle) / 2;
ChartPainter({required this.repaint}) : super(repaint: repaint);
@override
void paint(Canvas canvas, Size size) {
radius = size.shortestSide / 2 - _kPiePadding; // 当前圆半径
canvas.translate(size.width / 2, size.height / 2); // 画布原点移至中点
drawText(canvas);
drawArrow(canvas); // 绘制指针
canvas.rotate(pi / 2); // 将起始点旋转到 y 轴正方向
drawScale(canvas); // 绘制刻度
drawOutline(canvas); // 绘制弧线
}
void drawOutline(Canvas canvas) {
Path path = Path()..moveTo(radius, 0);
path.arcTo(
Rect.fromCenter(
center: Offset.zero, width: radius * 2, height: radius * 2),
pi / 180 * initAngle,
pi / 180 * _kAngle,
true);
PathMetrics pms = path.computeMetrics();
stokePaint..strokeWidth = _kStrokeWidth;
pms.forEach((PathMetric pm) {
canvas.drawPath(pm.extractPath(0, pm.length * _kColorStopRate),
stokePaint..color = _kColors[0]);
canvas.drawPath(
pm.extractPath(
pm.length * _kColorStopRate, pm.length * (1 - _kColorStopRate)),
stokePaint..color = _kColors[1]);
canvas.drawPath(
pm.extractPath(pm.length * (1 - _kColorStopRate), pm.length),
stokePaint..color = _kColors[2]);
});
}
void drawScale(Canvas canvas) {
canvas.save();
canvas.rotate(pi / 180 * initAngle);
double len = 0;
Color color = Colors.red;
int count = (_kMax * _kScaleDensity).toInt(); // 格线个数
for (int i = _kMin; i <= count; i++) {
if (i % 5 == 0 && i % 10 != 0) {
len = _kScaleHeightLever2;
} else if (i % 10 == 0) {
len = _kScaleHeightLever3;
} else {
len = _kScaleHeightLever1;
}
if (i < count * _kColorStopRate) {
color = Colors.green;
} else if (i < count * (1 - _kColorStopRate)) {
color = Colors.blue;
} else {
color = Colors.red;
}
canvas.drawLine(Offset(radius + _kStrokeWidth / 2, 0),
Offset(radius - len, 0), stokePaint..color = color..strokeWidth=1);
canvas.rotate(pi / 180 / _kMax * _kAngle / _kScaleDensity);
}
canvas.restore();
}
void drawArrow(Canvas canvas) {
var nowPer = value / _kMax;
Color color = Colors.red;
canvas.save();
double radians = pi / 180 * (-_kAngle / 2 + nowPer * _kAngle*repaint.value);
canvas.rotate(radians);
Path arrowPath = Path();
arrowPath.moveTo(0, 18);
arrowPath.relativeLineTo(-6, -10);
arrowPath.relativeLineTo(6, -radius + 10);
arrowPath.relativeLineTo(6, radius - 10);
arrowPath.close();
if (nowPer < _kColorStopRate) {
color = _kColors[0];
} else if (nowPer < (1 - _kColorStopRate)) {
color = _kColors[1];
} else {
color = _kColors[2];
}
canvas.drawPath(arrowPath, fillPaint..color = color);
canvas.drawCircle(Offset.zero, 3, stokePaint..color = Colors.yellow..strokeWidth=1);
canvas.drawCircle(Offset.zero, 3, fillPaint..color = Colors.white);
canvas.restore();
}
void drawText(Canvas canvas) {
_drawAxisText(canvas, 'km/s',
fontSize: 20,
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
alignment: Alignment.center,
color: Colors.black,
offset: Offset(0, -radius / 2));
_drawAxisText(canvas, '${value.toStringAsFixed(1)}',
fontSize: 16,
alignment: Alignment.center,
color: Colors.black,
offset: Offset(0, radius / 2));
int count = (_kMax - _kMin) * _kScaleDensity ~/ _kScaleTextStep;
Color color = Colors.red;
for (int i = _kMin; i <= count; i++) {
var thta = pi / 180 * (90 + initAngle + (_kAngle / count) * i);
if (i < count * _kColorStopRate) {
color = _kColors[0];
} else if (i < count * (1 - _kColorStopRate)) {
color = _kColors[1];
} else {
color = _kColors[2];
}
Rect rect = Rect.fromLTWH((radius - 40) * cos(thta) - 12,
(radius - 40) * sin(thta) - 8, 24, 16);
canvas.drawRRect(RRect.fromRectAndRadius(rect, Radius.circular(3)),
fillPaint..color = color);
_drawAxisText(canvas, '${i * _kScaleTextStep ~/ _kScaleDensity}',
fontSize: 11,
alignment: Alignment.center,
color: Colors.white,
offset: Offset((radius - 40) * cos(thta), (radius - 40) * sin(thta)));
}
}
void _drawAxisText(Canvas canvas, String str,
{Color color = Colors.black,
double fontSize = 11,
FontStyle fontStyle = FontStyle.normal,
Alignment alignment = Alignment.centerRight,
FontWeight fontWeight = FontWeight.normal,
Offset offset = Offset.zero}) {
TextSpan text = TextSpan(
text: str,
style: TextStyle(
fontSize: fontSize,
fontWeight: fontWeight,
fontStyle: fontStyle,
color: color,
));
_textPainter.text = text;
_textPainter.layout(); // 进行布局
Size size = _textPainter.size;
Offset offsetPos = Offset(-size.width / 2, -size.height / 2)
.translate(-size.width / 2 * alignment.x + offset.dx, 0.0 + offset.dy);
_textPainter.paint(canvas, offsetPos);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}

View File

@@ -0,0 +1,432 @@
import 'dart:math';
// import 'dart:ui' as ui;
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:convert' show json;
/// create by 张风捷特烈 on 2020/11/5
/// contact me by email 1981462002@qq.com
/// 说明:
class Paper extends StatelessWidget {
const Paper({super.key});
@override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
alignment: Alignment.center,
child: const ChinaMap(),
);
}
}
class ChinaMap extends StatefulWidget {
const ChinaMap({super.key});
@override
_ChinaMapState createState() => _ChinaMapState();
}
class _ChinaMapState extends State<ChinaMap> {
final String url = 'https://geo.datav.aliyun.com/areas_v2/bound/100000_full.json'; //全国点位详细信息
//请求点位信息地址
Future<MapRoot?> getMapRoot() async {
try {
final Response response = await Dio().get(url);
if (response.data != null) {
return MapRoot.fromJson(response.data);
} else {
return null;
}
} catch (e) {
print(e);
return null;
}
}
late Future<MapRoot?> _future;
@override
void initState() {
super.initState();
_future = getMapRoot();
}
@override
Widget build(BuildContext context) {
return FutureBuilder<MapRoot?>(
future: _future,
builder: (context, async) {
print(async.hasData);
if (async.hasData) {
return CustomPaint(
size: Size(500, 400),
painter: MapPainter(mapRoot: async.data),
);
} else {
return CupertinoActivityIndicator();
}
},
);
}
}
class MapPainter extends CustomPainter {
final MapRoot? mapRoot; //点位信息
late Paint _paint;
final List<Color> colors = [Colors.red, Colors.yellow, Colors.blue, Colors.green];
int colorIndex = 0;
MapPainter({required this.mapRoot}) {
_paint = Paint()
..strokeWidth = 0.1
..isAntiAlias = true;
}
@override
void paint(Canvas canvas, Size size) {
if(mapRoot == null) return;
if(mapRoot!.features ==null) return;
canvas.clipRect(
Rect.fromPoints(Offset.zero, Offset(size.width, size.height)));
canvas.translate(size.width / 2, size.height / 2);
double dx = mapRoot!.features![0]?.geometry?.coordinates[0][0][0].dx??0;
double dy = mapRoot!.features![0]?.geometry?.coordinates[0][0][0].dy??0;
canvas.translate( -dx, -dy);
double rate = 0.65;
canvas.translate(-700*rate, 350*rate);
canvas.scale(8*rate, -10.5*rate);
_drawMap(canvas, size);
}
void _drawMap(Canvas canvas, Size size) {
//全国省份循环
for (int i = 0; i < mapRoot!.features!.length; i++) {
var features = mapRoot!.features![i];
if(features ==null) return;
PaintingStyle style;
Color color = Colors.black;
style = PaintingStyle.fill;
Path path = Path();
if (features.properties?.name == "台湾省" ||
features.properties?.name == "海南省" ||
features.properties?.name == "河北省" ||
features.properties?.name == "") { //海南和台湾和九段线
Path otherPath = Path();
features.geometry?.coordinates.forEach((List<List<Offset>> lv3) {
for (var lv2 in lv3) {
otherPath.moveTo(lv2[0].dx, lv2[0].dy);
// for (var lv1 in lv2) {
// otherPath.lineTo(lv1.dx, lv1.dy); //优化一半点位
// print("========${lv1}==============================");
// }
}
});
path.addPath(otherPath, Offset.zero);
if (features.properties?.name == "") {
style = PaintingStyle.stroke;
color = Colors.black;
} else {
style = PaintingStyle.fill;
color = colors[colorIndex % 4];
}
colorIndex++;
} else {
final Offset first = features.geometry?.coordinates[0][0][0]??Offset.zero;
path.moveTo(first.dx, first.dy);
if(features.geometry ==null) return;
for (Offset d in features.geometry!.coordinates.first.first) {
path.lineTo(d.dx, d.dy);
}
style = PaintingStyle.fill;
color = colors[colorIndex % 4];
colorIndex++;
}
canvas.drawPath(path, _paint..color = color..style = style); //绘制地图
}
}
@override
bool shouldRepaint(MapPainter oldDelegate) => oldDelegate.mapRoot != mapRoot;
}
class MapRoot {
String type;
String name;
List<Features?>? features;
MapRoot({
required this.type,
required this.name,
required this.features,
});
static MapRoot? fromJson(jsonRes) {
if (jsonRes == null) return null;
List<Features?>? features = jsonRes['features'] is List ? [] : null;
if (features != null) {
for (var item in jsonRes['features']) {
if (item != null) {
features.add(Features.fromJson(item));
}
}
}
return MapRoot(
type: jsonRes['type'],
name: jsonRes['name'],
features: features,
);
}
Map<String, dynamic> toJson() => {
'type': type,
'name': name,
'features': features,
};
@override
String toString() {
return json.encode(this);
}
}
class Features {
String type;
Properties? properties;
Geometry? geometry;
Features({
required this.type,
required this.properties,
required this.geometry,
});
static Features? fromJson(jsonRes) => jsonRes == null
? null
: Features(
type: jsonRes['type'],
properties: Properties.fromJson(jsonRes['properties']),
geometry: Geometry.fromJson(jsonRes['geometry']),
);
Map<String, dynamic> toJson() => {
'type': type,
'properties': properties,
'geometry': geometry,
};
@override
String toString() {
return json.encode(this);
}
}
class Properties {
String? adcode;
String? name;
List<double>? center;
List<double>? centroid;
int? childrenNum;
String?level;
Parent? parent;
int? subFeatureIndex;
List<int>? acroutes;
Object? adchar;
Properties({
required this.adcode,
required this.name,
required this.center,
required this.centroid,
required this.childrenNum,
required this.level,
required this.parent,
required this.subFeatureIndex,
required this.acroutes,
required this.adchar,
});
static Properties? fromJson(jsonRes) {
if (jsonRes == null) return null;
List<double>? center = jsonRes['center'] is List ? [] : null;
if (center != null) {
for (var item in jsonRes['center']) {
if (item != null) {
center.add(item);
}
}
}
List<double>? centroid = jsonRes['centroid'] is List ? [] : null;
if (centroid != null) {
for (var item in jsonRes['centroid']) {
if (item != null) {
centroid.add(item);
}
}
}
List<int>? acroutes = jsonRes['acroutes'] is List ? [] : null;
if (acroutes != null) {
for (var item in jsonRes['acroutes']) {
if (item != null) {
acroutes.add(item);
}
}
}
return Properties(
adcode: jsonRes['adcode'],
name: jsonRes['name'],
center: center,
centroid: centroid,
childrenNum: jsonRes['childrenNum'],
level: jsonRes['level'],
parent: Parent.fromJson(jsonRes['parent']),
subFeatureIndex: jsonRes['subFeatureIndex'],
acroutes: acroutes,
adchar: jsonRes['adchar'],
);
}
Map<String, dynamic> toJson() => {
'adcode': adcode,
'name': name,
'center': center,
'centroid': centroid,
'childrenNum': childrenNum,
'level': level,
'parent': parent,
'subFeatureIndex': subFeatureIndex,
'acroutes': acroutes,
'adchar': adchar,
};
@override
String toString() {
return json.encode(this);
}
}
class Parent {
int adcode;
Parent({
required this.adcode,
});
static Parent? fromJson(jsonRes) => jsonRes == null
? null
: Parent(
adcode: jsonRes['adcode'],
);
Map<String, dynamic> toJson() => {
'adcode': adcode,
};
@override
String toString() {
return json.encode(this);
}
}
class Geometry {
String type;
List<List<List<Offset>>> coordinates;
Geometry({
required this.type,
required this.coordinates,
});
static Geometry? fromJson(jsonRes) {
if (jsonRes == null) return null;
List<List<List<Offset>>>? coordinates =
jsonRes['coordinates'] is List ? [] : null;
bool fourLever =false;
if (jsonRes['coordinates'] is List) {
if (jsonRes['coordinates'][0] is List){
if (jsonRes['coordinates'][0][0] is List){
if (jsonRes['coordinates'][0][0][0] is List){
fourLever =true;
}
}
}
}
if(!fourLever){
if (coordinates != null) {
for (var level0 in jsonRes['coordinates']) {
List<List<Offset>> lever0=[];
if (level0 != null) {
List<Offset> items1 = [];
for (var item1 in level0 is List ? level0 : []) {
if (item1 != null) {
Offset items2 = Offset(item1[0], item1[1]);
items1.add(items2);
}
lever0.add(items1);
}
}
coordinates.add(lever0);
}
}
}else{
if (coordinates != null) {
for (var level0 in jsonRes['coordinates']) {
if (level0 != null) {
List<List<Offset>> items1 = [];
for (var item1 in level0 is List ? level0 : []) {
if (item1 != null) {
List<Offset> items2 = [];
for (var item2 in item1 is List ? item1 : []) {
if (item2 != null && item2 is List) {
Offset items3 = Offset(item2[0], item2[1]);
items2.add(items3);
} else {
items2.add(Offset.zero);
}
items1.add(items2);
}
}
coordinates.add(items1);
}
}
}
}
}
return Geometry(
type: jsonRes['type'],
coordinates: coordinates??[],
);
}
Map<String, dynamic> toJson() => {
'type': type,
'coordinates': coordinates,
};
@override
String toString() {
return json.encode(this);
}
}