최근에 데이터 마이닝 과정을 설계하고 있는데 데이터 분석 결과를 사용자에게 직관적으로 표시해야 합니다. 이를 위해서는 데이터 통계 차트를 사용해야 합니다.
1.jfreechart-1.0.13.jar
2.jcommon-1.0.16.jar
3. gnujaxp.jar
최종 렌더링을 살펴보겠습니다.
주로 jfreechart-1.0.13.jar 이지만, 이 3개의 패키지가 완성되어야 합니다. 이 글에 있는 예제의 프로젝트(코드)와 함께 jfreechart 관련 모든 jar 패키지를 압축하여 업로드했습니다.
다운로드 주소: http://download.csdn.net/detail/pzhtpf/4327700
다음으로, 우리는 이 프로그램을 단계별로 구현합니다.
1. 예비 준비 작업에서 이 세 가지 타사 패키지가 이 기사의 프로젝트에 추가됩니다. 추가 프로세스는 매우 간단합니다. 이전에 Java를 사용하여 Excel 테이블의 데이터를 읽는 방법에 대한 블로그를 작성한 적이 있습니다. 살펴보세요: http://blog.csdn.net/pzhtpf/article/details/7506135), 타사 패키지도 추가해야 합니다. 추가 프로세스는 다음과 같습니다.
1. Java 프로젝트를 생성하고 이 프로젝트에 새 폴더 lib를 생성합니다.
2. 위의 세 가지 jar 패키지를 lib에 복사합니다.
3. 그런 다음 Java 프로젝트를 마우스 오른쪽 버튼으로 클릭하고 속성을 선택합니다.
4. 왼쪽 목록에서 Java Build Path(Java 빌드 경로)를 선택하고 오른쪽 목록에서 Libraries(라이브러리)를 선택합니다.
5. JAR 추가를 클릭합니다.
6. 그런 다음 이 프로젝트의 lib 폴더에서 세 개의 jar를 선택하고 확인을 클릭합니다.
성공하면 프로젝트에 참조 라이브러리라는 폴더가 하나 더 생깁니다.
2. 세로 막대형 차트를 구현하는 Java 코드:
import java.awt.Font; import org.jfree.chart.ChartPanel; import org.jfree.chart.axis.CategoryAxis; Chart.axis.ValueAxis 가져오기 org.jfree.chart.plot.CategoryPlot 가져오기 org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; public class BarChart { ChartPanel Frame1; ; JFreeChart 차트 = ChartFactory.createBarChart3D( "Fruit", // 차트 제목 "Fruit Type", // 카탈로그 축 표시 라벨 "Quantity", // 숫자 축 데이터세트의 라벨 표시, // 데이터 세트 PlotOrientation.VERTICAL, // 차트 방향: 가로, 세로 true , // 범례 표시 여부(간단한 막대 차트의 경우 false여야 함) false, // 도구 생성 여부 false // URL 링크 생성 여부) // 여기에서 CategoryPlot 시작; plot=chart.getCategoryPlot();//차트 영역 개체 가져오기 CategoryAxis domainAxis=plot.getDomainAxis(); //가로 하단 목록 domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14)) // 수평 하단 제목 domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12)); //세로 제목 ValueAxis rangeAxis=plot.getRangeAxis(); //열 형식 가져오기 rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15))chart.getLegend().setItemFont(new Font("黑体" , Font.BOLD, 15));chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//제목 글꼴 설정//여기서 끝납니다. 코드가 조금 길지만 한자 프레임이 깨지는 문제를 해결하기 위한 목적은 단 하나입니다. =new ChartPanel(chart,true); //chartFrame을 여기에서 사용할 수도 있으며, 이는 독립적인 프레임을 직접 생성할 수 있습니다. } private static CategoryDataset getDataSet() { DefaultCategoryDataset 데이터 세트 = new DefaultCategoryDataset(); dataset.addValue(100, "베이징", "애플");dataset.addValue(100, "상하이", "애플"); , "베이징", "배"); "배");dataset.addValue(300, "베이징", "포도");dataset.addValue(300, "상하이", "포도"); dataset.addValue(400, "베이징", "바나나");dataset.addValue(400, "상하이", "바나나") "바나나");dataset.addValue(500, "베이징", "여주");dataset.addValue(500, "상하이", "여주"); 데이터세트 반환 } 공개 ChartPanel getChartPanel(){ return 프레임1 } } 렌더링은 다음과 같습니다.
그러나 비공개 정적 CategoryDataset getDataSet(){} 메서드에서 데이터를 변경한 후에는 또 다른 효과가 형성됩니다. 예를 들어 다음과 같습니다.
private static CategoryDataset getDataSet() { DefaultCategoryDataset 데이터세트 = new DefaultCategoryDataset(); 데이터세트.addValue(100, "사과", "사과"); 데이터세트.addValue(200, "배", "배"); , "포도", "포도");dataset.addValue(400, "바나나", "바나나"); Dataset.addValue(500, "여주", "여주") 반환 데이터세트 }렌더링은 다음과 같습니다.
원형 차트를 구현하는 세 가지 Java 코드:
패키지 com.njue.testJFreeChart; import java.text.DecimalFormat; import javax.swing.JPanel; import org.jfree.chart .ChartPanel 가져오기 org.jfree.chart.JFreeChart; org.jfree.chart.labels.StandardPieSectionLabelGenerator; import org.jfree.data.general.DefaultPieDataset; public class PieChart(){ DefaultPieDataset data = getDataSet() ; JFreeChart 차트 = ChartFactory.createPieChart3D("Fruit Yield",data,true,false,false); //비율 설정 PiePlot Pieplot = (PiePlot) Chart.getPlot() DecimalFormat df = new DecimalFormat("0.00%"); 주로 소수점 문제를 설정하는 DecimalFormat 객체 NumberFormat nf = NumberFormat.getNumberInstance();//NumberFormat 객체 가져오기 StandardPieSectionLabelGenerator sp1 = new StandardPieSectionLabelGenerator("{0} {2}", nf, df); //StandardPieSectionLabelGenerator 객체 가져오기 Pieplot.setLabelGenerator(sp1); //원형 차트 표시 비율 설정 //데이터가 없을 때 표시되는 내용 Pieplot.setNoDataMessage("표시된 데이터 없음"); ); 파이플롯 .setLabelGap(0.02D); Pieplot.setIgnoreNullValues(true);//null 값을 표시하지 않도록 설정 pieplot.setIgnoreZeroValues(true);//음수 값을 표시하지 않도록 설정 frame1=new ChartPanel (chart,true)chart.getTitle(); setFont(new Font(" 송나라",Font.BOLD,20));//제목 글꼴 설정 PiePlot PiePlot= (PiePlot) Chart.getPlot();//차트 영역 개체 가져오기 PiePlot.setLabelFont(new Font("宋体",Font.BOLD,10));//깨진 문자 해결 Chart.getLegend().setItemFont(new Font("黑体", Font.BOLD,10)); } private static DefaultPieDataset getDataSet() { DefaultPieDataset 데이터세트 = new DefaultPieDataset(); 데이터 세트.setValue("사과",100); 데이터세트.setValue("포도",300); " ",500); 데이터세트 반환 } public ChartPanel getChartPanel(){ return Frame1 } } 렌더링은 다음과 같습니다.
4. 꺾은선형 차트를 구현하는 Java 코드:
패키지 com.njue.testJFreeChart; import java.text.SimpleDateFormat; import org.jfree.chart.ChartPanel; .jfree.chart.axis.DateAxis 가져오기; org.jfree.chart.axis.ValueAxis; import org.jfree.data.time.Month; import org.jfree.data; time.TimeSeriesCollection; import org.jfree.data.xy.XYDataset; 공용 클래스 TimeSeriesChart { ChartPanel 프레임1; TimeSeriesChart(){ ) jfreechart.getPlot(); DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis(); dateaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy")); //가로 하단 제목 dateaxis.setTickLabelFont(new Font("宋体",Font.BOLD,12)); //세로 제목 ValueAxis rangeAxis=xyplot.getRangeAxis(); //열 형식 가져오기 rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15)); jfreechart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15)); jfreechart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//제목 글꼴 설정} private static XYDataset createDataset() { //이 데이터 세트는 약간 많지만 이해하기 어렵지 않습니다. TimeSeries timeseries = new TimeSeries("법적 및 일반 유럽 지수 신뢰", org.jfree.data.time.Month.class) timeseries.add(new Month(2, 2001), 181.80000000000001D); timeseries.add(새 달(2001년 3월), 167.30000000000001D); timeseries.add(새 달(2001년 4월), 153.80000000000001D); 167.59999999999999D); timeseries.add(새 월(2001년 6월), 158.80000000000001D); timeseries.add(새 월(2001년 7월), 148.30000000000001D); 153.90000000000001D); timeseries.add(새 월(2001년 9월), 142.69999999999999D); timeseries.add(새 월(2001년 10월), 123.2D); 131.80000000000001D); timeseries.add(새 월(2001년 12월), 139.59999999999999D); timeseries.add(새 월(2002년 1월), 142.90000000000001D); 138.69999999999999D); timeseries.add(새 달(2002년 3월), 137.30000000000001D); timeseries.add(새 달(2002년 4월), 143.90000000000001D); 139.80000000000001D); timeseries.add(새 월(2002년 6월), 137D); Timeseries.add(새 월(2002년), 132.80000000000001D); org.jfree.data.time.Month.class); timeseries1.add(new Month(2, 2001), 129.59999999999999D); timeseries1.add(new Month(3, 2001), 123.2D); timeseries1.add(new 월(3, 2001), 123.2D) 월(4, 2001), 117.2D); 월(2001년 5월), 124.09999999999999D); timeseries1.add(새 월(2001년 6월), 122.5999999999999D); timeseries1.add(새 월(2001년 7월), 119.2D); 2001년 8월 8일), 116.5D); timeseries1.add(새 월(2001년 9월), 112.7D); timeseries1.add(새 월(2001년 10월), 101.5D); timeseries1.add(새 월(2001년 11월), 106.0999999999999D ); timeseries1.add(새 달(12, 2001), 110.3D); timeseries1.add(새 월(2002년 1월), 111.7D); timeseries1.add(새 월(2002년), 111D); timeseries1.add(새 월(2002년 3월) 109.59999999999999D); timeseries1.add(신규 월(2002년 4월), 113.2D); timeseries1.add(새 월(2002년 5월), 111.59999999999999D); timeseries1.add(새 월(2002년 6월), 108.8D); 2002년 7월 7일), 101.59999999999999D); TimeSeriesCollection timeseriescollection(); timeseriescollection.addSeries(timeseries1); return timeseriescollection(){ return frame1;렌더링은 다음과 같습니다.
주요 메소드를 살펴보겠습니다.
import java.awt.GridLayout; import javax.swing.JFrame; public static void main(String args[]){ JFrame 프레임=new JFrame("Java 데이터 차트")(new GridLayout( 2) ,2,10,10)); 프레임.add(new BarChart().getChartPanel()); //막대 차트 추가 frame.add(new BarChart1().getChartPanel()); //열 차트의 또 다른 효과를 추가합니다. //꺾은선형 차트를 추가합니다. frame.setBounds(50, 50, 800, 600); 5개 요약
위의 내용은 구현하기 위한 간단한 예시이며, 더 자세한 내용을 알고 싶은 학생은 스스로 확인해볼 수 있습니다. 실제로 위의 코드는 자신의 실제 상황을 조합하면 쉽게 이해할 수 있습니다. 이것은 Application에서 구현된 것을 볼 수 있습니다. 실제로 데이터 통계 차트는 JavaWeb에서 더 일반적으로 사용됩니다. 직접 배울 수도 있습니다.