用Echart创建简单的折线图

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>机组耗电量统计图</title>

<link rel="stylesheet" href="${path }/public/layui/css/layui.css">

<link rel="stylesheet" href="${path }/public/css/style.css">

<script type="text/javascript" src="${path }/public/layui/layui.js"></script>

<script src="${path }/public/layui/lay/lib/jquery.js"></script>

<script src="${path }/public/layui/lay/dest/layui.all.js"></script>

<script src="${path }/public/js/jquery-1.4.4.min.js"></script>

<script>

layui.use(‘element‘, function() {

var $ = layui.jquery, element = layui.element(); //Tab的切换功能,切换事件监听等,需要依赖element模块

//触发事件

var active = {

tabAdd : function() {

//新增一个Tab项

element.tabAdd(‘demo‘, {

title : ‘新选项‘ + (Math.random() * 1000 | 0) //用于演示

,

content : ‘内容‘ + (Math.random() * 1000 | 0)

})

},

tabDelete : function() {

//删除指定Tab项

element.tabDelete(‘demo‘, 2); //删除第3项(注意序号是从0开始计算)

},

tabChange : function() {

//切换到指定Tab项

element.tabChange(‘demo‘, 1); //切换到第2项(注意序号是从0开始计算)

}

};

$(‘.site-demo-active‘).on(‘click‘, function() {

var type = $(this).data(‘type‘);

active[type] ? active[type].call(this) : ‘‘;

});

});

</script>

</head>

<body>

<div class="layui-tab layui-tab-card">

<ul class="layui-tab-title">

<li id="power_day">日</li>

<li id="power_month">月</li>

<li id="power_year">年</li>

</ul>

<div class="layui-tab-content" style="height: 350px;">

<div class="layui-tab-item layui-show">

<div id="mainone" style="height:380px"></div>

</div>

<div class="layui-tab-item layui-show">

<div id="maintwo" style="height:380px"></div>

</div>

<div class="layui-tab-item layui-show">

<div id="mainthree" style="height:380px"></div>

</div>

</div>

</div>

</body>

<!-- ECharts单文件引入 -->

<script src="${path }/public/js/echarts.js"></script>

<script type="text/javascript">

// 路径配置

require.config({

paths : {

echarts : ‘http://echarts.baidu.com/build/dist‘

}

});

// 使用

require([ ‘echarts‘, ‘echarts/chart/line‘ // 使用柱状图就加载bar模块,按需加载 (需修改为相应的图形名称)

], function(ec) {

// 基于准备好的dom,初始化echarts图表

var myChart = ec.init(document.getElementById(‘mainone‘));

var myChartMonth = ec.init(document.getElementById(‘maintwo‘));

var myChartYear = ec.init(document.getElementById(‘mainthree‘));

loadEchartPower(myChart,1);

//日耗电量

var dayObj = document.getElementById("power_day");

dayObj.addEventListener("click", function(){

loadEchartPower(myChart,1);

}, true);

//月耗电

var monthObj = document.getElementById("power_month");

monthObj.addEventListener("click", function(){

loadEchartPower(myChartMonth,2);

}, true);

//年耗电

var yearObj = document.getElementById("power_year");

yearObj.addEventListener("click", function(){

loadEchartPower(myChartYear,3);

}, true);

});

//机组及蒸发器功率

function loadEchartPower(myChart, type){

var index = layer.load(2, {shade: false},{time: 5*1000}); //0代表加载的风格,支持0-2   添加加载层

$.ajax({

url:path+‘/chart/aircrewTmep‘,

dataType:‘json‘,

type:‘post‘,

data:{coding:‘${coding}‘,type:type},

success:function(data){

//console.log(JSON.stringify(data.series));

//console.log(data.legend);

layer.close(index); //关闭加载层

/*

var legendArr = [‘蒸发器1‘,‘蒸发器2‘,‘蒸发器3‘,‘机组1‘];

var seriesArr = [{"name":"蒸发器1","type":"line","stack":null,

"data":["0.5","1.00","1.20","1.00","2.50","1.50","4.00","2.50","3.00","1.00","2.00","1.00","2.30","4.00"],"smooth":false},

{"name":"蒸发器2","type":"line","stack":null,

"data":["1.00","1.2","1.5","1.8","2.0","1.5","1.2","2.2","2.3","1.5","2.5","2.8","3.0","3.5"],"smooth":false},

{"name":"蒸发器3","type":"line","stack":null,

"data":["1.2","1.5","1.8","2.0","1.8","1.5","2.2","2.5","2.7","2.0","1.9","2.63","2.8","3.7"],"smooth":false},

{"name":"机组1","type":"line","stack":null,

"data":["2.0","1.2","1.5","1.8","2.1","1.6","1.3","1.8","1.4","1.5","1.6","2.3","3.5","4.0"],"smooth":false}];

*/

option = {

title : {

text : ‘机组耗电量统计图‘,

subtext : ‘时间/耗电量‘

},

tooltip : {

trigger : ‘axis‘

},

legend : {

data : data.legend

},

toolbox: {

show : true,

feature : {

mark : {show: true},

dataView : {show: true, readOnly: false},

magicType : {show: true, type: [‘line‘, ‘bar‘, ‘stack‘, ‘tiled‘]},

restore : {show: true},

saveAsImage : {show: true}

}

},

calculable : true,

xAxis : [ {

type : ‘category‘,

boundaryGap : false,

data : data.time

} ],

yAxis : [ {

type : ‘value‘

} ],

series : data.series,

};

// 为echarts对象加载数据

myChart.setOption(option);

}

});

}

</script>

</html>

<!-----------------------------JAVA后台代码----------------------------------------->

@Controller

@RequestMapping("/chart")

public class ChartCtrl {

@Autowired

private ChartService chartService;

/**

* 机组耗电量

*/

@RequestMapping("/aircrewTmep")

@ResponseBody

public Map<String, Object> aircrewTmep(HttpSession session,HttpServletRequest req){

HashMap<String, Object> rlt = new HashMap<>();

try {

Object identityCode = session.getAttribute("identity_code");

if(identityCode==null||"".equals(identityCode)){

return null;

}

String type = req.getParameter("type");

HashMap<String, String> map = new HashMap<>();

map.put("identity_code", identityCode.toString());

map.put("coding", req.getParameter("coding"));

rlt=chartService.aircrewTmep(map, type);

} catch (Exception e) {

e.printStackTrace();

}

return rlt;

}

}

@Service

public class ChartService {

public HashMap<String, Object> aircrewTmep(HashMap<String, String> map,String type){

HashMap<String, Object> rlt = new HashMap<>();

List<ChactSeriesVO> chartserieslist = new ArrayList<>();

List<String> legendlist = new ArrayList<>();

String json = null;

String[] timeArr = null;

try {

if(1==Integer.parseInt(type)){ //日

map.put("day", DateTools.createDate());

json = HttpClientUtil.httpPost("http://localhost:8080/test/app/running/day/", map, "utf-8");

}else if(2==Integer.parseInt(type)){ //月

map.put("day", DateTools.yearMonth());

json = HttpClientUtil.httpPost("http://localhost:8080/test/app/running/month/", map, "utf-8");

}else{ //年

map.put("day", DateTools.nowYear());

json = HttpClientUtil.httpPost("http://localhost:8080/test/app/running/year/", map, "utf-8");

}

System.out.println("---json---"+json);

if(json != null){

ObjectMapper mapper = new ObjectMapper();

JsonNode tree = mapper.readTree(json);

JsonNode evaporatorList = tree.findValue("data").findPath("evaporator");

for (int i = 0; i < evaporatorList.size(); i++) {

JsonNode chart = evaporatorList.get(i);

JsonNode evaplist = chart.findPath("power");

ChactSeriesVO aevo = new ChactSeriesVO();

aevo.setName(chart.get("title").asText());

aevo.setType("line");

aevo.setSmooth(false);

aevo.setData(tempArr(evaplist));

chartserieslist.add(aevo);

legendlist.add(chart.get("title").asText());

}

JsonNode aircrewlist = tree.findValue("data").findValue("aircrew").findPath("power");

ChactSeriesVO tempaevo = new ChactSeriesVO();

tempaevo.setName(tree.findValue("data").findValue("aircrew").findValue("title").asText());

tempaevo.setType("line");

tempaevo.setSmooth(false);

tempaevo.setData(tempArr(aircrewlist));

chartserieslist.add(tempaevo);

legendlist.add(tree.findValue("data").findValue("aircrew").findValue("title").asText());

JsonNode timelist = tree.findValue("data").findPath("time");

timeArr = new String[timelist.size()];

for (int m = 0; m < timelist.size(); m++) {

String strtime = DateTools.stampToDate(timelist.get(m).asText());

if(1==Integer.parseInt(type)){

timeArr[m] = DateTools.strToHour(strtime);

}else if(2==Integer.parseInt(type)){

timeArr[m] = DateTools.strToDate(strtime);

}else{

timeArr[m] = DateTools.strToMonth(strtime);

}

}

//System.out.println("---evaporatorList--"+evaporatorList);

}

} catch (Exception e) {

e.printStackTrace();

}

rlt.put("time", timeArr);

rlt.put("series", chartserieslist);

rlt.put("legend", legendlist);

return rlt;

}

//字符串数组赋值

public String[] tempArr(JsonNode templist){

String[] powerarr = new String[templist.size()];

for (int i = 0; i < templist.size(); i++) {

powerarr[i] = new String(templist.get(i).asText());

}

return powerarr;

}

}

/*****************----series对应的实体类----固定字段---方便页面取值-----***********************/

package com.shangyukeji.icoolcloud.vo;

public class ChactSeriesVO {

private String name;

private String type;

private String stack;

private String[] data;

private boolean smooth;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public String getStack() {

return stack;

}

public void setStack(String stack) {

this.stack = stack;

}

public String[] getData() {

return data;

}

public void setData(String[] data) {

this.data = data;

}

public boolean isSmooth() {

return smooth;

}

public void setSmooth(boolean smooth) {

this.smooth = smooth;

}

}

时间: 2024-10-11 21:14:14

用Echart创建简单的折线图的相关文章

ASP.NET MVC中使用highcharts 生成简单的折线图

? ? 直接上步骤: ? 生成一个options,选项包含了一些基本的配置,如标题,坐标刻度,serial等: 配置X轴显示的Category数据,为一个数组: 配置Y轴显示的数据,也为一个数据: 用生成option构建一个Hightcharts对象,即可以自动画出一个折线图了: ? ? 1.配置BundleConfig ? bundles.Add(new ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-{

可视化数据matplotlib之安装与简单折线图

matplotlib是一个可视化数据的模块,安装前需要先安装Visual Studio Community:然后去https://pypi.python.org/pypi上查找matplotlib并下载:最后安装:python -m pip install matplotlib-2.0.2-cp36-cp36m-win_amd64.whl: (安装过程中需要联网下载相关模块) 本例使用matplotlib模块画简单的折线图: 1 from matplotlib import pyplot 2 f

Python中使用matplotlib 如何绘制折线图?

本文和大家分享的主要是python开发中matplotlib 绘制折线图相关内容,一起来看看吧,希望对大家学习和使用这部分内容有所帮助. matplotlib 1.安装matplotlib ① linux系统安装 # 安装matplotlib模块 $ sudo apt-get install python3-matplotlib# 如果是python2.7 执行如下命令 $ sudo apt-get install python-matplotlib# 如果你安装较新的Python,安装模块一乐

【Python】matplotlib绘制折线图

一.绘制简单的折线图 import matplotlib.pyplot as plt squares=[1,4,9,16,25] plt.plot(squares) plt.show() 我们首先导入模块pylot,并给他指定别名plt,然后创建列表,存储前述的平方数,再将这个列表传递给函数plot(),这个函数尝试根据这些数字绘制出有意义的图形.plot.show()打开matplotlib查看器,并显示绘制图形. 运行结果: 二.修改标签文字和线条粗细 #coding:UTF-8 impor

R in action读书笔记(15)第十一章 中级绘图 之二 折线图 相关图 马赛克图

第十一章 中级绘图 本节用到的函数有: plot legend corrgram mosaic 11.2折线图 如果将散点图上的点从左往右连接起来,那么就会得到一个折线图. 创建散点图和折线图: > opar<-par(no.readonly=TRUE) > par(mfrow=c(1,2)) > t1<-subset(Orange,Tree==1) > plot(t1$age,t1$circumference,xlab="Age(days)",yl

JavaScript数据可视化编程学习(一)Flotr2,包含简单的,柱状图,折线图,饼图,散点图

一.基础柱状图 二.基础的折线图 三.基础的饼图 四.基础的散点图 一.基础柱状图 如果你还没有想好你的数据用什么类型的图表来展示你的数据,你应该首先考虑是否可以做成柱状图.柱状图可以表示数据的变化过程或者表示多个数据之间的差异. 1.引入javascrippt 使用flotr2这个JavaScript库来创建图表.使用flotr2之前,不需要引入其他的JavaScript库(比如jquery),但是flotr2必须依赖HTML5的canvas元素的支持.canvas支持的主流浏览器有:chro

echart 折线图、柱状图、饼图、环形图颜色修改

之前在做报表的时候用过echart 用完也就完了,而这次在用的时候已经忘了,所以这里简单记录一下,好记性不如烂笔头!!! 1.折线图修改颜色: [javascript] view plain copy xAxis: { type: 'category', boundaryGap: false, data: ['年龄','20岁以下','30岁','40岁','50岁','60岁','60岁以上'] }, yAxis: { type: 'value' }, series: [ { name:'员工

利用Jpgraph创建折线图

在企业运营中,经常需要对各种数据进行统计,利用图表动态分析不同的数据表中的数据,可以使数据显示的更加直观. 例:应用Jpgraph技术绘制包含两种图书销售走势的折线图. 为了能够使用Jpgraph的功能,首先在程序中导入Jpgraph类库,然后创建两个数组分别表示两种图书的年度销售情况,创建Graph类的对象,并用创建的数组作为参数创建两个LinePlot类的对象.设置统计图的标题.刻度.背景色和折线颜色等参数,并将两个LinePlot对象添加到统计图对象中. 过程如下: (1)在程序中导入Jp

Echart饼图、柱状图、折线图(pie、bar、line)添加点击事件

var myChart= echarts.init(document.getElementById('myChart')); myChart.on('click', function (param) { alert('点击了我!'); }); Echart事件参数可以去config.js查找 Echart饼图.柱状图.折线图(pie.bar.line)添加点击事件