
- GWT Highcharts 教程
- GWT Highcharts - 首頁
- GWT Highcharts - 概述
- 環境設定
- 配置語法
- GWT Highcharts - 線形圖
- GWT Highcharts - 面積圖
- GWT Highcharts - 條形圖
- GWT Highcharts - 柱形圖
- GWT Highcharts - 餅圖
- GWT Highcharts - 散點圖
- GWT Highcharts - 動態圖表
- GWT Highcharts - 組合圖表
- GWT Highcharts - 3D 圖表
- GWT Highcharts - 地圖圖表
- GWT Highcharts 有用資源
- GWT Highcharts - 快速指南
- GWT Highcharts - 有用資源
- GWT Highcharts - 討論
GWT Highcharts - 配置語法
在本章中,我們將展示使用 GWT 中的 Highcharts API 繪製圖表所需的配置。
步驟 1:建立 GWT 應用程式
請按照以下步驟更新我們在GWT - 建立應用程式章節中建立的 GWT 應用程式:
步驟 | 描述 |
---|---|
1 | 在com.tutorialspoint包下建立一個名為HelloWorld的專案,如GWT - 建立應用程式章節中所述。 |
2 | 修改HelloWorld.gwt.xml、HelloWorld.html和HelloWorld.java,如下所述。保持其餘檔案不變。 |
3 | 編譯並執行應用程式以驗證已實現邏輯的結果。 |
以下是修改後的模組描述符src/com.tutorialspoint/HelloWorld.gwt.xml的內容。
<?xml version = "1.0" encoding = "UTF-8"?> <module rename-to = 'helloworld'> <inherits name = 'com.google.gwt.user.User'/> <inherits name = 'com.google.gwt.user.theme.clean.Clean'/> <entry-point class = 'com.tutorialspoint.client.HelloWorld'/> <inherits name="org.moxieapps.gwt.highcharts.Highcharts"/> <source path = 'client'/> <source path = 'shared'/> </module>
以下是修改後的 HTML 宿主檔案war/HelloWorld.html的內容。
<html> <head> <title>GWT Highcharts Showcase</title> <link rel = "stylesheet" href = "HelloWorld.css"/> <script language = "javascript" src = "helloworld/helloworld.nocache.js"> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" /> <script src = "https://code.highcharts.com/highcharts.js" /> </script> </head> <body> </body> </html>
在理解配置後,我們將在最後看到更新後的 HelloWorld.java。
步驟 2:建立配置
建立圖表
配置圖表的型別、標題和副標題。
Chart chart = new Chart() .setType(Type.SPLINE) .setChartTitleText("Monthly Average Temperature") .setChartSubtitleText("Source: WorldClimate.com");
xAxis
配置要在 X 軸上顯示的刻度。
XAxis xAxis = chart.getXAxis(); xAxis.setCategories("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
yAxis
配置要在 Y 軸上顯示的標題和繪圖線。
YAxis yAxis = chart.getYAxis(); yAxis.setAxisTitleText("Temperature °C"); yAxis.createPlotLine() .setValue(0) .setWidth(1) .setColor("#808080");
tooltip
配置工具提示。設定要在值(y 軸)後新增的字尾。
ToolTip toolTip = new ToolTip(); toolTip.setValueSuffix("°C"); chart.setToolTip(toolTip);
legend
配置要在圖表右側顯示的圖例以及其他屬性。
legend.setLayout(Legend.Layout.VERTICAL) .setAlign(Legend.Align.RIGHT) .setVerticalAlign(Legend.VerticalAlign.TOP) .setX(-10) .setY(100) .setBorderWidth(0); chart.setLegend(legend);
series
配置要在圖表上顯示的資料。Series 是一個數組,該陣列的每個元素代表圖表上的一條線。
chart.addSeries(chart.createSeries() .setName("Tokyo") .setPoints(new Number[] { 7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6 }) ); chart.addSeries(chart.createSeries() .setName("New York") .setPoints(new Number[] { -0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5 }) ); chart.addSeries(chart.createSeries() .setName("Berlin") .setPoints(new Number[] { -0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0 }) ); chart.addSeries(chart.createSeries() .setName("London") .setPoints(new Number[] { 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:將圖表新增到父面板。
我們將圖表新增到根面板。
RootPanel.get().add(chart);
示例
請考慮以下示例以進一步瞭解配置語法:
HelloWorld.java
package com.tutorialspoint.client; import org.moxieapps.gwt.highcharts.client.Chart; import org.moxieapps.gwt.highcharts.client.Legend; import org.moxieapps.gwt.highcharts.client.Series.Type; import org.moxieapps.gwt.highcharts.client.ToolTip; import org.moxieapps.gwt.highcharts.client.XAxis; import org.moxieapps.gwt.highcharts.client.YAxis; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.ui.RootPanel; public class HelloWorld implements EntryPoint { public void onModuleLoad() { Chart chart = new Chart() .setType(Type.SPLINE) .setChartTitleText("Monthly Average Temperature") .setChartSubtitleText("Source: WorldClimate.com"); XAxis xAxis = chart.getXAxis(); xAxis.setCategories("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); YAxis yAxis = chart.getYAxis(); yAxis.setAxisTitleText("Temperature °C"); yAxis.createPlotLine() .setValue(0) .setWidth(1) .setColor("#808080"); ToolTip toolTip = new ToolTip(); toolTip.setValueSuffix("°C"); chart.setToolTip(toolTip); Legend legend = new Legend(); legend.setLayout(Legend.Layout.VERTICAL) .setAlign(Legend.Align.RIGHT) .setVerticalAlign(Legend.VerticalAlign.TOP) .setX(-10) .setY(100) .setBorderWidth(0); chart.setLegend(legend); chart.addSeries(chart.createSeries() .setName("Tokyo") .setPoints(new Number[] { 7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6 }) ); chart.addSeries(chart.createSeries() .setName("New York") .setPoints(new Number[] { -0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5 }) ); chart.addSeries(chart.createSeries() .setName("Berlin") .setPoints(new Number[] { -0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0 }) ); chart.addSeries(chart.createSeries() .setName("London") .setPoints(new Number[] { 3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8 }) ); RootPanel.get().add(chart); } }
結果
驗證結果。

廣告