public JFreeChart linePlot(String xLabel, String yLabel){ int numDatasets = dataset.size(); JFreeChart result = ChartFactory.createXYLineChart( chartTitle, // chart title xLabel, // x axis label yLabel, // y axis label dataset.get(0), // data PlotOrientation.VERTICAL, true, // include legend true, // tooltips false // urls ); XYPlot plot = result.getXYPlot(); plot.getRenderer().setSeriesStroke(0, new BasicStroke(1.0f)); plot.getRenderer().setSeriesPaint(0, seriesColor.get(0)); for(int i=1;i<numDatasets;i++){ plot.setDataset(i,dataset.get(i)); //XYItemRenderer renderer = plot.getRenderer(i-0); //plot.setRenderer(i, new XYLineAndShapeRenderer(false, true)); plot.getRenderer(i).setSeriesStroke(0, new BasicStroke(1.0f)); plot.getRenderer(i).setSeriesPaint(0,seriesColor.get(i)); } return result; }
/** * Creates and returns a default instance of a candlesticks chart. * * @param title the chart title (<code>null</code> permitted). * @param timeAxisLabel a label for the time axis (<code>null</code> * permitted). * @param valueAxisLabel a label for the value axis (<code>null</code> * permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param legend a flag specifying whether or not a legend is required. * * @return A candlestick chart. */ public static JFreeChart createCandlestickChart(String title, String timeAxisLabel, String valueAxisLabel, OHLCDataset dataset, boolean legend) { ValueAxis timeAxis = new DateAxis(timeAxisLabel); NumberAxis valueAxis = new NumberAxis(valueAxisLabel); XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, null); plot.setRenderer(new CandlestickRenderer()); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); return chart; }
/** * Draws the visual representation of a single data item. * * @param g2 the graphics device. * @param state the renderer state. * @param dataArea the area within which the plot is being drawn. * @param info collects info about the drawing. * @param plot the plot (can be used to obtain standard color * information etc). * @param domainAxis the domain axis. * @param rangeAxis the range axis. * @param dataset the dataset. * @param series the series index (zero-based). * @param item the item index (zero-based). * @param crosshairState crosshair information for the plot * (<code>null</code> permitted). * @param pass the pass index. */ public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) { PlotOrientation orientation = plot.getOrientation(); if (orientation == PlotOrientation.HORIZONTAL) { drawHorizontalItem(g2, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState, pass); } else if (orientation == PlotOrientation.VERTICAL) { drawVerticalItem(g2, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState, pass); } }
/** * Updates the preselected y-value. */ private void updateYFieldValue() { // update preselected y value because range axis has been changed if (mousePosition != null) { Rectangle2D plotArea = engine.getChartPanel().getScreenDataArea(); if (engine.getChartPanel().getChart().getPlot() instanceof XYPlot) { XYPlot plot = (XYPlot) engine.getChartPanel().getChart().getPlot(); // calculate y value for (int i = 0; i < plot.getRangeAxisCount(); i++) { ValueAxis config = plot.getRangeAxis(i); if (config != null && config.getLabel() != null) { if (config.getLabel().equals(String.valueOf(rangeAxisSelectionCombobox.getSelectedItem()))) { double chartY = config.java2DToValue(mousePosition.getY(), plotArea, plot.getRangeAxisEdge()); yField.setText(String.valueOf(chartY)); } } } } } }
private Point2D.Double _getPointForCandlestick(int plotIndex, int seriesIndex, int dataIndex) { final ChartPanel chartPanel = this.chartJDialog.getChartPanel(); final XYPlot plot = this.chartJDialog.getPlot(plotIndex); final ValueAxis domainAxis = plot.getDomainAxis(); final RectangleEdge domainAxisEdge = plot.getDomainAxisEdge(); final ValueAxis rangeAxis = plot.getRangeAxis(); final RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge(); final org.jfree.data.xy.DefaultHighLowDataset defaultHighLowDataset = (org.jfree.data.xy.DefaultHighLowDataset)plot.getDataset(seriesIndex); if (dataIndex >= defaultHighLowDataset.getItemCount(0)) { /* Not ready yet. */ return null; } final double xValue = defaultHighLowDataset.getXDate(0, dataIndex).getTime(); final double yValue = defaultHighLowDataset.getCloseValue(0, dataIndex); final Rectangle2D plotArea = chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(plotIndex).getDataArea(); final double xJava2D = domainAxis.valueToJava2D(xValue, plotArea, domainAxisEdge); final double yJava2D = rangeAxis.valueToJava2D(yValue, plotArea, rangeAxisEdge); // Use Double version, to avoid from losing precision. return new Point2D.Double(xJava2D, yJava2D); }
private void plotTradeBubblesOnChart(ArrayList<Integer> toPlot, String p, int k, int j) { final Plot main_plot = (Plot)((CombinedDomainXYPlot)this.candlestickChart.getPlot()).getSubplots().get(0); final XYPlot plot = (XYPlot) main_plot; final TimeSeries series = new TimeSeries(p); ///* for(Integer i: toPlot) { switch(j) { case 0: series.add(new Minute(new Date(chartDatas.get(i).getStartTimeStamp())),chartDatas.get(i).getOpen()); break; case 1: series.add(new Minute(new Date(chartDatas.get(i).getStartTimeStamp())),chartDatas.get(i).getHigh()); break; case 2: series.add(new Minute(new Date(chartDatas.get(i).getStartTimeStamp())),chartDatas.get(i).getLow()); break; case 3: series.add(new Minute(new Date(chartDatas.get(i).getStartTimeStamp())),chartDatas.get(i).getClose()); break; } } /* for (int i = 0; i < defaultHighLowDataset.getSeriesCount(); i++) { series.add(new Minute(defaultHighLowDataset.getXDate(0, i)),plot[i]); } */ XYDataset dataSet = new TimeSeriesCollection(series); plot.setDataset(k, dataSet); XYItemRenderer ir = new XYShapeRenderer(); //ir.s plot.setRenderer(k, ir); }
/** * Draws the visual representation of a single data item. * * @param g2 the graphics device. * @param state the renderer state. * @param dataArea the area within which the data is being drawn. * @param info collects information about the drawing. * @param plot the plot (can be used to obtain standard color * information etc). * @param domainAxis the domain (horizontal) axis. * @param rangeAxis the range (vertical) axis. * @param dataset the dataset. * @param series the series index (zero-based). * @param item the item index (zero-based). * @param crosshairState crosshair information for the plot * (<code>null</code> permitted). * @param pass the pass index. */ public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) { if (pass == 0) { drawItemPass0(g2, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState); } else if (pass == 1) { drawItemPass1(g2, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState); } }
/** * Helper method which returns a value if it lies * inside the visible dataArea and otherwise the corresponding * coordinate on the border of the dataArea. The PlotOrientation * is taken into account. * Useful to avoid possible sun.dc.pr.PRException: endPath: bad path * which occurs when trying to draw lines/shapes which in large part * lie outside of the visible dataArea. * * @param value the value which shall be * @param dataArea the area within which the data is being drawn. * @param plot the plot (can be used to obtain standard color information etc). * @return <code>double</code> value inside the data area. */ protected static double restrictValueToDataArea(double value, XYPlot plot, Rectangle2D dataArea) { double min = 0; double max = 0; if (plot.getOrientation() == PlotOrientation.VERTICAL) { min = dataArea.getMinY(); max = dataArea.getMaxY(); } else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { min = dataArea.getMinX(); max = dataArea.getMaxX(); } if (value < min) { value = min; } else if (value > max) { value = max; } return value; }
private JFreeChart createChart(final XYDataset dataset) { final JFreeChart chart = ChartFactory.createXYLineChart( "MONA STORAGE GRAPH", "KeyGeneration per users ", "KeyGenerating size in Bytes", dataset, PlotOrientation.VERTICAL, true, true, false); chart.setBackgroundPaint(Color.white); final XYPlot plot1 = chart.getXYPlot(); plot1.setBackgroundPaint(Color.lightGray); plot1.setDomainGridlinePaint(Color.white); plot1.setRangeGridlinePaint(Color.white); final XYPlot plot2 = chart.getXYPlot(); plot2.setBackgroundPaint(Color.lightGray); plot2.setDomainGridlinePaint(Color.white); plot2.setRangeGridlinePaint(Color.white); final XYPlot plot3 = chart.getXYPlot(); plot3.setBackgroundPaint(Color.lightGray); plot3.setDomainGridlinePaint(Color.white); plot3.setRangeGridlinePaint(Color.white); return chart; }
/** * Replaces the dataset and checks that it has changed as expected. */ public void testReplaceDataset() { // create a dataset... XYSeries series1 = new XYSeries("Series 1"); series1.add(10.0, 10.0); series1.add(20.0, 20.0); series1.add(30.0, 30.0); XYDataset dataset = new XYSeriesCollection(series1); LocalListener l = new LocalListener(); this.chart.addChangeListener(l); XYPlot plot = (XYPlot) this.chart.getPlot(); plot.setDataset(dataset); assertEquals(true, l.flag); ValueAxis axis = plot.getRangeAxis(); Range range = axis.getRange(); assertTrue("Expecting the lower bound of the range to be around 10: " + range.getLowerBound(), range.getLowerBound() <= 10); assertTrue("Expecting the upper bound of the range to be around 30: " + range.getUpperBound(), range.getUpperBound() >= 30); }
/** * Draws the annotation. * * @param g2 the graphics device. * @param plot the plot. * @param dataArea the data area. * @param domainAxis the domain axis. * @param rangeAxis the range axis. */ public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis domainAxis, ValueAxis rangeAxis) { PlotOrientation orientation = plot.getOrientation(); RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(plot.getDomainAxisLocation(), orientation); RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(plot.getRangeAxisLocation(), orientation); float j2DX = (float) domainAxis.valueToJava2D(this.x, dataArea, domainEdge); float j2DY = (float) rangeAxis.valueToJava2D(this.y, dataArea, rangeEdge); Rectangle2D area = new Rectangle2D.Double(j2DX - this.width / 2.0, j2DY - this.height / 2.0, this.width, this.height); this.drawable.draw(g2, area); }
public void inicializacionJFreeChart(String title, String xAxisLabel, String yAxisLabel, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) { chart1 = ChartFactory.createXYLineChart( title, // chart title Titulo local del grafico xAxisLabel, // x axis label yAxisLabel, // y axis label null, // data orientation, legend, // include legend tooltips, // tooltips urls // urls ); // get a reference to the plot for further customisation... XYPlot plot = chart1.getXYPlot(); }
/** * Creates a sample chart. * * @param dataset the dataset. * * @return A sample chart. */ private JFreeChart createChart(IntervalXYDataset dataset,String s) { final JFreeChart chart = ChartFactory.createXYBarChart( "Histogram Plot: "+s, "Keyword index", false, "frequency", dataset, PlotOrientation.VERTICAL, true, true, false ); XYPlot plot = (XYPlot) chart.getPlot(); final IntervalMarker target = new IntervalMarker(400.0, 700.0); //target.setLabel("Target Range"); target.setLabelFont(new Font("SansSerif", Font.ITALIC, 11)); target.setLabelAnchor(RectangleAnchor.LEFT); target.setLabelTextAnchor(TextAnchor.CENTER_LEFT); target.setPaint(new Color(222, 222, 255, 128)); plot.addRangeMarker(target, Layer.BACKGROUND); return chart; }
private static void addBuySellSignals(TimeSeries series, Strategy strategy, XYPlot plot) { // Running the strategy TimeSeriesManager seriesManager = new TimeSeriesManager(series); List<Trade> trades = seriesManager.run(strategy).getTrades(); // Adding markers to plot for (Trade trade : trades) { // Buy signal double buySignalTickTime = new Minute( Date.from(series.getTick(trade.getEntry().getIndex()).getEndTime().toInstant())) .getFirstMillisecond(); Marker buyMarker = new ValueMarker(buySignalTickTime); buyMarker.setPaint(Color.GREEN); buyMarker.setLabel("B"); plot.addDomainMarker(buyMarker); // Sell signal double sellSignalTickTime = new Minute( Date.from(series.getTick(trade.getExit().getIndex()).getEndTime().toInstant())) .getFirstMillisecond(); Marker sellMarker = new ValueMarker(sellSignalTickTime); sellMarker.setPaint(Color.RED); sellMarker.setLabel("S"); plot.addDomainMarker(sellMarker); } }
/** * Creates and returns a default instance of a candlesticks chart. * * @param title the chart title (<code>null</code> permitted). * @param timeAxisLabel a label for the time axis (<code>null</code> permitted). * @param valueAxisLabel a label for the value axis (<code>null</code> permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param legend a flag specifying whether or not a legend is required. * * @return a candlestick chart. */ public static JFreeChart createCandlestickChart(String title, String timeAxisLabel, String valueAxisLabel, OHLCDataset dataset, boolean legend) { ValueAxis timeAxis = new DateAxis(timeAxisLabel); NumberAxis valueAxis = new NumberAxis(valueAxisLabel); XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, null); plot.setRenderer(new CandlestickRenderer()); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); return chart; }
/** * Creates and returns a default instance of a high-low-open-close chart. * * @param title the chart title (<code>null</code> permitted). * @param timeAxisLabel a label for the time axis (<code>null</code> permitted). * @param valueAxisLabel a label for the value axis (<code>null</code> permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param legend a flag specifying whether or not a legend is required. * * @return a high-low-open-close chart. */ public static JFreeChart createHighLowChart(String title, String timeAxisLabel, String valueAxisLabel, OHLCDataset dataset, boolean legend) { ValueAxis timeAxis = new DateAxis(timeAxisLabel); NumberAxis valueAxis = new NumberAxis(valueAxisLabel); HighLowRenderer renderer = new HighLowRenderer(); renderer.setToolTipGenerator(new HighLowItemLabelGenerator()); XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, renderer); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); return chart; }
/** * Creates and returns a default instance of a signal chart. * * @param title the chart title (<code>null</code> permitted). * @param timeAxisLabel a label for the time axis (<code>null</code> permitted). * @param valueAxisLabel a label for the value axis (<code>null</code> permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param legend a flag specifying whether or not a legend is required. * * @return a signal chart. */ public static JFreeChart createSignalChart(String title, String timeAxisLabel, String valueAxisLabel, SignalsDataset dataset, boolean legend) { ValueAxis timeAxis = new DateAxis(timeAxisLabel); NumberAxis valueAxis = new NumberAxis(valueAxisLabel); XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, null); plot.setRenderer(new SignalRenderer()); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); return chart; }
/** * Creates and returns a default instance of a box and whisker chart. * * @param title the chart title (<code>null</code> permitted). * @param timeAxisLabel a label for the time axis (<code>null</code> permitted). * @param valueAxisLabel a label for the value axis (<code>null</code> permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param legend a flag specifying whether or not a legend is required. * * @return a box and whisker chart. */ public static JFreeChart createBoxAndWhiskerChart(String title, String timeAxisLabel, String valueAxisLabel, BoxAndWhiskerXYDataset dataset, boolean legend) { ValueAxis timeAxis = new DateAxis(timeAxisLabel); NumberAxis valueAxis = new NumberAxis(valueAxisLabel); valueAxis.setAutoRangeIncludesZero(false); XYBoxAndWhiskerRenderer renderer = new XYBoxAndWhiskerRenderer(10.0); XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, renderer); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); return chart; }
/** * Creates a wind plot with default settings. * * @param title the chart title (<code>null</code> permitted). * @param xAxisLabel a label for the x-axis (<code>null</code> permitted). * @param yAxisLabel a label for the y-axis (<code>null</code> permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param legend a flag that controls whether or not a legend is created. * @param tooltips configure chart to generate tool tips? * @param urls configure chart to generate URLs? * * @return A wind plot. * */ public static JFreeChart createWindPlot(String title, String xAxisLabel, String yAxisLabel, WindDataset dataset, boolean legend, boolean tooltips, boolean urls) { ValueAxis xAxis = new DateAxis(xAxisLabel); ValueAxis yAxis = new NumberAxis(yAxisLabel); yAxis.setRange(-12.0, 12.0); WindItemRenderer renderer = new WindItemRenderer(); if (tooltips) { renderer.setToolTipGenerator(new StandardXYToolTipGenerator()); } if (urls) { renderer.setURLGenerator(new StandardXYURLGenerator()); } XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); return chart; }
/** * Fills a band between two values on the axis. This can be used to color bands between the * grid lines. * * @param g2 the graphics device. * @param plot the plot. * @param axis the domain axis. * @param dataArea the data area. * @param start the start value. * @param end the end value. */ public void fillDomainGridBand(Graphics2D g2, XYPlot plot, ValueAxis axis, Rectangle2D dataArea, double start, double end) { double x1 = axis.valueToJava2D(start, dataArea, plot.getDomainAxisEdge()); double x2 = axis.valueToJava2D(end, dataArea, plot.getDomainAxisEdge()); // TODO: need to change the next line to take account of plot orientation... Rectangle2D band = new Rectangle2D.Double( x1, dataArea.getMinY(), x2 - x1, dataArea.getMaxY() - dataArea.getMinY() ); Paint paint = plot.getDomainTickBandPaint(); if (paint != null) { g2.setPaint(paint); g2.fill(band); } }
/** * Fills a band between two values on the axis. This can be used to color * bands between the grid lines. * * @param g2 the graphics device. * @param plot the plot. * @param axis the domain axis. * @param dataArea the data area. * @param start the start value. * @param end the end value. */ public void fillDomainGridBand(Graphics2D g2, XYPlot plot, ValueAxis axis, Rectangle2D dataArea, double start, double end) { double x1 = axis.valueToJava2D(start, dataArea, plot.getDomainAxisEdge()); double x2 = axis.valueToJava2D(end, dataArea, plot.getDomainAxisEdge()); // TODO: need to change the next line to take account of plot // orientation... Rectangle2D band = new Rectangle2D.Double(x1, dataArea.getMinY(), x2 - x1, dataArea.getMaxY() - dataArea.getMinY()); Paint paint = plot.getDomainTickBandPaint(); if (paint != null) { g2.setPaint(paint); g2.fill(band); } }
public static void plot(double[] predicts, double[] actuals, String name) { double[] index = new double[predicts.length]; for (int i = 0; i < predicts.length; i++) index[i] = i; int min = minValue(predicts, actuals); int max = maxValue(predicts, actuals); final XYSeriesCollection dataSet = new XYSeriesCollection(); addSeries(dataSet, index, predicts, "Predicts"); addSeries(dataSet, index, actuals, "Actuals"); final JFreeChart chart = ChartFactory.createXYLineChart( "Prediction Result", // chart title "Index", // x axis label name, // y axis label dataSet, // data PlotOrientation.VERTICAL, true, // include legend true, // tooltips false // urls ); XYPlot xyPlot = chart.getXYPlot(); // X-axis final NumberAxis domainAxis = (NumberAxis) xyPlot.getDomainAxis(); domainAxis.setRange((int) index[0], (int) (index[index.length - 1] + 2)); domainAxis.setTickUnit(new NumberTickUnit(20)); domainAxis.setVerticalTickLabels(true); // Y-axis final NumberAxis rangeAxis = (NumberAxis) xyPlot.getRangeAxis(); rangeAxis.setRange(min, max); rangeAxis.setTickUnit(new NumberTickUnit(50)); final ChartPanel panel = new ChartPanel(chart); final JFrame f = new JFrame(); f.add(panel); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); }
public static void setTimeSeriesRender(Plot plot, boolean isShowData, boolean isShapesVisible) { XYPlot xyplot = (XYPlot) plot; xyplot.setNoDataMessage(NO_DATA_MSG); xyplot.setInsets(new RectangleInsets(10, 10, 5, 10)); XYLineAndShapeRenderer xyRenderer = (XYLineAndShapeRenderer) xyplot.getRenderer(); xyRenderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator()); xyRenderer.setBaseShapesVisible(false); if (isShowData) { xyRenderer.setBaseItemLabelsVisible(true); xyRenderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator()); xyRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.BOTTOM_CENTER)); } xyRenderer.setBaseShapesVisible(isShapesVisible); DateAxis domainAxis = (DateAxis) xyplot.getDomainAxis(); domainAxis.setAutoTickUnitSelection(false); DateTickUnit dateTickUnit = new DateTickUnit(DateTickUnitType.YEAR, 1, new SimpleDateFormat("yyyy-MM")); domainAxis.setTickUnit(dateTickUnit); StandardXYToolTipGenerator xyTooltipGenerator = new StandardXYToolTipGenerator("{1}:{2}", new SimpleDateFormat("yyyy-MM-dd"), new DecimalFormat("0")); xyRenderer.setBaseToolTipGenerator(xyTooltipGenerator); setXY_XAixs(xyplot); setXY_YAixs(xyplot); }
/** * Draws the chart with a <code>null</code> info object to make sure that * no exceptions are thrown (particularly by code in the renderer). */ public void testDrawWithNullInfo() { boolean success = false; try { DefaultTableXYDataset dataset = new DefaultTableXYDataset(); XYSeries s1 = new XYSeries("Series 1", true, false); s1.add(5.0, 5.0); s1.add(10.0, 15.5); s1.add(15.0, 9.5); s1.add(20.0, 7.5); dataset.addSeries(s1); XYSeries s2 = new XYSeries("Series 2", true, false); s2.add(5.0, 5.0); s2.add(10.0, 15.5); s2.add(15.0, 9.5); s2.add(20.0, 3.5); dataset.addSeries(s2); XYPlot plot = new XYPlot(dataset, new NumberAxis("X"), new NumberAxis("Y"), new StackedXYAreaRenderer()); JFreeChart chart = new JFreeChart(plot); /* BufferedImage image = */ chart.createBufferedImage(300, 200, null); success = true; } catch (NullPointerException e) { e.printStackTrace(); success = false; } assertTrue(success); }
@Override public void actionPerformed(ActionEvent ae) { // Visibility checkbox - show or hide a series if(ae.getSource() instanceof JCheckBox){ // Determine which series to hide JCheckBox source = (JCheckBox) ae.getSource(); String seriesLabel = source.getText(); // Find the series XYPlot plot = this.getChart().getXYPlot(); for(int i=0; i<plot.getSeriesCount(); i++){ // Toggle visibility if(plot.getDataset().getSeriesKey(i).equals(seriesLabel)){ XYItemRenderer renderer = plot.getRenderer(); boolean currentlyVisible = renderer.getItemVisible(i, 0); renderer.setSeriesVisible(i, new Boolean(!currentlyVisible)); } } // Update the tooltip text to reflect the new state source.setToolTipText(this.generateToolTipTextForVisibilityCheckBox(source)); } }
/** * Creates a sample plot. * * @return A sample plot. */ private CombinedRangeXYPlot createPlot() { // create subplot 1... XYDataset data1 = createDataset1(); XYItemRenderer renderer1 = new StandardXYItemRenderer(); NumberAxis rangeAxis1 = new NumberAxis("Range 1"); XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1); subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); XYTextAnnotation annotation = new XYTextAnnotation("Hello!", 50.0, 10000.0); annotation.setFont(new Font("SansSerif", Font.PLAIN, 9)); annotation.setRotationAngle(Math.PI / 4.0); subplot1.addAnnotation(annotation); // create subplot 2... XYDataset data2 = createDataset2(); XYItemRenderer renderer2 = new StandardXYItemRenderer(); NumberAxis rangeAxis2 = new NumberAxis("Range 2"); rangeAxis2.setAutoRangeIncludesZero(false); XYPlot subplot2 = new XYPlot(data2, null, rangeAxis2, renderer2); subplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT); // parent plot... CombinedRangeXYPlot plot = new CombinedRangeXYPlot(new NumberAxis("Range")); plot.setGap(10.0); // add the subplots... plot.add(subplot1, 1); plot.add(subplot2, 1); plot.setOrientation(PlotOrientation.VERTICAL); return plot; }
/** * Creates a new controller to handle plot shifts. * * @param chartPanel * The panel displaying the plot. */ public ChartPanelShiftController(ChartPanel chartPanel) { super(); this.chartPanel = chartPanel; // Check to see if plot is shiftable Plot plot = chartPanel.getChart().getPlot(); if ((plot instanceof XYPlot) || (plot instanceof FastScatterPlot)) { plotSupported = true; axesSwaped = isHorizontalPlot(plot); } }
@Override public Collection<String> resolveYAxis(int axisIndex) { Plot p = chart.getPlot(); Collection<String> names = new LinkedList<>(); if (p instanceof XYPlot) { XYPlot plot = (XYPlot) p; for (int i = 0; i < plot.getRangeAxisCount(); i++) { ValueAxis domain = plot.getRangeAxis(i); names.add(domain.getLabel()); } } return names; }
/** * Decreases the length of the domain axis, centered about the given coordinate on the screen. * The length of the domain axis is reduced by the value of {@link #getZoomInFactor()}. * * @param x * the x coordinate (in screen coordinates). * @param y * the y-coordinate (in screen coordinates). */ public void shrinkSelectionOnDomain(double x, double y, MouseEvent selectionEvent) { Plot p = this.chart.getPlot(); if (p instanceof XYPlot) { XYPlot plot = (XYPlot) p; Selection selectionObject = new Selection(); for (int i = 0; i < plot.getDomainAxisCount(); i++) { ValueAxis domain = plot.getDomainAxis(i); double zoomFactor = getZoomInFactor(); shrinkSelectionXAxis(x, y, selectionObject, domain, i, zoomFactor); } informSelectionListener(selectionObject, selectionEvent); } }
/** * Increases the length of the domain axis, centered about the given coordinate on the screen. * The length of the domain axis is increased by the value of {@link #getZoomOutFactor()}. * * @param x * the x coordinate (in screen coordinates). * @param y * the y-coordinate (in screen coordinates). */ public void enlargeSelectionOnDomain(double x, double y, MouseEvent selectionEvent) { Plot p = this.chart.getPlot(); if (p instanceof XYPlot) { XYPlot plot = (XYPlot) p; Selection selectionObject = new Selection(); for (int i = 0; i < plot.getDomainAxisCount(); i++) { ValueAxis domain = plot.getDomainAxis(i); double zoomFactor = getZoomOutFactor(); shrinkSelectionXAxis(x, y, selectionObject, domain, i, zoomFactor); } informSelectionListener(selectionObject, selectionEvent); } }
/** * Two objects that are equal are required to return the same hashCode. */ public void testHashcode() { XYPlot plot1 = new XYPlot(); LegendTitle t1 = new LegendTitle(plot1); LegendTitle t2 = new LegendTitle(plot1); assertTrue(t1.equals(t2)); int h1 = t1.hashCode(); int h2 = t2.hashCode(); assertEquals(h1, h2); }
private JFreeChart createNumericalChart() { JFreeChart chart; XYDataset dataset = createNumericalDataSet(); // create the chart... String domainName = dataTable == null ? MODEL_DOMAIN_AXIS_NAME : dataTable.getColumnName(plotColumn); chart = ChartFactory.createXYLineChart(null, // chart title domainName, // x axis label RANGE_AXIS_NAME, // y axis label dataset, // data PlotOrientation.VERTICAL, true, // include legend true, // tooltips false // urls ); DeviationRenderer renderer = new DeviationRenderer(true, false); Stroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); if (dataset.getSeriesCount() == 1) { renderer.setSeriesStroke(0, stroke); renderer.setSeriesPaint(0, Color.RED); renderer.setSeriesFillPaint(0, Color.RED); } else { for (int i = 0; i < dataset.getSeriesCount(); i++) { renderer.setSeriesStroke(i, stroke); Color color = getColorProvider().getPointColor((double) i / (double) (dataset.getSeriesCount() - 1)); renderer.setSeriesPaint(i, color); renderer.setSeriesFillPaint(i, color); } } renderer.setAlpha(0.12f); XYPlot plot = (XYPlot) chart.getPlot(); plot.setRenderer(renderer); return chart; }
private void pushDataAndRendererIntoPlot(XYPlot plot, int rangeAxisIdx, XYItemRenderer renderer, XYDataset dataset) throws ChartPlottimeException { if (dataset != null && renderer != null) { int datasetIdx = plot.getDatasetCount(); if (datasetIdx > 0 && plot.getDataset(datasetIdx - 1) == null) { datasetIdx -= 1; } // push dataset and renderer into plot try { plot.setDataset(datasetIdx, dataset); // if Eclipse states that // dataset might not be // initialized, you did // not consider all // possibilities in the // condition block above } catch (RuntimeException e) { // probably this is because the domain axis contains values less // then zero and the scaling is logarithmic. // The shitty JFreeChart implementation does not throw a proper // exception stating what happened, // but just a RuntimeException with a string, so this is our // best guess: if (isProbablyZeroValuesOnLogScaleException(e)) { throw new ChartPlottimeException("gui.plotter.error.log_axis_contains_zero", "domain axis"); } else { throw e; } } plot.mapDatasetToRangeAxis(datasetIdx, rangeAxisIdx); plot.setRenderer(datasetIdx, renderer); } else { ChartPlottimeException chartPlottimeException = new ChartPlottimeException(new PlotConfigurationError( "generic_plotter_error")); throw chartPlottimeException; } }
/** * Creates a sample plot. * * @return A sample plot. */ private CombinedDomainXYPlot createPlot() { // create subplot 1... XYDataset data1 = createDataset1(); XYItemRenderer renderer1 = new StandardXYItemRenderer(); NumberAxis rangeAxis1 = new NumberAxis("Range 1"); XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1); subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); XYTextAnnotation annotation = new XYTextAnnotation("Hello!", 50.0, 10000.0); annotation.setFont(new Font("SansSerif", Font.PLAIN, 9)); annotation.setRotationAngle(Math.PI / 4.0); subplot1.addAnnotation(annotation); // create subplot 2... XYDataset data2 = createDataset2(); XYItemRenderer renderer2 = new StandardXYItemRenderer(); NumberAxis rangeAxis2 = new NumberAxis("Range 2"); rangeAxis2.setAutoRangeIncludesZero(false); XYPlot subplot2 = new XYPlot(data2, null, rangeAxis2, renderer2); subplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT); // parent plot... CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis("Domain")); plot.setGap(10.0); // add the subplots... plot.add(subplot1, 1); plot.add(subplot2, 1); plot.setOrientation(PlotOrientation.VERTICAL); return plot; }
/** * Creates a line chart (based on an {@link XYDataset}) with default * settings. * * @param title the chart title (<code>null</code> permitted). * @param xAxisLabel a label for the X-axis (<code>null</code> permitted). * @param yAxisLabel a label for the Y-axis (<code>null</code> permitted). * @param dataset the dataset for the chart (<code>null</code> permitted). * @param orientation the plot orientation (horizontal or vertical) * (<code>null</code> NOT permitted). * @param legend a flag specifying whether or not a legend is required. * @param tooltips configure chart to generate tool tips? * @param urls configure chart to generate URLs? * * @return The chart. */ public static JFreeChart createXYLineChart(String title, String xAxisLabel, String yAxisLabel, XYDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) { if (orientation == null) { throw new IllegalArgumentException("Null 'orientation' argument."); } NumberAxis xAxis = new NumberAxis(xAxisLabel); xAxis.setAutoRangeIncludesZero(false); NumberAxis yAxis = new NumberAxis(yAxisLabel); XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false); XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer); plot.setOrientation(orientation); if (tooltips) { renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator()); } if (urls) { renderer.setURLGenerator(new StandardXYURLGenerator()); } JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); return chart; }
private static JFreeChart createChart(XYDataset dataset, String chartName) { // create the chart... if(chartName == null) chartName = FunctionToPlot; JFreeChart chart = ChartFactory.createXYLineChart( chartName, Var, Value, dataset, PlotOrientation.VERTICAL, true, true, false ); chart.setBackgroundPaint(Color.white); XYPlot plot = (XYPlot) chart.getPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer(); //renderer.setShapesVisible(true); //renderer.setShapesFilled(true); // change the auto tick unit selection to integer units only... NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setLabelAngle(Math.PI/2.0); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); // OPTIONAL CUSTOMISATION COMPLETED. return chart; }
/** * Creates a chart. * * @param dataset a dataset. * * @return A chart. */ private static JFreeChart createChart(XYDataset dataset) { JFreeChart chart = ChartFactory.createTimeSeriesChart( "Legal & General Unit Trust Prices", // title "Date", // x-axis label "Price Per Unit", // y-axis label dataset, // data true, // create legend? true, // generate tooltips? false // generate URLs? ); chart.setBackgroundPaint(Color.white); XYPlot plot = (XYPlot) chart.getPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); plot.setDomainCrosshairVisible(true); plot.setRangeCrosshairVisible(true); XYItemRenderer r = plot.getRenderer(); if (r instanceof XYLineAndShapeRenderer) { XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r; renderer.setBaseShapesVisible(true); renderer.setBaseShapesFilled(true); } DateAxis axis = (DateAxis) plot.getDomainAxis(); axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy")); return chart; }
/** * Draws the block representing the specified item. * * @param g2 the graphics device. * @param state the state. * @param dataArea the data area. * @param info the plot rendering info. * @param plot the plot. * @param domainAxis the x-axis. * @param rangeAxis the y-axis. * @param dataset the dataset. * @param series the series index. * @param item the item index. * @param crosshairState the crosshair state. * @param pass the pass index. */ public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) { double x = dataset.getXValue(series, item); double y = dataset.getYValue(series, item); double z = 0.0; if (dataset instanceof XYZDataset) { z = ((XYZDataset) dataset).getZValue(series, item); } Paint p = this.paintScale.getPaint(z); double xx0 = domainAxis.valueToJava2D(x + this.xOffset, dataArea, plot.getDomainAxisEdge()); double yy0 = rangeAxis.valueToJava2D(y + this.yOffset, dataArea, plot.getRangeAxisEdge()); double xx1 = domainAxis.valueToJava2D(x + this.blockWidth + this.xOffset, dataArea, plot.getDomainAxisEdge()); double yy1 = rangeAxis.valueToJava2D(y + this.blockHeight + this.yOffset, dataArea, plot.getRangeAxisEdge()); Rectangle2D block; PlotOrientation orientation = plot.getOrientation(); if (orientation.equals(PlotOrientation.HORIZONTAL)) { block = new Rectangle2D.Double(Math.min(yy0, yy1), Math.min(xx0, xx1), Math.abs(yy1 - yy0), Math.abs(xx0 - xx1)); } else { block = new Rectangle2D.Double(Math.min(xx0, xx1), Math.min(yy0, yy1), Math.abs(xx1 - xx0), Math.abs(yy1 - yy0)); } g2.setPaint(p); g2.fill(block); g2.setStroke(new BasicStroke(1.0f)); g2.draw(block); }
private static JFreeChart createXYLineChart(String title,String category,String value, XYSeriesCollection dataset) { JFreeChart mChart = ChartFactory.createXYLineChart( title, category, value, dataset, PlotOrientation.VERTICAL, true, true, false); // ChartPanel chartPanel = new ChartPanel( mChart); // chartPanel.setPreferredSize( new java.awt.Dimension( 560 , 367 ) ); final XYPlot plot = mChart.getXYPlot();// 获取折线图plot对象 plot.setBackgroundPaint(new Color(240,240,240)); NumberAxis na= (NumberAxis)plot.getRangeAxis(); NumberAxis domainAxis = (NumberAxis)plot.getDomainAxis(); // na.setAutoTickUnitSelection(false); na.setNumberFormatOverride(df);//设置轴坐标 domainAxis.setNumberFormatOverride(df); XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer( );// 设置样式 renderer.setBaseShapesVisible(false); renderer.setSeriesPaint(0,Color.RED); renderer.setSeriesPaint(1,Color.GREEN); renderer.setSeriesPaint(2,new Color(255,150,24)); renderer.setSeriesPaint(3,new Color(82,101,115)); plot.setRenderer( renderer ); return mChart; }