使用 java struts 2 在 amcharts 中加载动态数据
load dynamic data in amcharts using java struts 2
我想使用 amcharts 显示 var 图表,数据来自数据库,现在显示了 varchart,但问题是它只包含三个值中的一个,请看下面的代码,我附上我的代码 json 输出认为这里的问题是 struts.xml
替代方案我想在我的项目目录中写入 json 文件请参阅我的 java 操作 class(代码块),我在此处写入 json 文件并输出没问题,但问题是我如何在 amchart dataloader 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操作class是
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;
}
我的sqlclass是
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
数组应如下所示:
"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"
的 属性(请注意您的字符串中的 space - 这可能是错误的)当前不在您的数据中.您需要将其包含在创建 JSON 结果集的代码中。
这里有一个 fiddle 演示了正确的 graphs
设置,在您的示例数据中使用 "institute"
的虚拟值。
这里的问题是我的 json 输出,我只是更改了我的查询,现在没问题了...
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'
我想使用 amcharts 显示 var 图表,数据来自数据库,现在显示了 varchart,但问题是它只包含三个值中的一个,请看下面的代码,我附上我的代码 json 输出认为这里的问题是 struts.xml
替代方案我想在我的项目目录中写入 json 文件请参阅我的 java 操作 class(代码块),我在此处写入 json 文件并输出没问题,但问题是我如何在 amchart dataloader 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操作class是
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;
}
我的sqlclass是
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
数组应如下所示:
"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"
的 属性(请注意您的字符串中的 space - 这可能是错误的)当前不在您的数据中.您需要将其包含在创建 JSON 结果集的代码中。
这里有一个 fiddle 演示了正确的 graphs
设置,在您的示例数据中使用 "institute"
的虚拟值。
这里的问题是我的 json 输出,我只是更改了我的查询,现在没问题了...
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'