List of usage examples for org.jfree.data DefaultKeyedValues DefaultKeyedValues
public DefaultKeyedValues()
From source file:org.jfree.data.DefaultKeyedValuesTest.java
/** * Some checks for the getKey() methods. *//*from w w w. j a va2 s .c o m*/ @Test public void testGetKey() { DefaultKeyedValues v1 = new DefaultKeyedValues(); try { /* Comparable k = */ v1.getKey(-1); assertTrue(false); } catch (IndexOutOfBoundsException e) { // expected } try { /* Comparable k = */ v1.getKey(0); assertTrue(false); } catch (IndexOutOfBoundsException e) { // expected } DefaultKeyedValues v2 = new DefaultKeyedValues(); v2.addValue("K1", new Integer(1)); v2.addValue("K2", new Integer(2)); v2.addValue("K3", new Integer(3)); assertEquals("K2", v2.getKey(1)); }
From source file:weka.gui.beans.JFreeChartOffscreenChartRenderer.java
/** * Render histogram(s) (numeric attribute) or bar chart (nominal attribute). * Some implementations may not be able to render more than one histogram/bar * on the same chart - the implementation can either throw an exception or * just process the first series in this case. * // ww w . j a v a2 s .c o m * @param width the width of the resulting chart in pixels * @param height the height of the resulting chart in pixels * @param series a list of Instances - one for each series to be plotted * @param attsToPlot the attribute to plot corresponding to the Instances in * the series list * @param optionalArgs optional arguments to the renderer (may be null) * * @return a BufferedImage containing the chart * @throws Exception if there is a problem rendering the chart */ public BufferedImage renderHistogram(int width, int height, List<Instances> series, String attToPlot, List<String> additionalArgs) throws Exception { String plotTitle = "Bar Chart"; String userTitle = getOption(additionalArgs, "-title"); plotTitle = (userTitle != null) ? userTitle : plotTitle; String colorAtt = getOption(additionalArgs, "-color"); String pareto = getOption(additionalArgs, "-pareto"); boolean doPareto = false; if (pareto != null && pareto.length() == 0 && series.size() == 1) { doPareto = true; } if (series.size() == 1 && colorAtt != null && colorAtt.length() > 0) { int colIndex = getIndexOfAttribute(series.get(0), colorAtt); if (colIndex >= 0 && series.get(0).attribute(colIndex).isNominal() && !doPareto) { // split single series out into multiple instances objects - one // per class series = splitToClasses(series.get(0), colIndex); for (Instances insts : series) { insts.setClassIndex(colIndex); } } } Instances masterInstances = series.get(0); int attIndex = getIndexOfAttribute(masterInstances, attToPlot); if (attIndex < 0) { attIndex = 0; } if (!(series.get(0).attribute(attIndex).isNominal() || series.get(0).attribute(attIndex).isRelationValued())) { doPareto = false; } // Do a pareto chart if (doPareto) { final DefaultKeyedValues data = new DefaultKeyedValues(); AttributeStats attStats = masterInstances.attributeStats(attIndex); double[] attValFreqs = attStats.nominalWeights; for (int i = 0; i < attValFreqs.length; i++) { Number freq = new Double(attValFreqs[i]); data.addValue(masterInstances.attribute(attIndex).value(i), freq); } data.sortByValues(SortOrder.DESCENDING); final KeyedValues cumulative = DataUtilities.getCumulativePercentages(data); final CategoryDataset dataset = DatasetUtilities .createCategoryDataset(masterInstances.attribute(attIndex).name(), data); final JFreeChart chart = ChartFactory.createBarChart(plotTitle, masterInstances.attribute(attIndex).name(), "Fequency/weight mass", dataset, PlotOrientation.VERTICAL, true, false, false); final CategoryPlot plot = chart.getCategoryPlot(); final CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setLowerMargin(0.02); domainAxis.setUpperMargin(0.02); LineAndShapeRenderer renderer2 = new LineAndShapeRenderer(); CategoryDataset dataset2 = DatasetUtilities.createCategoryDataset("Cumulative", cumulative); final NumberAxis axis2 = new NumberAxis("Percent"); axis2.setNumberFormatOverride(NumberFormat.getPercentInstance()); // plot. plot.setRangeAxis(1, axis2); plot.setDataset(1, dataset2); plot.setRenderer(1, renderer2); plot.mapDatasetToRangeAxis(1, 1); plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); chart.setBackgroundPaint(java.awt.Color.white); BufferedImage image = chart.createBufferedImage(width, height); return image; } boolean seriesAreClasses = false; int classIndex = masterInstances.classIndex(); if (classIndex >= 0 && !masterInstances.attribute(classIndex).isNumeric() && !masterInstances.attribute(classIndex).isRelationValued() && masterInstances.attributeStats(classIndex).distinctCount == 1) { // series correspond to class labels (assume that subsequent series only // contain instances of one class)... seriesAreClasses = true; } // bar chart for a nominal attribute if (masterInstances.attribute(attIndex).isNominal() || masterInstances.attribute(attIndex).isString()) { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); // do the master series String masterSeriesTitle = masterInstances.relationName(); if (seriesAreClasses) { for (int i = 0; i < masterInstances.numInstances(); i++) { Instance current = masterInstances.instance(i); if (!current.isMissing(classIndex)) { masterSeriesTitle = current.stringValue(classIndex); break; } } } AttributeStats attStats = masterInstances.attributeStats(attIndex); double[] attValFreqs = attStats.nominalWeights; for (int i = 0; i < attValFreqs.length; i++) { Number freq = new Double(attValFreqs[i]); dataset.addValue(freq, masterSeriesTitle, masterInstances.attribute(attIndex).value(i)); } // any subsequent series for (int i = 1; i < series.size(); i++) { Instances nextSeries = series.get(i); String seriesTitle = nextSeries.relationName(); if (seriesAreClasses) { for (int j = 0; j < nextSeries.numInstances(); j++) { Instance current = nextSeries.instance(j); if (!current.isMissing(classIndex)) { seriesTitle = current.stringValue(classIndex); break; } } attStats = nextSeries.attributeStats(attIndex); attValFreqs = attStats.nominalWeights; for (int j = 0; j < attValFreqs.length; j++) { Number freq = new Double(attValFreqs[j]); dataset.addValue(freq, seriesTitle, nextSeries.attribute(attIndex).value(j)); } } } JFreeChart chart = null; if (series.size() == 1) { chart = ChartFactory.createBarChart(plotTitle, masterInstances.attribute(attIndex).name(), "Fequency/weight mass", dataset, PlotOrientation.VERTICAL, true, false, false); } else { chart = ChartFactory.createStackedBarChart(plotTitle, masterInstances.attribute(attIndex).name(), "Fequency/weight mass", dataset, PlotOrientation.VERTICAL, true, false, false); } chart.setBackgroundPaint(java.awt.Color.white); BufferedImage image = chart.createBufferedImage(width, height); return image; } else { // histogram for numeric attributes HistogramDataset dataset = new HistogramDataset(); // combine all series in order to get overall std dev, and range Instances temp = new Instances(masterInstances); for (int i = 1; i < series.size(); i++) { Instances additional = series.get(i); for (Instance tempI : additional) { temp.add(tempI); } } AttributeStats stats = temp.attributeStats(attIndex); Stats numericStats = stats.numericStats; double intervalWidth = 3.49 * numericStats.stdDev * StrictMath.pow(temp.numInstances(), (-1.0 / 3.0)); double range = numericStats.max - numericStats.min; int numBins = StrictMath.max(1, (int) StrictMath.round(range / intervalWidth)); // do the master series String masterSeriesTitle = masterInstances.relationName(); if (seriesAreClasses) { for (int i = 0; i < masterInstances.numInstances(); i++) { Instance current = masterInstances.instance(i); if (!current.isMissing(current.classAttribute())) { masterSeriesTitle = current.stringValue(current.classAttribute()); break; } } } // have to set min, max and num bins (using heuristic from AttSummPanel). // Make sure // to set series length to num instances - num missing values for att stats = masterInstances.attributeStats(attIndex); /* * numericStats = stats.numericStats; //numericStats.calculateDerived(); * intervalWidth = StrictMath.max(1, 3.49 * numericStats.stdDev * * StrictMath.pow(masterInstances.numInstances(), (-1.0/3.0))); */ double[] seriesVals = new double[masterInstances.numInstances() - stats.missingCount]; int count = 0; for (int i = 0; i < masterInstances.numInstances(); i++) { Instance current = masterInstances.instance(i); if (!current.isMissing(attIndex)) { seriesVals[count++] = current.value(attIndex); } } dataset.addSeries(masterSeriesTitle, seriesVals, numBins, numericStats.min, numericStats.max); // any subsequent series for (int i = 1; i < series.size(); i++) { Instances nextSeries = series.get(i); String seriesTitle = nextSeries.relationName(); if (seriesAreClasses) { for (int j = 0; j < nextSeries.numInstances(); j++) { Instance current = nextSeries.instance(j); if (!current.isMissing(nextSeries.classAttribute())) { seriesTitle = current.stringValue(nextSeries.classAttribute()); break; } } } stats = nextSeries.attributeStats(attIndex); /* * numericStats = stats.numericStats; // * numericStats.calculateDerived(); intervalWidth = StrictMath.max(1, * 3.49 * numericStats.stdDev * * StrictMath.pow(masterInstances.numInstances(), (-1.0/3.0))); range = * numericStats.max - numericStats.min; numBins = StrictMath.max(1, * (int) StrictMath.round(range / intervalWidth)); */ seriesVals = new double[nextSeries.numInstances() - stats.missingCount]; count = 0; for (int j = 0; j < nextSeries.numInstances(); j++) { Instance current = nextSeries.instance(j); if (!current.isMissing(attIndex)) { seriesVals[count++] = current.value(attIndex); } } dataset.addSeries(seriesTitle, seriesVals, numBins, numericStats.min, numericStats.max); } JFreeChart chart = ChartFactory.createHistogram(plotTitle, masterInstances.attribute(attIndex).name(), null, dataset, PlotOrientation.VERTICAL, true, false, false); // chart.setBackgroundPaint(java.awt.Color.white); XYPlot xyplot = (XYPlot) chart.getPlot(); xyplot.setForegroundAlpha(0.50F); XYBarRenderer xybarrenderer = (XYBarRenderer) xyplot.getRenderer(); xybarrenderer.setDrawBarOutline(false); xybarrenderer.setShadowVisible(false); BufferedImage image = chart.createBufferedImage(width, height); return image; } }
From source file:org.jfree.data.junit.DefaultKeyedValuesTest.java
/** * Some checks for the getKey() methods. *///w w w . j a v a 2 s.c o m public void testGetKey() { DefaultKeyedValues v1 = new DefaultKeyedValues(); try { /* Comparable k = */ v1.getKey(-1); assertTrue(false); } catch (IndexOutOfBoundsException e) { // expected } try { /* Comparable k = */ v1.getKey(0); assertTrue(false); } catch (IndexOutOfBoundsException e) { // expected } DefaultKeyedValues v2 = new DefaultKeyedValues(); v2.addValue("K1", new Integer(1)); v2.addValue("K2", new Integer(2)); v2.addValue("K3", new Integer(3)); assertEquals("K2", v2.getKey(1)); }
From source file:org.jfree.data.DefaultKeyedValuesTest.java
/** * Some checks for the getIndex() methods. */// w w w.j a v a 2 s.com @Test public void testGetIndex() { DefaultKeyedValues v1 = new DefaultKeyedValues(); assertEquals(-1, v1.getIndex("K1")); DefaultKeyedValues v2 = new DefaultKeyedValues(); v2.addValue("K1", new Integer(1)); v2.addValue("K2", new Integer(2)); v2.addValue("K3", new Integer(3)); assertEquals(2, v2.getIndex("K3")); // try null boolean pass = false; try { v2.getIndex(null); } catch (IllegalArgumentException e) { pass = true; } assertTrue(pass); }
From source file:ca.sqlpower.architect.swingui.ProfileGraphPanel.java
public ProfileGraphPanel(ProfilePanel panel, int rowCount) { this.profilePanel = panel; this.rowCount = rowCount; FormLayout displayLayout = new FormLayout("4dlu, default, 4dlu, 100dlu, 4dlu, fill:default:grow, 4dlu", // columns "4dlu, default, 6dlu"); // rows CellConstraints cc = new CellConstraints(); validResultsPanel = ProfileGraphPanel.logger.isDebugEnabled() ? new FormDebugPanel(displayLayout) : new JPanel(displayLayout); validResultsPanel.setBorder(BorderFactory.createEtchedBorder()); Font bodyFont = validResultsPanel.getFont(); Font titleFont = bodyFont.deriveFont(Font.BOLD, bodyFont.getSize() * 1.25F); title = new JLabel("Column Name"); title.setFont(titleFont);//from w ww. j a va 2 s .co m PanelBuilder pb = new PanelBuilder(displayLayout, validResultsPanel); pb.add(title, cc.xyw(2, 2, 5)); int row = 4; rowCountDisplay = makeInfoRow(pb, "RowCount", row); row += 2; nullableLabel = makeInfoRow(pb, "Nullable", row); row += 2; nullCountLabel = makeInfoRow(pb, "Null Count", row); row += 2; nullPercentLabel = makeInfoRow(pb, "% Null Records", row); row += 2; minLengthLabel = makeInfoRow(pb, "Minimum Length", row); row += 2; maxLengthLabel = makeInfoRow(pb, "Maximum Length", row); row += 2; uniqueCountLabel = makeInfoRow(pb, "Unique Values", row); row += 2; uniquePercentLabel = makeInfoRow(pb, "% Unique", row); row += 2; minValue = makeInfoRow(pb, "Minimum Value", row); row += 2; maxValue = makeInfoRow(pb, "Maximum Value", row); row += 2; avgValue = makeInfoRow(pb, "Average Value", row); row += 2; freqValueTable = new FreqValueTable(null); freqValueSp = new JScrollPane(freqValueTable); pb.appendRow("fill:10dlu:grow"); pb.appendRow("fill:default:grow"); pb.add(freqValueSp, cc.xyw(2, row + 1, 3)); // Now add something to represent the chart JFreeChart createPieChart = ChartFactory.createPieChart("", new DefaultPieDataset(new DefaultKeyedValues()), false, false, false); chartPanel = new ChartPanel(createPieChart); chartPanel.setPreferredSize(new Dimension(300, 300)); if (panel.getProfileManager().getWorkspaceContainer() instanceof ArchitectSession && ((ArchitectSession) panel.getProfileManager().getWorkspaceContainer()).isEnterpriseSession()) { pb.add(new JLabel("Column Profile Notes"), cc.xy(6, 2)); notesField = new JTextArea(); notesField.setLineWrap(true); notesField.setWrapStyleWord(true); JScrollPane notesScroll = new JScrollPane(notesField); notesScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); notesScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); pb.add(notesScroll, cc.xywh(6, 4, 1, row - 4)); pb.appendRow("fill:4dlu:grow"); pb.appendRow("4dlu"); pb.add(chartPanel, cc.xy(6, row + 1)); } else { pb.appendRow("fill:4dlu:grow"); pb.appendRow("4dlu"); pb.add(chartPanel, cc.xywh(6, 4, 1, row - 2)); } invalidResultsPanel = new JPanel(new BorderLayout()); invalidResultsLabel = new JLabel("No error message yet"); invalidResultsPanel.add(invalidResultsLabel); displayArea = new JPanel(new GridLayout(1, 1)); displayArea.setPreferredSize(validResultsPanel.getPreferredSize()); }
From source file:org.jfree.data.DefaultKeyedValuesTest.java
/** * Another check for the getIndex(Comparable) method. *//* www . j a va 2s.c o m*/ @Test public void testGetIndex2() { DefaultKeyedValues v = new DefaultKeyedValues(); assertEquals(-1, v.getIndex("K1")); v.addValue("K1", 1.0); assertEquals(0, v.getIndex("K1")); v.removeValue("K1"); assertEquals(-1, v.getIndex("K1")); }
From source file:org.jfree.data.junit.DefaultKeyedValuesTest.java
/** * Some checks for the getIndex() methods. *///w w w . j a v a 2s . co m public void testGetIndex() { DefaultKeyedValues v1 = new DefaultKeyedValues(); assertEquals(-1, v1.getIndex("K1")); DefaultKeyedValues v2 = new DefaultKeyedValues(); v2.addValue("K1", new Integer(1)); v2.addValue("K2", new Integer(2)); v2.addValue("K3", new Integer(3)); assertEquals(2, v2.getIndex("K3")); // try null boolean pass = false; try { v2.getIndex(null); } catch (IllegalArgumentException e) { pass = true; } assertTrue(pass); }
From source file:org.jfree.data.DefaultKeyedValuesTest.java
/** * Some checks for the addValue() method. *//*from w w w.j a va2 s .c o m*/ @Test public void testAddValue() { DefaultKeyedValues v1 = new DefaultKeyedValues(); v1.addValue("A", 1.0); assertEquals(new Double(1.0), v1.getValue("A")); v1.addValue("B", 2.0); assertEquals(new Double(2.0), v1.getValue("B")); v1.addValue("B", 3.0); assertEquals(new Double(3.0), v1.getValue("B")); assertEquals(2, v1.getItemCount()); v1.addValue("A", null); assertNull(v1.getValue("A")); assertEquals(2, v1.getItemCount()); boolean pass = false; try { v1.addValue(null, 99.9); } catch (IllegalArgumentException e) { pass = true; } assertTrue(pass); }
From source file:org.jfree.data.junit.DefaultKeyedValuesTest.java
/** * Another check for the getIndex(Comparable) method. *///from w w w.j ava 2 s. com public void testGetIndex2() { DefaultKeyedValues v = new DefaultKeyedValues(); assertEquals(-1, v.getIndex("K1")); v.addValue("K1", 1.0); assertEquals(0, v.getIndex("K1")); v.removeValue("K1"); assertEquals(-1, v.getIndex("K1")); }
From source file:org.jfree.data.junit.DefaultKeyedValuesTest.java
/** * Some checks for the addValue() method. *//*from w ww.j a v a 2s. c o m*/ public void testAddValue() { DefaultKeyedValues v1 = new DefaultKeyedValues(); v1.addValue("A", 1.0); assertEquals(new Double(1.0), v1.getValue("A")); v1.addValue("B", 2.0); assertEquals(new Double(2.0), v1.getValue("B")); v1.addValue("B", 3.0); assertEquals(new Double(3.0), v1.getValue("B")); assertEquals(2, v1.getItemCount()); v1.addValue("A", null); assertNull(v1.getValue("A")); assertEquals(2, v1.getItemCount()); boolean pass = false; try { v1.addValue(null, 99.9); } catch (IllegalArgumentException e) { pass = true; } assertTrue(pass); }