ChartDirector使用练习笔记(无乱码)

时间:2022-12-07 08:59:02
SecondBean.java


public class SecondBean {

public static double[] data = {};

public static String[] labels = {};

public static String title;

public static String tableFormat;


public static double[] getData() {

return data;

}


public static void setData(double[] data) {

SecondBean.data = data;

}


public static String[] getLabels() {

return labels;

}


public static void setLabels(String[] labels) {

SecondBean.labels = labels;

}


public static String getTitle() {

return title;

}


public static void setTitle(String title) {

SecondBean.title = title;

}


public static String getTableFormat() {

return tableFormat;

}


public static void setTableFormat(String tableFormat) {

SecondBean.tableFormat = tableFormat;

}


public static void storeData() {

double[] dt = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };

String[] lb = { "测试实例一", "TWO", "THREE", "FOUR", "FIVE", "SIX",

"SEVEN", "EIGHT", "NINE", "TEN" };

String tt = "TEST";

String tf = "{percent}%";

SecondBean.setData(dt);

SecondBean.setLabels(lb);

SecondBean.setTitle(tt);

SecondBean.setTableFormat(tf);

}


}

TestArrBean.java


public class TestArrBean {

public static double[][] DATA = null;

public static String[] LABELS = null;


public static double[][] getData() {

return DATA;

}


public static void setData(double[][] DATA) {

TestArrBean.DATA = DATA;

}


public static String[] getLabels() {

return LABELS;

}


public static void setLabels(String[] LABELS) {

TestArrBean.LABELS = LABELS;

}


public static void storeInfo() {


// setData(new double[][] { { 5, 12, 17 }, { 3, 6, 7 }, { 10, 17, 22 },

// { 4, 6, 7 }, { 3, 7, 18 }, { 5, 9, 13 }, { 2, 4, 5 },

// { 5, 7, 11 } });

setData(new double[][] { { 5, 12, 17, 53, 43, 43, 65, 87 },

{ 3, 6, 7, 22, 45, 97, 87, 75 },

{ 10, 54, 43, 65, 89, 23, 65, 17 } });

setLabels(new String[] { "测试例子A", "测试例子B", "测试例子C", "测试例子D", "测试例子E"

// , "F" , "G" , "H"

});

}

}

SelectShape.java


import ChartDirector.BaseChart;

import ChartDirector.Chart;

import ChartDirector.LegendBox;

import ChartDirector.PieChart;

import ChartDirector.PolarChart;

import ChartDirector.TextBox;

import ChartDirector.XYChart;

public class SelectShape {

public static BaseChart BC = null;

/**

*

* 概述:生成饼状图的方法

*

* @Title: createPieChart

* @param data

* @param labels

* void

* @user <a href=mailto:linfenliang@126.com>linfenliang</a>

*/

public static void createPieChart(double[] data, String[] labels) {

BC = new PieChart(600, 500);

BC.setDefaultFonts("simfang");

((PieChart) BC).setPieSize(250, 250, 100);

BC.addTitle(SecondBean.getTitle(), "黑体").setBackground(

Chart.metalColor(0xff9999));

// BC.set3D();

((PieChart) BC).setLabelLayout(0);

// 显示图标帮助框

BC.addLegend(450, 80);

((PieChart) BC).setLabelFormat(SecondBean.getTableFormat());

((PieChart) BC).setData(data, labels);

// 突出重点显示

// ((PieChart) BC).setExplode(0);

// 设置立体字体效果

TextBox t = ((PieChart) BC).setLabelStyle();

t.setBackground(Chart.SameAsMainColor, Chart.Transparent, Chart

.glassEffect());

t.setRoundedCorners(5);

}


/**

*

* 概述:生成柱状图的方法

*

* @Title: createXYChart

* @param data

* @param labels

* void

* @user <a href=mailto:linfenliang@126.com>linfenliang</a>

*/

public static void createXYChart(double[] data, String[] labels) {

BC = new XYChart(600, 550, 0xffffff, -1, 2);

BC.setDefaultFonts("simfang");

((XYChart) BC).setPlotArea(100, 20, 500, 500);

((XYChart) BC).addBarLayer3(data).setBorderColor(Chart.Transparent,

Chart.softLighting(Chart.Left));


((XYChart) BC).xAxis().setLabels(labels);

((XYChart) BC).yAxis().setTitle("数据值/小时", "simfang", 9).setFontAngle(0);

}


/**

*

* 概述:生成极坐标图的方法

*

* @Title: createPolarChart

* @param data

* @param labels

* void

* @user <a href=mailto:linfenliang@126.com>linfenliang</a>

*/

public static void createPolarChart(double[][] data, String[] labels) {

BC.setDefaultFonts("simfang");

BC = new PolarChart(600, 550, 0xe0e0e0, 0x000000, 1);

BC.setDefaultFonts("simfang");

//设置分类

for(int i=0;i<data.length;i++){

((PolarChart) BC).addAreaLayer(data[i], -1, ("系列"+(i+1)));

}

// BC.setSearchPath(getServletConfig().getServletContext(), request);

((PolarChart) BC).setPlotArea(230, 280, 180, 0xffffff);

((PolarChart) BC).setGridStyle(false);

LegendBox b = BC.addLegend(230, 35, false, "Arial Bold", 9);

b.setAlignment(Chart.TopCenter);

b.setBackground(Chart.Transparent, Chart.Transparent, 1);

b.setFontStyle("宋体");

((PolarChart) BC).angularAxis().setLabels(labels);

//防止乱码

((PolarChart) BC).angularAxis().setLabelStyle("black");

((PolarChart) BC).radialAxis().setLabelFormat("{value}");

//对坐标值的设定

((PolarChart) BC).radialAxis().setLabelStyle("simfang").setBackground( 0x40cccccc, 0);

}


}



allRef.jsp


<%@ page language="java" import="ChartDirector.*" pageEncoding="UTF-8"%>

<%

try {

out.clear();

GetSessionImage.getImage(request, response);

if (2 == 2)

return;

} catch (IllegalStateException e) {

String temp ="allRef.chart?" + request.getQueryString();

response.sendRedirect(response.encodeRedirectURL(temp));

return;

}

%>


callJsp.jsp


<%@ page language="java"

import="ChartDirector.*,com.bonree.test.SecondBean,com.bonree.test.TestArrBean,com.bonree.test.SelectShape;"

pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

</head>


<body>

<%

BaseChart BC = null;



SecondBean.storeData();

double[] data = SecondBean.getData();

String[] labels = SecondBean.getLabels();

SelectShape.createPieChart(data, labels);

BC = SelectShape.BC;

String chart1URL = BC.makeSession(request, "chart1");

String imageMap1 = BC.getHTMLImageMap("", "",

"title='{label}: 数据量:{value} (所占比例:"

+ SecondBean.getTableFormat() + ")'");



SelectShape.createXYChart(data, labels);

BC = SelectShape.BC;

String chart2URL = BC.makeSession(request, "chart2");

String imageMap2 = BC.getHTMLImageMap("", "",

"title='{xLabel}: 数据量:{value} '");



TestArrBean.storeInfo();

double[][] dt= TestArrBean.getData();

String[] ls = TestArrBean.getLabels();

SelectShape.createPolarChart(dt,ls);

BC = SelectShape.BC;

String chart3URL = BC.makeSession(request, "chart3");

String imageMap3 = BC.getHTMLImageMap("", "", "title='[{label}] {dataSetName}: {value}%'");


%>

<img

src='http://blog.163.com/linfenliang@126/blog/<%=response.encodeURL("allRef.jsp?" + chart1URL)%>'

usemap='#map1' border='0'>

<map name="map1"><%=imageMap1%></map>

<img src='http://blog.163.com/linfenliang@126/blog/<%=response.encodeURL("allRef.jsp?" + chart2URL)%>'

usemap="#map2" border="0">

<map name="map2"><%=imageMap2%></map>

<img src='http://blog.163.com/linfenliang@126/blog/<%=response.encodeURL("allRef.jsp?" + chart3URL)%>'

usemap="#map3" border="0">

<map name="map3"><%=imageMap3%></map>

</body>

</html>



效果图:

ChartDirector使用练习笔记(无乱码)