java如何绘制图表_java绘制excel图表(jxcell)
import com.jxcell.*;
import java.awt.*;
public class ChartFormatTest
{
public static void main(String args[])
{
View m_view = new View();
RangeRef newRange = null;
try {
m_view.getLock();
//標(biāo)題 setTextAsValue(行,列,值);
m_view.setTextAsValue(1,2,"Jan");
m_view.setTextAsValue(1,3,"Feb");
m_view.setTextAsValue(1,4,"Mar");
m_view.setTextAsValue(1,5,"Apr");
//分項(xiàng)
m_view.setTextAsValue(2,1,"香蕉");
m_view.setTextAsValue(3,1,"大鴨梨");
m_view.setTextAsValue(4,1,"芒果");
m_view.setTextAsValue(5,1,"水果1");
m_view.setTextAsValue(6,1,"水果2");
m_view.setTextAsValue(7,1,"共計(jì)");
//
m_view.setTextAsValue(1,6,"time");
m_view.setNumber(2,6,1);
m_view.setNumber(3,6,2);
m_view.setNumber(4,6,3);
m_view.setNumber(5,6,4);
m_view.setNumber(6,6,5);
m_view.setNumber(7,6,6);
//數(shù)據(jù)區(qū)域隨機(jī)賦值
for(int col = 2; col <= 5; col++)
for(int row = 2; row <= 7; row++)
m_view.setFormula(row, col, "rand()");//rand()為excle隨機(jī)函數(shù)
//設(shè)置公式
m_view.setFormula(7, 2, "SUM(C3:C7)");
//選中單元格區(qū)域
m_view.setSelection("C8:F8");
//編輯復(fù)制 向右復(fù)制
m_view.editCopyRight();
//繪圖區(qū)坐標(biāo)addChart(左上列x,左上行y,右下列x,右下行y)
ChartShape chart = m_view.addChart(0, 9.1, 7, 20.4);
//圖標(biāo)形式
chart.setChartType(ChartShape.TypeLine);
/* TypeBar:橫向柱狀圖 TypePie:餅狀圖 TypeLine:線狀圖 TypeArea:面積圖 TypeDoughnut:圈圖 TypeScatter:線點(diǎn)圖 TypeBubble:沒怎么看懂,就是一個(gè)灰圖,不過查閱資料,貌似是泡狀圖 */
//設(shè)置連接區(qū)域
chart.setLinkRange("Sheet1!$C$2", false);
//添加一個(gè)系列
chart.addSeries();
//系列名字
chart.setSeriesName(0, "Sheet1!$C$2");
//系列值
chart.setSeriesYValueFormula(0, "Sheet1!$C$3:$C$7");
//系列分類
chart.setCategoryFormula("Sheet1!$B$3:$B$7");
chart.addSeries();
chart.setSeriesName(1, "Sheet1!$D$2");
chart.setSeriesYValueFormula(1, "Sheet1!$D$3:$D$7");
chart.addSeries();
chart.setSeriesName(2, "Sheet1!$E$2");
chart.setSeriesYValueFormula(2, "Sheet1!$E$3:$E$7");
chart.addSeries();
chart.setSeriesName(3, "Sheet1!$F$2");
chart.setSeriesYValueFormula(3, "Sheet1!$F$3:$F$7");
// chart.getChart().validateData();
//設(shè)置橫坐標(biāo)標(biāo)題
chart.setAxisTitle(ChartShape.XAxis, 0, "橫坐標(biāo)標(biāo)題");
//設(shè)置縱坐標(biāo)標(biāo)題
chart.setAxisTitle(ChartShape.YAxis, 0, "縱坐標(biāo)標(biāo)題");
//設(shè)置圖表樣式
ChartFormat cf = chart.getChartFormat();
//設(shè)置背景色
cf.setPattern((short)1);
cf.setPatternFG(Color.LIGHT_GRAY.getRGB());
chart.setChartFormat(cf);
//設(shè)置繪圖區(qū)顏色
cf = chart.getPlotFormat();
cf.setPattern((short)1);
cf.setPatternFG(new Color(204, 255, 255).getRGB());
chart.setPlotFormat(cf);
//設(shè)置橫坐標(biāo)文字大小
cf = chart.getAxisFormat(ChartShape.XAxis, 0);
cf.setFontSizeInPoints(8.5);
chart.setAxisFormat(ChartShape.XAxis, 0, cf);
//設(shè)置縱坐標(biāo)文字大小
cf = chart.getAxisFormat(ChartShape.YAxis, 0);
cf.setFontSizeInPoints(8.5);
chart.setAxisFormat(ChartShape.YAxis, 0, cf);
//設(shè)置圖標(biāo)內(nèi)標(biāo)線樣式
cf = chart.getSeriesFormat(0);//地0個(gè)
cf.setLineStyle((short)1);
cf.setLineWeight(3*20);
cf.setLineColor((new Color(0, 0, 128)).getRGB());
cf.setMarkerAuto(false);
cf.setMarkerStyle((short)0);
chart.setSeriesFormat(0, cf);
cf = chart.getSeriesFormat(1);
cf.setLineStyle((short)1);
cf.setLineWeight(3*20);
cf.setLineColor((new Color(255, 0, 255)).getRGB());
cf.setMarkerAuto(false);
cf.setMarkerStyle((short)0);
chart.setSeriesFormat(1, cf);
cf = chart.getSeriesFormat(2);
cf.setLineStyle((short)1);
cf.setLineWeight(3*20);
cf.setLineColor((new Color(255, 255, 0)).getRGB());
cf.setMarkerAuto(false);
cf.setMarkerStyle((short)0);
chart.setSeriesFormat(2, cf);
cf = chart.getSeriesFormat(3);
cf.setLineStyle((short)1);
cf.setLineWeight(3*20);
cf.setLineColor((new Color(0, 255, 255)).getRGB());
cf.setMarkerAuto(false);
cf.setMarkerStyle((short)0);
chart.setSeriesFormat(3, cf);
//主格網(wǎng)
cf = chart.getMajorGridFormat(ChartShape.YAxis, 0);
cf.setLineStyle((short)2);
cf.setLineColor((new Color(255, 0, 0)).getRGB());
cf.setLineAuto();
chart.setMajorGridFormat(ChartShape.YAxis, 0, cf);
//圖利位置
chart.setLegendPosition(ChartFormat.LegendPlacementRight);
//圖利樣式
cf = chart.getLegendFormat();
cf.setFontBold(true);
cf.setFontSizeInPoints(8);
chart.setLegendFormat(cf);
//excel寫出路徑
m_view.write("c:\\out.xls");
System.out.println("end");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
finally
{
m_view.releaseLock();
}
}
}
總結(jié)
以上是生活随笔為你收集整理的java如何绘制图表_java绘制excel图表(jxcell)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 存储过程 排序_更改MySQ
- 下一篇: 800平办公楼要安装多少喷淋头?