gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:35:19 26 4
gpt4 key购买 nike

我想使用 amcharts 显示 var 图表,数据来自数据库,现在 varchart 已显示,但问题是它只包含三个值中的一个,请参阅下面的代码,我用 json 输出附加我的代码,认为这里的问题是 struts.xml

替代方案我想在我的项目目录中编写json文件请参阅我的java操作类( block 代码)这里我编写json文件并且输出没问题但问题是我如何在amchart数据加载器url中添加文件路径:请参阅下面的示例 https://www.amcharts.com/kbase/dynamically-loading-chart-datasets/

我的js文件

   var chart = AmCharts.makeChart("chartdiv", {

"type": "serial",
//"dataProvider": generateChartData(),
/* "dataLoader": {
"url": "getInstituteChartDataListDash.do"
}, */
"dataLoader": {
"url": "getInstituteChartDataListDash.do",
"format": "json"
},
"valueAxes": [{
"gridColor": "#FFFFFF",
"gridAlpha": 0.2,
"dashLength": 0
}],
"gridAboveGraphs": true,
"startDuration": 1,
"graphs": [{
"balloonText": "[[category]]: <b>[[value]]</b>",
"fillAlphas": 0.8,
"lineAlpha": 0.2,
"type": "column",
"valueField": "totalInstitute"
}],

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


"chartCursor": {
"categoryBalloonEnabled": false,
"cursorAlpha": 0,
"zoomable": false
},
"categoryField": " institute",
"categoryAxis": {
"gridPosition": "start",
"gridAlpha": 0,
"tickPosition": "start",
"tickLength": 20
}
});

我的java Action 类是

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;
}

我的 sql 类是

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;
}

json 输出:

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

struts.xml

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

</action>

最佳答案

问题在于您如何定义图表。 graphs 属性是 graph 对象的数组。通过在配置中创建重复的图表,您可以将图表数组重新定义为另一个单元素数组,而不是包含其他图表对象。如果您想显示数据中的所有三个属性,您的 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它在示例数据中使用 "institute" 的虚拟值演示了正确的graphs 设置。

关于java - 使用java struts 2在amcharts中加载动态数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43366042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com