📄 chartserviceimpl.java
字号:
chart = org.jfree.chart.ChartFactory.createXYLineChart(chartInfo.getTitle(), chartInfo.getPlot().getSettings().getCategory().getDomainAxisLabel(), chartInfo.getPlot().getSettings().getCategory().getRangeAxisLabel(), (org.jfree.data.XYSeriesCollection) dataset, chartInfo.getPlot().getSettings().getCategory().getPlotOrientation().equals("PlotOrientation.HORIZONTAL") ? org.jfree.chart.plot.PlotOrientation.HORIZONTAL : org.jfree.chart.plot.PlotOrientation.VERTICAL, chartInfo.getLegend(), false, false); } else if (chartInfo.getType().equals("TimeSeries")) { chart = org.jfree.chart.ChartFactory.createTimeSeriesChart(chartInfo.getTitle(), chartInfo.getPlot().getSettings().getCategory().getDomainAxisLabel(), chartInfo.getPlot().getSettings().getCategory().getRangeAxisLabel(), (org.jfree.data.time.TimeSeriesCollection) dataset, chartInfo.getLegend(), false, false); } else if (chartInfo.getType().equals("StepXY")) { if (dataset instanceof org.jfree.data.XYSeriesCollection) chart = org.jfree.chart.ChartFactory.createXYStepChart(chartInfo.getTitle(), chartInfo.getPlot().getSettings().getCategory().getDomainAxisLabel(), chartInfo.getPlot().getSettings().getCategory().getRangeAxisLabel(), (org.jfree.data.XYSeriesCollection) dataset, chartInfo.getPlot().getSettings().getCategory().getPlotOrientation().equals("PlotOrientation.HORIZONTAL") ? org.jfree.chart.plot.PlotOrientation.HORIZONTAL : org.jfree.chart.plot.PlotOrientation.VERTICAL, chartInfo.getLegend(), false, false); else chart = org.jfree.chart.ChartFactory.createXYStepChart(chartInfo.getTitle(), chartInfo.getPlot().getSettings().getCategory().getDomainAxisLabel(), chartInfo.getPlot().getSettings().getCategory().getRangeAxisLabel(), (org.jfree.data.time.TimeSeriesCollection) dataset, chartInfo.getPlot().getSettings().getCategory().getPlotOrientation().equals("PlotOrientation.HORIZONTAL") ? org.jfree.chart.plot.PlotOrientation.HORIZONTAL : org.jfree.chart.plot.PlotOrientation.VERTICAL, chartInfo.getLegend(), false, false); } else if (chartInfo.getType().equals("StepAreaXY")) { if (dataset instanceof org.jfree.data.XYSeriesCollection) chart = org.jfree.chart.ChartFactory.createXYStepAreaChart(chartInfo.getTitle(), chartInfo.getPlot().getSettings().getCategory().getDomainAxisLabel(), chartInfo.getPlot().getSettings().getCategory().getRangeAxisLabel(), (org.jfree.data.XYSeriesCollection) dataset, chartInfo.getPlot().getSettings().getCategory().getPlotOrientation().equals("PlotOrientation.HORIZONTAL") ? org.jfree.chart.plot.PlotOrientation.HORIZONTAL : org.jfree.chart.plot.PlotOrientation.VERTICAL, chartInfo.getLegend(), false, false); else chart = org.jfree.chart.ChartFactory.createXYStepAreaChart(chartInfo.getTitle(), chartInfo.getPlot().getSettings().getCategory().getDomainAxisLabel(), chartInfo.getPlot().getSettings().getCategory().getRangeAxisLabel(), (org.jfree.data.time.TimeSeriesCollection) dataset, chartInfo.getPlot().getSettings().getCategory().getPlotOrientation().equals("PlotOrientation.HORIZONTAL") ? org.jfree.chart.plot.PlotOrientation.HORIZONTAL : org.jfree.chart.plot.PlotOrientation.VERTICAL, chartInfo.getLegend(), false, false); } } catch (ClassCastException e) { throw new Exception("Incorrect dataset type."); } if (chartInfo.getBackgroundPaint() != null) chart.setBackgroundPaint(createPaint(chartInfo.getBackgroundPaint())); if (chartInfo.getSubtitle() != null) chart.addSubtitle(new org.jfree.chart.title.TextTitle(chartInfo.getSubtitle().getText(), chartInfo.getSubtitle().getFont() != null ? createFont(chartInfo.getSubtitle().getFont()) : new java.awt.Font("Serif", java.awt.Font.PLAIN, 10))); Plot plotInfo = chartInfo.getPlot(); org.jfree.chart.plot.Plot plot = chart.getPlot(); if (plotInfo.getBackgroundPaint() != null) plot.setBackgroundPaint(createPaint(plotInfo.getBackgroundPaint())); plot.setNoDataMessage(plotInfo.getNoDataMessage().getValue()); plot.setNoDataMessageFont(createFont(plotInfo.getNoDataMessage().getFont())); plot.setNoDataMessagePaint(createPaint(plotInfo.getNoDataMessage().getPaint())); plot.setForegroundAlpha(plotInfo.getForegroundAlpha()); try { for (int i = 0; i < plotInfo.getSeriesPaintCount(); ++i) { if (chartDescriptor.getFileInfo().getDataset().getType().equals("org.jfree.data.DefaultPieDataset")) ((org.jfree.chart.plot.PiePlot) chart.getPlot()).setSectionPaint(i, createPaint(plotInfo.getSeriesPaint(i))); else if (chartDescriptor.getFileInfo().getDataset().getType().equals("org.jfree.data.DefaultCategoryDataset") || chartDescriptor.getFileInfo().getDataset().getType().equals("org.jfree.data.gantt.TaskSeriesCollection")) chart.getCategoryPlot().getRenderer().setSeriesPaint(i, createPaint(plotInfo.getSeriesPaint(i))); else if (chartDescriptor.getFileInfo().getDataset().getType().equals("org.jfree.data.XYSeriesCollection") || chartDescriptor.getFileInfo().getDataset().getType().equals("org.jfree.data.time.TimeSeriesCollection")) chart.getXYPlot().getRenderer().setSeriesPaint(i, createPaint(plotInfo.getSeriesPaint(i))); } } catch (ClassCastException e) { } if (plotInfo.getSettings().getPie() != null) try { ((org.jfree.chart.plot.PiePlot) plot).setLabelGenerator(new org.jfree.chart.labels.StandardPieItemLabelGenerator(plotInfo.getSettings().getPie().getLabelGenerator())); } catch (ClassCastException e) { } Image image = chartDescriptor.getFileInfo().getImage(); if (image.getType().equals("JPEG")) { File chartFile = secureDirectoryService.getFile(secureDirectoryService.createFileLocationID(userID, image.getAppName(), image.getFilename() + ".jpeg")); if (chartFile == null) throw new IOException("Cannot create chartfile"); org.jfree.chart.ChartUtilities.saveChartAsJPEG(chartFile, image.getQuality(), chart, image.getWidth(), image.getHeight()); } else if (image.getType().equals("PNG")) { File chartFile = secureDirectoryService.getFile(secureDirectoryService.createFileLocationID(userID, image.getAppName(), image.getFilename() + ".png")); if (chartFile == null) throw new IOException("Cannot create chartfile"); org.jfree.chart.ChartUtilities.saveChartAsPNG(chartFile, chart, image.getWidth(), image.getHeight()); } else throw new IOException("Image type (" + image.getType() + ") is not supported."); chartDescriptor.getFileInfo().getImage().setTimestamp(new Date().getTime()); chartDescriptor.getChartInfo().setRefresh(false); writeChartDescriptor(userID, appName, resource, chartDescriptor); return true; } private java.awt.Paint createPaint(PaintType paint) { if (paint.getColor() != null) return new java.awt.Color(paint.getColor().getRed(), paint.getColor().getGreen(), paint.getColor().getBlue()); GradientPoint gradientPoint0 = paint.getGradient().getGradientPoint(0); GradientPoint gradientPoint1 = paint.getGradient().getGradientPoint(1); return new java.awt.GradientPaint(gradientPoint0.getX(), gradientPoint0.getY(), new java.awt.Color(gradientPoint0.getColor().getRed(), gradientPoint0.getColor().getGreen(), gradientPoint0.getColor().getBlue()), gradientPoint1.getX(), gradientPoint1.getY(), new java.awt.Color(gradientPoint1.getColor().getRed(), gradientPoint1.getColor().getGreen(), gradientPoint1.getColor().getBlue())); } private java.awt.Font createFont(FontType font) { return new java.awt.Font(font.getFamily(), font.getStyle(), font.getSize()); } private ChartDescriptor createChartDescriptor(String userID, String appName, String resource, String type, org.jfree.data.Dataset inDataset) throws IOException, Exception { writeDataset(userID, appName, resource, inDataset); Image image = new Image(); image.setAppName(appName); image.setFilename(resource); image.setTimestamp(0); Dataset dataset = new Dataset(); dataset.setAppName(appName); dataset.setFilename(resource); dataset.setType(inDataset.getClass().getName()); dataset.setTimestamp(new Date().getTime()); FileInfo fileInfo = new FileInfo(); fileInfo.setImage(image); fileInfo.setDataset(dataset); NoDataMessage noDataMessage = new NoDataMessage(); Color color = new Color(); color.setRed(255); Paint paint = new Paint(); paint.setColor(color); noDataMessage.setFont(new Font()); noDataMessage.setPaint(paint); Plot plot = new Plot(); plot.setNoDataMessage(noDataMessage); Settings settings = new Settings(); if (type.equals("Pie") || type.equals("Pie3D")) settings.setPie(new Pie()); else settings.setCategory(new Category()); plot.setSettings(settings); ChartInfo chartInfo = new ChartInfo(); chartInfo.setType(type); chartInfo.setPlot(plot); Color backgroundColor = new Color(); backgroundColor.setRed(238); backgroundColor.setGreen(238); backgroundColor.setBlue(238); BackgroundPaint backgroundPaint = new BackgroundPaint(); backgroundPaint.setColor(backgroundColor); chartInfo.setBackgroundPaint(backgroundPaint); ChartDescriptor chartDescriptor = new ChartDescriptor(); chartDescriptor.setFileInfo(fileInfo); chartDescriptor.setChartInfo(chartInfo); return chartDescriptor; } private ChartDescriptor readChartDescriptor(String userID, String appName, String resource) throws IOException, MarshalException, ValidationException { File file = secureDirectoryService.getFile(secureDirectoryService.createFileLocationID(userID, CHART_SERVICE_DIRECTORY, appName + "/" + resource + ".xml")); FileReader reader = new FileReader(file); ChartDescriptor chartDescriptor = (ChartDescriptor) ChartDescriptor.unmarshal(reader); reader.close(); return chartDescriptor; } private void writeChartDescriptor(String userID, String appName, String resource, ChartDescriptor chartDescriptor) throws IOException, MarshalException, ValidationException { File file = secureDirectoryService.getFile(secureDirectoryService.createFileLocationID(userID, CHART_SERVICE_DIRECTORY, appName + "/" + resource + ".xml")); FileWriter writer = new FileWriter(file); chartDescriptor.marshal(writer); writer.flush(); writer.close(); } private void writeDataset(String userID, String appName, String resource, org.jfree.data.Dataset inDataset) throws IOException, Exception { File file = secureDirectoryService.getFile(secureDirectoryService.createFileLocationID(userID, appName, resource + ".xml")); try { if (inDataset.getClass().getName().equals("org.jfree.data.DefaultPieDataset")) DatasetUtilities.writeDefaultPieDataset(file, (org.jfree.data.DefaultPieDataset) inDataset); else if (inDataset.getClass().getName().equals("org.jfree.data.DefaultCategoryDataset")) DatasetUtilities.writeDefaultCategoryDataset(file, (org.jfree.data.DefaultCategoryDataset) inDataset); else if (inDataset.getClass().getName().equals("org.jfree.data.gantt.TaskSeriesCollection")) DatasetUtilities.writeTaskSeriesCollection(file, (org.jfree.data.gantt.TaskSeriesCollection) inDataset); else if (inDataset.getClass().getName().equals("org.jfree.data.time.TimeSeriesCollection")) DatasetUtilities.writeTimeSeriesCollection(file, (org.jfree.data.time.TimeSeriesCollection) inDataset); else if (inDataset.getClass().getName().equals("org.jfree.data.XYSeriesCollection")) DatasetUtilities.writeXYSeriesCollection(file, (org.jfree.data.XYSeriesCollection) inDataset); } catch (MarshalException e) { file.delete(); throw new Exception("Unable to write dataset file (MarshalException).", e); } catch (ValidationException e) { file.delete(); throw new Exception("Unable to write dataset file (ValidationException).", e); } } private org.jfree.data.Dataset readDataset(String userID, String appName, String resource, String type) throws IOException, MarshalException, ValidationException, Exception { File file = secureDirectoryService.getFile(secureDirectoryService.createFileLocationID(userID, appName, resource + ".xml")); org.jfree.data.Dataset dataset = null; if (type.equals("org.jfree.data.DefaultPieDataset")) dataset = DatasetUtilities.readDefaultPieDataset(file); else if (type.equals("org.jfree.data.DefaultCategoryDataset")) dataset = DatasetUtilities.readDefaultCategoryDataset(file); else if (type.equals("org.jfree.data.gantt.TaskSeriesCollection")) dataset = DatasetUtilities.readTaskSeriesCollection(file); else if (type.equals("org.jfree.data.time.TimeSeriesCollection")) dataset = DatasetUtilities.readTimeSeriesCollection(file); else if (type.equals("org.jfree.data.XYSeriesCollection")) dataset = DatasetUtilities.readXYSeriesCollection(file); else throw new Exception("Unsupported dataset type."); return dataset; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -