开发者社区> 问答> 正文

echart 在ascx控件中使用?报错

@Kener-林峰 目前新建一个web,在aspx页面可以画出图形,放到ascx中就会报错,提示require未定义,

请问怎么在ascx控件中使用echarts啊?

展开
收起
爱吃鱼的程序员 2020-06-22 15:16:00 586 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    require没定义,模块加载器用的是啥?加进去了吗?

    @Kener-林峰我是把require.js跟echarts放在同一个目录下,下面这段代码只取 <script>里的内容放到ascx控件里,再加上src="../../../js/require.js"不报错了,但是图出不来,帮忙看下,谢谢。

    <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>

    <!DOCTYPEhtml>
    <htmllang="en">
    <head>
       <metacharset="utf-8"/>
       <title>ECharts</title>
       <!--Step:1Importamoduleloader,suchasesl.jsorrequire.js-->
       <!--Step:1引入一个模块加载器,如esl.js或者require.js-->
       <scripttype="text/javascript"src="js/echarts/doc/asset/js/esl/esl.js"></script>
    </head>

    <body>
       <!--Step:2PrepareadomforEChartswhich(must)hassize(width&hight)-->
       <!--Step:2为ECharts准备一个具备大小(宽高)的Dom-->
       <divid="main"style="height:500px;border:1pxsolid#ccc;padding:10px;"></div>
       
       <scripttype="text/javascript">
           //Step:3conifgECharts'spath,linktoecharts.jsfromcurrentpage.
           //Step:3为模块加载器配置echarts的路径,从当前页面链接到echarts.js,定义所需图表路径
           require.config({
               packages:[
           {
               name:'echarts',
               location:'js/echarts/src',
               main:'echarts'
           },
           {
               name:'zrender',
               location:'js/zrender/src',//zrender与echarts在同一级目录
               main:'zrender'
           }
       ]
           });


           //Step:4requireechartsanduseitinthecallback.
           //Step:4动态加载echarts然后在回调函数中开始使用,注意保持按需加载结构定义图表路径
           require(
           [
               'echarts',
               'echarts/chart/bar',
              'echarts/chart/line'
           ],
           function(ec){
               varmyChart=ec.init(document.getElementById('main'));
               varoption={
                   tooltip:{
                       trigger:'axis'
                   },
                   legend:{
                       data:['蒸发量','降水量']
                   },
                   toolbox:{
                       show:true,
                       feature:{
                           mark:true,
                           dataView:{readOnly:false},
                           magicType:['line','bar'],
                           restore:true,
                           saveAsImage:true
                       }
                   },
                   calculable:true,
                   xAxis:[
                       {
                           type:'category',
                           data:['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
                       }
                   ],
                   yAxis:[
                       {
                           type:'value',
                           splitArea:{show:true}
                       }
                   ],
                   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]
                       },
                       {
                           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]
                       }
                   ]
               };

               myChart.setOption(option);
           }
       );
       </script>
    </body>
    </html>

     

    head里引入esl,script里就是packet路径问题了,看看network里404请求就明白了解决了,是路径问题
    2020-06-22 15:16:17
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载