如何在 android 中从数据库值动态填充 AnyChart?

How to populate AnyChart from database values dynamically in android?

我正在寻找使用 android 中的 AnyChart 库创建折线图的方法。这些值是通过我的 api link(采用 json 格式)动态获取的。我已经尝试了很多次,但无法做到。一些帮助将不胜感激。谢谢你。

代码如下-

 try {
      JSONObject jsonObject = new JSONObject(response);
      String success = jsonObject.getString("status");
      String cdata = jsonObject.getString("chart_data"); // for extra condition
      final JSONArray jsonArray1 = jsonObject.getJSONArray("chart_data");
      if (success.equals("true") && !cdata.equals(null)) {
      for (int i = 0; i < jsonArray1.length(); i++) {
           JSONObject object1 = jsonArray1.getJSONObject(i);
           String devinfo = object1.getString("Device Info");
           dev[i] = object1.getString("Device Info");
           String value = object1.getString("Value");

           // chart AnyChart
           Cartesian cartesian = AnyChart.line();
           cartesian.animation(false);
           cartesian.padding(10d, 20d, 5d, 20d);
           cartesian.crosshair().enabled(true);
           cartesian.crosshair()
                    .yLabel(true)
                  // TODO ystroke
                    .yStroke((Stroke) null, null, null, (String) null, (String) null);
           cartesian.tooltip().positionMode(TooltipPositionMode.POINT);
//         cartesian.title("Trend of Sales of the Most Popular Products of ACME Corp.");
//         cartesian.yAxis(0).title("Number of Bottles Sold (thousands)");
           cartesian.xAxis(0).labels().padding(1d, 1d, 1d, 1d);
           List<DataEntry> seriesData = new ArrayList<>();
           seriesData.add(new CustomDataEntry(dev[i], Integer.parseInt(value)));

           Set set = Set.instantiate();
           set.data(seriesData);
           Mapping series1Mapping = set.mapAs("{ x: 'x', value: 'value' }");
//         Mapping series2Mapping = set.mapAs("{ x: 'x', value: 'value2' }");
//         Mapping series3Mapping = set.mapAs("{ x: 'x', value: 'value3' }");

           Line series1 = cartesian.line(series1Mapping);
           series1.name("Brand");
           series1.hovered().markers().enabled(true);
           series1.hovered().markers()
                            .type(MarkerType.SQUARE)
                            .size(4d);
           series1.tooltip().position("right")
                            .anchor(Anchor.RIGHT_CENTER)
                            .offsetX(5d)
                            .offsetY(5d);

           cartesian.legend().enabled(true);
           cartesian.legend().fontSize(13d);
           cartesian.legend().padding(0d, 0d, 10d, 0d);
           anyChartView.setChart(cartesian);

          }
        }

     } catch (JSONException e) {
        e.printStackTrace();
       }

在您的代码中,您重新创建图表、系列、DataEntries 列表并为每个点配置图表。要正确创建图表并应用来自数据库的数据,您应该按顺序执行以下步骤:

  1. 只创建一次 DataEntries 列表
  2. 在循环中将 JSON 解析为 DataEntry(就像您所做的一样,但应该在循环之前只创建一次列表)
  3. 像您一样创建集,将 DataEntries 列表应用到集
  4. 创建图表、系列、配置(仅一次)

对于正在寻找代码实现的任何人。

AnyChartView anyChartView = findViewById(R.id.any_chart_view);
anyChartView.setProgressBar(findViewById(R.id.loadingbuble));

Scatter bubble = AnyChart.bubble();
bubble.animation(true);
bubble.title().enabled(false);
bubble.title().useHtml(true);
bubble.padding(20d, 20d, 10d, 20d);
bubble.yGrid(true)
                .xGrid(false)
                .xMinorGrid(false)
                .yMinorGrid(false);
bubble.minBubbleSize(5d)
                .maxBubbleSize(40d);
bubble.xAxis(0)
              .title("Variable Cost (₹/MWh)")
              .minorTicks(true);
bubble.yAxis(0)
              .title("Increment/Decrement (MWh)")
              .minorTicks(true);
bubble.legend().enabled(true);
bubble.labels().padding(0d, 0d, 10d, 0d);

anyChartView.setChart(bubble);

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_SCED,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            String success = jsonObject.getString("status");
                            JSONArray jsonArray = jsonObject.getJSONArray("data");

                            if (success.equals("true")) {
                                loadingbuble.setVisibility(View.GONE);

                                List<DataEntry> data1 = new ArrayList<>();
                                List<DataEntry> data2 = new ArrayList<>();
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject object = jsonArray.getJSONObject(i);

                                    String SCED_Generator = object.getString("SCED_Generator");
                                    String VC = object.getString("VC");
                                    String Decrement_due_to_SCED = object.getString("Decrement_due_to_SCED");
                                    String Increment_due_to_SCED = object.getString("Increment_due_to_SCED");

                                    data1.add(new CustomBubbleDataEntry(SCED_Generator, Integer.valueOf(VC), Integer.valueOf(Increment_due_to_SCED), Integer.valueOf(Increment_due_to_SCED) / 100));
                                    data2.add(new CustomBubbleDataEntry(SCED_Generator, Integer.valueOf(VC), Integer.valueOf(Decrement_due_to_SCED), Math.abs(Integer.valueOf(Decrement_due_to_SCED)) / 100));
                                }

                                bubble.tooltip()
                                        .useHtml(true)
                                        .fontColor("#fff")
                                        .format("function() {\n" +
                                                "        return this.getData('plant') + '<br/>' +\n" +
                                                "          'Plant Name: <span style=\"color: #ffffff; font-size: 12px\">' +\n" +
                                                "          this.getData('plant') + '</span></strong><br/>' +\n" +
                                                "          'Increment/Decrement: <span style=\"color: #ffffff; font-size: 12px\">' +\n" +
                                                "          this.getData('value') + ' MWh</span></strong><br/>' +\n" +
                                                "          'Variable Cost: <span style=\"color: #ffffff; font-size: 12px\">' +\n" +
                                                "          this.getData('x') + ' ₹/MWh.</span></strong>';\n" +
                                                "      }");

                                bubble.bubble(data1).name("Increment due to SCED").color("#ba68c8");
                                bubble.bubble(data2).name("Decrement due to SCED");

                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }