npm i --save nativescript-ui-highcharts
- 版本:1.2.2
- GitHub: https://github.com/mhtghn/nativescript-ui-highcharts
- NPM: https://npmjs.net.cn/package/nativescript-ui-highcharts
- 下载
- 昨天: 16
- 上周: 130
- 上个月: 427
NativeScript Highcharts
:warning: 如果您正在使用 NativeScript 7.0.0 及以上版本: 使用此包 @mhtghn/nativescript-highcharts
此插件允许您在 NativeScript 中使用 Highcharts。
截图
演示应用
NativeScript-Core (XML)
查看 demo 文件夹。这是如何克隆和运行它的说明
git clone https://github.com/mhtghn/nativescript-ui-highcharts
cd nativescript-ui-highcharts/src
npm run demo.ios # or demo.android
NativeScript-Angular
查看 demo-angular 文件夹。这是如何克隆和运行它的说明
git clone https://github.com/mhtghn/nativescript-ui-highcharts
cd nativescript-ui-highcharts/src
npm run demo-angular.ios # or demo-angular.android
安装
tns plugin add nativescript-ui-highcharts
用法
NativeScript-Core
TypeScript
创建与纯 Highcharts 一样的 Highcharts 选项对象。您可以在 此处 找到所有可能的选项。然后将选项转换为字符串,因为目前您必须以字符串形式传递选项到插件中。
...
export class HomeViewModel extends Observable {
chartOptions = {
chart: {
type: 'column'
},
title: {
text: 'Stacked column chart'
},
xAxis: {
categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
},
yAxis: {
min: 0,
title: {
text: 'Total fruit consumption'
},
stackLabels: {
enabled: true,
style: {
fontWeight: 'bold',
color: 'gray'
}
}
},
legend: {
align: 'right',
x: -30,
verticalAlign: 'top',
y: 25,
floating: true,
backgroundColor: 'white',
borderColor: '#CCC',
borderWidth: 1,
shadow: false
},
tooltip: {
headerFormat: '<b>{point.x}</b><br/>',
pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}'
},
plotOptions: {
column: {
stacking: 'normal',
dataLabels: {
enabled: true
}
}
},
series: [{
name: 'John',
data: [5, 3, 4, 7, 2]
}, {
name: 'Jane',
data: [2, 2, 3, 2, 1]
}, {
name: 'Joe',
data: [3, 4, 4, 2, 5]
}]
};
chartOptionsString = JSON.stringify(this.chartOptions);
...
}
XML
<Page class="page"
navigatingTo="onNavigatingTo"
xmlns="http://schemas.nativescript.org/tns.xsd" xmlns:ui="nativescript-ui-highcharts">
<ActionBar class="action-bar">
<Label class="action-bar-title" text="Home"></Label>
</ActionBar>
<GridLayout>
<!-- Add your page content here -->
<ui:Highcharts options="{{chartOptionsString}}"></ui:Highcharts>
</GridLayout>
</Page>
NativeScript Angular
TypeScript
在您的模块中导入 HighchartsModule。
...
import {HighchartsModule} from "nativescript-ui-highcharts/angular";
@NgModule({
imports: [
...
HighchartsModule
],
...
})
export class HomeModule { }
创建与纯 Highcharts 一样的 Highcharts 选项对象。您可以在 此处 找到所有可能的选项。然后将选项转换为字符串,因为目前您必须以字符串形式传递选项到插件中。
import { Component, OnInit } from "@angular/core";
@Component({
selector: "Home",
moduleId: module.id,
templateUrl: "./home.component.html"
})
export class HomeComponent implements OnInit {
chartOptions = {
chart: {
type: 'column'
},
title: {
text: 'Stacked column chart'
},
xAxis: {
categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
},
yAxis: {
min: 0,
title: {
text: 'Total fruit consumption'
},
stackLabels: {
enabled: true,
style: {
fontWeight: 'bold',
color: 'gray'
}
}
},
legend: {
align: 'right',
x: -30,
verticalAlign: 'top',
y: 25,
floating: true,
backgroundColor: 'white',
borderColor: '#CCC',
borderWidth: 1,
shadow: false
},
tooltip: {
headerFormat: '<b>{point.x}</b><br/>',
pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}'
},
plotOptions: {
column: {
stacking: 'normal',
dataLabels: {
enabled: true
}
}
},
series: [{
name: 'John',
data: [5, 3, 4, 7, 2]
}, {
name: 'Jane',
data: [2, 2, 3, 2, 1]
}, {
name: 'Joe',
data: [3, 4, 4, 2, 5]
}]
};
chartOptionsString = JSON.stringify(this.chartOptions);
...
}
HTML
<ActionBar class="action-bar">
<ActionBar class="action-bar">
<Label class="action-bar-title" text="Home"></Label>
</ActionBar>
<GridLayout class="page">
<!-- Add your page content here -->
<Highcharts options="{{chartOptionsString}}"></Highcharts>
</GridLayout>
Highcharts 兼容性
- highcharts.js: v7.2.0
- highcharts-more.js: v7.2.0
- highcharts-3d.js: v7.2.0
- sankey.js: v7.2.0
- organization.js: v7.2.0
关于性能
此插件针对真正希望在 NS 应用中使用 Highcharts 的人。因为它使用 WebView 来显示图表。所以从性能的角度来看,这不是最佳解决方案。如果您需要一个纯原生解决方案,您应该使用 NativeScript UI 的图表组件
提示
- 将以下选项添加到图表选项中,以禁用在 WebView 中显示 Highcharts.com 超链接
credits: {
enabled: false
}
致谢
此插件深受 Eddy Verbruggen 的此演示 的启发 Eddy Verbruggen