private ChartPanel getChartPanel(DefaultXYZDataset dataset, int i) { String key = (String) dataset.getSeriesKey(0); DateAxis yAxis = new DateAxis("Date"); yAxis.setLowerMargin(0.0); yAxis.setUpperMargin(0.0); yAxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 1)); NumberAxis xAxis = new NumberAxis("Hour"); xAxis.setUpperMargin(0.0); xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); XYBlockRenderer renderer = new XYBlockRenderer(); renderer.setBlockWidth(1000.0 * 60.0 * 60.0 * 24.0); renderer.setBlockAnchor(RectangleAnchor.BOTTOM_LEFT); // range scaling PaintScale paintScale; if (colorSort.getValue().equals("gray")) // gray scaling { paintScale = new GrayPaintScale(0.0, maxArray[i]); } else { int color_max, color_min; if (colorSort.getValue().equals("red")) { // red scaling color_max = 0xFF33FF; color_min = 0xFF3300; } else if (colorSort.getValue().equals("blue")) { // blue scaling color_max = 0x0033FF; color_min = 0x003300; } else { // yellow scaling color_max = 0xFFFFFF; color_min = 0xFFFF00; } paintScale = new LookupPaintScale(0.0, maxArray[i], new Color(color_max)); int d = Math.max((color_max - color_min) / ((int) maxArray[i] + 1), 1); for (int k = 0; k <= maxArray[i]; k++) { ((LookupPaintScale) paintScale) .add(new Double(k + 0.5), new Color(color_max - (k + 1) * d)); } } renderer.setPaintScale(paintScale); XYPlot plot = new XYPlot(dataset, yAxis, xAxis, renderer); plot.setOrientation(PlotOrientation.HORIZONTAL); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); plot.setForegroundAlpha(0.66F); JFreeChart chart = new JFreeChart(key, plot); chart.removeLegend(); chart.setBackgroundPaint(Color.white); // adding data ranges NumberAxis scaleAxis = new NumberAxis(null); scaleAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); scaleAxis.setLowerBound(0.0); scaleAxis.setUpperBound(maxArray[i]); PaintScaleLegend psl = new PaintScaleLegend(paintScale, scaleAxis); psl.setAxisOffset(5.0); psl.setPosition(RectangleEdge.BOTTOM); psl.setMargin(new RectangleInsets(5, 5, 5, 5)); chart.addSubtitle(psl); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setBorder(BorderFactory.createLineBorder(Color.lightGray)); return chartPanel; }
public static JFreeChart createChartZhu(IntervalXYDataset dataset) { JFreeChart chart = ChartFactory.createXYBarChart( "时间段内车辆出入统计柱形图", // title "数据日期从" + min + "到" + max, true, // x-axis label "车辆出入数量", // y-axis label dataset, PlotOrientation.VERTICAL, // data true, // create legend? true, // generate tooltips? false // generate URLs? ); chart.setBackgroundPaint(Color.white); chart.getTitle().setFont(new Font("宋体", Font.BOLD, 15)); // chart.getCategoryPlot().getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45); XYPlot plot2 = (XYPlot) chart.getPlot(); plot2.setBackgroundPaint(Color.lightGray); plot2.setDomainGridlinePaint(Color.white); plot2.setRangeGridlinePaint(Color.white); plot2.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); // 设定坐标轴与图表数据显示部分距离 plot2.setDomainCrosshairVisible(true); plot2.setRangeCrosshairVisible(true); plot2.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 15)); // 横轴框里的标题字体 chart.getLegend().setItemFont(new Font("宋体", Font.ITALIC, 10)); // 横轴列表字体 plot2.getDomainAxis().setTickLabelFont(new Font("新宋体", 1, 10)); // 横轴小标题字体 plot2.getDomainAxis().setLabelFont(new Font("新宋体", 1, 10)); plot2.setNoDataMessage("没有数据"); plot2.setBackgroundAlpha(0.5f); XYBarRenderer xyBarRender = new XYBarRenderer(); xyBarRender.setMargin(0.5); // 设置柱形图之间的间隔 GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, Color.black, 0.0F, 0.0F, new Color(0, 0, 64)); GradientPaint gradientpaint3 = new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, new Color(64, 0, 0)); xyBarRender.setSeriesPaint(0, gradientpaint1); xyBarRender.setSeriesPaint(1, gradientpaint3); xyBarRender.setSeriesVisibleInLegend(1, true, true); xyBarRender.setBarAlignmentFactor(0.5); xyBarRender.setDrawBarOutline(false); xyBarRender.setSeriesStroke( 0, new BasicStroke( 2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f, new float[] {10.0f, 6.0f}, 0.0f)); xyBarRender.setSeriesStroke( 1, new BasicStroke( 5.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 5.0f, new float[] {15.0f, 10.0f}, 10.0f)); plot2.setRenderer(xyBarRender); ClusteredXYBarRenderer clusteredxybarrenderer = new ClusteredXYBarRenderer(0.001D, false); plot2.setRenderer(clusteredxybarrenderer); // CategoryAxis横抽,通过getDomainAxis取得,NumberAxis纵轴,通过getRangeAxis取得。 DateAxis axis = (DateAxis) plot2.getDomainAxis(); // axis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd")); if (type == 0) // type = 0则以1天为横轴最小坐标,为1则以1月为横轴最小坐标 { axis.setDateFormatOverride(new SimpleDateFormat("dd")); // 设置时间格式 axis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 1)); } else { axis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM")); axis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 1)); } axis.setLabelFont(new Font("黑体", Font.TRUETYPE_FONT, 12)); // axis.setLabelAngle(0.85); axis.setAutoTickUnitSelection(false); // 时间轴的数据标签是否自动确 // ItemLabelPosition itemlabelposition = new // ItemLabelPosition(ItemLabelAnchor.INSIDE12,TextAnchor.CENTER_RIGHT,TextAnchor.CENTER_RIGHT, // -1.57D); // 下面这一块是用来设置轴的时间间隔的,可能会出现,出错时可以简单设处日期就行了 // 设置X轴间隔的另一种方法更好不出错 int a = 1; a = (int) (daycount / 10) + 1; // axis.setTickUnit(new DateTickUnit(DateTickUnit.DAY,a)); // axis.setLabelAngle(0.45); // axis.setLabelInsets(, true); NumberAxis numberaxis = (NumberAxis) plot2.getRangeAxis(); numberaxis.setAxisLineVisible(false); // 是否显示纵坐标 numberaxis.setTickMarksVisible(false); // 是否显示坐标标尺 numberaxis.setAutoRangeIncludesZero(false); // 是否自动包含0起点? 默认为true numberaxis.setAutoTickUnitSelection(false); // 数据轴的数据标签是否自动确定 int space_y = (int) ((highValue_Y * 1.1 - minValue_Y * 0.9) / 10); if (space_y == 0) space_y = 1; System.out.println("spacespace sapce space" + space_y); numberaxis.setTickUnit(new NumberTickUnit(space_y)); // 设置刻度显示的密度 numberaxis.setAutoRangeMinimumSize(2D); return chart; }
static JFreeChart createChart( NavigableMap<TimeAxisKey, DiffStat> aggregatedDiffstats, DiffStatConfiguration configuration) { boolean legend = false; boolean tooltips = false; boolean urls = false; Font helvetica = new Font("Helvetica", Font.PLAIN, 11 * configuration.multiplierInt()); XYDatasetMinMax datasetMinMax = createDeltaDataset("Additions and Delections", aggregatedDiffstats); XYDataset dataset = datasetMinMax.dataset; JFreeChart chart = ChartFactory.createTimeSeriesChart("", "", "", dataset, legend, tooltips, urls); chart.setBackgroundPaint(WHITE); chart.setBorderVisible(false); float strokeWidth = 1.2f * configuration.multiplierFloat(); XYPlot plot = chart.getXYPlot(); plot.setOrientation(VERTICAL); plot.setBackgroundPaint(WHITE); plot.setDomainGridlinesVisible(true); plot.setDomainGridlinePaint(AXIS_LINE_COLOR); plot.setDomainGridlineStroke(new BasicStroke(1.0f * configuration.multiplierFloat())); plot.setRangeGridlinesVisible(false); plot.setOutlineVisible(false); DateAxis dateAxis = (DateAxis) plot.getDomainAxis(); dateAxis.setDateFormatOverride(new SimpleDateFormat("MM/yy")); dateAxis.setTickLabelFont(helvetica); dateAxis.setAxisLineVisible(false); dateAxis.setTickUnit(computeDateTickUnit(aggregatedDiffstats)); RectangleInsets insets = new RectangleInsets( 8.0d * configuration.multiplierDouble(), 4.0d * configuration.multiplierDouble(), 4.0d * configuration.multiplierDouble(), 4.0d * configuration.multiplierDouble()); dateAxis.setTickLabelInsets(insets); NumberAxis additionDeletionAxis = (NumberAxis) plot.getRangeAxis(0); additionDeletionAxis.setAxisLineVisible(false); additionDeletionAxis.setLabel("Additions and Deletions"); additionDeletionAxis.setLabelFont(helvetica); additionDeletionAxis.setTickLabelFont(helvetica); additionDeletionAxis.setRangeType(RangeType.FULL); int lowerBound = datasetMinMax.min + (int) (datasetMinMax.min * 0.1d); additionDeletionAxis.setLowerBound(lowerBound); int upperBound = datasetMinMax.max + (int) (datasetMinMax.max * 0.1d); additionDeletionAxis.setUpperBound(upperBound); additionDeletionAxis.setNumberFormatOverride(new AbbreviatingNumberFormat()); additionDeletionAxis.setMinorTickMarksVisible(false); additionDeletionAxis.setTickMarkInsideLength(5.0f * configuration.multiplierFloat()); additionDeletionAxis.setTickMarkOutsideLength(0.0f); additionDeletionAxis.setTickMarkStroke(new BasicStroke(2.0f * configuration.multiplierFloat())); additionDeletionAxis.setTickUnit( new NumberTickUnit(computeTickUnitSize(datasetMinMax.max + abs(datasetMinMax.min)))); XYAreaRenderer areaRenderer = new XYAreaRenderer(XYAreaRenderer.AREA); areaRenderer.setOutline(true); areaRenderer.setSeriesOutlinePaint(0, ADDED_STROKE); areaRenderer.setSeriesOutlineStroke(0, new BasicStroke(strokeWidth)); areaRenderer.setSeriesPaint(0, ADDED_FILL); areaRenderer.setSeriesOutlinePaint(1, REMOVED_STROKE); areaRenderer.setSeriesOutlineStroke(1, new BasicStroke(strokeWidth)); areaRenderer.setSeriesPaint(1, REMOVED_FILL); plot.setRenderer(0, areaRenderer); // Total Axis NumberAxis totalAxis = new NumberAxis("Total Lines"); totalAxis.setAxisLineVisible(false); totalAxis.setLabelPaint(VALUE_LABEL); totalAxis.setTickLabelPaint(TOTAL_LABEL); totalAxis.setLabelFont(helvetica); totalAxis.setTickLabelFont(helvetica); totalAxis.setNumberFormatOverride(new AbbreviatingNumberFormat()); totalAxis.setMinorTickMarksVisible(false); totalAxis.setTickMarkInsideLength(5.0f * configuration.multiplierFloat()); totalAxis.setTickMarkOutsideLength(0.0f); totalAxis.setTickMarkStroke(new BasicStroke(2.0f * configuration.multiplierFloat())); totalAxis.setTickMarkPaint(TOTAL_LABEL); plot.setRangeAxis(1, totalAxis); plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT); XYDatasetAndTotal datasetAndTotal = createTotalDataset("Total Lines", aggregatedDiffstats); XYDataset totalDataSet = datasetAndTotal.dataset; plot.setDataset(1, totalDataSet); plot.mapDatasetToRangeAxis(1, 1); // XYItemRenderer totalRenderer = new XYSplineRenderer(); XYItemRenderer totalRenderer = new StandardXYItemRenderer(); totalRenderer.setSeriesPaint(0, TOTAL_FILL); totalRenderer.setSeriesStroke( 0, new BasicStroke( strokeWidth, CAP_ROUND, JOIN_ROUND, 10.0f * configuration.multiplierFloat(), new float[] { 6.0f * configuration.multiplierFloat(), 3.0f * configuration.multiplierFloat() }, 0.0f)); plot.setRenderer(1, totalRenderer); totalAxis.setTickUnit(new NumberTickUnit(computeTickUnitSize(datasetAndTotal.total))); return chart; }