點擊獲取工具>>
本文將爲你們介紹如何使用Kendo UI在Vue.js中構建一個氣候視圖UI組件,並完成與圖表組件的界面。前端
首先讓咱們一塊兒來了解一下背景。vue
Kendo UI是一個全面的Web用戶界面框架,其中包含JavaScript UI組件的集合,這些JavaScript UI組件具備jQuery庫以及Vue、React和Angular等更現代的庫。 Kendo UI開發人員使用大量的UI小部件和數據可視化組件來構建交互式且響應迅速的高性能應用程序。vue-router
使用Kendo UI,您能夠輕鬆地將真正高級的UI組件添加到您選擇庫的前端項目中,沒必要擔憂接口的關鍵功能,並且能夠節省時間,只需關注專有功能便可。 Kendo UI附帶了易於使用的集成,支持您喜歡的每一個前端Web框架(例如Vue.js)。npm
除基本的UI組件外,Kendo UI還爲您提供了高級的數據可視化UI元素,可確保您沒必要向項目中添加其餘庫便可處理圖表,這些高級UI元素也可自定義。sass
您將使用Kendo UI在Vue.js中構建氣候視圖UI組件,將首先使用VS Code中的Kendo UI starter擴展scaffolding,而後使用Vue Router建立路由,最後使用一些Kendo UI圖表組件來構建界面。服務器
假設您已經安裝了Vue; 若是沒有,請轉到安裝指南。如今打開您的VS Code,要作的第一件事是安裝Kendo UI模板嚮導擴展。您能夠經過轉到VS Code的擴展程序標籤並搜索 「Kendo UI Template」來實現,繼續下載,從新啓動VS Code以後,如今可使用它來搭建新項目了。app
您能夠先選擇一個項目名稱和一個文件夾來開始,單擊Next時,系統會提示您選擇要使用的前端庫,Kendo UI具備Vue、Angular和React的組件元素。框架
下一步是選擇新項目中所需的頁面,對於此項目,您能夠選擇一個帶有路由的圖表。選擇以後,模板嚮導將在後臺運行Vue create命令,併爲您搭建Vue應用程序。svg
既然已經完成了應用程序的scaffolding工做,那麼您必須經過運行如下命令來確保正確安裝了全部依賴項:函數
cd newapp
npm install
導航到新應用程序的根目錄,並在main.js文件中,確保它看起來是這樣:
`import Vue from 'vue'
import App from './App.vue'
import router from "./router";
Vue.config.productionTip = false
new Vue({
render: h => h(App),
router
}).$mount('#app')`
您能夠看到路由已引入到項目中,若是您沒有使用Kendo UI template wizard或選擇了一個空白項目,則能夠在項目終端中使用如下命令輕鬆添加路由:
vue add router
在app.vue文件中,將內容替換爲如下代碼塊:
`<template>
<div id="root">
<div class="content">
<router-view></router-view>
</div>
</div>
</template>
<script>
export default {
name: 'app',
components: {
}
}
</script>`
如今已將導入的路由引入這裏,若是打開路由文件夾,將看到已在其中註冊路由的路由腳本(index.js)。 確保您的外觀以下所示:
`import Vue from "vue";
import Router from "vue-router";
import Home from "../components/Home";
import Chart1 from "../components/Chart1";
import Chart2 from "../components/Chart2";
Vue.use(Router);
export default new Router({
mode: "history",
routes: [
{
path: "/",
name: "Home",
component: Home
}
,{
path: "/Chart1",
name: "Chart1",
component: Chart1
},{
path: "/Chart2",
name: "Chart2",
component: Chart2
}
]
});
`
如今,當您查看app.vue文件時,將看到這些定義的路由引入的位置。要定義這些單獨的路由,請導航到components文件夾。您將看到不少組件,刪除全部組件,僅保留主組件。
您的主組件home.vue應如如下代碼塊所示:
`<template>
<div class="container mt-5">
<div class='row'>
<div class='col-12'>
<h1 class='welcome mb-0'>Global Climate Vue Application</h1>
<h2 class='sub-header mt-0'>Get information about climate conditions of any region in the world with one click</h2>
</div>
</div>
<div class='row'>
<div class='col-12'>
<h1 class='get-started'>Pick a Subject</h1>
</div>
</div>
<div class='row justify-content-center'>
<div class='col-6 text-right'>
<div class='kendoka-div'>
<img class='kendoka' src="../assets/images/kendoka-vue.svg" alt='kendoka' />
</div>
</div>
<div class='col-6 components-list'>
<p>
<img src="../assets/images/components.svg" alt='components' />
<router-link to="/Chart1">World Population</router-link>
</p>
<p>
<img src="../assets/images/styles.svg" alt='styles' />
<router-link to="/Chart1">Afforestation</router-link>
</p>
<p>
<img src="../assets/images/blogs.svg" alt='blogs' />
<router-link to="/Chart1">Elevation</router-link>
</p>
<p>
<img src="../assets/images/tutorials.svg" alt='tutorials' />
<router-link to="/Chart1">Topography</router-link>
</p>
<p>
<img src="../assets/images/styles.svg" alt='styles' />
<router-link to="/Chart1">Vegetation</router-link>
</p>
<p>
<img src="../assets/images/components.svg" alt='components' />
<router-link to="/Chart1">Prevailing Winds</router-link>
</p>
</div>
</div>
</div>
</template>
<script>
export default {
data: function() {
return {
publicPath: process.env.BASE_URL
}
}
}
</script>`
您可能會遇到錯誤,可能會詢問您在路由腳本中定義的路由。 要建立這些文件,請在components文件夾中建立一個chart1.vue文件,並在其中複製如下代碼塊:
`<template>
<div class='container-fluid'>
<div class='row my-4 mt-5'>
<div class='col-12 col-lg-9 border-right' >
<div v-if="loading" class="k-loading-mask">
<span class="k-loading-text">Loading</span>
<div class="k-loading-image"></div>
<div class="k-loading-color"></div>
</div>
<Chart :title-text="'World Population'"
:title-font="'19pt sans-serif'"
:title-margin-bottom="50"
:legend-position="'bottom'"
:series="series"
:theme="'sass'">
</Chart>
</div>
<div class='col-12 col-lg-3 mt-3 mt-lg-0'>
<h2>View by Continent</h2>
<ul>
<li>
<h3><router-link to="/Chart2">Asia</router-link></h3>
</li>
<li>
<h3><router-link to="/Chart2">Africa</router-link></h3>
</li>
<li>
<h3><router-link to="/Chart2">North America</router-link></h3>
</li>
<li>
<h3><router-link to="/Chart2">South America</router-link></h3>
</li>
<li>
<h3><router-link to="/Chart2">Australia</router-link></h3>
</li>
</ul>
</div>
</div>
</div>
</template>
<script>
import '@progress/kendo-ui/js/kendo.dataviz.chart'
import { Chart } from '@progress/kendo-charts-vue-wrapper';
export default {
mounted: function(){
setTimeout(() => {
this.loading = false;
}, 300);
},
components: {
Chart
},
data: function() {
return {
loading: true,
series: [{
type: "pie",
labels: {
visible: true,
format: "p0"
},
connectors: {
width: 0
},
data: [
{ category: 'EUROPE', value: 0.09 },
{ category: 'NORTH AMERICA', value: 0.06 },
{ category: 'AUSTRALIA', value: 0.02 },
{ category: 'ASIA', value: 0.60 },
{ category: 'SOUTH AMERICA', value: 0.06 },
{ category: 'AFRICA', value: 0.17 }
]
}]
}
}
}
</script>
`
在這裏,咱們使用Kendo UI圖表組件,該組件帶有一個特殊的動畫,由於它能夠根據大陸人口來定義大陸。 Kendo UI與Vue完美地集成在一塊兒,您能夠在特定組件的Vue返回函數中添加不少選項。
對於第二種方法,在components文件夾中建立一個Chart2.vue文件,而後在其中複製下面的代碼塊:
`<template>
<div class="container-fluid col-12 col-lg-9 mt-5">
<h2 >Climate Report for Africa</h2>
<div class="climate ">
<h3>Climate control history</h3>
<kendo-sparkline :data="pressLogData" :theme="'sass'"></kendo-sparkline> | 980 <span>mb</span> |
<kendo-sparkline :type="'column'" :data="tempLogData" :tooltip-format="'{0} %'" :theme="'sass'"> </kendo-sparkline> | 21 <span>°C</span> |
<kendo-sparkline :type="'area'" :data="humLogData" :tooltip-format="'{0} %'" :theme="'sass'"> </kendo-sparkline> | 32 <span>%</span> |
</div>
<div class="temperature">
<h3>Temperature control</h3>
<div class="stats">
<kendo-sparkline id="temp-range"
:type="'bullet'"
:data="tempRangeData"
:tooltip-visible="false"
:value-axis="tempRangeValueAxisOpt"
:theme="'sass'">
</kendo-sparkline>
</div>
</div>
<div class="conditioner">
<h3>Conditioner working time</h3>
<ul class="pie-list stats">
<li>
MON
<kendo-sparkline id="stats-mon"
:type="'pie'"
:data="[14,10]"
:theme="'sass'">
</kendo-sparkline>
</li>
<li>
TUE
<kendo-sparkline id="stats-tue"
:type="'pie'"
:data="[8,6]"
:theme="'sass'">
</kendo-sparkline>
</li>
<li>
WED
<kendo-sparkline id="stats-wed"
:type="'pie'"
:data="[8,16]"
:theme="'sass'">
</kendo-sparkline>
</li>
<li>
THU
<kendo-sparkline id="stats-thu"
:type="'pie'"
:data="[12,12]"
:theme="'sass'">
</kendo-sparkline>
</li>
<li>
FRI
<kendo-sparkline id="stats-fri"
:type="'pie'"
:data="[6,18]"
:theme="'sass'">
</kendo-sparkline>
</li>
<li>
SAT
<kendo-sparkline id="stats-sat"
:type="'pie'"
:data="[1,23]"
:theme="'sass'">
</kendo-sparkline>
</li>
<li>
SUN
<kendo-sparkline id="stats-sun"
:type="'pie'"
:data="[5,19]"
:theme="'sass'">
</kendo-sparkline>
</li>
</ul>
</div>
</div>
</template>
<script>
import Vue from 'vue'
import '@progress/kendo-ui';
import { KendoSparkline } from '@progress/kendo-charts-vue-wrapper';
Vue.component('kendo-sparkline', KendoSparkline);
export default {
components: {
KendoSparkline
},
data: function() {
return {
pressLogData: [
936, 968, 1025, 999, 998, 1014, 1017, 1010, 1010, 1007,
1004, 988, 990, 988, 987, 995, 946, 954, 991, 984,
974, 956, 986, 936, 955, 1021, 1013, 1005, 958, 953,
952, 940, 937, 980, 966, 965, 928, 916, 910, 980
],
tempLogData: [
16, 17, 18, 19, 20, 21, 21, 22, 23, 22,
20, 18, 17, 17, 16, 16, 17, 18, 19, 20,
21, 22, 23, 25, 24, 24, 22, 22, 23, 22,
22, 21, 16, 15, 15, 16, 19, 20, 20, 21
],
humLogData: [
71, 70, 69, 68, 65, 60, 55, 55, 50, 52,
73, 72, 72, 71, 68, 63, 57, 58, 53, 55,
63, 59, 61, 64, 58, 53, 48, 48, 45, 45,
63, 64, 63, 67, 58, 56, 53, 59, 51, 54
],
tempRangeData: [21, 23],
tempRangeValueAxisOpt: {
min: 0,
max: 30,
plotBands: [{
from: 0, to: 15, color: '#787878', opacity: 0.15
}, {
from: 15, to: 22, color: '#787878', opacity: 0.3
}, {
from: 22, to: 30, color: '#787878', opacity: 0.15
}]
}
}
}
}
</script>
<style>
.temperature, .conditioner {
margin: 0;
padding: 30px 0 0 0;
}
.history {
border-collapse: collapse;
width: 100%;
}
.history td {
padding: 0;
vertical-align: bottom;
}
.history td.spark {
line-height: 30px;
}
.history td.value {
font-size: 1.6em;
font-weight: normal;
line-height: 50px;
}
.history td.value span {
font-size: .5em;
vertical-align: top;
line-height: 40px;
}
.stats {
text-align: center;
}
.pie-list {
margin: 0;
padding: 0;
list-style-type: none;
}
.pie-list li {
display: inline-block;
text-align: center;
width: 34px;
font-size: 12px;
}
display: block;
width: 40px;
line-height: 35px;
}
width: 100%;
line-height: 40px;
}
</style>`
第二條路線使用Kendo UI Sparkline組件描繪氣候條件,將全部內容捆綁在一塊兒,您如今可使用如下命令在開發服務器中運行該應用程序:
npm run serve
您將擁有一個具備路由功能和全部預約義圖表功能完善的UI組件。
這篇文章介紹了針對Vue.js的Kendo UI,以及如何使用Kendo UI模板嚮導輕鬆啓動Vue項目。 它還顯示了實現Vue路由並將Kendo UI組件引入Vue項目是多麼輕鬆。