private JFreeChart createChart(String chartType) { CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new DateAxis("Date")); JFreeChart chart = new JFreeChart(getEditorInput().getName(), plot); chart.setBackgroundPaint(Color.white); TimeSeriesCollection dataset = createTotalDataset(); StandardXYItemRenderer standardXYItemRenderer = new StandardXYItemRenderer(); standardXYItemRenderer.setBaseToolTipGenerator( StandardXYToolTipGenerator.getTimeSeriesInstance()); XYPlot subplot1 = new XYPlot(dataset, null, new NumberAxis("Value"), standardXYItemRenderer); plot.add(subplot1, 70); subplot1.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); subplot1.setDomainCrosshairVisible(true); subplot1.setRangeCrosshairVisible(true); XYAreaRenderer xyAreaRenderer = new XYAreaRenderer(); xyAreaRenderer.setBaseToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance()); XYPlot subplot2 = new XYPlot(createGainLossDataset(), null, new NumberAxis("Gain/Loss"), xyAreaRenderer); subplot2.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); subplot2.setDomainCrosshairVisible(true); subplot2.setRangeCrosshairVisible(true); plot.add(subplot2, 30); plot.setDrawingSupplier(new PeanutsDrawingSupplier()); DateAxis axis = (DateAxis) plot.getDomainAxis(); axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy")); timeChart = new TimeChart(chart, dataset); timeChart.setChartType(chartType); return chart; }
private JFreeChart buildChart(TimeSeriesCollection dataset, String title, boolean endPoints) { // Create the chart JFreeChart chart = ChartFactory.createTimeSeriesChart(title, "Date", "Price", dataset, true, true, false); // Display each series in the chart with its point shape in the legend LegendTitle sl = chart.getLegend(); // sl.setDisplaySeriesShapes(true); // Setup the appearance of the chart chart.setBackgroundPaint(Color.white); XYPlot plot = chart.getXYPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); plot.setAxisOffset(new RectangleInsets(UnitType.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); plot.setDomainCrosshairVisible(true); plot.setRangeCrosshairVisible(true); // Display data points or just the lines? if (endPoints) { XYItemRenderer renderer = plot.getRenderer(); if (renderer instanceof StandardXYItemRenderer) { StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer; // rr.setPlotShapes(true); rr.setShapesFilled(true); rr.setItemLabelsVisible(true); } } // Tell the chart how we would like dates to read DateAxis axis = (DateAxis) plot.getDomainAxis(); axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy")); return chart; }
private static JFreeChart createChart() { DateAxis dateaxis = new DateAxis("Date"); dateaxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE); NumberAxis numberaxis = new NumberAxis("Value"); IntervalXYDataset intervalxydataset = createDataset1(); XYBarRenderer xybarrenderer = new XYBarRenderer(0.20000000000000001D); xybarrenderer.setBaseToolTipGenerator( new StandardXYToolTipGenerator( "{0}: ({1}, {2})", new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00"))); XYPlot xyplot = new XYPlot(intervalxydataset, dateaxis, numberaxis, xybarrenderer); NumberAxis numberaxis1 = new NumberAxis("Value 2"); xyplot.setRangeAxis(1, numberaxis1); XYDataset xydataset = createDataset2A(); StandardXYItemRenderer standardxyitemrenderer = new StandardXYItemRenderer(); standardxyitemrenderer.setBaseToolTipGenerator( new StandardXYToolTipGenerator( "{0}: ({1}, {2})", new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00"))); xyplot.setDataset(1, xydataset); xyplot.setRenderer(1, standardxyitemrenderer); XYDataset xydataset1 = createDataset2B(); xyplot.setDataset(2, xydataset1); xyplot.setRenderer(2, new StandardXYItemRenderer()); xyplot.mapDatasetToRangeAxis(2, 1); xyplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); xyplot.setOrientation(PlotOrientation.VERTICAL); JFreeChart jfreechart = new JFreeChart("Overlaid XYPlot Demo 2", JFreeChart.DEFAULT_TITLE_FONT, xyplot, true); ChartUtilities.applyCurrentTheme(jfreechart); return jfreechart; }
public JPanel createChart( Date from, Date to, StockInfo stock1, StockInfo stock2, Color color1, Color color2, StockPriceType priceType) { LOG.info( "Generating chart for tickers:" + stock1.getTickerSymbol() + " & " + stock2.getTickerSymbol()); String chartTitle = df.format(from) + " to " + df.format(to) + " " + priceType; XYDataset dataset1 = createDataset(stock1, priceType); XYDataset dataset2 = createDataset(stock2, priceType); JFreeChart chart = ChartFactory.createTimeSeriesChart( chartTitle, DOMAIN_AXIS_TITLE, stock1.getTickerSymbol() + RANGE_AXIS_SUFFIX, dataset1, true, true, false); XYPlot plot = chart.getXYPlot(); NumberAxis axis2 = new NumberAxis(stock2.getTickerSymbol() + RANGE_AXIS_SUFFIX); Font tickLabelFont = axis2.getTickLabelFont().deriveFont(11.0F); Font labelFont = axis2.getLabelFont().deriveFont(15.0F).deriveFont(Font.BOLD); axis2.setTickLabelFont(tickLabelFont); axis2.setLabelFont(labelFont); axis2.setAutoRangeIncludesZero(false); plot.setRangeAxis(1, axis2); plot.setDataset(1, dataset2); plot.mapDatasetToRangeAxis(1, 1); StandardXYItemRenderer renderer = new StandardXYItemRenderer(); renderer.setSeriesPaint(0, color1); renderer.setBaseShapesVisible(true); renderer.setBaseToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance()); plot.setRenderer(renderer); StandardXYItemRenderer renderer2 = new StandardXYItemRenderer(); renderer2.setSeriesPaint(0, color2); renderer2.setBaseShapesVisible(true); renderer2.setBaseToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance()); plot.setRenderer(1, renderer2); DateAxis axis = (DateAxis) plot.getDomainAxis(); axis.setDateFormatOverride(df); ChartPanel chartPanel = new ChartPanel(chart); return chartPanel; }
/** * Creates a sample chart. * * @param index the chart index. * @param dataset the dataset. * @return A chart. */ private JFreeChart createChart(int index, XYDataset dataset) { // create the chart... JFreeChart chart = ChartFactory.createXYLineChart( "Chart " + (index + 1), // chart title "X", // x axis label "Y", // y axis label dataset, // data PlotOrientation.VERTICAL, false, // include legend false, // tooltips false // urls ); // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART... chart.setBackgroundPaint(Color.white); // get a reference to the plot for further customisation... XYPlot plot = chart.getXYPlot(); plot.setBackgroundPaint(Color.lightGray); // plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer(); renderer.setPlotShapes(true); renderer.setShapesFilled(true); // change the auto tick unit selection to integer units only... ValueAxis domainAxis = plot.getDomainAxis(); domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); ValueAxis rangeAxis = plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); // OPTIONAL CUSTOMISATION COMPLETED. return chart; }
/** * Creates the demo chart. * * @return The chart. */ private JFreeChart createChart() { // create some sample data final XYSeries series1 = new XYSeries("Something"); series1.add(0.0, 30.0); series1.add(1.0, 10.0); series1.add(2.0, 40.0); series1.add(3.0, 30.0); series1.add(4.0, 50.0); series1.add(5.0, 50.0); series1.add(6.0, 70.0); series1.add(7.0, 70.0); series1.add(8.0, 80.0); final XYSeriesCollection dataset1 = new XYSeriesCollection(); dataset1.addSeries(series1); final XYSeries series2 = new XYSeries("Something else"); series2.add(0.0, 5.0); series2.add(1.0, 4.0); series2.add(2.0, 1.0); series2.add(3.0, 5.0); series2.add(4.0, 0.0); final XYSeriesCollection dataset2 = new XYSeriesCollection(); dataset2.addSeries(series2); // create the chart final JFreeChart result = ChartFactory.createXYLineChart( "Tick Label Demo", "Domain Axis 1", "Range Axis 1", dataset1, PlotOrientation.VERTICAL, false, true, false); result.setBackgroundPaint(Color.white); final XYPlot plot = result.getXYPlot(); plot.setOrientation(PlotOrientation.VERTICAL); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); // plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); final StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer(); renderer.setPaint(Color.black); // DOMAIN AXIS 2 final NumberAxis xAxis2 = new NumberAxis("Domain Axis 2"); xAxis2.setAutoRangeIncludesZero(false); plot.setDomainAxis(1, xAxis2); // RANGE AXIS 2 final DateAxis yAxis1 = new DateAxis("Range Axis 1"); plot.setRangeAxis(yAxis1); final DateAxis yAxis2 = new DateAxis("Range Axis 2"); plot.setRangeAxis(1, yAxis2); plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT); plot.setDataset(1, dataset2); plot.mapDatasetToDomainAxis(1, 1); plot.mapDatasetToRangeAxis(1, 1); return result; }
/** * Updates the plot properties to match the properties defined on the panel. * * @param plot The plot. */ public void updatePlotProperties(Plot plot) { // set the plot properties... plot.setOutlinePaint(getOutlinePaint()); plot.setOutlineStroke(getOutlineStroke()); plot.setBackgroundPaint(getBackgroundPaint()); plot.setInsets(getPlotInsets()); // then the axis properties... if (this.domainAxisPropertyPanel != null) { Axis domainAxis = null; if (plot instanceof CategoryPlot) { CategoryPlot p = (CategoryPlot) plot; domainAxis = p.getDomainAxis(); } else if (plot instanceof XYPlot) { XYPlot p = (XYPlot) plot; domainAxis = p.getDomainAxis(); } if (domainAxis != null) { this.domainAxisPropertyPanel.setAxisProperties(domainAxis); } } if (this.rangeAxisPropertyPanel != null) { Axis rangeAxis = null; if (plot instanceof CategoryPlot) { CategoryPlot p = (CategoryPlot) plot; rangeAxis = p.getRangeAxis(); } else if (plot instanceof XYPlot) { XYPlot p = (XYPlot) plot; rangeAxis = p.getRangeAxis(); } else if (plot instanceof PolarPlot) { PolarPlot p = (PolarPlot) plot; rangeAxis = p.getAxis(); } if (rangeAxis != null) { this.rangeAxisPropertyPanel.setAxisProperties(rangeAxis); } } if (this.plotOrientation != null) { if (plot instanceof CategoryPlot) { CategoryPlot p = (CategoryPlot) plot; p.setOrientation(this.plotOrientation); } else if (plot instanceof XYPlot) { XYPlot p = (XYPlot) plot; p.setOrientation(this.plotOrientation); } } if (this.drawLines != null) { if (plot instanceof CategoryPlot) { CategoryPlot p = (CategoryPlot) plot; CategoryItemRenderer r = p.getRenderer(); if (r instanceof LineAndShapeRenderer) { ((LineAndShapeRenderer) r).setLinesVisible(this.drawLines.booleanValue()); } } else if (plot instanceof XYPlot) { XYPlot p = (XYPlot) plot; XYItemRenderer r = p.getRenderer(); if (r instanceof StandardXYItemRenderer) { ((StandardXYItemRenderer) r).setPlotLines(this.drawLines.booleanValue()); } } } if (this.drawShapes != null) { if (plot instanceof CategoryPlot) { CategoryPlot p = (CategoryPlot) plot; CategoryItemRenderer r = p.getRenderer(); if (r instanceof LineAndShapeRenderer) { ((LineAndShapeRenderer) r).setShapesVisible(this.drawShapes.booleanValue()); } } else if (plot instanceof XYPlot) { XYPlot p = (XYPlot) plot; XYItemRenderer r = p.getRenderer(); if (r instanceof StandardXYItemRenderer) { ((StandardXYItemRenderer) r).setBaseShapesVisible(this.drawShapes.booleanValue()); } } } // dmo: added this panel for colorbar control. (start dmo additions) if (this.colorBarAxisPropertyPanel != null) { ColorBar colorBar = null; if (plot instanceof ContourPlot) { ContourPlot p = (ContourPlot) plot; colorBar = p.getColorBar(); } if (colorBar != null) { this.colorBarAxisPropertyPanel.setAxisProperties(colorBar); } } // dmo: (end dmo additions) }
protected JPanel createPlotPanel(Plot plot) { this.plotInsets = plot.getInsets(); this.backgroundPaintSample = new PaintSample(plot.getBackgroundPaint()); this.outlineStrokeSample = new StrokeSample(plot.getOutlineStroke()); this.outlinePaintSample = new PaintSample(plot.getOutlinePaint()); if (plot instanceof CategoryPlot) { this.plotOrientation = ((CategoryPlot) plot).getOrientation(); } else if (plot instanceof XYPlot) { this.plotOrientation = ((XYPlot) plot).getOrientation(); } if (plot instanceof CategoryPlot) { CategoryItemRenderer renderer = ((CategoryPlot) plot).getRenderer(); if (renderer instanceof LineAndShapeRenderer) { LineAndShapeRenderer r = (LineAndShapeRenderer) renderer; this.drawLines = BooleanUtilities.valueOf(r.getBaseLinesVisible()); this.drawShapes = BooleanUtilities.valueOf(r.getBaseShapesVisible()); } } else if (plot instanceof XYPlot) { XYItemRenderer renderer = ((XYPlot) plot).getRenderer(); if (renderer instanceof StandardXYItemRenderer) { StandardXYItemRenderer r = (StandardXYItemRenderer) renderer; this.drawLines = BooleanUtilities.valueOf(r.getPlotLines()); this.drawShapes = BooleanUtilities.valueOf(r.getBaseShapesVisible()); } } setLayout(new BorderLayout()); this.availableStrokeSamples = new StrokeSample[4]; this.availableStrokeSamples[0] = new StrokeSample(null); this.availableStrokeSamples[1] = new StrokeSample(new BasicStroke(1.0f)); this.availableStrokeSamples[2] = new StrokeSample(new BasicStroke(2.0f)); this.availableStrokeSamples[3] = new StrokeSample(new BasicStroke(3.0f)); // create a panel for the settings... JPanel panel = new JPanel(new BorderLayout()); panel.setBorder( BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), plot.getPlotType() + localizationResources.getString(":"))); JPanel general = new JPanel(new BorderLayout()); general.setBorder(BorderFactory.createTitledBorder(localizationResources.getString("General"))); JPanel interior = new JPanel(new LCBLayout(7)); interior.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); // interior.add(new JLabel(localizationResources.getString("Insets"))); // JButton button = new JButton( // localizationResources.getString("Edit...") // ); // button.setActionCommand("Insets"); // button.addActionListener(this); // // this.insetsTextField = new InsetsTextField(this.plotInsets); // this.insetsTextField.setEnabled(false); // interior.add(this.insetsTextField); // interior.add(button); interior.add(new JLabel(localizationResources.getString("Outline_stroke"))); JButton button = new JButton(localizationResources.getString("Select...")); button.setActionCommand("OutlineStroke"); button.addActionListener(this); interior.add(this.outlineStrokeSample); interior.add(button); interior.add(new JLabel(localizationResources.getString("Outline_Paint"))); button = new JButton(localizationResources.getString("Select...")); button.setActionCommand("OutlinePaint"); button.addActionListener(this); interior.add(this.outlinePaintSample); interior.add(button); interior.add(new JLabel(localizationResources.getString("Background_paint"))); button = new JButton(localizationResources.getString("Select...")); button.setActionCommand("BackgroundPaint"); button.addActionListener(this); interior.add(this.backgroundPaintSample); interior.add(button); if (this.plotOrientation != null) { boolean isVertical = this.plotOrientation.equals(PlotOrientation.VERTICAL); int index = isVertical ? ORIENTATION_VERTICAL : ORIENTATION_HORIZONTAL; interior.add(new JLabel(localizationResources.getString("Orientation"))); this.orientationCombo = new JComboBox(orientationNames); this.orientationCombo.setSelectedIndex(index); this.orientationCombo.setActionCommand("Orientation"); this.orientationCombo.addActionListener(this); interior.add(new JPanel()); interior.add(this.orientationCombo); } if (this.drawLines != null) { interior.add(new JLabel(localizationResources.getString("Draw_lines"))); this.drawLinesCheckBox = new JCheckBox(); this.drawLinesCheckBox.setSelected(this.drawLines.booleanValue()); this.drawLinesCheckBox.setActionCommand("DrawLines"); this.drawLinesCheckBox.addActionListener(this); interior.add(new JPanel()); interior.add(this.drawLinesCheckBox); } if (this.drawShapes != null) { interior.add(new JLabel(localizationResources.getString("Draw_shapes"))); this.drawShapesCheckBox = new JCheckBox(); this.drawShapesCheckBox.setSelected(this.drawShapes.booleanValue()); this.drawShapesCheckBox.setActionCommand("DrawShapes"); this.drawShapesCheckBox.addActionListener(this); interior.add(new JPanel()); interior.add(this.drawShapesCheckBox); } general.add(interior, BorderLayout.NORTH); JPanel appearance = new JPanel(new BorderLayout()); appearance.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); appearance.add(general, BorderLayout.NORTH); JTabbedPane tabs = createPlotTabs(plot); tabs.add(localizationResources.getString("Appearance"), appearance); panel.add(tabs); return panel; }