本篇文章為大家展示了如何在vue項目中引入highcharts圖表,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
npm進行highchars的導入,導入完成后就可以進行highchars的可視化組件開發了
npm install highcharts --save
1、components目錄下新建一個chart.vue組件
<template>
<div class="chart" id="myChart" >
<div class="emcs_charts" :id="id" ></div>
</div>
</template>
<script>
// 引入highCharts模塊
import HighCharts from 'highcharts'
// 引入這個圖表的外部資源數據
import data from '../echarts_data/chart.js'
export default {
data() {
// 將引入的數據寫在自己的組件中
let dataObj = data.bar
return{
id: 'chart',
dataObj: dataObj
}
},
mounted() {//鉤子函數掛載時實例化這個圖表
// chart(參數1,參數2);第一個參數掛載組件的容器,第二個參數為圖表所需要的數據對象
HighCharts.chart(this.id,this.dataObj)
}
}
</script>
<style scoped lang='stylus'>
.chart{
float left ;
background-color #fff;
padding 10px 0;
margin-top 20px;
border-radius 6px
width 49.5%;
.emcs_charts{
min-width 890px;
height 280px;
}
}
</style>2、chart組件建好后,開始創建chart-options目錄,里面創建一個chart.js用來存放模擬的chart數據
如下圖我寫的一個面積圖的數據
module.exports = {
bar: {
chart: {//圖表樣式
type:'area',//指定圖表的類型,這里是面積圖
},
//是否啟用Labels。x,y軸默認值都是true,如果想禁用(或不顯示)Labels,設置該屬性為false即可
credits: {
enabled:false
},
title: {//指定圖表標題
text: ' 設備監控',
align: 'left',
style:{
color: '#666',
fontSize:'16px',
}
},
colors: ['rgba(86,199,99,1)','rgba(226,188,37,1)','rgba(255,133,133,1)'],
xAxis: {//圖表的橫坐標,一個軸為{ }
title:{//橫坐標標題
text:''
},
//x坐標軸的刻度值
categories: ['4:40','4:41','4:42','4:43','4:44', '4:45', '4:46', '4:47', '4:48', '4:49', '4:50','4:51','4:52','4:53','4:54', '4:55', '4:56', '4:57', '4:58', '4:59', '5:00', '5:01', '5:02', '5:03', '5:04', '5:05', '5:06', '5:07', '5:08', '5:09', '5:10', '5:11', '5:12', '5:13', '5:14', '5:15', '5:16', '5:17', '5:18', '5:19', '5:20', '5:21', '5:22', '5:23', '5:24', '5:25', '5:26', '5:27', '5:28', '5:29', '5:30', '5:31', '5:32', '5:33', '5:34', '5:35', '5:36', '5:37', '5:38', '5:39', '5:40'], //指定x軸分組
labels: {//坐標軸上的刻度值(顯示間隔、樣式、單位)
style: {
color: '#999999'
},
format:'{value}pm',//刻度值的單位
align: 'center'
},
lineColor: '#dfdfdf',//坐標軸的顏色
tickColor: '#dfdfdf',//坐標軸上的刻度線的顏色
tickLength: 5,//坐標軸上刻度線的長度
gridLineWidth:1,//網格線寬度。x軸默認為0,y軸默認為1px。
gridLineColor:'#f2f2f2',//網格線顏色。默認為:#C0C0C0。
// gridLineDashStyle: 'Dash',//網格線線條樣式。和Css border-style類似,常用的有:Solid、Dot、Dash
tickInterval: 5,//刻度間隔
tickmarkPlacement: 'between',//刻度線對齊方式,有between和on可選,默認是between
style: {
color: '#999999',
fontSize:10
},
crosshair:{//鼠標放上后顯示縱軸的數據
color:'#999',
width:1
}
},
yAxis: [{//圖表的縱坐標,多個軸[{軸一},{軸二}]
gridLineWidth: 1,
gridLineColor:'#f2f2f2',
tickPositions: [0, 25, 50, 75, 100],//y軸刻度值
tickLength:0,
title: {//縱坐標標題
text: ' ',
margin:0,
style: {
color: '#999999',
fontSize:10
}
},
labels:{//坐標軸上刻度的樣式及單位
style: {
color: '#999999',
fontSize:10
},
format:'{value}%',//坐標軸上的單位
},
offset:-10,//距離坐標軸的距離
},{
gridLineWidth: 1,
gridLineColor:'#f2f2f2',
tickColor: '#fff',
tickInterval:25,
tickLength:0,
title: {
text: '',
margin:0,
style: {
color: '#999999',
fontSize:10
}
},
labels:{
style: {
color: '#999999',
fontSize:10
},
format:'{value}℃'
},
opposite:true,//設置opposite: true表示該軸位置反轉,即為y軸時顯示在右側
offset:-10
}],
tooltip: {//數據提示框
headerFormat: '<small>{point.key}</small><br/>',//標題格式
pointFormat: '<span >{series.name}</span>:{point.y}<br/>',
shared: true,
followPointer:true,//跟隨鼠標
followPointerMove:true,//是否跟隨手指移動
// footerFormat: 'muzi',//尾部格式化字符串
style:{
fontSize:10,
fontFamily:'微軟雅黑',
fontWeight:'normal',
color:'#666'
}
},
//標示線總是垂直于它屬于的軸。它可單獨定義在x軸或y軸,也可以同時定義在x軸和y軸
plotOptions: {
area: {
//pointStart: 1940,
marker: {
enabled: false,
symbol: 'circle',
radius: 2,
states: {
hover: {
enabled: true
}
}
},
fillOpacity:0.2,
lineWidth:1
}
},
legend: {//圖例居中顯示在圖表下方
align: 'center',
symbolRadius:5,//圖標圓角
symbolWidth:10,//圖標寬度
symbolHeight:10,//圖標高度
itemStyle: {
color: '#999999',
fontWeight:'normal',
fontSize:12
},
itemMarginBottom: -14,//圖例項底部外邊距
},
series: [{//數據列是一組數據集合
name: 'CPU',//name 代表數據列的名字,并且會顯示在數據提示框(Tooltip)及圖例(Legend)中
data: [
5, 6, 10, 20, 50, 45, 30, 20, 10, 15,
16, 17, 18, 18, 30, 26, 25, 24, 20, 26,
36, 46, 50, 51, 52, 40, 30, 20, 19, 18,
30, 50, 55, 56, 70, 72, 73, 60, 55, 54,
53, 40, 39, 35, 32, 30, 20, 18, 3, 5,
10, 12, 13, 23, 34, 56, 60, 70, 80, 90, 80
],
tooltip: {
valueSuffix:'%'
}
}, {
name: 'RAM',
data:[
16, 17, 18, 18, 30, 26, 25, 24, 20, 26,
36, 46, 50, 51, 52, 40, 30, 20, 19, 18,
30, 50, 55, 56, 70, 72, 73, 60, 55, 54,
53, 40, 39, 35, 32, 30, 20, 18, 3, 5,
10, 12, 13, 23, 34, 56, 60, 70, 80, 90,
5, 6, 10, 20, 50, 45, 30, 20, 10, 15, 20
],
tooltip: {
valueSuffix:'%'
}
}, {
name: '溫度',
data:[
10, 11, 11, 12, 12, 13, 14, 15, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16
],
tooltip: {
valueSuffix:'℃'//值的前綴、后綴及小數點
},
yAxis:1
}]
}
}3、父組件引用chart.vue子組件
<template>
<div class="charts" id="myChart" >
<x-chart ></x-chart>
</div>
</template>
<script>
// 導入chart.vue子組件
import XChart from './chart.vue'
export default {
components: {
XChart
}
}
</script>
<style scoped lang='stylus'>
</style>效果如下圖所示

上述內容就是如何在vue項目中引入highcharts圖表,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。