Echarts温度计 原生

<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8">

        <title>ECharts</title>

        <!-- 引入 echarts.js -->

        <script src="http://lib.baomitu.com/echarts/5.0.2/echarts.common.js""></script>

    </head>

    <body>

        <div id="main" style="width: 500px;height:500px;"></div>

        <script type="text/javascript">

            var myChart = echarts.init(document.getElementById('main'));

            var TP_value = 40;

            var kd = [];

            var Gradient = [];

            var leftColor = '';

            var showValue = '';

            var boxPosition = [65, 0];

            var TP_txt = ''

            for(var i = 0, len = 135; i <= len; i++) {

                if(i < 10 || i > 130) {

                    kd.push('')

                } else {

                    if((i - 10) % 20 === 0) {

                        kd.push('-3');

                    } else if((i - 10) % 4 === 0) {

                        kd.push('-1');

                    } else {

                        kd.push('');

                    }

                }

            }

            //中间线的渐变色和文本内容

            if(TP_value > 20) {

                TP_txt = '温度偏高';

                Gradient.push({

                    offset: 0,

                    color: '#93FE94'

                }, {

                    offset: 0.5,

                    color: '#E4D225'

                }, {

                    offset: 1,

                    color: '#E01F28'

                })

            } else if(TP_value > -20) {

                TP_txt = '温度正常';

                Gradient.push({

                    offset: 0,

                    color: '#93FE94'

                }, {

                    offset: 1,

                    color: '#E4D225'

                })

            } else {

                TP_txt = '温度偏低';

                Gradient.push({

                    offset: 1,

                    color: '#93FE94'

                })

            }

            if(TP_value > 62) {

                showValue = 62

            } else {

                if(TP_value < -60) {

                    showValue = -60

                } else {

                    showValue = TP_value

                }

            }

            if(TP_value < -10) {

                boxPosition = [65, -120];

            }

            leftColor = Gradient[Gradient.length - 1].color;

            // 因为柱状初始化为0,温度存在负值,所以加上负值60和空出距离10

            var option = {

                backgroundColor: '#0C2F6F',

                title: {

                    text: '温度计',

                    show: false

                },

                yAxis: [{

                    show: false,

                    data: [],

                    min: 0,

                    max: 135,

                    axisLine: {

                        show: false

                    }

                }, {

                    show: false,

                    min: 0,

                    max: 50,

                }, {

                    type: 'category',

                    data: ['', '', '', '', '', '', '', '', '', '', '°C'],

                    position: 'left',

                    offset: -80,

                    axisLabel: {

                        fontSize: 10,

                        color: 'white'

                    },

                    axisLine: {

                        show: false

                    },

                    axisTick: {

                        show: false

                    },

                }],

                xAxis: [{

                    show: false,

                    min: -10,

                    max: 80,

                    data: []

                }, {

                    show: false,

                    min: -10,

                    max: 80,

                    data: []

                }, {

                    show: false,

                    min: -10,

                    max: 80,

                    data: []

                }, {

                    show: false,

                    min: -5,

                    max: 80,

                }],

                series: [{

                    name: '条',

                    type: 'bar',

                    // 对应上面XAxis的第一个对)象配置

                    xAxisIndex: 0,

                    data: [{

                        value: (showValue + 70),

                        label: {

                            normal: {

                                show: true,

                                position: boxPosition,

                                backgroundColor: {

                                    image: ''",//文字框背景图

                                },

                                width: 200,

                                height: 100,

                                formatter: '{back| ' + TP_value + ' }{unit|°C}\n{downTxt|' + TP_txt + '}',

                                rich: {

                                    back: {

                                        align: 'center',

                                        lineHeight: 50,

                                        fontSize: 40,

                                        fontFamily: 'digifacewide',

                                        color: leftColor

                                    },

                                    unit: {

                                        fontFamily: '微软雅黑',

                                        fontSize: 15,

                                        lineHeight: 50,

                                        color: leftColor

                                    },

                                    downTxt: {

                                        lineHeight: 50,

                                        fontSize: 25,

                                        align: 'center',

                                        color: '#fff'

                                    }

                                }

                            }

                        }

                    }],

                    barWidth: 18,

                    itemStyle: {

                        normal: {

                            color: new echarts.graphic.LinearGradient(0, 1, 0, 0, Gradient)

                        }

                    },

                    z: 2

                }, {

                    name: '白框',

                    type: 'bar',

                    xAxisIndex: 1,

                    barGap: '-100%',

                    data: [134],

                    barWidth: 28,

                    itemStyle: {

                        normal: {

                            color: '#0C2E6D',

                            barBorderRadius: 50,

                        }

                    },

                    z: 1

                }, {

                    name: '外框',

                    type: 'bar',

                    xAxisIndex: 2,

                    barGap: '-100%',

                    data: [135],

                    barWidth: 38,

                    itemStyle: {

                        normal: {

                            color: '#4577BA',

                            barBorderRadius: 50,

                        }

                    },

                    z: 0

                }, {

                    name: '圆',

                    type: 'scatter',

                    hoverAnimation: false,

                    data: [0],

                    xAxisIndex: 0,

                    symbolSize: 48,

                    itemStyle: {

                        normal: {

                            color: '#93FE94',

                            opacity: 1,

                        }

                    },

                    z: 2

                }, {

                    name: '白圆',

                    type: 'scatter',

                    hoverAnimation: false,

                    data: [0],

                    xAxisIndex: 1,

                    symbolSize: 60,

                    itemStyle: {

                        normal: {

                            color: '#0C2E6D',

                            opacity: 1,

                        }

                    },

                    z: 1

                }, {

                    name: '外圆',

                    type: 'scatter',

                    hoverAnimation: false,

                    data: [0],

                    xAxisIndex: 2,

                    symbolSize: 70,

                    itemStyle: {

                        normal: {

                            color: '#4577BA',

                            opacity: 1,

                        }

                    },

                    z: 0

                }, {

                    name: '刻度',

                    type: 'bar',

                    yAxisIndex: 0,

                    xAxisIndex: 3,

                    label: {

                        normal: {

                            show: true,

                            position: 'left',

                            distance: 10,

                            color: 'white',

                            fontSize: 14,

                            formatter: function(params) {

                                if(params.dataIndex > 130 || params.dataIndex < 10) {

                                    return '';

                                } else {

                                    if((params.dataIndex - 10) % 20 === 0) {

                                        return params.dataIndex - 70;

                                    } else {

                                        return '';

                                    }

                                }

                            }

                        }

                    },

                    barGap: '-100%',

                    data: kd,

                    barWidth: 1,

                    itemStyle: {

                        normal: {

                            color: 'white',

                            barBorderRadius: 120,

                        }

                    },

                    z: 0

                }]

            };

            // 使用刚指定的配置项和数据显示图表。

            myChart.setOption(option);

        </script>

    </body>

</html>

要使用原生JavaScript复现ECharts柱图,你需要先引入ECharts库,并创建一个DOM元素作为图表的容器。然后,使用JavaScript代码配置图表的数据和样式,最后将图表渲染到容器中。 以下是一个简单的示例代码,演示如何使用原生JavaScript复现ECharts柱图: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ECharts柱图原生JavaScript复现</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> // 创建图表容器 var chartContainer = document.getElementById('chart'); // 初始化图表实例 var chart = echarts.init(chartContainer); // 配置图表的数据和样式 var option = { title: { text: '柱图示例' }, xAxis: { type: 'category', data: ['A', 'B', 'C', 'D', 'E'] }, yAxis: { type: 'value' }, series: [{ type: 'bar', data: [10, 20, 30, 40, 50] }] }; // 将配置应用于图表 chart.setOption(option); // 渲染图表 chart.resize(); </script> </body> </html> ``` 在上述示例中,我们首先引入了ECharts库的JavaScript文件,然后创建了一个id为"chart"的div作为图表容器。接下来,使用`echarts.init`方法初始化了一个图表实例,并通过`setOption`方法配置了图表的数据和样式。最后,调用`resize`方法将图表渲染到容器中。 你可以根据自己的需求修改示例代码中的数据和样式,来实现你想要的柱图效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值