<highcharts :options="options" ref="mychart"></highcharts>
<div @click="download">导出</div>
<script>
import Highcharts from 'highcharts/highstock'
export default {
data () {
return {
// 初始化空对象
options: {
chart: {
type: 'heatmap',
scrollablePlotArea: { // 滚动
scrollPositionX: 0, // 初始滚动位置 0-左 1-右
opacity: 1
}
},
xAxis: {
categories: [],
opposite: true
},
yAxis: {
categories: [],
title: null,
reversed: true,
labels: {
formatter: function () {
return this.value.length > 15 ? this.value.slice(0, 14) + '...' : this.value
}
}
},
colorAxis: {
min: -1,
max: 1,
stops: [
[0, '#009900'],
[0.3, Highcharts.Color('#009900').setOpacity(0.6).get('rgba')],
[0.5, '#ffffff'],
[0.7, Highcharts.Color('#fb4b4c').setOpacity(0.6).get('rgba')],
[1, Highcharts.Color('#fb4b4c').setOpacity(1).get('rgba')]
]
},
credits: {
enabled: false
},
tooltip: {
formatter: function () {
return '<b>' + this.series.xAxis.categories[this.point.x] + '</b><br><b>' +
this.series.yAxis.categories[this.point.y] + '</b><br><b>' + '相关系数:' + '</b><b>' +
this.point.value + '</b>'
}
},
exporting: {
enabled: false
},
series: [{
name: '相关系数',
borderWidth: 0,
data: [],
dataLabels: {
enabled: true,
// useHTML: true,
color: '#033E14',
style: { textOutline: 'none', fontWeight: 'normal', fontSize: '13px', fontFamily: 'Microsoft YaHei,SimSun,sans-serif' }
}
}]
},
nameArr: ['春', '夏', '秋', '冬'],
lth: null
}
},
methods: {
// 绘图
drawGraph (data) {
this.lth = data.length
if (this.lth > 1) { // 必须至少2个数据才有相关系数
this.options.chart.scrollablePlotArea.scrollPositionX = 0
this.options.chart.scrollablePlotArea.minWidth = 136 * lth
this.options.chart.height = 40 * lth + 40
const serieData = []
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < data.length; j++) {
if (i === j) {
data[i][j] = 1
const series = [i, j, '1.0000']
serieData.push(series)
} else {
if (data[i][j] !== '-') {
const series = [i, j, data[i][j])]
serieData.push(series)
} else {
const series = {
x: i,
y: j,
value: '-',
color: 'white'
}
serieData.push(series)
}
}
}
}
this.options.series[0].data = serieData
// 参数
this.options.xAxis.categories = this.nameArr
this.options.yAxis.categories = this.nameArr
}
},
download () {
this.$refs.mychart.chart.exportChart({
type: 'image/png',
sourceWidth: this.lth * 136
})
}
}
}
</script>
