Skip to content

Commit baa518d

Browse files
authored
Merge pull request #24 from warioddly/GRA-17
Implement Echarts Gradient
2 parents 657d529 + a2a4520 commit baa518d

File tree

5 files changed

+157
-20
lines changed

5 files changed

+157
-20
lines changed

example/lib/charts/basic_area_chart.dart

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,41 +35,59 @@ class _BasicAreaChartState extends State<BasicAreaChart> {
3535
Widget build(BuildContext context) {
3636
return GraphifyView(
3737
controller: controller,
38-
initialOptions: const {
39-
"xAxis": {
38+
initialOptions: {
39+
"xAxis": const {
4040
"type": "category",
4141
"boundaryGap": false,
4242
"data": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
4343
},
44-
"yAxis": {
44+
"yAxis": const {
4545
"type": "value"
4646
},
47-
'tooltip': {
47+
'tooltip': const {
4848
'trigger': 'axis',
4949
},
5050
"series": [
5151
{
52-
"data": [150, 230, 224, 218, 135, 147, 260],
52+
"data": const [150, 230, 224, 218, 135, 147, 260],
5353
"type": "line",
5454
'smooth': true,
5555
'areaStyle': {
56-
'color': {
57-
'type': 'RadialGradient',
58-
'x': 0.1,
59-
'y': 0.6,
60-
'r': 1,
61-
'colorStops': [
62-
{
63-
'color': 'rgba(255, 145, 124, 0.1)',
64-
'offset': 0,
65-
},
66-
{
67-
'color': 'rgba(255, 145, 124, 0.9)',
68-
'offset': 1,
69-
},
56+
'color': const GraphifyLinearGradient(
57+
x: 0.1,
58+
y: 0.6,
59+
x2: 0.9,
60+
y2: 0.1,
61+
colorStops: [
62+
GraphifyGradientColorStop(
63+
color: Colors.green,
64+
offset: 1,
65+
),
66+
GraphifyGradientColorStop(
67+
color: Colors.red,
68+
offset: .4,
69+
),
7070
],
71-
},
71+
).toJson()
7272
},
73+
// 'areaStyle': {
74+
// 'color': {
75+
// 'type': 'RadialGradient',
76+
// 'x': 0.1,
77+
// 'y': 0.6,
78+
// 'r': 1,
79+
// 'colorStops': [
80+
// {
81+
// 'color': 'rgba(255, 145, 124, 0.1)',
82+
// 'offset': 0,
83+
// },
84+
// {
85+
// 'color': 'rgba(255, 145, 124, 0.9)',
86+
// 'offset': 1,
87+
// },
88+
// ],
89+
// },
90+
// },
7391
}
7492
]
7593
},

lib/graphify.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ export 'src/controller/implements/facade.dart'
66
export 'src/view/implements/facade.dart'
77
if (dart.library.io) 'src/view/implements/mobile.dart'
88
if (dart.library.html) 'src/view/implements/web.dart';
9+
10+
export 'src/utils/gradient/graphify_gradient.dart';
11+
export 'src/utils/gradient/graphify_linear_gradient.dart';
12+
export 'src/utils/gradient/graphify_radial_gradient.dart';
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import 'dart:ui';
2+
3+
abstract class GraphifyGradient {
4+
const GraphifyGradient({
5+
this.colorStops = const [],
6+
this.global = false,
7+
this.id,
8+
});
9+
10+
String get type;
11+
12+
final num? id;
13+
final List<GraphifyGradientColorStop> colorStops;
14+
final bool global;
15+
16+
void addColorStop(num offset, Color color) {
17+
colorStops.add(
18+
GraphifyGradientColorStop(
19+
offset: offset,
20+
color: color,
21+
),
22+
);
23+
}
24+
25+
Map<String, dynamic> toJson() {
26+
return <String, dynamic>{
27+
if (id != null) 'id': id,
28+
'type': type,
29+
'colorStops': colorStops.map((e) => e.toJson()).toList(),
30+
'global': global,
31+
};
32+
}
33+
}
34+
35+
class GraphifyGradientColorStop {
36+
const GraphifyGradientColorStop({
37+
required this.offset,
38+
required this.color,
39+
});
40+
41+
final num offset;
42+
final Color color;
43+
44+
Map<String, dynamic> toJson() {
45+
return <String, dynamic>{
46+
'offset': offset,
47+
'color':
48+
'rgba(${(color.r * 255).toInt()}, ${(color.g * 255).toInt()}, ${(color.b * 255).toInt()}, ${color.a})',
49+
};
50+
}
51+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import 'package:graphify/src/utils/gradient/graphify_gradient.dart';
2+
3+
class GraphifyLinearGradient extends GraphifyGradient {
4+
5+
const GraphifyLinearGradient({
6+
this.x = 0,
7+
this.y = 0,
8+
this.x2 = 1,
9+
this.y2 = 0,
10+
super.colorStops,
11+
super.global,
12+
super.id,
13+
});
14+
15+
@override
16+
String get type => 'linear';
17+
18+
final num x;
19+
final num y;
20+
final num x2;
21+
final num y2;
22+
23+
@override
24+
Map<String, dynamic> toJson() {
25+
return <String, dynamic>{
26+
...super.toJson(),
27+
'x': x,
28+
'y': y,
29+
'x2': x2,
30+
'y2': y2,
31+
};
32+
}
33+
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'package:graphify/src/utils/gradient/graphify_gradient.dart';
2+
3+
class GraphifyRadialGradient extends GraphifyGradient {
4+
5+
const GraphifyRadialGradient({
6+
this.x = .5,
7+
this.y = .5,
8+
this.r = .5,
9+
super.colorStops,
10+
super.global,
11+
super.id,
12+
});
13+
14+
@override
15+
String get type => 'radial';
16+
17+
final num x;
18+
final num y;
19+
final num r;
20+
21+
@override
22+
Map<String, dynamic> toJson() {
23+
return <String, dynamic>{
24+
...super.toJson(),
25+
'x': x,
26+
'y': y,
27+
'r': r,
28+
};
29+
}
30+
}

0 commit comments

Comments
 (0)