使用java struts 2在amcharts中加载动态数据

时间:2017-04-12 09:47:16

标签: java json struts2 amcharts

我想显示var图表使用amcharts和数据来自数据库现在varchart是显示但问题是它只包含三个中的一个值请看下面的代码在这里我附上我的代码与json输出认为这里的问题是struts。 XML

替代我想在我的项目目录中写json文件看到我的java动作类(块代码)在这里我写了json文件并且输出是好的但是问题是我如何在amchart dataloader url中添加文件路径:请参阅以下示例 https://www.amcharts.com/kbase/dynamically-loading-chart-datasets/

my js file

public String getInstituteChartDataList(){

        instituteChartList = dashbordsql.getInstituteChartDataList();

        Gson gson = new Gson();
        json = gson.toJson(instituteChartList);
         /*try {

             ServletContext context = ServletActionContext.getServletContext();
             String path = context.getRealPath("/");

             File file = new File(path+"allPages/homePages/testPage.json");
             System.out.println("Path == "+path);
             System.out.println("json == "+json);
             if (!file.exists()) {
                    file.createNewFile();
                }

             System.out.println("file == "+file);

             FileWriter writer = new FileWriter(file);
             writer.write(json);



             writer.flush();
             writer.close();



        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/



        return ActionSupport.SUCCESS;
    }

我的java动作类是

public List<InstituteBasicInfo> getInstituteChartDataList() {
        List<InstituteBasicInfo> instituteBasicList = new ArrayList<InstituteBasicInfo>();
        InstituteBasicInfo instituteBasicBeen = new InstituteBasicInfo();
        boolean fg = true;
        connection = dbConnection.connectDB();

        if (connection == null) {
            fg = false;
        }
        if (fg) {
            try {
                st = connection.createStatement();
                query = "select sum(total_inst) as toIns, sum(mpo_inst) as toMpo, sum(non_mpo_inst) as toNonMpo from ( select count(*) as total_inst , 0 as mpo_inst, 0 non_mpo_inst from institutes where eiin is not null and institute_name_new is not null and stop is null "
                        + "UNION ALL "
                        + "select 0 as total_inst,  count(*) as mpo_inst,  0 as non_mpo_inst    from institutes  where eiin is not null and institute_name_new is not null and stop is null and MPO_STATUS='1'"
                        + "UNION ALL "
                        + " select 0 as total_inst, 0 as  mpo_inst ,count(*) as non_mpo_inst from institutes   where eiin is not null and institute_name_new is not null and stop is null and MPO_STATUS='2')";
                System.out.println("Qry :" + query);

                rs = st.executeQuery(query);
                while (rs.next()) {

                    instituteBasicBeen = new InstituteBasicInfo();
                    instituteBasicBeen.setTotalInstitute(rs.getString("toIns"));
                    instituteBasicBeen.setTotalMpoInstitute(rs.getString("toMpo"));
                    instituteBasicBeen.setTotalNonMpoInstitute(rs.getString("toNonMpo"));



                    instituteBasicList.add(instituteBasicBeen);
                }

            } catch (SQLException sq) {
                instituteBasicList = null;
                sq.printStackTrace();
            } finally {
                try {
                    rs.close();
                    st.close();
                    connection.close();
                } catch (SQLException ex) {
                    instituteBasicList = null;
                    ex.printStackTrace();
                }
            }
        }
        return instituteBasicList;
    }

,我的sql类是

[{"totalInstitute":"35408","totalMpoInstitute":"25582","totalNonMpoInstitute":"6516"}]

json out:

<action name="*Dash" class="com.dd.dashbord.action.DashbordAction" method="{1}">
    <result type="json">
        <param name="root">instituteChartList</param>
    </result>       

</action>

struts.xml中

.tpignore

2 个答案:

答案 0 :(得分:0)

问题在于您如何定义图表。 graphs属性是graph个对象的数组。通过在配置中创建重复的graphs,您可以将图形数组重新定义为另一个单个元素数组,而不是包含其他图形对象。如果要在数据中显示所有三个属性,则graphs数组应如下所示:

  "graphs": [{
    "balloonText": "[[category]]: <b>[[value]]</b>",
    "fillAlphas": 0.8,
    "lineAlpha": 0.2,
    "type": "column",
    "valueField": "totalInstitute"
  }, {
    "balloonText": "[[category]]: <b>[[value]]</b>",
    "fillAlphas": 0.8,
    "lineAlpha": 0.2,
    "type": "column",
    "valueField": "totalMpoInstitute"
  }, {
    "balloonText": "[[category]]: <b>[[value]]</b>",
    "fillAlphas": 0.8,
    "lineAlpha": 0.2,
    "type": "column",
    "valueField": "totalNonMpoInstitute"
  }],

您更新的categoryField引用了一个名为" institute"的属性(请注意字符串中的空格 - 这可能是错误的)当前不在您的数据中。您需要在创建JSON结果集的代码中包含它。

这是一个fiddle,可以在示例数据中使用graphs的虚拟值来演示正确的"institute"设置。

答案 1 :(得分:0)

这里的问题是我的json out put我只是改变我的查询现在可以......

SELECT 'Institude' INS_TYPE, COUNT (*) AS TOTAL_INST
  FROM INSTITUTES
 WHERE EIIN IS NOT NULL AND INSTITUTE_NAME_NEW IS NOT NULL AND STOP IS NULL
UNION ALL
SELECT 'MPO' INS_TYPE, COUNT (*) AS TOTAL_INST
  FROM INSTITUTES
 WHERE     EIIN IS NOT NULL
       AND INSTITUTE_NAME_NEW IS NOT NULL
       AND STOP IS NULL
       AND MPO_STATUS = '1'
UNION ALL
SELECT 'Non- MPO' INS_TYPE, COUNT (*) AS TOTAL_INST
  FROM INSTITUTES
 WHERE     EIIN IS NOT NULL
       AND INSTITUTE_NAME_NEW IS NOT NULL
       AND STOP IS NULL
       AND MPO_STATUS = '2'