// Main code for creating charts public void createChart(ChartViewer viewer, int chartIndex) { // The data for the bar chart double[] data = {450, 560, 630, 800, 1100, 1350, 1600, 1950, 2300, 2700}; // The labels for the bar chart String[] labels = { "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005" }; // Create a XYChart object of size 600 x 360 pixels XYChart c = new XYChart(600, 360); // Add a title to the chart using 18pt Times Bold Italic font c.addTitle("Annual Revenue for Star Tech", "Times New Roman Bold Italic", 18); // Set the plotarea at (60, 40) and of size 500 x 280 pixels. Use a vertical gradient color // from light blue (eeeeff) to deep blue (0000cc) as background. Set border and grid lines // to white (ffffff). c.setPlotArea( 60, 40, 500, 280, c.linearGradientColor(60, 40, 60, 280, 0xeeeeff, 0x0000cc), -1, 0xffffff, 0xffffff); // Add a multi-color bar chart layer using the supplied data. Use soft lighting effect with // light direction from left. c.addBarLayer3(data).setBorderColor(Chart.Transparent, Chart.softLighting(Chart.Left)); // Set x axis labels using the given labels c.xAxis().setLabels(labels); // Draw the ticks between label positions (instead of at label positions) c.xAxis().setTickOffset(0.5); // Add a title to the y axis with 10pt Arial Bold font c.yAxis().setTitle("USD (millions)", "Arial Bold", 10); // Set axis label style to 8pt Arial Bold c.xAxis().setLabelStyle("Arial Bold", 8); c.yAxis().setLabelStyle("Arial Bold", 8); // Set axis line width to 2 pixels c.xAxis().setWidth(2); c.yAxis().setWidth(2); // Output the chart viewer.setChart(c); // include tool tip for the chart viewer.setImageMap(c.getHTMLImageMap("clickable", "", "title='Year {xLabel}: US$ {value}M'")); }
public static ChartResult drawChart(HttpServletRequest request, ChartInfo chartInfo) throws Exception { ChartResult chartResult = new ChartResult(); // 创建图标 XYChart c = new XYChart(chartInfo.chartWidth, chartInfo.chartHeight, 0xeeeeff, 0x000000, 2); // c.setBackground(c.linearGradientColor(0, 0, 0, 100, 0x99ccff, 0xffffff), 0x888888); c.setRoundedFrame(); c.setDropShadow(); // 图标标题 c.addTitle( chartInfo.getChartTitle(), chartInfo.chartTitleFontType, chartInfo.chartTitleFontSize) .setMargin2(0, 0, 10, 0); // Set the plotarea at (60, 80) and of 510 x 275 pixels in size. Use transparent // border and dark grey (444444) dotted grid lines // 创建一个框,放在(60, 80) PlotArea plotArea = c.setPlotArea( 70, 80, c.getWidth() - 55 * 2, c.getHeight() - 80 * 2, -1, -1, Chart.Transparent, c.dashLineColor(0x444444, 0x0101), -1); // 线的说明框 LegendBox legendBox = c.addLegend(plotArea.getLeftX(), 45, false, chartInfo.lineFontType, chartInfo.lineFontSize); // legendBox.setAlignment(Chart.TopRight); legendBox.setBackground(Chart.Transparent, Chart.Transparent); // xy轴的密度 // c.yAxis().setTickDensity(plotArea.getHeight() / 20); // c.xAxis().setTickDensity(chartInfo.xDensity); // xy轴颜色,默认设置成透明 c.xAxis().setColors(Chart.Transparent); c.yAxis().setColors(Chart.Transparent); // Set the x-axis margins to 15 pixels, so that the horizontal grid lines can extend // beyond the leftmost and rightmost vertical grid lines c.xAxis().setMargin(10, 10); // 设置xy轴节点的字体和大小 c.xAxis().setLabelStyle(chartInfo.xAxisFontType, chartInfo.xAxisFontSize); c.yAxis().setLabelStyle(chartInfo.yAxisFontType, chartInfo.yAxisFontSize); // c.yAxis2().setLabelStyle("Arial Bold", 8); // 设置y轴说明 c.yAxis().setTitle(chartInfo.getyTitle(), chartInfo.yTitleFontType, chartInfo.yTitleFontSize); // 设置x轴数据,并设置x轴文子的角度 if (chartInfo.getxLabel() == null) { throw new Exception("没有图表的x轴数据,请设置(xLabel)"); } else { c.xAxis().setLabels(chartInfo.getxLabel()).setFontAngle(chartInfo.xLableFontAngle); c.xAxis().setLabelStyle(chartInfo.xAxisFontType, chartInfo.xAxisFontSize).setFontAngle(45); c.yAxis().setLabelFormat("{value|,}"); } /*Mark mark = c.xAxis2().addMark(5, 0x809933ff, "今天", "华文楷体");*/ /*// Set the mark line width to 2 pixels mark.setLineWidth(2); mark.setValue(5); //mark.setMargin(10,0,0,0); // Set the mark label font color to purple (0x9933ff) mark.setFontColor(0x9933ff);*/ // 设置y轴 Layer layer = c.addLineLayer2(); for (ChartDataSet dataSet : chartInfo.getChartDataList()) { // 如果数据集中的数据位空,则跳过 if (dataSet == null || dataSet.getyData() == null) { continue; } // 数据集中的x轴为空则x轴为默认, // 如果有设置x轴数据,则表示线的开始和结束有变化需要重新开一个layer if (dataSet.getxDataStart() != null) { layer = c.addLineLayer2(); } // 设定Y周数据、 颜色(-1是自动分配颜色)、标题,并设置节点形状和大小 layer .addDataSet(dataSet.getyData(), -1, dataSet.getTitle()) .setDataSymbol(Chart.CircleShape, 8); // 如果有x轴的数据则要设置x轴 layer.setXData2( dataSet.getxDataStart() == null ? 0 : dataSet.getxDataStart(), dataSet.getxDataEnd() == null ? c.xAxis().getMaxValue() : dataSet.getxDataEnd()); // 设施线的粗细 layer.setLineWidth(2); } // Output the chart if (StringUtils.isEmpty(chartInfo.getChartName())) { throw new Exception("没有图表的名称,请设置(chartName)"); } String chartURL = c.makeSession(request, chartInfo.getChartName()); // Include tool tip for the chart String imageMap = c.getHTMLImageMap("", "", "title='" + chartInfo.getAlertTitle() + "'"); chartResult.setChartURL(chartURL); chartResult.setImageMap(imageMap); chartResult.setChartName(chartInfo.getChartName()); return chartResult; }