我正在使用带有 Bootstrap 的 Highcharts。
每次单击选项卡时如何重绘图表?
请在下面找到演示...
$(function () {
// Everything in common between the charts
var commonOptions = {
colors: ['#f68936', '#70ba47', '#33b5e6', '#fd8f40', '#e7ca60', '#40abaf', '#f6f7f8', '#e9e9eb'],
chart: {
style: {
fontFamily: 'Roboto Light',
fontWeight: 'normal',
fontSize: '12px',
color: '#585858',
}
},
title: {
text: null
},
subtitle: {
text: null
},
credits: {
enabled: false
},
exporting: {
enabled: false
},
xAxis: {
title: {
style: {
fontFamily: 'Roboto',
color: "#bbb",
}
},
labels: {
style:{ color: '#bbb' },
},
lineWidth: 1,
tickLength: 0,
},
yAxis: {
title: {
style: {
fontFamily: 'Roboto',
color: "#bbb",
}
},
offset:-6,
labels: {
style:{ color: '#bbb' },
//format:'{value}K',
},
tickInterval: 100,
lineWidth: 1,
gridLineDashStyle: 'dash',
},
series: [{
//backgroundColor: "rgba(0 ,0, 0, 1)",
}],
// Area Chart
plotOptions: {
series: {
fillOpacity: 0.1
},
area: {
lineWidth: 1,
marker: {
lineWidth: 2,
symbol: 'circle',
fillColor: 'white',
radius:3,
},
legend: {
radius: 2,
}
}
},
tooltip: {
useHTML: true,
shared: true,
backgroundColor: '#5f5f5f',
borderWidth: 0,
style: {
padding:10,
color: '#fefefe',
}
},
legend: {
itemStyle: {
fontFamily: 'Roboto Light',
fontWeight: 'normal',
fontSize: '12',
color: '#666666',
},
marker: {
symbol: 'square',
verticalAlign: 'middle',
radius: '4',
},
symbolHeight: 6,
symbolWidth: 6,
},
};
// -----------------------------------------------------
$('.areaChartTwoWay').each(function() {
$(this).highcharts(Highcharts.merge(commonOptions, {
chart: { type: 'area' },
xAxis: {
title: { text: 'Date', },
type: 'datetime',
dateTimeLabelFormats: {
day: '%eth %b'
},
},
yAxis: {
title: { text: 'Count', },
},
series: [{
name: 'Forwards',
color: '#f68936',
marker: { lineColor: '#f68936', fillColor: 'white', },
data: [185, 290, 198, 220, 180, 140, 220, 335, 197, 240],
legendIndex:1,
pointStart: Date.UTC(2016, 2, 11),
pointInterval: 24 * 3600 * 1000 // one day
}, {
name: 'Unique opens',
color: '#70ba47',
marker: { lineColor: '#70ba47', fillColor: 'white', },
data: [90, 95, 98, 80, 70, 68, 85, 71, 64, 90],
legendIndex:0,
pointStart: Date.UTC(2016, 2, 11),
pointInterval: 24 * 3600 * 1000 // one day
}]
}))
});
// -----------------------------------------------------
$('.areaChartOneWay').each(function() {
$(this).highcharts(Highcharts.merge(commonOptions, {
chart: { type: 'area' },
xAxis: {
title: { text: 'Date', },
type: 'datetime',
dateTimeLabelFormats: {
day: '%eth %b'
},
},
yAxis: {
title: { text: 'Count', },
},
series: [{
name: 'Forwards',
color: '#f68936',
marker: { lineColor: '#f68936', fillColor: 'white', },
data: [215, 325, 235, 380],
legendIndex:1,
pointStart: Date.UTC(2016, 2, 11),
pointInterval: 24 * 3600 * 1000 // one day
}, {
name: 'Unique opens',
color: '#70ba47',
marker: { lineColor: '#70ba47', fillColor: 'white', },
data: [15,53,42,38],
legendIndex:0,
pointStart: Date.UTC(2016, 2, 11),
pointInterval: 24 * 3600 * 1000 // one day
}]
}))
});
$('.highcharts-grid > path:last-child').remove();
$('.highcharts-markers > path:last-child').remove();
});
jQuery(document).on( 'shown.bs.tab', 'a[data-toggle="tab"]', function (e) {
jQuery( ".newChart" ).each(function() {
var chart = jQuery(this).highcharts();
chart.reflow();
});
})
body{margin:20px;}
.tab-content{padding-top:30px;border:1px solid #ddd;border-top:none;}
.nav{padding-left:50px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.highcharts.com/highcharts.js"></script>
<div class="clearfix">
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#chart1" aria-controls="chart1" role="tab" data-toggle="tab">Chart 1</a></li>
<li role="presentation"><a href="#chart2" aria-controls="chart2" role="tab" data-toggle="tab">Chart 2</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="chart1">
<div class="areaChartTwoWay newChart" style="height: 250px; margin: 0 auto"></div>
</div>
<div role="tabpanel" class="tab-pane" id="chart2">
<div class="areaChartOneWay newChart" style="height: 250px; margin: 0 auto"></div>
</div>
</div>
</div>