简体   繁体   中英

How to change the background color of the Highcharts(HighMaps in specific) dynamically?

when a user selects a theme in a page,the backgroundcolor of the highchart should also change based on the user selection . My code:

    var colorCode="#fff";



var chart1=new Highcharts.mapChart('geoMap', {
 chart: {

     backgroundColor:colorCode,
     type: 'map'


 },
 title: {
     text: 'US'
 },

 legend: {
     enabled: true
 },
......
......
......
$('#theme1').click(function() {
    // colorCode="default"
     colorCode={
               linearGradient: [0, 0, 500, 500],
               stops: [
                 [0, 'rgb(142, 158, 171)'],
                 [1, 'rgb(238, 242, 243)']
               ]
             }

chart1.redraw();
});

But the chart isn't getting refreshed so the colorCode is not getting applied. Any kind of suggestion would be helpful.

You need to actually assign the new color to the chart options

chart1.options.chart.backgroundColor = colorCode

i made a small fiddle that takes the color from a dropdown (which would emulate your user changing the theme) and sets it as the background of you chart http://jsfiddle.net/grLrmxhd/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM