原創|使用教程|編輯:鄭恭琳|2016-05-27 12:58:25.000|閱讀 1049 次
概述:本文主要給大家介紹如何使用ChartDirector創建單獨的Java應用,附有最新版免費下載和完整代碼。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
本文主要給大家介紹如何使用ChartDirector創建單獨的Java應用。在創建程序之前看是否已經下載了該控件,并且配置好了相應的Java IDE開發環境。使用ChartDirector控件創建圖表相對簡單,控件提供了完善的API以及事例和幫助文檔,開發人員可以輕松上手。
這里我也嘗試用ChartDirector免費版畫了一張圖表(圖右),只是在圖表下方會出現一根黃條,有礙美觀,不建議使用。
在利用該控件創建Java圖表應用程序之前,應該大致了解該控件創建圖表的大致過程以及使用到的相關函數,我們大致介紹下該事例中將要使用到的API:
完整的代碼如下:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import ChartDirector.*; public class simplebar implements DemoModule { //Name of demo program public String toString() { return "Simple Bar Chart (1)"; } //Number of charts produced in this demo public int getNoOfCharts() { return 1; } //Main code for creating charts public void createChart(ChartViewer viewer, int chartIndex) { // The data for the bar chart double[] data = {85, 156, 179.5, 211, 123}; // The labels for the bar chart String[] labels = {"Mon", "Tue", "Wed", "Thu", "Fri"}; // Create a XYChart object of size 250 x 250 pixels XYChart c = new XYChart(250, 250); // Set the plotarea at (30, 20) and of size 200 x 200 pixels c.setPlotArea(30, 20, 200, 200); // Add a bar chart layer using the given data c.addBarLayer(data); // Set the labels on the x axis. c.xAxis().setLabels(labels); // Output the chart viewer.setChart(c); //include tool tip for the chart viewer.setImageMap(c.getHTMLImageMap("clickable", "", "title='{xLabel}: {value} GBytes'")); } //Allow this module to run as standalone program for easy testing public static void main(String[] args) { //Instantiate an instance of this demo module DemoModule demo = new simplebar(); //Create and set up the main window JFrame frame = new JFrame(demo.toString()); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); frame.getContentPane().setBackground(Color.white); // Create the chart and put them in the content pane ChartViewer viewer = new ChartViewer(); demo.createChart(viewer, 0); frame.getContentPane().add(viewer); // Display the window frame.pack(); frame.setVisible(true); } }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網