Echarts可视化图表

ECharts 特性

ECharts 包含了以下特性:

  • 丰富的可视化类型: 提供了常规的折线图、柱状图、散点图、饼图、K线图,用于统计的盒形图,用于地理数据可视化的地图、热力图、线图,用于关系数据可视化的关系图、treemap、旭日图,多维数据可视化的平行坐标,还有用于 BI 的漏斗图,仪表盘,并且支持图与图之间的混搭。

  • 多种数据格式无需转换直接使用: 内置的 dataset 属性(4.0+)支持直接传入包括二维表,key-value 等多种格式的数据源,此外还支持输入 TypedArray 格式的数据。

  • 千万数据的前端展现: 通过增量渲染技术(4.0+),配合各种细致的优化,ECharts 能够展现千万级的数据量。

  • 移动端优化: 针对移动端交互做了细致的优化,例如移动端小屏上适于用手指在坐标系中进行缩放、平移。 PC 端也可以用鼠标在图中进行缩放(用鼠标滚轮)、平移等。

  • 多渲染方案,跨平台使用: 支持以 Canvas、SVG(4.0+)、VML 的形式渲染图表。

  • 深度的交互式数据探索: 提供了 图例、视觉映射、数据区域缩放、tooltip、数据刷选等开箱即用的交互组件,可以对数据进行多维度数据筛取、视图缩放、展示细节等交互操作。

  • 多维数据的支持以及丰富的视觉编码手段: 对于传统的散点图等,传入的数据也可以是多个维度的。

  • 动态数据: 数据的改变驱动图表展现的改变。

  • 绚丽的特效: 针对线数据,点数据等地理数据的可视化提供了吸引眼球的特效。

  • 通过 GL 实现更多更强大绚丽的三维可视化: 在 VR,大屏场景里实现三维的可视化效果。

  • 无障碍访问(4.0+): 支持自动根据图表配置项智能生成描述,使得盲人可以在朗读设备的帮助下了解图表内容,让图表可以被更多人群访问!

Echarts实例:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>第一个 ECharts 实例</title>
    <!-- 引入 echarts.js -->
    <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
</head>
<body>
    <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
    <div id="main" style="width: 600px;height:400px;"></div>
    <script type="text/javascript">
        // 基于准备好的dom,初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));
 
        // 指定图表的配置项和数据
        var option = {
            title: {
                text: '第一个 ECharts 实例'
            },
            tooltip: {},
            legend: {
                data:['销量']
            },
            xAxis: {
                data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
            },
            yAxis: {},
            series: [{
                name: '销量',
                type: 'bar',
                data: [5, 20, 36, 10, 10, 20]
            }]
        };
 
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    </script>
</body>
</html>

Echarts旭日图:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ECharts 实例</title>
    <!-- 引入 echarts.js -->
    <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
</head>
<body>
    <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
    <div id="main" style="width: 600px;height:400px;"></div>
    <script type="text/javascript">
        // 基于准备好的dom,初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));
 
        var colors = ['#FFAE57', '#FF7853', '#EA5151', '#CC3F57', '#9A2555'];
        var bgColor = '#2E2733';

        var itemStyle = {
            star5: {
                color: colors[0]
            },
            star4: {
                color: colors[1]
            },
            star3: {
                color: colors[2]
            },
            star2: {
                color: colors[3]
            }
        };

        var data = [{
            name: '虚构',
            itemStyle: {
                normal: {
                    color: colors[1]
                }
            },
            children: [{
                name: '小说',
                children: [{
                    name: '5☆',
                    children: [{
                        name: '疼'
                    }, {
                        name: '慈悲'
                    }, {
                        name: '楼下的房客'
                    }]
                }, {
                    name: '4☆',
                    children: [{
                        name: '虚无的十字架'
                    }, {
                        name: '无声告白'
                    }, {
                        name: '童年的终结'
                    }]
                }, {
                    name: '3☆',
                    children: [{
                        name: '疯癫老人日记'
                    }]
                }]
            }, {
                name: '其他',
                children: [{
                    name: '5☆',
                    children: [{
                        name: '纳博科夫短篇小说全集'
                    }]
                }, {
                    name: '4☆',
                    children: [{
                        name: '安魂曲'
                    }, {
                        name: '人生拼图版'
                    }]
                }, {
                    name: '3☆',
                    children: [{
                        name: '比起爱你,我更需要你'
                    }]
                }]
            }]
        }, {
            name: '非虚构',
            itemStyle: {
                color: colors[2]
            },
            children: [{
                name: '设计',
                children: [{
                    name: '5☆',
                    children: [{
                        name: '无界面交互'
                    }]
                }, {
                    name: '4☆',
                    children: [{
                        name: '数字绘图的光照与渲染技术'
                    }, {
                        name: '日本建筑解剖书'
                    }]
                }, {
                    name: '3☆',
                    children: [{
                        name: '奇幻世界艺术\n&RPG地图绘制讲座'
                    }]
                }]
            }, {
                name: '社科',
                children: [{
                    name: '5☆',
                    children: [{
                        name: '痛点'
                    }]
                }, {
                    name: '4☆',
                    children: [{
                        name: '卓有成效的管理者'
                    }, {
                        name: '进化'
                    }, {
                        name: '后物欲时代的来临',
                    }]
                }, {
                    name: '3☆',
                    children: [{
                        name: '疯癫与文明'
                    }]
                }]
            }, {
                name: '心理',
                children: [{
                    name: '5☆',
                    children: [{
                        name: '我们时代的神经症人格'
                    }]
                }, {
                    name: '4☆',
                    children: [{
                        name: '皮格马利翁效应'
                    }, {
                        name: '受伤的人'
                    }]
                }, {
                    name: '3☆',
                }, {
                    name: '2☆',
                    children: [{
                        name: '迷恋'
                    }]
                }]
            }, {
                name: '居家',
                children: [{
                    name: '4☆',
                    children: [{
                        name: '把房子住成家'
                    }, {
                        name: '只过必要生活'
                    }, {
                        name: '北欧简约风格'
                    }]
                }]
            }, {
                name: '绘本',
                children: [{
                    name: '5☆',
                    children: [{
                        name: '设计诗'
                    }]
                }, {
                    name: '4☆',
                    children: [{
                        name: '假如生活糊弄了你'
                    }, {
                        name: '博物学家的神秘动物图鉴'
                    }]
                }, {
                    name: '3☆',
                    children: [{
                        name: '方向'
                    }]
                }]
            }, {
                name: '哲学',
                children: [{
                    name: '4☆',
                    children: [{
                        name: '人生的智慧'
                    }]
                }]
            }, {
                name: '技术',
                children: [{
                    name: '5☆',
                    children: [{
                        name: '代码整洁之道'
                    }]
                }, {
                    name: '4☆',
                    children: [{
                        name: 'Three.js 开发指南'
                    }]
                }]
            }]
        }];

        for (var j = 0; j < data.length; ++j) {
            var level1 = data[j].children;
            for (var i = 0; i < level1.length; ++i) {
                var block = level1[i].children;
                var bookScore = [];
                var bookScoreId;
                for (var star = 0; star < block.length; ++star) {
                    var style = (function (name) {
                        switch (name) {
                            case '5☆':
                                bookScoreId = 0;
                                return itemStyle.star5;
                            case '4☆':
                                bookScoreId = 1;
                                return itemStyle.star4;
                            case '3☆':
                                bookScoreId = 2;
                                return itemStyle.star3;
                            case '2☆':
                                bookScoreId = 3;
                                return itemStyle.star2;
                        }
                    })(block[star].name);

                    block[star].label = {
                        color: style.color,
                        downplay: {
                            opacity: 0.5
                        }
                    };

                    if (block[star].children) {
                        style = {
                            opacity: 1,
                            color: style.color
                        };
                        block[star].children.forEach(function (book) {
                            book.value = 1;
                            book.itemStyle = style;

                            book.label = {
                                color: style.color
                            };

                            var value = 1;
                            if (bookScoreId === 0 || bookScoreId === 3) {
                                value = 5;
                            }

                            if (bookScore[bookScoreId]) {
                                bookScore[bookScoreId].value += value;
                            }
                            else {
                                bookScore[bookScoreId] = {
                                    color: colors[bookScoreId],
                                    value: value
                                };
                            }
                        });
                    }
                }

                level1[i].itemStyle = {
                    color: data[j].itemStyle.color
                };
            }
        }

        option = {
            backgroundColor: bgColor,
            color: colors,
            series: [{
                type: 'sunburst',
                center: ['50%', '48%'],
                data: data,
                sort: function (a, b) {
                    if (a.depth === 1) {
                        return b.getValue() - a.getValue();
                    }
                    else {
                        return a.dataIndex - b.dataIndex;
                    }
                },
                label: {
                    rotate: 'radial',
                    color: bgColor
                },
                itemStyle: {
                    borderColor: bgColor,
                    borderWidth: 2
                },
                levels: [{}, {
                    r0: 0,
                    r: 40,
                    label: {
                        rotate: 0
                    }
                }, {
                    r0: 40,
                    r: 105
                }, {
                    r0: 115,
                    r: 140,
                    itemStyle: {
                        shadowBlur: 2,
                        shadowColor: colors[2],
                        color: 'transparent'
                    },
                    label: {
                        rotate: 'tangential',
                        fontSize: 10,
                        color: colors[0]
                    }
                }, {
                    r0: 140,
                    r: 145,
                    itemStyle: {
                        shadowBlur: 80,
                        shadowColor: colors[0]
                    },
                    label: {
                        position: 'outside',
                        textShadowBlur: 5,
                        textShadowColor: '#333',
                    },
                    downplay: {
                        label: {
                            opacity: 0.5
                        }
                    }
                }]
            }]
        };

 
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    </script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ECharts 是一款基于 JavaScript 的开源可视化图表库,支持多种类型的图表,如折线图、柱状图、饼图等。下面简单介绍一下使用 ECharts 实现可视化图表的步骤: 1. 引入 ECharts 库 在 HTML 文件中引入 ECharts 库,可以从官网下载或使用 CDN 引入。例如: ```html <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script> ``` 2. 准备数据 准备需要展示的数据,可以是数组、对象等形式。例如: ```javascript const data = [ { name: '北京', value: 100 }, { name: '上海', value: 200 }, { name: '广州', value: 150 }, { name: '深圳', value: 180 } ]; ``` 3. 创建容器 在 HTML 文件中创建一个容器,用于展示图表。例如: ```html <div id="chart" style="width: 600px; height:400px"></div> ``` 4. 初始化图表 在 JavaScript 文件中获取容器对象,并使用 ECharts 的 `init` 方法初始化图表。例如: ```javascript const chart = echarts.init(document.getElementById('chart')); ``` 5. 配置图表 使用 ECharts 的配置项配置图表,如设置图表类型、数据、颜色、样式等。例如: ```javascript const option = { title: { text: '城市销售情况' }, tooltip: {}, xAxis: { data: data.map(item => item.name) }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: data.map(item => item.value), itemStyle: { color: '#3398DB' } }] }; ``` 6. 渲染图表 使用 ECharts 的 `setOption` 方法渲染图表。例如: ```javascript chart.setOption(option); ``` 完整的代码如下: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>ECharts Demo</title> <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script> </head> <body> <div id="chart" style="width: 600px; height:400px"></div> <script> const data = [ { name: '北京', value: 100 }, { name: '上海', value: 200 }, { name: '广州', value: 150 }, { name: '深圳', value: 180 } ]; const chart = echarts.init(document.getElementById('chart')); const option = { title: { text: '城市销售情况' }, tooltip: {}, xAxis: { data: data.map(item => item.name) }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: data.map(item => item.value), itemStyle: { color: '#3398DB' } }] }; chart.setOption(option); </script> </body> </html> ``` 这是一个简单的柱状图示例,实际上 ECharts 还支持更多类型的图表和更多的配置项。需要根据实际需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值