首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Json Api的扑图

Json Api的扑图
EN

Stack Overflow用户
提问于 2021-09-06 07:17:45
回答 1查看 34关注 0票数 0

我正在使用JSON API在我的应用程序中制作折线图。API输出为

代码语言:javascript
运行
复制
[{"date":"2021-09-03","price":"29490"},
{"date":"2021-09-04","price":"29490"},
{"date":"2021-09-05","price":"29490"},
{"date":"2021-09-06","price":"29490"}]

现在,我的新JSON输出采用以下格式

代码语言:javascript
运行
复制
[{"2021-09-01":"23200","2021-09-02":"23200","2021-09-03":"23200","2021-09-04":"23200"}]

有人能让我知道我需要在Flutter代码中做哪些更改才能使其工作。

代码语言:javascript
运行
复制
               Future<String> getJsonFromFirebase(productasin) async {
              String url = "https://example.com/chart.json";
              http.Response response =
              await http.post(Uri.parse(url), body: {'productchart': productasin});
              return response.body;
                 }

            Future loadSalesData() async {
            final String jsonString = await getJsonFromFirebase(productasin);
            final dynamic jsonResponse = json.decode(jsonString);
            for (Map<String, dynamic> i in jsonResponse)
            chartData.add(SalesData.fromJson(i));
               }

                class SalesData {
               SalesData(this.date, this.price);

              final String date;
              final double price;

             factory SalesData.fromJson(Map<String, dynamic> parsedJson) {
             return SalesData(
             parsedJson['date'].toString(), double.parse(parsedJson['price']));
              }
              }
               



                             child: FutureBuilder(
                future: getJsonFromFirebase(productasin),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    loadSalesData();
                    return SfCartesianChart(
                        trackballBehavior: _trackballBehavior,
                        primaryXAxis: CategoryAxis(
                          //Hide the gridlines of x-axis
                          majorGridLines: MajorGridLines(width: 0),
                          //Hide the axis line of x-axis
                          axisLine: AxisLine(width: 0),
                        ),
                        primaryYAxis: NumericAxis(
                            edgeLabelPlacement: EdgeLabelPlacement.shift,
                            //Hide the gridlines of y-axis
                            majorGridLines: MajorGridLines(width: 0),
                            //Hide the axis line of y-axis
                            axisLine: AxisLine(width: 0)),
                        tooltipBehavior: _tooltipBehavior,
                        series: <ChartSeries<SalesData, String>>[
                          StackedAreaSeries<SalesData, String>(
                              dataLabelSettings: DataLabelSettings(
                                isVisible: true,
                                labelAlignment: ChartDataLabelAlignment.top,
                                useSeriesColor: true,
                                showCumulativeValues: true,
                              ),
                              name: 'Price',
                              dataSource: chartData,
                              borderColor: Colors.white,
                              borderWidth: 2,
                              xValueMapper: (SalesData sales, _) =>
                                  sales.date,
                              yValueMapper: (SalesData sales, _) =>
                                  sales.price,
                              markerSettings:
                                  MarkerSettings(isVisible: true)
                              //gradient: gradientColors
                              )
                        ]);
                  } else {
                    return Center(child: CircularProgressIndicator());
                  }
                })),
EN

回答 1

Stack Overflow用户

发布于 2021-09-11 06:53:22

这一行可以指导你

代码语言:javascript
运行
复制
jsonResponse[0].forEach((k, v) =>   chartData.add(SalesData(k, Double.tryParse(v)); );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69070576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档