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

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

转载于:https://www.cnblogs.com/gaoyawei/p/6723416.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值