ECharts 上传图片Example

前端

1.为ECharts准备一个div

<div id="main" style="Height:400px"></div>

2.引入ECharts BaiDu CDN
<script src="http://echarts.baidu.com/build/dist/echarts.js"></script>
3.路径配置   
<script type="text/javascript">
 // 路径配置
require.config({
     paths: {
         echarts: 'http://echarts.baidu.com/build/dist'
      }
});
    
4.使用
    require(
        [
            'echarts',
            'echarts/chart/bar' // 使用柱状图就加载bar模块,按需加载
        ],
        function (ec) {
5.基于准备好的dom,初始化echarts图表
            var myChart = ec.init(document.getElementById('main'));
6.准备数据          
            var option = {
                    title : {
                        text: '某地区蒸发量和降水量',
                        subtext: '纯属虚构'
                    },
                    tooltip : {
                        trigger: 'axis'
                    },
                    legend: {
                        data:['蒸发量','降水量']
                    },
                    toolbox: {
                        show : true,
                        feature : {
                            mark : {show: true},
                            dataView : {show: true, readOnly: false},
                            magicType : {show: true, type: ['line', 'bar']},
                            restore : {show: true},
                            saveAsImage : {show: true}
                        }
                    },
                    calculable : false,
                        animation : false,
                    xAxis : [
                        {
                            type : 'category',
                            data : ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
                        }
                    ],
                    yAxis : [
                        {
                            type : 'value'
                        }
                    ],series : [
                            {
                                name:'蒸发量',
                                type:'bar',
                                    data:[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],
                                    markPoint : {
                                        data : [
                                            {type : 'max', name: '最大值'},
                                            {type : 'min', name: '最小值'}
                                        ]
                                    },
                                    markLine : {
                                        data : [
                                            {type : 'average', name: '平均值'}
                                        ]
                                    }
                                },
                                {
                                    name:'降水量',
                                    type:'bar',
                                    data:[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],
                                    markPoint : {
                                        data : [
                                            {name : '年最高', value : 182.2, xAxis: 7, yAxis: 183, symbolSize:18},
                                            {name : '年最低', value : 2.3, xAxis: 11, yAxis: 3}
                                        ]
                                    },
                                    markLine : {
                                        data : [
                                            {type : 'average', name : '平均值'}
                                        ]
                                    }
                                }
                            ]
                    };
            myChart.setOption(option);

7.通过ajax发送到后台           
            var data1 = myChart.getDataURL("png");
            $(function(){
                
                $('#b').click(function(){
             
                     $.ajax({
             
                         type: "POST",
             
                         url: "enterprise/image",
             
                         data: {a:data1},
             
                         success: function(data){alert(data);  }
                                  
                     });
             
                });
             
            });
            
            
        }
    );
    </script>
    <button id="b">上传</button>

后端

@RequestMapping("/imageUpload")
        public String image(String a) throws IOException{
            
                String[] url = a.split(",");
                String u = url[1];
                // Base64解码
                Base64 base64 = new Base64();  
                byte[] b = base64.decodeBase64(new String(u).getBytes());  // base64解码导入 import org.apache.commons.codec.binary.Base64;

       //maven方式导入<dependency>
                //<groupId>commons-codec</groupId>
                //<artifactId>commons-codec</artifactId>
                //<version>20041127.091804</version>
                //</dependency>  
                

                // 生成图片此处异常抛出,可自行捕获处理
                OutputStream out = new FileOutputStream(new  File("E:\\"+System.currentTimeMillis()+".png"));
                out.write(b);
                out.flush();
                out.close();
            
                return "1";
            
        }

欢迎交流沟通,共同进步!

posted @ 2017-04-17 16:09  丨Mars  阅读(1662)  评论(0编辑  收藏  举报