html按钮没有触发事件到必需的图形容器触发到Highchart JS

时间:2017-09-01 09:58:58

标签: javascript jquery html highcharts

我在2个不同的div(container1,container2)中有2个图表,并有两组按钮。两组按钮仅触发图形2。当单击图1的按钮时,它应该更改图形1,当我单击图形2按钮时,它应该更改图形2.但是图形1和图形2按钮都触发到仅限图2。

JS小提琴代码http://jsfiddle.net/h5t4c8hj/7/

 var chart = Highcharts.chart('container', {
    title: {
        text: 'Graph 1'
    },
    subtitle: {
        text: 'plain'
    },
    xAxis: {
        categories: ['Bin1s', 'Bin2', 'Bin3', 'Bin4', 'Bin5', 'Bin6', 'Bin7', 'Bin8', 'Bin9', 'Bin10']
    },
    series: [{
        type: 'column',
        colorByPoint: false,
        data: [22.3, 42.3, 96.4, 29.2, 44.0, 76.0, 35.6, 48.5, 16.4, 92.3],
        showInLegend: false
    }],
    legend: {
        enabled: false
    },
    plotOptions: {
        series: {
            borderWidth: 0,
            dataLabels: {
                enabled: true,
                format: '{point.y:.1f}%'
            }
        }
    },
}, function(chart) {
    $.each(chart.series[0].data, function (i, data) {
        if (data.y >= 70)
            data.update({
                color: 'yellow'
            });
        if (data.y >= 90)
            data.update({
                color: 'red'
            });
    })
});

$('#plain').click(function() {
    chart.update({
        chart: {
            inverted: false,
            polar: false
        },
        subtitle: {
            text: 'Plain'
        }
    });
});

$('#inverted').click(function() {
    chart.update({
        chart: {
            inverted: true,
            polar: false
        },
        subtitle: {
            text: 'inverted'
        }
    });
});

$('#polar').click(function() {
    chart.update({
        chart: {
            inverted: false,
            polar: true
        },
        subtitle: {
            text: 'Polar'
        }
    });
});

 var chart = Highcharts.chart('container1', {
                title: {
                    text: 'Graph 2'
                },
                subtitle: {
                    text: 'Filled %'
                },
                xAxis: {
                    categories: ['id-1', 'id-2', 'id-3', 'id-4', 'id-5']
                },

                series: [{
                    type: 'column',
                    colorByPoint: false,
                    data: [42.4, 71.5, 19.4, 29.2, 44.0],
                    showInLegend: false
                }],
                plotOptions: {
                    series: {
                        borderWidth: 0,
                        dataLabels: {
                            enabled: true,
                            format: '{point.y:.1f}%'
                        }
                    }
                },

            },
                 function (chart) {
                    $.each(chart.series[0].data, function (i, data) {
                        if (data.y >= 70)
                            data.update({
                                color: 'yellow'
                            });
                        if (data.y >= 90)
                            data.update({
                                color: 'red'
                            });
                    })
                });

            //code for color change


            $('#plain1').click(function () {
                chart.update({
                    chart: {
                        inverted: false,
                        polar: false
                    },
                    subtitle: {
                        text: 'Sewage'
                    }
                });
            });

            $('#inverted1').click(function () {
                chart.update({
                    chart: {
                        inverted: true,
                        polar: false
                    },
                    subtitle: {
                        text: 'Inverted'
                    }
                });
            });

            $('#polar1').click(function () {
                chart.update({
                    chart: {
                        inverted: false,
                        polar: true
                    },
                    subtitle: {
                        text: 'Polar'
                    }
                });
            });

3 个答案:

答案 0 :(得分:1)

Yuu将这两个名字命名为 您必须将chart1用于第二个字符,并在更新中引用它

 var chart1 = Highcharts.chart('container1', {
            title: {
                text: 'Graph 2'
            },
            subtitle: {
                text: 'Filled %'
            },
            xAxis: {
                categories: ['id-1', 'id-2', 'id-3', 'id-4', 'id-5']
            },

            series: [{
                type: 'column',
                colorByPoint: false,
                data: [42.4, 71.5, 19.4, 29.2, 44.0],
                showInLegend: false
            }],
            plotOptions: {
                series: {
                    borderWidth: 0,
                    dataLabels: {
                        enabled: true,
                        format: '{point.y:.1f}%'
                    }
                }
            },

        },
             function (chart) {
                $.each(chart.series[0].data, function (i, data) {
                    if (data.y >= 70)
                        data.update({
                            color: 'yellow'
                        });
                    if (data.y >= 90)
                        data.update({
                            color: 'red'
                        });
                })
            });

        //code for color change


        $('#plain1').click(function () {
            chart1.update({
                chart: {
                    inverted: false,
                    polar: false
                },
                subtitle: {
                    text: 'Sewage'
                }
            });
        });

        $('#inverted1').click(function () {
            chart1.update({
                chart: {
                    inverted: true,
                    polar: false
                },
                subtitle: {
                    text: 'Inverted'
                }
            });
        });

        $('#polar1').click(function () {
            chart1.update({
                chart: {
                    inverted: false,
                    polar: true
                },
                subtitle: {
                    text: 'Polar'
                }
            });
        });

http://jsfiddle.net/6h9o5m2w/

答案 1 :(得分:0)

因为它们具有相同的变量名称。考虑正确命名它们

// first chart
var chart1 = Highcharts.chart('container'

// second chart
var chart2 = Highcharts.chart('container1'

然后使用各自的变量触发您想要的事件

希望有所帮助

答案 2 :(得分:0)

您在同一个变量chart上声明了两个图表。所以最新宣布的图表会覆盖第一个。所以:

 var chart = Highcharts.chart('container', {
   title: {
     text: 'Graph 1'
   }
   ...
 });

 var chart = Highcharts.chart('container1', {
   title: {
     text: 'Graph 2'
   }
   ...
 });

变为:

 var chart = Highcharts.chart('container', {
   title: {
     text: 'Graph 1'
   }
   ...
 });

 var chart1 = Highcharts.chart('container1', {
   title: {
     text: 'Graph 2'
   }
   ...
 });

您还针对每个click事件定位了相同的变量:

 $('#polar').click(function() {
   chart.update({
     ...
   });
 });

 $('#polar1').click(function() {
   chart.update({
     ...
   });
 });

变为:

 $('#polar').click(function() {
   chart.update({
     ...
   });
 });

 $('#polar1').click(function() {
   chart1.update({
     ...
   });
 });

Here is your working fiddle