zoukankan      html  css  js  c++  java
  • 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";
                
            }

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

    欢迎指正,交流沟通,共同进步!对您有帮助的话点下推荐~~
  • 相关阅读:
    003-notepad++插件
    002-notepad++语言,编码,字体,背景色
    001-notepad++下载安装、添加右键
    015-elasticsearch5.4.3【五】-搜索API【四】Joining 多文档查询、GEO查询、moreLikeThisQuery、script脚本查询、span跨度查询
    014-elasticsearch5.4.3【五】-搜索API【三】复合查询boolQuery、constantScoreQuery、disMaxQuery
    013-elasticsearch5.4.3【五】-搜索API【二】term术语查询-termQuery、rangeQuery、existsQuery、prefixQuery、wildcardQuery、regexpQuery、fuzzyQuery
    012-elasticsearch5.4.3【五】-搜索API【一】搜索匹配所有matchAllQuery、全文查询[matchQuery、multiMatchQuery、commonTermsQuery、queryStringQuery、simpleQueryStringQuery]
    011-elasticsearch5.4.3【四】-聚合操作【二】-桶聚合【bucket】过滤、嵌套、反转、分组、排序、范围
    010-elasticsearch5.4.3【四】-聚合操作【一】-度量聚合【metrics】-min、max、sum、avg、count
    009-elasticsearch5.4.3【三】搜索概述-查询模型、分页、ES数据类型
  • 原文地址:https://www.cnblogs.com/gaoyawei/p/6723416.html
Copyright © 2011-2022 走看看