温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

H5跨平台开发app之横竖屏导致的图表混乱问题

发布时间:2020-07-04 21:27:48 阅读:590 作者:IT学无止境 栏目:开发技术
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在跨平台开发中,犹豫使用的是非原生开发语言,会导致很多不方便的地方,最近公司做一个统计图表的东西,如果用原生开发的话比较容易控制图表显示时手机屏幕的方向,在跨平台开发中比较麻烦,

如果用户打开了手机屏幕自动旋转,有可能导致图表显示的错误。

解决方案:

<!DOCTYPE html>

<html lang='en'>

    <head>
        <meta charset='UTF-8'>
        <title>ichartjs designer</title>
        <script src='http://www.ichartjs.com/ichart.latest.min.js'></script>
        <script src="js/jquery-2.1.1.min.js"></script>
        <script type="text/javascript">
            var evt = "onorientationchange" in window ? "orientationchange" : "resize";

            window.addEventListener(evt, function() {
                console.log(evt);
                var width = document.documentElement.clientWidth;
                var height = document.documentElement.clientHeight;
                $print = $('#ichart-render');
                if(width > height) {
                    $print.width(width);
                    $print.height(height);
                    $print.css('top'0);
                    $print.css('left'0);
                    $print.css('transform''none');
                    $print.css('transform-origin''50% 50%');
                    //这里取得了图表显示控件后要对其进行设置CSS,否则来回旋转将导致布局错误
                    var x = document.getElementById("ichart-render");
                    x.style.position = "absolute";
                    x.style.top = "20%";
                } else {
                    $print.width(height);
                    $print.height(width);
                    $print.css('top'0);
                    $print.css('left'0);
                    $print.css('transform''none');
                    $print.css('transform-origin''50% 50%');
                }

            }, false);
        </script>
        <script type='text/javascript'>
            var w = document.documentElement.clientWidth;
            var h = document.documentElement.clientHeight;
            var diameter = Math.min(w, h);
            //这里获得了手机屏幕的宽高后,为了旋转手机的时候导致界面不混乱,需要将显
            示的图表设置为宽高最小的正方形(取巧)
            console.log(diameter);
            $(function({
                var chart = iChart.create({
                    render"ichart-render",
                    width: w,
                    height: w,
                    background_color"#fefefe",
                    gradientfalse,
                    color_factor0.2,
                    border: {
                        color"BCBCBC",
                        width1
                    },
                    align"center",
                    offsetx0,
                    offsety0,
                    sub_option: {
                        border: {
                            color"#BCBCBC",
                            width1
                        },
                        label: {
                            fontweight500,
                            fontsize11,
                            color"#4572a7",
                            sign"square",
                            sign_size12,
                            border: {
                                color"#BCBCBC",
                                width1
                            },
                            background_color"#fefefe"
                        }
                    },
                    shadowtrue,
                    shadow_color"#666666",
                    shadow_blur2,
                    showpercentfalse,
                    column_width"70%",
                    bar_height"70%",
                    radius"90%",
                    title: {
                        text"利用ichartjs制作漂亮图表",
                        color"#111111",
                        fontsize20,
                        font"微软雅黑",
                        textAlign"center",
                        height30,
                        offsetx0,
                        offsety0
                    },
                    subtitle: {
                        text"",
                        color"#111111",
                        fontsize16,
                        font"微软雅黑",
                        textAlign"center",
                        height20,
                        offsetx0,
                        offsety0
                    },
                    footnote: {
                        text"",
                        color"#111111",
                        fontsize12,
                        font"微软雅黑",
                        textAlign"right",
                        height20,
                        offsetx0,
                        offsety0
                    },
                    legend: {
                        enablefalse,
                        background_color"#fefefe",
                        color"#333333",
                        fontsize12,
                        border: {
                            color"#BCBCBC",
                            width1
                        },
                        column1,
                        align"right",
                        valign"center",
                        offsetx0,
                        offsety0
                    },
                    coordinate: {
                        width"80%",
                        height"84%",
                        background_color"#ffffff",
                        axis: {
                            color"#a5acb8",
                            width: [1""1""]
                        },
                        grid_color"#d9d9d9",
                        label: {
                            fontweight500,
                            color"#666666",
                            fontsize11
                        }
                    },
                    label: {
                        fontweight500,
                        color"#666666",
                        fontsize11
                    },
                    type"pie2d",
                    data: [{
                        name"名称A",
                        value20,
                        color"#4572a7"
                    }, {
                        name"名称B",
                        value30,
                        color"#aa4643"
                    }, {
                        name"名称C",
                        value40,
                        color"#89a54e"
                    }, {
                        name"新增",
                        value10,
                        color"#4572a7"
                    }]
                });
                chart.draw();
            });
        </script>
        <style type="text/css">
            #ichart-render {
                position: absolute;
                top: 20%;
            }
        </style>
    </head>

    <body >
        <div id='ichart-render'></div>
    </body>

</html>

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×