List of usage examples for org.jfree.data.xy XYSeriesCollection getItemCount
@Override public int getItemCount(int series)
From source file:statistic.graph.gui.Charts.java
private static void initXAxis(XYPlot plot, XYDataset dataset) { plot.setDomainAxis(new NumberAxis(plot.getDomainAxis().getLabel())); XYSeriesCollection collection = (XYSeriesCollection) dataset; double max = Double.NEGATIVE_INFINITY; double min = Double.POSITIVE_INFINITY; if (collection != null) { for (int s = 0; s < collection.getSeriesCount(); s++) { for (int d = 0; d < collection.getItemCount(s); d++) { XYDataItem data = collection.getSeries(s).getDataItem(d); if (data.getX().longValue() == Integer.MAX_VALUE || data.getX().longValue() == Integer.MIN_VALUE) { continue; }/*from w w w . ja v a2s .co m*/ if (data.getX().doubleValue() > max) { max = data.getX().doubleValue(); } if (data.getX().doubleValue() < min) { min = data.getX().doubleValue(); } } } if (min < max) { plot.getDomainAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits()); plot.getDomainAxis().setRange(min - 0.5, max + 0.5); for (int s = 0; s < collection.getSeriesCount(); s++) { XYSeries series = collection.getSeries(s); if (series.indexOf(Integer.MIN_VALUE) >= 0) { XYDataItem item = series.remove((Number) Integer.MIN_VALUE); if (series.indexOf(min) < 0) { series.add(min, item.getY()); } } if (series.indexOf(Integer.MAX_VALUE) >= 0) { XYDataItem item = series.remove((Number) Integer.MAX_VALUE); if (series.indexOf(max) < 0) { series.add(max, item.getY()); } } } } else { plot.getDomainAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits()); plot.getDomainAxis().setRange(0 - 0.5, 1 + 0.5); } } else { plot.getDomainAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits()); plot.getDomainAxis().setRange(0 - 0.5, 1 + 0.5); } }
From source file:umontreal.iro.lecuyer.charts.EmpiricalChart.java
/** * Synchronizes <SPAN CLASS="MATH"><I>x</I></SPAN>-axis ticks to the <SPAN CLASS="MATH"><I>s</I></SPAN>-th series <SPAN CLASS="MATH"><I>x</I></SPAN>-values. * /*w ww . java 2 s.c o m*/ * @param s series used to define ticks. * * */ public void setTicksSynchro(int s) { XYSeriesCollection seriesCollection = (XYSeriesCollection) this.dataset.getSeriesCollection(); double[] values = new double[seriesCollection.getItemCount(s)]; for (int i = 0; i < seriesCollection.getItemCount(s); i++) values[i] = seriesCollection.getXValue(s, i); XAxis.setLabels(values); }
From source file:org.pentaho.platform.uifoundation.chart.XYSeriesCollectionChartComponent.java
@Override public Document getXmlContent() { // Create a document that describes the result Document result = DocumentHelper.createDocument(); IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext(); String contextPath = requestContext.getContextPath(); setXslProperty("baseUrl", contextPath + "/"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ String mapName = "chart" + AbstractChartComponent.chartCount++; //$NON-NLS-1$ Document chartDefinition = jcrHelper.getSolutionDocument(definitionPath, RepositoryFilePermission.READ); if (chartDefinition == null) { Element errorElement = result.addElement("error"); //$NON-NLS-1$ errorElement.addElement("title").setText( //$NON-NLS-1$ Messages.getInstance().getString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART")); //$NON-NLS-1$ String message = Messages.getInstance().getString("CHARTS.ERROR_0001_CHART_DEFINIION_MISSING", //$NON-NLS-1$ definitionPath);//from ww w . j av a2 s .c om errorElement.addElement("message").setText(message); //$NON-NLS-1$ error(message); return result; } // create a pie definition from the XML definition dataDefinition = createChart(chartDefinition); if (dataDefinition == null) { Element errorElement = result.addElement("error"); //$NON-NLS-1$ errorElement.addElement("title").setText( //$NON-NLS-1$ Messages.getInstance().getString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART")); //$NON-NLS-1$ String message = Messages.getInstance().getString("CHARTS.ERROR_0002_CHART_DATA_MISSING", actionPath); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ errorElement.addElement("message").setText(message); //$NON-NLS-1$ // System .out.println( result.asXML() ); return result; } // create an image for the dial using the JFreeChart engine PrintWriter printWriter = new PrintWriter(new StringWriter()); // we'll dispay the title in HTML so that the dial image does not have // to // accommodate it String chartTitle = ""; //$NON-NLS-1$ try { if (width == -1) { width = Integer.parseInt(chartDefinition.selectSingleNode("/chart/width").getText()); //$NON-NLS-1$ } if (height == -1) { height = Integer.parseInt(chartDefinition.selectSingleNode("/chart/height").getText()); //$NON-NLS-1$ } } catch (Exception e) { // go with the default } if (chartDefinition.selectSingleNode("/chart/" + AbstractChartComponent.URLTEMPLATE_NODE_NAME) != null) { //$NON-NLS-1$ urlTemplate = chartDefinition.selectSingleNode("/chart/" + AbstractChartComponent.URLTEMPLATE_NODE_NAME) //$NON-NLS-1$ .getText(); } if (chartDefinition.selectSingleNode("/chart/paramName") != null) { //$NON-NLS-1$ paramName = chartDefinition.selectSingleNode("/chart/paramName").getText(); //$NON-NLS-1$ } Element root = result.addElement("charts"); //$NON-NLS-1$ XYSeriesCollection chartDataDefinition = (XYSeriesCollection) dataDefinition; if (chartDataDefinition.getSeriesCount() > 0) { // create temporary file names String[] tempFileInfo = createTempFile(); String fileName = tempFileInfo[AbstractChartComponent.FILENAME_INDEX]; String filePathWithoutExtension = tempFileInfo[AbstractChartComponent.FILENAME_WITHOUT_EXTENSION_INDEX]; ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); JFreeChartEngine.saveChart(chartDataDefinition, chartTitle, "", filePathWithoutExtension, width, height, //$NON-NLS-1$ JFreeChartEngine.OUTPUT_PNG, printWriter, info, this); applyOuterURLTemplateParam(); populateInfo(info); Element chartElement = root.addElement("chart"); //$NON-NLS-1$ chartElement.addElement("mapName").setText(mapName); //$NON-NLS-1$ chartElement.addElement("width").setText(Integer.toString(width)); //$NON-NLS-1$ chartElement.addElement("height").setText(Integer.toString(height)); //$NON-NLS-1$ for (int row = 0; row < chartDataDefinition.getSeriesCount(); row++) { for (int column = 0; column < chartDataDefinition.getItemCount(row); column++) { Number value = chartDataDefinition.getY(row, column); Comparable rowKey = chartDataDefinition.getSeriesKey(row); Number columnKey = chartDataDefinition.getX(row, column); Element valueElement = chartElement.addElement("value2D"); //$NON-NLS-1$ valueElement.addElement("value").setText(value.toString()); //$NON-NLS-1$ valueElement.addElement("row-key").setText(rowKey.toString()); //$NON-NLS-1$ valueElement.addElement("column-key").setText(columnKey.toString()); //$NON-NLS-1$ } } String mapString = ImageMapUtilities.getImageMap(mapName, info); chartElement.addElement("imageMap").setText(mapString); //$NON-NLS-1$ chartElement.addElement("image").setText(fileName); //$NON-NLS-1$ } return result; }
From source file:org.fhcrc.cpl.viewer.gui.SpectrumComponent.java
/** * update the chart with new data/*from w w w .j av a 2s. c o m*/ */ public void updateChart(boolean preserveDomain) { _log.debug("updateChart: start"); float[][] spectrumIN = null; MSRun run = (MSRun) ApplicationContext.getProperty(SharedProperties.MS_RUN); MSRun.MSScan selectedScan = getSelectedScan(); FloatRange rangeSpectrum = null; // restricted range of displayed data (for autoZoom) Pair<Spectrum.Peak, Spectrum.Peak> chartRange = null; // tell other components what we are displaying Range domainRange = null; if (preserveDomain && _chart != null) domainRange = _chart.getXYPlot().getDomainAxis().getRange(); if (null == run || null == selectedScan) { clearChart(); //if no chart to display, show white background chartArea.setBackground(java.awt.Color.WHITE); return; } chartArea.setBackground(_backgroundColor); if ("scan".equals(_type)) { scanSpinner.setValue(new Integer(selectedScan.getNum())); double t = selectedScan.getDoubleRetentionTime(); timeTextField.setText("" + Math.round(t * 10) / 10.0); scanSpinner.setEnabled(true); //mzTextField.setText(""); //mzTextField.setEnabled(false); mzSpinner.setValue(new Double(_selectedMZ)); mzSpinner.setEnabled(false); /* ArrayList l = new ArrayList(); l.add(selectedScan.getSpectrum()); spectrumIN = Spectrum.CombineRawSpectra( run.getTemplateSpectrum(), l, new FloatRange(selectedScan.getLowMz(),selectedScan.getHighMz()) ); */ spectrumIN = selectedScan.getSpectrum(); if (autoZoom()) { // we don't want to filter the analyzed data, in case // that affects any of the algorithms // we only filter the result Datasets // UNDONE: choose range based on size of MSDetailPanel // range of 27+ is enough to see 4 ICAT labelled features (9*(4-1)) rangeSpectrum = new FloatRange((float) _selectedMZ - 10F, (float) _selectedMZ + 30F); } if (null == rangeSpectrum) { chartRange = new Pair<Spectrum.Peak, Spectrum.Peak>( new Spectrum.Peak(_selectedScanNum, spectrumIN[0][0], 0.0F), new Spectrum.Peak(_selectedScanNum, spectrumIN[0][spectrumIN[0].length - 1])); } else { chartRange = new Pair<Spectrum.Peak, Spectrum.Peak>( new Spectrum.Peak(_selectedScanNum, rangeSpectrum.min, 0.0F), new Spectrum.Peak(_selectedScanNum, rangeSpectrum.max, 0.0F)); } } else // elution { if (_selectedMZ == -1) { clearChart(); return; } int RESAMPLE = 36; float mz = (float) Math.round(_selectedMZ * RESAMPLE) / RESAMPLE; _log.debug("updateChart mz=" + mz); scanSpinner.setValue(new Integer(selectedScan.getNum())); scanSpinner.setEnabled(false); double t = selectedScan.getDoubleRetentionTime(); timeTextField.setText("" + Math.round(t * 10) / 10.0); //mzTextField.setText("" + mz); //mzTextField.setEnabled(true); mzSpinner.setValue(new Double(_selectedMZ)); mzSpinner.setEnabled(true); FloatArray arrayIntensity = new FloatArray(); FloatArray arrayScan = new FloatArray(); int scanIndex = run.getIndexForScanNum(selectedScan.getNum()); int scanStart = Math.max(0, scanIndex - 64); int scanEnd = Math.min(run.getScanCount() - 1, scanStart + 128); FloatRange r = new FloatRange(mz, mz); for (int s = scanStart; s < scanEnd; s++) { MSRun.MSScan scan = run.getScan(s); float[][] spectrumRaw = scan.getSpectrum(); float[] resample = Spectrum.Resample(spectrumRaw, r, RESAMPLE); float f = resample[0]; arrayIntensity.add(f); //arrayScan.add((float)scan.getDoubleRetentionTime()); // arrayScan.add((float)s); arrayScan.add((float) scan.getNum()); } spectrumIN = new float[][] { arrayScan.toArray(null), arrayIntensity.toArray(null) }; chartRange = new Pair<Spectrum.Peak, Spectrum.Peak>( new Spectrum.Peak(run.getScan(scanStart).getNum(), mz, 0.0F), new Spectrum.Peak(run.getScan(scanEnd).getNum(), mz, 0.0F)); } String mode = (String) displayMode.getSelectedItem(); java.util.List<XYSeriesCollection> listMultipleCharts = null; XYSeriesCollection series = new XYSeriesCollection(); series.setIntervalWidth(0.0); // // Process source spectrum // float[][] spectrum = new float[][] { (float[]) spectrumIN[0].clone(), (float[]) spectrumIN[1].clone() }; ProcessSpectrum: { if ("raw".equals(mode)) { break ProcessSpectrum; } if ("background centroided".equals(mode)) { if (run.getHeaderInfo().getDataProcessing().getCentroided() == 1) spectrum = FeatureStrategyCentroided.backgroundSpectrum(spectrum); break ProcessSpectrum; } if ("clean centroided".equals(mode)) { if (run.getHeaderInfo().getDataProcessing().getCentroided() == 1) spectrum = FeatureStrategyCentroided.cleanSpectrum(spectrum); break ProcessSpectrum; } // all subsequent processing expects resampling // don't resample elution profile if ("scan".equals(_type)) { int len = spectrum[0].length; spectrum = Spectrum.ResampleSpectrum(spectrumIN, new FloatRange((float) Math.floor(spectrum[0][0]), (float) Math.ceil(spectrum[0][len - 1])), 36, false); } else { spectrum[1] = Spectrum.MedianSmooth(spectrum[1]); Spectrum.SmoothALittle(spectrum[1]); } if ("resampled".equals(mode)) break ProcessSpectrum; /* if ("compressed".equals(mode)) (data encoding test) { float[] s = spectrum[1]; short c[] = new short[s.length]; double m = 1.0; for (int i = 0 ; i<c.length ; i++) m = Math.max(m, s[i]); double sqrt = Math.sqrt(m+1); m = Math.round(Math.log(sqrt))+1; double f = Math.floor(0x7fff / Math.log(sqrt)); m = 0; for (int i = 0 ; i<c.length ; i++) { c[i] = (short)(Math.round(Math.log((s[i]+1)/sqrt) * f)); m = Math.max(m, Math.abs(c[i])); } System.err.println("MAX " + m); for (int i = 0 ; i<c.length ; i++) s[i] = (float)((Math.exp((double)c[i]/f) * sqrt) - 1); break ProcessSpectrum; } */ // remove background for further processing if (run.getHeaderInfo().getDataProcessing().getCentroided() != 1) { int window = "scan".equals(_type) ? 72 : 15; float x[] = spectrum[1]; float bg[] = Spectrum.MinimaWindow(x, spectrum[0].length, window, null); for (int i = 0; i < bg.length; i++) bg[i] = Math.max(0, x[i] - bg[i]); spectrum = new float[][] { spectrum[0], bg }; } if ("subtract background".equals(mode)) break ProcessSpectrum; if (mode.startsWith("threshold")) { spectrum[1] = Spectrum.WaveletD3(spectrum[1], null); break ProcessSpectrum; } if ("peaks".equals(mode) || "smoothed".equals(mode)) { if ("scan".equals(_type)) { double s = Smooth2D.smoothYfactor; spectrum[1] = Spectrum.FFTsmooth(spectrum[1], s, false); } else { //FeatureStrategyPeakClusters.smoothTHRESHOLD sm = new FeatureStrategyPeakClusters.smoothTHRESHOLD(); spectrum[1] = SmootherCreator._thresholdElution(spectrum[1]); } break ProcessSpectrum; } } // ProcessSpectrum: // // This is the source spectrum // String name = mode.indexOf("peaks") != -1 ? "-spectrum" : "|spectrum"; series.addSeries(new SpectrumXYSeries(name, spectrum, rangeSpectrum)); _spectrum = spectrum; // // add additional series/charts // // show min/med for reference if (mode.equals("resampled") || mode.equals("subtract background")) { float[] T; if (mode.equals("resampled")) { T = Spectrum.MinimaWindow(spectrum[1], spectrum[0].length, 72, null); series.addSeries(new SpectrumXYSeries("-min", new float[][] { spectrum[0], T }, rangeSpectrum)); } T = Spectrum.MedianWindow(spectrum[1], spectrum[0].length, 72, false); series.addSeries(new SpectrumXYSeries("-med", new float[][] { spectrum[0], T }, rangeSpectrum)); } if (mode.startsWith("wavelet decomposition") || mode.startsWith("wavelet multiresolution")) { Filter f = new Filter("haar"); float[] signal = spectrum[1]; // Spectrum.PadToDouble(spectrum[1], 32); int N = spectrum[0].length; // signal.length; int levels = 5; //if ("wavelet decomposition 2".equals(mode)) Spectrum.SmoothALittle(signal); float[][] modwt1 = Transform.decompose(signal, N, levels, f, "modwt", "periodic", null); float[][] t = modwt1; if ("wavelet multiresolution".equals(mode)) { t = Transform.multiresolution(t, N, levels, f, "modwt", "periodic", null); } else if ("wavelet decomposition 2".equals(mode)) { float[][] mra = Transform.multiresolution(t, N, levels, f, "modwt", "periodic", null); float[][] modwt2 = Transform.decompose(t[2], N, levels, f, "modwt", "periodic", null); // show original, d1, and d2 float[] a = modwt1[2]; float[] b = modwt2[2]; float[] m = mra[2]; Spectrum.Rotate(a, -3); Spectrum.Rotate(b, -7); t = new float[][] { a, /*b,*/ m }; // b is a mirror image of m } // copy array into data series listMultipleCharts = new java.util.LinkedList<XYSeriesCollection>(); XYSeriesCollection ds = new XYSeriesCollection( new SpectrumXYSeries("spectrum", spectrum, rangeSpectrum)); ds.setIntervalWidth(0.0); listMultipleCharts.add(ds); for (int i = 0; i < t.length; i++) { //float[] s = Spectrum.UnpadToFloat(t[i], 32, null); float[][] l = new float[][] { spectrum[0], t[i] }; ds = new XYSeriesCollection(new SpectrumXYSeries("level " + (i + 1), l, rangeSpectrum)); ds.setIntervalWidth(0.0); listMultipleCharts.add(ds); } } else if (mode.startsWith("haar")) { int l = Integer.parseInt(mode.substring(4)); listMultipleCharts = new LinkedList<XYSeriesCollection>(); float[] t1 = Haar.transform(spectrum[1], l); float[] t2 = Haar.transform(t1, l); XYSeriesCollection ds = new XYSeriesCollection( new SpectrumXYSeries("spectrum", spectrum, rangeSpectrum)); ds.setIntervalWidth(0.0); listMultipleCharts.add(ds); XYSeriesCollection ds1 = new XYSeriesCollection( new SpectrumXYSeries(mode, new float[][] { spectrum[0], t1 }, rangeSpectrum)); ds1.setIntervalWidth(0.0); listMultipleCharts.add(ds1); XYSeriesCollection ds2 = new XYSeriesCollection( new SpectrumXYSeries(mode, new float[][] { spectrum[0], t2 }, rangeSpectrum)); ds2.setIntervalWidth(0.0); listMultipleCharts.add(ds2); } else if ("peaks".equals(mode) || "threshold peaks".equals(mode)) { double noise = 0.1; //"peaks".equals(mode) ? 2.0 : 1.0; // Spectrum.Noise(spectrum[1], 0, spectrum[1].length); int[] peakList = Spectrum.PickPeakIndexes(spectrum[1], noise); float[][] peaks = new float[2][peakList.length]; for (int i = 0; i < peakList.length; i++) { int p = peakList[i]; if (p >= spectrum[0].length) continue; peaks[0][i] = spectrum[0][p]; peaks[1][i] = spectrum[1][p]; } series.addSeries(new SpectrumXYSeries("|peaks", peaks, rangeSpectrum)); _spectrum = peaks; } else if ("wavelet peaks".equals(mode)) { //Spectrum.Peak[] peaks = Spectrum.WaveletPeaks(spectrum); Spectrum.Peak[] peaks = Spectrum.WaveletPeaksD3(spectrum); float[][] peakSpectrum = new float[2][peaks.length]; for (int p = 0; p < peaks.length; p++) { peakSpectrum[0][p] = peaks[p].mz; peakSpectrum[1][p] = peaks[p].intensity; } // show d3 and thresholded spectrum int levels = 3, N = spectrum[0].length; Filter f = new Filter("haar"); float[][] modwt = Transform.decompose(spectrum[1], N, levels, f, "modwt", "periodic", null); float[][] mra = Transform.multiresolution(modwt, N, levels, f, "modwt", "periodic", null); float[] thresholded = new float[N]; for (int i = 0; i < N; i++) thresholded[i] = mra[2][i] + mra[3][i]; series.removeAllSeries(); //series.addSeries(new SpectrumXYSeries("-spectrum", new float[][] {spectrum[0],thresholded}, rangeSpectrum)); series.addSeries(new SpectrumXYSeries("-mra", new float[][] { spectrum[0], mra[2] }, rangeSpectrum)); series.addSeries(new SpectrumXYSeries("|peaks", peakSpectrum, rangeSpectrum)); _spectrum = peakSpectrum; } // // now update or create chart // Color[] colors = series.getSeriesCount() == 3 ? new Color[] { Color.RED, Color.BLUE, Color.BLUE } : series.getSeriesCount() == 2 ? new Color[] { Color.BLUE, Color.RED } : new Color[] { Color.RED }; // NOTE: the more often we call setDatasets instead of creating new chart the better // CONSIDER: if we don't save chart, at least preserve zoom settings if (false && _chart != null && _chartPanel != null && !(_chart.getPlot() instanceof CombinedDomainXYPlot) && listMultipleCharts == null) { SpectrumChartFactory.setColors(_chartPanel, colors); _chart.getXYPlot().setDataset(series); _chartPanel.updateUI(); } else { if (listMultipleCharts == null) { _log.debug( "updateChart: series=" + series.getSeriesCount() + " length(0)=" + series.getItemCount(0)); _chartPanel = SpectrumChartFactory.CreateChartPanel(series, colors); } else { _log.debug("updateChart: charts=" + listMultipleCharts.size()); _chartPanel = SpectrumChartFactory.CreateChartPanel(listMultipleCharts, colors); } _chart = _chartPanel.getChart(); // there seem to be mystery margins so give a little extra space Dimension size = chartArea.getSize(); _chartPanel.setPreferredSize(new Dimension(size.width - 10, size.height - 10)); chartArea.removeAll(); chartArea.add(_chartPanel); chartArea.doLayout(); } if (null != domainRange) _chart.getXYPlot().getDomainAxis().setRange(domainRange); //dhmay: if the user has locked the Y axis, then try to use the stored Y axis range if (isYAxisLocked()) { //a locked axis value won't be available if the axis was _just_ locked if (null == _lockedYAxisRange) { //if we've already displayed a chart, use that chart's axis range. Otherwise, //don't force, and record this chart's axis range if (_previousYAxisRange != null) _lockedYAxisRange = _previousYAxisRange; else _lockedYAxisRange = _chart.getXYPlot().getRangeAxis().getRange(); } _chart.getXYPlot().getRangeAxis().setRange(_lockedYAxisRange); } else { //if the Y axis isn't locked, then dump the stored range so it isn't used later _lockedYAxisRange = null; } //always store the previous Y axis range if (_chart.getXYPlot() != null && _chart.getXYPlot().getRangeAxis() != null && _chart.getXYPlot().getRangeAxis().getRange() != null) { _previousYAxisRange = _chart.getXYPlot().getRangeAxis().getRange(); } ApplicationContext.setProperty(SharedProperties.CHART_RANGE, chartRange); }