private void incluirLegenda(final XYPlot plot) { if (isLegenda()) { LegendTitle lt = new LegendTitle(plot); lt.setItemFont(new Font("Dialog", Font.PLAIN, 11)); lt.setBackgroundPaint(new Color(255, 255, 255, 100)); lt.setBorder(new BlockBorder(new Color(180, 180, 180))); lt.setPosition(RectangleEdge.TOP); XYTitleAnnotation ta = new XYTitleAnnotation(0.01, 0.98, lt, RectangleAnchor.TOP_LEFT); ta.setMaxWidth(0.48); plot.addAnnotation(ta); } }
/** * Two objects that are equal are required to return the same hashCode. */ public void testHashCode() { TextTitle t = new TextTitle("Title"); XYTitleAnnotation a1 = new XYTitleAnnotation(1.0, 2.0, t); XYTitleAnnotation a2 = new XYTitleAnnotation(1.0, 2.0, t); assertTrue(a1.equals(a2)); int h1 = a1.hashCode(); int h2 = a2.hashCode(); assertEquals(h1, h2); }
/** * Draws the chart with a <code>null</code> info object to make sure that * no exceptions are thrown. */ 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 XYLineAndShapeRenderer()); plot.addAnnotation(new XYTitleAnnotation(5.0, 6.0, new TextTitle("Hello World!"))); JFreeChart chart = new JFreeChart(plot); /* BufferedImage image = */ chart.createBufferedImage(300, 200, null); success = true; } catch (NullPointerException e) { e.printStackTrace(); success = false; } assertTrue(success); }
private void setPlotMessage(String messageText) { chart.getXYPlot().clearAnnotations(); TextTitle tt = new TextTitle(messageText); tt.setTextAlignment(HorizontalAlignment.RIGHT); tt.setFont(chart.getLegend().getItemFont()); tt.setBackgroundPaint(new Color(200, 200, 255, 50)); tt.setFrame(new BlockBorder(Color.white)); tt.setPosition(RectangleEdge.BOTTOM); XYTitleAnnotation message = new XYTitleAnnotation(0.5, 0.5, tt, RectangleAnchor.CENTER); chart.getXYPlot().addAnnotation(message); }
public GraphGenerator(final String title, AthenaFeatures athenaFeatures, String feature) { super(title); this.feature = feature; final XYDataset dataset = createDatasetFromFeatureData(athenaFeatures, feature); final JFreeChart chart = createChart(dataset); chart.setTitle(""); LegendTitle legend = (LegendTitle) chart.getLegend(); chart.removeLegend(); Font nwfont = new Font("Arial",1,12); legend.setItemFont(nwfont); legend.setPosition(RectangleEdge.TOP); // legend.setWidth(200); legend.setItemLabelPadding(new RectangleInsets(3, 3, 3, 3)); legend.setHeight(10); // legend.setPadding(new RectangleInsets(10, 10, 10, 10)); XYTitleAnnotation ta = new XYTitleAnnotation(0.99, 0.98, legend, RectangleAnchor.TOP_RIGHT); ta.setMaxWidth(0.95); // chart.addLegend(legend); XYPlot plot = (XYPlot) chart.getPlot(); plot.setBackgroundPaint(Color.white); plot.setDomainZeroBaselinePaint(Color.gray); plot.setDomainGridlinePaint(Color.gray); plot.setDomainGridlineStroke(new BasicStroke(0.7f)); plot.setRangeGridlinePaint(Color.gray); plot.setRangeGridlineStroke(new BasicStroke(0.7f)); plot.setDomainMinorGridlinePaint(Color.black); plot.addAnnotation(ta); final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); renderer.setSeriesPaint(0, Color.black); renderer.setSeriesShape(0, ShapeUtilities.createDiamond(5)); renderer.setSeriesPaint(1, Color.red); renderer.setSeriesShape(1, ShapeUtilities.createUpTriangle(5)); renderer.setSeriesPaint(2, Color.blue); Shape shape = new Ellipse2D.Double(-5.0,-5.0,10,10); renderer.setSeriesShape(2, shape); renderer.setShapesFilled(false); // renderer.setSeriesShapesVisible(1, false); //apply theme // StandardChartTheme.createJFreeTheme().apply(chart); plot.setRenderer(renderer); NumberAxis yAxis = (NumberAxis) plot.getRangeAxis(); yAxis.setLabel(feature + " (K)"); yAxis.setAxisLineVisible(false); yAxis.setTickUnit(new NumberTickUnit(50000)); yAxis.setNumberFormatOverride(new ByteFormat()); yAxis.setRange(new Range(0, 160000)); plot.getRenderer().setBaseItemLabelsVisible(true); DateAxis xAxis = (DateAxis) plot.getDomainAxis(); xAxis.setAxisLineVisible(false); xAxis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss")); xAxis.setTickUnit(new DateTickUnit(DateTickUnit.MINUTE, 3)); xAxis.setLabelFont(new Font("Arial",1,12)); final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(631, 381)); chartPanel.setMouseZoomable(true, true); setContentPane(chartPanel); try { ChartUtilities.saveChartAsPNG(new File("result.png"), chart, 631, 381); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
ScatterPlotPanel(TopComponent parentDialog, String helpId) { super(parentDialog, helpId, CHART_TITLE, false); userSettingsMap = new HashMap<>(); productRemovedListener = new ProductManager.Listener() { @Override public void productAdded(ProductManager.Event event) { } @Override public void productRemoved(ProductManager.Event event) { final UserSettings userSettings = userSettingsMap.remove(event.getProduct()); if (userSettings != null) { userSettings.dispose(); } } }; xAxisRangeControl = new AxisRangeControl("X-Axis"); yAxisRangeControl = new AxisRangeControl("Y-Axis"); scatterPlotModel = new ScatterPlotModel(); bindingContext = new BindingContext(PropertyContainer.createObjectBacked(scatterPlotModel)); scatterpointsDataset = new XYIntervalSeriesCollection(); acceptableDeviationDataset = new XYIntervalSeriesCollection(); regressionDataset = new XYIntervalSeriesCollection(); r2Annotation = new XYTitleAnnotation(0, 0, new TextTitle("")); chart = ChartFactory.createScatterPlot(CHART_TITLE, "", "", scatterpointsDataset, PlotOrientation.VERTICAL, true, true, false); chart.getXYPlot().setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); createDomainAxisChangeListener(); final PropertyChangeListener userSettingsUpdateListener = evt -> { if (getRaster() != null) { final VectorDataNode pointDataSourceValue = scatterPlotModel.pointDataSource; final AttributeDescriptor dataFieldValue = scatterPlotModel.dataField; final UserSettings userSettings = getUserSettings(getRaster().getProduct()); userSettings.set(getRaster().getName(), pointDataSourceValue, dataFieldValue); } }; bindingContext.addPropertyChangeListener(PROPERTY_NAME_DATA_FIELD, userSettingsUpdateListener); bindingContext.addPropertyChangeListener(PROPERTY_NAME_POINT_DATA_SOURCE, userSettingsUpdateListener); }
private void computeCoefficientOfDetermination() { int numberOfItems = scatterpointsDataset.getSeries(0).getItemCount(); double arithmeticMeanOfX = 0; //arithmetic mean of X double arithmeticMeanOfY = 0; //arithmetic mean of Y double varX = 0; //variance of X double varY = 0; //variance of Y double coVarXY = 0; //covariance of X and Y; //compute arithmetic means for (int i = 0; i < numberOfItems; i++) { arithmeticMeanOfX += scatterpointsDataset.getXValue(0, i); arithmeticMeanOfY += scatterpointsDataset.getYValue(0, i); } arithmeticMeanOfX /= numberOfItems; arithmeticMeanOfY /= numberOfItems; //compute variances and covariance for (int i = 0; i < numberOfItems; i++) { varX += Math.pow(scatterpointsDataset.getXValue(0, i) - arithmeticMeanOfX, 2); varY += Math.pow(scatterpointsDataset.getYValue(0, i) - arithmeticMeanOfY, 2); coVarXY += (scatterpointsDataset.getXValue(0, i) - arithmeticMeanOfX) * (scatterpointsDataset.getYValue(0, i) - arithmeticMeanOfY); } //computation of coefficient of determination double r2 = Math.pow(coVarXY, 2) / (varX * varY); r2 = MathUtils.round(r2, Math.pow(10.0, 5)); final double[] coefficients = Regression.getOLSRegression(scatterpointsDataset, 0); final double intercept = coefficients[0]; final double slope = coefficients[1]; final String linearEquation; if (intercept >= 0) { linearEquation = "y = " + (float) slope + "x + " + (float) intercept; } else { linearEquation = "y = " + (float) slope + "x - " + Math.abs((float) intercept); } TextTitle tt = new TextTitle(linearEquation + "\nR² = " + r2); tt.setTextAlignment(HorizontalAlignment.RIGHT); tt.setFont(chart.getLegend().getItemFont()); tt.setBackgroundPaint(new Color(200, 200, 255, 100)); tt.setFrame(new BlockBorder(Color.white)); tt.setPosition(RectangleEdge.BOTTOM); r2Annotation = new XYTitleAnnotation(0.98, 0.02, tt, RectangleAnchor.BOTTOM_RIGHT); r2Annotation.setMaxWidth(0.48); getPlot().addAnnotation(r2Annotation); }