vue調用 Highcharts 實現多個數據可視化展示


一創建一個 options.js 

代碼為:

export const option1 = {
bar: {
title: {
text: '珠海豬場' // 指定圖表標題
},
credits: {
enabled: false
},
chart: {
backgroundColor: 'black',
type: 'bar'
},
plotOptions: {
column: {
colorByPoint: true
},
line: {
dataLabels: {
// 開啟數據標簽
enabled: true
}
// 關閉鼠標跟蹤,對應的提示框、點擊事件會失效
// enableMouseTracking: false
}
},
xAxis: {
categories: ['今日已入場', '已交易單次', '已交易數量', '已繳費'] // 指定x軸分組
},
series: [{
colorByPoint: true,
data: [500, 235, 809, 5000],
dataLabels: {
enabled: true
}
}]
}
}
export const option2 = {
bar: {
chart: {
backgroundColor: 'black',
type: 'column', // 指定圖表的類型,默認是折線圖(line)
enabled: true
},
credits: {
enabled: false
},
title: {
text: '珠海豬場' // 指定圖表標題
},
colors: ['#058DC7', '#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#50B432', '#ED561B', '#DDDF00', '#24CBE5'],
xAxis: {
categories: ['今日已入場', '屠宰進度', '已交易單次', '分切進度', '已交易數量', '冷鏈車', '已交易額', '生產效率比', '已繳費'] // 指定x軸分組
},
yAxis: {
title: {
text: '最近七天' // 指定y軸的標題
},
credits: {
enabled: false
}
},
plotOptions: {
column: {
colorByPoint: true
// enabled: true
}
},
series: [
{ // 指定數據列
name: '珠海豬場信息',
dataLabels: {
enabled: true
},
data: [
{
y: 1000,
color: 'red'
}, 5000, 4000, 5000, 2000, 5000, 4000, 5000, 2000], // 數據
enabled: true
}
]
}
}

export const option3 = {// id => 類似<div id="HighChartid"></div>
chart: {
backgroundColor: 'black',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: ''
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
credits: {
enabled: false
},
plotOptions: {
credits: {
enabled: false
},
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '{point.percentage:.1f} %',
style: {
color: 'black' // (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
},
colors: [
'#21c393', // 不合格
'#4879df', // 合格
'#fad567', // 良好
'#f44a36' // 優秀
]
}
},
series: [{
type: 'pie',
name: '售數量',
data: [{
name: ' 今日已入場',
y: 61.41,
sliced: true,
selected: true
}, {
name: ' 屠宰進度',
y: 11.84
}, {
name: '已交易單次',
y: 10.85
}, {
name: '分切進度',
y: 4.67
}, {
name: '已交易數量',
y: 4.18
}, {
name: '冷鏈車',
y: 1.64
}, {
name: '已交易額',
y: 1.6
}, {
name: '生產率',
y: 1.2
}, {
name: '已繳費',
y: 2.61
}] // 為要處理的格式,對應官網api的格式填寫即可
}]
}

export const option4 = {
chart: {
backgroundColor: 'black',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie',
height: 300
},
// 隱藏logo
credits: {
enabled: false
},
title: {
text: ' 珠海豬場大數據'
},
tooltip: {
pointFormat: '{series.name} : <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
tooltip: {
valueSuffix: '%'
},
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %'
},
pie: {
allowPointSelect: true,
enabled: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
style: {
color: 'black'
}
},
showInLegend: true
}
},
series: [{
type: 'pie',
// enabled: true,
// allowPointSelect: true,
keys: ['name', 'y', 'selected', 'sliced'],
data: [
// ['Apples', 29.9, false],
// ['Pears', 71.5, false],
// ['Oranges', 106.4, false],
// ['Plums', 129.2, false],
['分切進度', 144.0, false],
['屠宰進度', 176.0, false],
['生產率', 135.6, true, true],
['已交易額', 148.5, false]
]
// enabled: true
}]
}

export const option5 = {
chart: {
backgroundColor: 'black',
type: 'line',
watch: 100,
height: 300
},
credits: {
enabled: false
},
title: {
text: '中珠牧業月銷售'
},
subtitle: {
text: null
},
xAxis: {
categories: ['一日', '二日', '三日', '四日', '五日', '六日', '七日', '八日', '九日', '十日', '十一日', '十二日', '十三日', '十四日', '十五日', '十六日', '十七日', '十八日', '十九日', '二十日', '二十一日', '二十二日', '二十三日', '二十三日', '二十四日', '二十五日', '二十六日', '二十一七日', '二十八日', '二十九日', '三十日']
},
yAxis: {
title: {
text: '銷售 (數量)'
}
},
plotOptions: {
line: {
dataLabels: {
// 開啟數據標簽

enabled: true
}
// 關閉鼠標跟蹤,對應的提示框、點擊事件會失效
}
},
series: [{
name: '今日已入場',
data: [7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6, 7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6, 7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2]
}, {
name: '已繳費',
data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8, 3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8, 3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0]
}]
}

export const option6 = {
chart: {
backgroundColor: 'black',
type: 'column'
},
credits: {
enabled: false
},
title: {
text: '豬場效率優化數據分析圖',
color: 'MediumSpringGreen'
},
xAxis: {
categories: [
'前天數量',
'昨天數量',
'今天數量'
]
},
yAxis: [{
min: 0,
title: {
text: '入場豬與屠宰'
}
}, {
title: {
text: '已交易數利潤'
},
opposite: true
}],
legend: {
shadow: false
},
tooltip: {
shared: true
},
plotOptions: {
column: {
grouping: false,
shadow: false,
borderWidth: 0
}
},
series: [{
name: '豬入場數',
color: 'rgba(165,170,217,1)',
data: [150, 73, 20],
pointPadding: 0.3, // 通過 pointPadding 和 pointPlacement 控制柱子位置
pointPlacement: -0.2
}, {
name: '屠宰數',
color: 'rgba(126,86,134,.9)',
data: [140, 90, 40],
pointPadding: 0.4,
pointPlacement: -0.2
}, {
name: '已交易數',
color: 'rgba(248,161,63,1)',
data: [183.6, 178.8, 198.5],
tooltip: { // 為當前數據列指定特定的 tooltip 選項
valuePrefix: '$',
valueSuffix: ' M'
},
pointPadding: 0.3,
pointPlacement: 0.2,
yAxis: 1 // 指定數據列所在的 yAxis
}, {
name: '已繳費數',
color: 'rgba(186,60,61,.9)',
data: [203.6, 198.8, 208.5],
tooltip: {
valuePrefix: '$',
valueSuffix: ' M'
},
pointPadding: 0.4,
pointPlacement: 0.2,
yAxis: 1
}]
}

export const option7 = {
chart: {
marginTop: 40
},
title: {
text: '2017 年公司運營情況'
},
xAxis: {
categories: ['<span class="hc-cat-title">營收</span><br/> 千美元']
},
yAxis: {
plotBands: [{
from: 0,
to: 150,
color: '#666'
}, {
from: 150,
to: 225,
color: '#999'
}, {
from: 225,
to: 9e9,
color: '#bbb'
}],
title: null
},
series: [{
data: [{
y: 275,
target: 250
}]
}],
tooltip: {
pointFormat: '<b>{point.y}</b> (目標值 {point.target})'
}
}

export const option8 = {

}

 

2.然后創建一個index.vue文件

代碼為:

 

<template>
<div class="dsj">
<div id="Container">
 
<div id="header" style="font-size:3.5vw; color: PowderBlue; text-align: center; padding-top:2vw;">中珠牧業生產實時測試系統
<img src="../../assets/bb.png" alt="" style="position:absolute;left:2vw;top:0.5vw">
</div>
<div id="cc" style="font-size:1.7vw; color:#CDB38B; text-align: center;padding-top:1.5vw"><span>{{ now | dateformat('YYYY-MM-DD') }}</span><br>
<span style="font-size:1.5vw; color: #CDB38B; text-align: center;">{{ now | dateformat('HH:mm:ss') }}</span>
</div>


<div style="width:100%;height:28vw;">
<!-- 左邊 -->
<div style="width:30%;height:100%;float:left">
<div style="width:24vw;height:10vw;margin:1vw auto 0">
<table style="width:461px;height:193px" class="define-table">
<tr>
<td style="font-size:1vw; color: PowderBlue;">今日入場&nbsp;:<span style="font-size:1.5vw; color: PowderBlue;">1000</span></td>
<td style="font-size:1vw; color: PowderBlue;">已交易數&nbsp;:<span style="font-size:1.5vw; color: PowderBlue;">1000</span></td>
</tr>
<tr>
<td style="font-size:1vw; color: PowderBlue;">交易單次&nbsp;:<span style="font-size:1.5vw; color: PowderBlue;">1000</span></td>
<td style="font-size:1vw; color: PowderBlue;">冷&nbsp;鏈&nbsp;車&nbsp;:<span style="font-size:1.5vw; color: PowderBlue;">1000</span></td>
</tr>
<tr>
<td style="font-size:1vw; color: PowderBlue;">已&nbsp;繳&nbsp;費&nbsp;:<span style="font-size:1.5vw; color: PowderBlue;">1000</span></td>
<td style="font-size:1vw; color: PowderBlue;">今日交易額&nbsp;:<span style="font-size:1.5vw; color: PowderBlue;">1000</span></td>
</tr>
</table>
</div>
<div style="width:28vw;height:15vw;margin:1vw auto 0">
<x-chart :id="test4" :option="option4" ></x-chart>
</div>
</div>

<!-- 中間 -->
<div style="width:40%;height:100%;float:left">
<!-- 中上 -->
<div style="width:30vw; height:3vw; margin:1vw auto 0; font-size:1.5vw;text-align:center; color: PowderBlue; ">今日交易金額(元):<span style="font-size:2.5vw; ">10000</span></div>
 
<!-- 中下 -->
<div style="width:39vw;height:22.5vw;margin:1vw auto 0">
<div id="highmaps" class="container"></div>
</div>
</div>

<!-- 右邊 -->
<div style="width:30%;height:100%;float:left">
<div style="width:26vw;height:30vw;margin:1vw auto 0">
<div style="margin:1vw auto 0; font-size:1.2vw;text-align:center; color: PowderBlue;">實時交易數</div>
<ul style="list-style-type:none">
<div style="width:24vw;height:10vw;margin:1vw auto 0">
<table style="width:461px;height:193px" class="define-table">
<tr>
<td style="font-size:1vw; color: PowderBlue;">批發商&nbsp;:<span style="font-size:1.5vw; color: PowderBlue;">1000</span></td>
<td style="font-size:1vw; color: PowderBlue;">經銷商&nbsp;:<span style="font-size:1.5vw; color: PowderBlue;">1000</span></td>
</tr>
<tr>
<td style="font-size:1vw; color: PowderBlue;">頭&nbsp;數&nbsp;:<span style="font-size:1.5vw; color: PowderBlue;">1000</span></td>
<td style="font-size:1vw; color: PowderBlue;">金&nbsp;額&nbsp;:<span style="font-size:1.5vw; color: PowderBlue;">1000</span></td>
</tr>
</table>
</div>
</ul>

<div>
<x-chart :id="test1" ref="option1" :option="option1" ></x-chart>
</div>
</div>
 
<!-- <div style="width:28vw;height:15vw;margin:1vw auto 0"> -->
<!-- <div style="margin-left:7vw;">
<el-progress title="這是紫色" type="circle" :percentage="data1" color="rgba(142, 113, 199, 0.7)"></el-progress>
<el-progress type="circle" :percentage="data2" color="rgba(140, 1001, 51, 0.7)"></el-progress>
</div> -->

<div style="margin-left:7vw;margin-top:1.5vw">
<el-progress type="circle" :percentage="data4" color="rgba(43, 113, 19, 155)"></el-progress>
<el-progress type="circle" :percentage="data5" color="rgba(142, 11, 33, 0.7)"></el-progress>
</div>
<div></div>
</div>
</div>
</div>
<div style="height:15vw;margin-top:1vw;">>
<x-chart :id="test5" :option="option5" class="ff"></x-chart>
</div>
</div>
</template>

<script>
// 導入chart組件
import XChart from './chart.vue'
import HighCharts from 'highcharts'
import img_404 from '@/assets/bg.jpg'
import Highcharts from 'highcharts/highstock'
import HighchartsMore from 'highcharts/highcharts-more'
import HighchartsDrilldown from 'highcharts/modules/drilldown'
import Highcharts3D from 'highcharts/highcharts-3d'
import Highmaps from 'highcharts/modules/map'
import $ from 'jquery'
import HighchartsComponent from './HighchartsComponent.vue'
HighchartsMore(Highcharts)
HighchartsDrilldown(Highcharts)
Highcharts3D(Highcharts)
Highmaps(Highcharts)

// 導入chart組件模擬數據
import { option1, option2, option3, option4, option5, option6, option7, option8 } from '@/chart-options/options'
import { pay } from '@/api/car'
import moment from 'moment'
export default {
name: 'app',
data() {
return {
chart: null,
img_404,
test1: 'test1',
test2: 'test2',
test3: 'test3',
test4: 'test4',
test5: 'test5',
test6: 'test6',
test7: 'test7',
option1: option1.bar,
option2: option2.bar,
option3: option3,
option4: option4,
option5: option5,
option6: option6,
option7: option7,
now: moment(),
data1: 0,
data2: 0,
data3: 0,
data4: 0,
data5: 0,
data6: 0,
data7: 0,
payMoney: null
}
},
components: {
XChart,
HighchartsComponent
},
filters: {
dateformat(dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
return moment(dataStr).format(pattern)
}
},
created() {
this.option1.series[0].data[0]
console.log(this.$refs.option1)
new HighCharts.Map('map', option8)
},
mounted() {
this.createChart()
this.highmaps()
setInterval(() => {
this.now = moment()
}, 100)
setInterval(() => {
if (this.data1 === 100) {
this.data1 = 0
}
if (this.data2 === 100) {
this.data2 = 0
}
if (this.data3 === 100) {
this.data3 = 0
}
if (this.data4 === 100) {
this.data4 = 0
}
if (this.data5 === 100) {
this.data5 = 0
}
if (this.option5 === 100) {
this.option5 = 0
}
this.data1 = Math.floor(Math.random() * 100)
this.data2 = Math.floor(Math.random() * 100)
this.data3 = Math.floor(Math.random() * 100)
this.data4 = Math.floor(Math.random() * 100)
this.data5 = Math.floor(Math.random() * 100)
this.option5 = Math.floor(Math.random() * 100)

pay().then((res) => {
this.payMoney = res.data
})
}, 1000)
},
methods: {
createChart() {
Highcharts.setOptions({
lang: {
rangeSelectorZoom: ''
}
})
},
highmaps() {
Highcharts.setOptions({
lang: {
drillUpText: '< 返回 "{series.name}"'
}
})
var map = null
var geochina = 'https://data.jianshukeji.com/jsonp?filename=geochina/'
$.getJSON(geochina + 'china.json&callback=?', function(mapdata) {
var data = []
// 隨機數據
Highcharts.each(mapdata.features, function(md, index) {
var tmp = {
name: md.properties.name,
value: Math.floor((Math.random() * 100) + 1) // 生成 1 ~ 100 隨機值
}
if (md.properties.drilldown) {
tmp.drilldown = md.properties.drilldown
}
data.push(tmp)
})
map = new Highcharts.Map('highmaps', {
chart: {
backgroundColor: 'black',
marginBottom: -0,
// borderWidth: 50,
plotBorderWidth: -0,
height: 430,
width: 800,
marginTop: 0,
// width: 1000,
// borderWidth: 1,
events: {
drilldown: function(e) {
console.log(e)
// 異步下鑽
// if (e.point.drilldown) {
// var pointName = e.point.properties.fullname
// // 獲取二級行政地區數據並更新圖表
// $.getJSON(geochina + e.point.drilldown + '.json&callback=?', function(data) {
// data = Highcharts.geojson(data)
// Highcharts.each(data, function(d) {
// if (d.properties.drilldown) {
// d.drilldown = d.properties.drilldown
// }
// d.value = Math.floor((Math.random() * 100) + 1) // 生成 1 ~ 100 隨機值
// })
// map.hideLoading()
// map.addSeriesAsDrilldown(e.point, {
// name: e.point.name,
// data: data,
// dataLabels: {
// enabled: true,
// format: '{point.name}'
// }
// })
// map.setTitle({
// text: pointName
// })
// })
// }
},
drillup: function() {
map.setTitle({
text: '中國'
})
}
}
},
title: {
text: null
},
legend: {
// layout: 'vertical',
// align: 'right',
// verticalAlign: 'top',
// y: 50,
padding: 5
// itemMarginTop: 5,
// itemMarginBottom: 50,
// itemStyle: {
// lineHeight: '14px'
// }
},
credits: {
enabled: false
},
mapNavigation: {
// enabled: true,
buttonOptions: {
// 顯示地圖放大的標簽+與-
// verticalAlign: 'bottom'
}
},
tooltip: {
useHTML: true,
headerFormat: '<table><tr><td>{point.name}</td></tr>',
pointFormat: '<tr><td>全稱</td><td>{point.properties.fullname}</td></tr>' +
'<tr><td>行政編號</td><td>{point.properties.areacode}</td></tr>' +
'<tr><td>父級</td><td>{point.properties.parent}</td></tr>' +
'<tr><td>經緯度</td><td>{point.properties.longitude},{point.properties.latitude}</td></tr>',
footerFormat: '</table>'
},
// 這里時讓地圖更多的顏色也顯示導航欄
// colorAxis: {
// min: 0,
// minColor: '#fff',
// maxColor: '#006cee',
// labels: {
// style: {
// 'color': 'red', 'fontWeight': 'bold'
// }
// }
// },
series: [{
data: data,
mapData: mapdata,
joinBy: 'name',
name: '中國地圖',
states: {
hover: {
color: '#a4edba'
}
}
}]
})
})
}
}
}
</script>

<style>
.dsj{
width:100%;
height:100%;
background-color:#000000;
}
/*頂部面板樣式*/
#header {
width:100%;
height:8vw;
}
/* 上中間右上邊的DIV */
#cc{
width:16vw;
float:right;
margin: -7vw 1vw 0 0;
}
svg{
height:100% !important;
width:100% !important;
}
.define-table{
border-collapse:collapse;
  border-spacing:0;
border-left:1px solid #888;
border-top:1px solid #888;
}
.define-table th,.define-table td{
border-right:1px solid #888;
  border-bottom:1px solid #888;
padding:5px 15px;
}
.define-table th{
  font-weight:bold;background:#ccc;
  }

</style>
 

3.創建一個chart.vue

代碼為:

<template>
<div class="x-bar">
<div :id="id" :option="option"></div>
</div>
</template>
<script>
import HighCharts from 'highcharts'
export default {
// 驗證類型
props: {
id: {
type: String
},
option: {
type: Object
}
},
data() {
return {
chart: null
}
},
mounted() {
this.chart = HighCharts.chart(this.id, this.option)
}
}
</script>

 

實現的效果為下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM