高图仪表未显示

拉尔斯

我是使用HighCharts的新手。

我有一个问题,当页面上有多个仪表时,第一个仪表会消失。

这个例子很好用:

<%@ Page Language="C#" AutoEventWireup="true" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>
        HighChart test
    </title>  
    <script type="text/javascript" src="Scripts/jquery-2.1.1.min.js"></script>
    <script src="Scripts/highcharts/4.1.5/highcharts.js"></script>
    <script src="Scripts/highcharts/4.1.5/highcharts-more.js"></script>
    <script src="Scripts/highcharts/4.1.5/modules/solid-gauge.js"></script>    
</head>

<body style="background-color: lightgrey">
    <form runat="server">
        <div id="gauge1" style="width: 300px; height: 200px; float: left; margin: 10px;" data-highcharts-chart="0"></div>
    </form>
    <script>
        $(function () {

            var gaugeOptions = {

                chart: {
                    type: 'solidgauge'
                },

                title: null,

                pane: {
                    center: ['50%', '85%'],
                    size: '140%',
                    startAngle: -90,
                    endAngle: 90,
                    background: {
                        backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || '#EEE',
                        innerRadius: '60%',
                        outerRadius: '100%',
                        shape: 'arc'
                    }
                },

                tooltip: {
                    enabled: false
                },

                // the value axis
                yAxis: {
                    stops: [
                        [0.3, '#DF5353'], // red
                        [0.5, '#DDDF0D'], // yellow
                        [0.8, '#55BF3B'] // green
                    ],
                    lineWidth: 0,
                    minorTickInterval: null,
                    tickPixelInterval: 400,
                    tickWidth: 0,
                    title: {
                        y: -70
                    },
                    labels: {
                        y: 16
                    }
                },

                plotOptions: {
                    solidgauge: {
                        dataLabels: {
                            y: 5,
                            borderWidth: 0,
                            useHTML: true
                        }
                    }
                }
            };

            // The 1 gauge
            $('#gauge1').highcharts(Highcharts.merge(gaugeOptions, {
                yAxis: {
                    min: 0,
                    max: 100,
                    title: {
                        text: 'Gauge 1'
                    }
                },

                credits: {
                    enabled: false
                },

                series: [{
                    name: 'Gauge 1',
                    data: [0],
                    dataLabels: {
                        format: '<div style="text-align:center"><span style="font-size:25px;color:' +
                            ((Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black') + '">{y}&nbsp;%</span></div>'
                    },
                    tooltip: {
                        valueSuffix: ' %'
                    }
                }]

            }));

            // Bring to life 
            setInterval(function () {
                setValue('#gauge1');
            }, 2000);

            function setValue(div) {
                var chart = $(div).highcharts(), point, newVal, inc;

                if (chart) {
                    point = chart.series[0].points[0];
                    inc = Math.round((Math.random() - 0.5) * 100);
                    newVal = point.y + inc;

                    if (newVal < 0 || newVal > 100) {
                        newVal = point.y - inc;
                    }

                    point.update(newVal);
                }
            }
        });
    </script>
</body>
</html>

但是,当我添加另一个仪表时,第一个仪表消失了:

<%@ Page Language="C#" AutoEventWireup="true" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>
        HighChart test
    </title>  
    <script type="text/javascript" src="Scripts/jquery-2.1.1.min.js"></script>
    <script src="Scripts/highcharts/4.1.5/highcharts.js"></script>
    <script src="Scripts/highcharts/4.1.5/highcharts-more.js"></script>
    <script src="Scripts/highcharts/4.1.5/modules/solid-gauge.js"></script>    
</head>

<body style="background-color: lightgrey">
    <form runat="server">
        <div id="gauge1" style="width: 300px; height: 200px; float: left; margin: 10px;" data-highcharts-chart="0"></div>
        <div id="gauge2" style="width: 300px; height: 200px; float: left; margin: 10px;" data-highcharts-chart="0"></div>
    </form>
    <script>
        $(function () {

            var gaugeOptions = {

                chart: {
                    type: 'solidgauge'
                },

                title: null,

                pane: {
                    center: ['50%', '85%'],
                    size: '140%',
                    startAngle: -90,
                    endAngle: 90,
                    background: {
                        backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || '#EEE',
                        innerRadius: '60%',
                        outerRadius: '100%',
                        shape: 'arc'
                    }
                },

                tooltip: {
                    enabled: false
                },

                // the value axis
                yAxis: {
                    stops: [
                        [0.3, '#DF5353'], // red
                        [0.5, '#DDDF0D'], // yellow
                        [0.8, '#55BF3B'] // green
                    ],
                    lineWidth: 0,
                    minorTickInterval: null,
                    tickPixelInterval: 400,
                    tickWidth: 0,
                    title: {
                        y: -70
                    },
                    labels: {
                        y: 16
                    }
                },

                plotOptions: {
                    solidgauge: {
                        dataLabels: {
                            y: 5,
                            borderWidth: 0,
                            useHTML: true
                        }
                    }
                }
            };

            // The 1 gauge
            $('#gauge1').highcharts(Highcharts.merge(gaugeOptions, {
                yAxis: {
                    min: 0,
                    max: 100,
                    title: {
                        text: 'Gauge 1'
                    }
                },

                credits: {
                    enabled: false
                },

                series: [{
                    name: 'Gauge 1',
                    data: [0],
                    dataLabels: {
                        format: '<div style="text-align:center"><span style="font-size:25px;color:' +
                            ((Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black') + '">{y}&nbsp;%</span></div>'
                    },
                    tooltip: {
                        valueSuffix: ' %'
                    }
                }]

            }));

            // The 2 gauge
            $('#gauge2').highcharts(Highcharts.merge(gaugeOptions, {
                yAxis: {
                    min: 0,
                    max: 100,
                    title: {
                        text: 'Gauge 2'
                    }
                },

                credits: {
                    enabled: false
                },

                series: [{
                    name: 'Gauge 2',
                    data: [0],
                    dataLabels: {
                        format: '<div style="text-align:center"><span style="font-size:25px;color:' +
                            ((Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black') + '">{y}&nbsp;%</span></div>'
                    },
                    tooltip: {
                        valueSuffix: ' %'
                    }
                }]

            }));

            // Bring to life 
            setInterval(function () {
                setValue('#gauge1');
                setValue('#gauge2');
            }, 2000);

            function setValue(div) {
                var chart = $(div).highcharts(), point, newVal, inc;

                if (chart) {
                    point = chart.series[0].points[0];
                    inc = Math.round((Math.random() - 0.5) * 100);
                    newVal = point.y + inc;

                    if (newVal < 0 || newVal > 100) {
                        newVal = point.y - inc;
                    }

                    point.update(newVal);
                }
            }
        });
    </script>
</body>
</html>

任何人都知道我在做什么错。

这里是到JSFiddle的链接

示例1- https://jsfiddle.net/eszygfjb/1/

示例2- https://jsfiddle.net/2cfpL019/

马丁·施耐德(Martin Schneider)

这是因为您将data-highcharts-chart两个div元素上-Attribute设置为相同的值。据我所知,Highcharts在内部使用它来确定您可以访问Highcharts.charts-Array中的各个图表的顺序。

通过将它们都设置为零索引,基本上可以在创建第二个图表时覆盖第一个图表的对象实例。只需删除这些属性,两个压力表就可以正常显示。

<div id="gauge1" style="width: 300px; height: 200px; float: left; margin: 10px;"></div>
<div id="gauge2" style="width: 300px; height: 200px; float: left; margin: 10px;"></div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章