如何创建步骤折线图,如下图
我试图搜索此图表,但找不到任何引用。
我希望有人可以帮助我:)。
最佳答案
您可以在下面复制粘贴运行完整代码
您可以使用https://pub.dev/packages/flutter_echarts
它是Webview
和Javascript
的基础。
您可以使用option
字符串配置图表
步骤折线图示例https://echarts.apache.org/examples/en/editor.html?c=line-step
程式码片段
Container(
child: Echarts(
option: '''
{
title: {
text: ''
},
tooltip: {
trigger: 'axis'
},
legend: {
data: ['Step Start', 'Step Middle', 'Step End']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
工作演示完整的代码
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_echarts/flutter_echarts.dart';
import 'package:number_display/number_display.dart';
final display = createDisplay(decimal: 2);
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
List<Map<String, Object>> _data1 = [
{'name': 'Please wait', 'value': 0}
];
getData1() async {
await Future.delayed(Duration(seconds: 4));
const dataObj = [
{
'name': 'Jan',
'value': 8726.2453,
},
{
'name': 'Feb',
'value': 2445.2453,
},
{
'name': 'Mar',
'value': 6636.2400,
},
{
'name': 'Apr',
'value': 4774.2453,
},
{
'name': 'May',
'value': 1066.2453,
},
{
'name': 'Jun',
'value': 4576.9932,
},
{
'name': 'Jul',
'value': 8926.9823,
}
];
this.setState(() {
this._data1 = dataObj;
});
}
@override
void initState() {
super.initState();
this.getData1();
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text('Echarts Demon'),
),
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Center(
child: Column(
children: <Widget>[
Container(
child: Echarts(
option: '''
{
title: {
text: ''
},
tooltip: {
trigger: 'axis'
},
legend: {
data: ['Step Start', 'Step Middle', 'Step End']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
toolbox: {
feature: {
saveAsImage: {}
}
},
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
name: 'Step Start',
type: 'line',
step: 'start',
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: 'Step Middle',
type: 'line',
step: 'middle',
data: [220, 282, 201, 234, 290, 430, 410]
},
{
name: 'Step End',
type: 'line',
step: 'end',
data: [450, 432, 401, 454, 590, 530, 510]
}
]
} ''',
extraScript: '''
chart.on('click', (params) => {
if(params.componentType === 'series') {
Messager.postMessage(JSON.stringify({
type: 'select',
payload: params.dataIndex,
}));
}
});
''',
onMessage: (String message) {
Map<String, Object> messageAction = jsonDecode(message);
print(messageAction);
if (messageAction['type'] == 'select') {
final item = _data1[messageAction['payload']];
_scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text(item['name'].toString() +
': ' +
display(item['value'])),
duration: Duration(seconds: 2),
));
}
},
),
width: 300,
height: 250,
),
],
),
),
),
);
}
}