//获取数据并处理
public String getBarData(){
String beginDate = StringUtils.trimToEmpty(request.getParameter("beginDate"));
String endDate = StringUtils.trimToEmpty(request.getParameter("endDate"));
List<Wh13ChannelInfo> channelList = indexService.getChannelList();
int[] positiveModuleAry = null;// 正面
int[] plusModuleAry = null; // 负面
String positiveModuleStr = "";
String plusModuleStr = "";
if(channelList != null){
for(Wh13ChannelInfo channel: channelList){
if(channel.getModuleId() == null){
positiveModuleStr = channel.getChildModules();
}else{
plusModuleStr += channel.getChildModules() + ",";
}
}
}
positiveModuleAry = stringAryToIntAry(positiveModuleStr.split(","));
plusModuleAry = stringAryToIntAry(plusModuleStr.substring(0, plusModuleStr.lastIndexOf(",")).split(","));
Map<String, int[]> yqModuleMap = new LinkedHashMap<String, int[]>();
yqModuleMap.put("正面", positiveModuleAry);
yqModuleMap.put("负面", plusModuleAry);
Map<String, Object> resultMap = new HashMap<String, Object>();// 结果数据
String[] legend = null;// 图例数据
String[] xAxis = null;// x轴数据
int[][] yAxis = null;// y轴数据
int max = 0;// 最大值
int min = 0;// 最小值
if(yqModuleMap != null){
int moduleSize = yqModuleMap.size();
legend = new String[moduleSize];
yAxis = new int[moduleSize][];// 初始化二维数组之创建第一维
int i = 0;
for(Map.Entry<String, int[]> moduleEntry : yqModuleMap.entrySet()){
String moduleKey = moduleEntry.getKey();
int[] moduleValue = moduleEntry.getValue();
legend[i++] = moduleKey;
Map<String, Object> filter = new HashMap<String, Object>();
filter.put(CommonPublic.PRODUCTATTR, CommonPublic.PRODUCTID);
filter.put(CommonPublic.AREAATTR, CommonPublic.WUHAN_AREAS_ARY);
filter.put(CommonPublic.MODULEATTR, moduleValue);
NewsStat newsStat = new NewsStat();
Map<String, Integer> statMap = newsStat.getNewsStatByTime(null, SphinxCommon.GROUPTYPE_DAY, filter, beginDate, endDate, "yyyy-MM-dd");
//System.out.println(moduleKey+":"+statMap);
if(statMap != null){
int statSize = statMap.size();
xAxis = new String[statSize];
//yAxis = new int[moduleSize][statSize];// 注意:不能在这里这样初始化二维数组
yAxis[i-1] = new int[statSize];// 初始化二维数组之创建第二维
int j = statSize;
for(Map.Entry<String, Integer> statEntry : statMap.entrySet()){
String statKey = statEntry.getKey();
Integer statValue = statEntry.getValue();
if("负面".equals(moduleKey)){
statValue = - statValue;
}
xAxis[--j] = DateUtil.format(DateUtil.parse(statKey, "yyyyMMdd"), "yyyy.MM.dd");
yAxis[i-1][j] = statValue;
//System.out.println(yAxis[i-1][j]);
if(statValue > max){
max = statValue;
}
if(statValue < min){
min = statValue;
}
}
}
}
}
resultMap.put("legend", legend);
resultMap.put("xAxis", xAxis);
resultMap.put("yAxis", yAxis);
resultMap.put("max", max);
resultMap.put("min", min);
return JsonMapper.toJson(resultMap);
}
/**
* string数组转int数组
* @param strAry
* @return
*/
private int[] stringAryToIntAry(String[] strAry){
int[] intAry = null;
if(strAry != null){
intAry = new int[strAry.length];
int i = 0;
for(; i < strAry.length; i++){
intAry[i] = Integer.parseInt(strAry[i]);
}
}
return intAry;
}
前端的echarts3.0代码
<script type="text/javascript">
// 获取正负面柱状图
function getBarZFChart(id, data){
var legend = [];// 图例
var xAxis = [];// x轴
var yAxis = [];// y轴
var series = [];
var itemStyle = {
normal: {
},
emphasis: {
barBorderWidth: 1,
shadowBlur: 10,
shadowOffsetX: 0,
shadowOffsetY: 0,
shadowColor: 'rgba(0,0,0,0.5)'
}
};
if(data){
legend = data["legend"];
xAxis = data["xAxis"];
yAxis = data["yAxis"];
for(var i=0,len=yAxis.length; i<len; i++){
var seriesItem = {
name: legend[i],
type: 'bar',
stack: 'one',
//itemStyle: itemStyle,
data: yAxis[i]
};
series.push(seriesItem);
}
}
option = {
//backgroundColor: '#eee',
legend: {
data: legend,
align: 'left',
left: 10
},
brush: {
toolbox: ['rect', 'polygon', 'lineX', 'lineY', 'keep', 'clear'],
xAxisIndex: 0
},
toolbox: {
feature: {
magicType: {
type: ['stack', 'tiled']
},
dataView: {}
}
},
//tooltip: {},
tooltip : {
trigger: 'axis',
axisPointer : { // 坐标轴指示器,坐标轴触发有效
type : 'shadow' // 默认为直线,可选为:'line' | 'shadow'
}
},
xAxis: {
data: xAxis,
name: '时间轴',
silent: false,
axisLine: {onZero: true},
splitLine: {show: false},
splitArea: {show: false}
},
yAxis: {
inverse: false,// y轴反转
splitArea: {show: false}
},
grid: {
left: 100
},
visualMap: {
type: 'continuous',
dimension: 1,
text: ['高', '低'],
inverse: false,
itemHeight: 200,
calculable: true,
min: data.min,
max: data.max,
top: 60,
left: 10,
inRange: {
colorLightness: [0.4, 0.8]
},
outOfRange: {
color: '#bbb'
},
controller: {
inRange: {
color: '#2f4554'
}
}
},
series: series
};
function renderBrushed(params) {
var brushed = [];
var brushComponent = params.batch[0];
for (var sIdx = 0; sIdx < brushComponent.selected.length; sIdx++) {
var rawIndices = brushComponent.selected[sIdx].dataIndex;
//brushed.push('[Series ' + sIdx + '] ' + rawIndices.join(', '));
brushed.push('[' + legend[sIdx] + '] ' + rawIndices.join(', '));
}
myChart.setOption({
title: {
backgroundColor: '#333',
//text: 'SELECTED DATA INDICES: \n' + brushed.join('\n'),
text: '选定的数据指标: \n' + brushed.join('\n'),
bottom: 0,
right: 0,
width: 100,
textStyle: {
fontSize: 12,
color: '#fff'
}
}
});
}
var myChart = echarts.init(document.getElementById(id));
myChart.setOption(option);
myChart.on('brushSelected', renderBrushed);
}
</script>