Search in sources :

Example 1 with DefaultValueDataset

use of org.jfree.data.general.DefaultValueDataset in project dhis2-core by dhis2.

the class DefaultChartService method getJFreeChart.

/**
     * Returns a JFreeChart of type defined in the chart argument.
     */
private JFreeChart getJFreeChart(BaseChart chart) {
    final CategoryDataset[] dataSets = getCategoryDataSet(chart);
    final CategoryDataset dataSet = dataSets[0];
    final BarRenderer barRenderer = getBarRenderer();
    final LineAndShapeRenderer lineRenderer = getLineRenderer();
    // ---------------------------------------------------------------------
    // Plot
    // ---------------------------------------------------------------------
    CategoryPlot plot = null;
    if (chart.isType(ChartType.LINE)) {
        plot = new CategoryPlot(dataSet, new CategoryAxis(), new NumberAxis(), lineRenderer);
        plot.setOrientation(PlotOrientation.VERTICAL);
    } else if (chart.isType(ChartType.COLUMN)) {
        plot = new CategoryPlot(dataSet, new CategoryAxis(), new NumberAxis(), barRenderer);
        plot.setOrientation(PlotOrientation.VERTICAL);
    } else if (chart.isType(ChartType.BAR)) {
        plot = new CategoryPlot(dataSet, new CategoryAxis(), new NumberAxis(), barRenderer);
        plot.setOrientation(PlotOrientation.HORIZONTAL);
    } else if (chart.isType(ChartType.AREA)) {
        return getStackedAreaChart(chart, dataSet);
    } else if (chart.isType(ChartType.PIE)) {
        return getMultiplePieChart(chart, dataSets);
    } else if (chart.isType(ChartType.STACKED_COLUMN)) {
        return getStackedBarChart(chart, dataSet, false);
    } else if (chart.isType(ChartType.STACKED_BAR)) {
        return getStackedBarChart(chart, dataSet, true);
    } else if (chart.isType(ChartType.RADAR)) {
        return getRadarChart(chart, dataSet);
    } else if (chart.isType(ChartType.GAUGE)) {
        Number number = dataSet.getValue(0, 0);
        ValueDataset valueDataSet = new DefaultValueDataset(number);
        return getGaugeChart(chart, valueDataSet);
    } else {
        throw new IllegalArgumentException("Illegal or no chart type: " + chart.getType());
    }
    if (chart.isRegression()) {
        plot.setDataset(1, dataSets[1]);
        plot.setRenderer(1, lineRenderer);
    }
    JFreeChart jFreeChart = new JFreeChart(chart.getName(), TITLE_FONT, plot, !chart.isHideLegend());
    setBasicConfig(jFreeChart, chart);
    if (chart.isTargetLine()) {
        plot.addRangeMarker(getMarker(chart.getTargetLineValue(), chart.getTargetLineLabel()));
    }
    if (chart.isBaseLine()) {
        plot.addRangeMarker(getMarker(chart.getBaseLineValue(), chart.getBaseLineLabel()));
    }
    if (chart.isHideSubtitle()) {
        jFreeChart.addSubtitle(getSubTitle(chart));
    }
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
    // ---------------------------------------------------------------------
    // Category label positions
    // ---------------------------------------------------------------------
    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
    domainAxis.setLabel(chart.getDomainAxisLabel());
    ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.setLabel(chart.getRangeAxisLabel());
    return jFreeChart;
}
Also used : DefaultValueDataset(org.jfree.data.general.DefaultValueDataset) NumberAxis(org.jfree.chart.axis.NumberAxis) ValueDataset(org.jfree.data.general.ValueDataset) DefaultValueDataset(org.jfree.data.general.DefaultValueDataset) JFreeChart(org.jfree.chart.JFreeChart) CategoryAxis(org.jfree.chart.axis.CategoryAxis) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset) CategoryDataset(org.jfree.data.category.CategoryDataset) ValueAxis(org.jfree.chart.axis.ValueAxis)

Example 2 with DefaultValueDataset

use of org.jfree.data.general.DefaultValueDataset in project pentaho-platform by pentaho.

the class DialChartComponent method getXmlContent.

@Override
public Document getXmlContent() {
    // Create a document that describes the result
    Document result = DocumentHelper.createDocument();
    IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    setXslProperty("baseUrl", requestContext.getContextPath());
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    setXslProperty("fullyQualifiedServerUrl", PentahoSystem.getApplicationContext().getFullyQualifiedServerURL());
    // $NON-NLS-1$
    String mapName = "chart" + AbstractChartComponent.chartCount++;
    Document chartDefinition = jcrHelper.getSolutionDocument(definitionPath, RepositoryFilePermission.READ);
    if (chartDefinition == null) {
        // $NON-NLS-1$
        Element errorElement = result.addElement("error");
        errorElement.addElement("title").setText(// $NON-NLS-1$ //$NON-NLS-2$
        Messages.getInstance().getString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART"));
        // $NON-NLS-1$
        String message = Messages.getInstance().getString("CHARTS.ERROR_0001_CHART_DEFINIION_MISSING", definitionPath);
        // $NON-NLS-1$
        errorElement.addElement("message").setText(message);
        error(message);
        return result;
    }
    dataDefinition = createChart(chartDefinition);
    if (dataDefinition == null) {
        // $NON-NLS-1$
        Element errorElement = result.addElement("error");
        errorElement.addElement("title").setText(// $NON-NLS-1$ //$NON-NLS-2$
        Messages.getInstance().getString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART"));
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        String message = Messages.getInstance().getString("CHARTS.ERROR_0002_CHART_DATA_MISSING", actionPath);
        // $NON-NLS-1$
        errorElement.addElement("message").setText(message);
        // 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
    // $NON-NLS-1$
    String chartTitle = "";
    try {
        if (width == -1) {
            // $NON-NLS-1$
            width = Integer.parseInt(chartDefinition.selectSingleNode("/chart/width").getText());
        }
        if (height == -1) {
            // $NON-NLS-1$
            height = Integer.parseInt(chartDefinition.selectSingleNode("/chart/height").getText());
        }
    } catch (Exception e) {
    // go with the default
    }
    if (chartDefinition.selectSingleNode("/chart/" + AbstractChartComponent.URLTEMPLATE_NODE_NAME) != null) {
        // $NON-NLS-1$
        urlTemplate = // $NON-NLS-1$
        chartDefinition.selectSingleNode("/chart/" + AbstractChartComponent.URLTEMPLATE_NODE_NAME).getText();
    }
    if (chartDefinition.selectSingleNode("/chart/paramName") != null) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        paramName = chartDefinition.selectSingleNode("/chart/paramName").getText();
    }
    // $NON-NLS-1$
    Element root = result.addElement("charts");
    DefaultValueDataset chartDataDefinition = (DefaultValueDataset) dataDefinition;
    // if (dataDefinition.getRowCount() > 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, JFreeChartEngine.OUTPUT_PNG, printWriter, info, // $NON-NLS-1$
    this);
    applyOuterURLTemplateParam();
    populateInfo(info);
    // $NON-NLS-1$
    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));
    // for (int row = 0; row < chartDataDefinition.getRowCount(); row++) {
    // for (int column = 0; column < chartDataDefinition.getColumnCount(); column++) {
    // Number value = chartDataDefinition.getValue(row, column);
    // Comparable rowKey = chartDataDefinition.getRowKey(row);
    // Comparable columnKey = chartDataDefinition.getColumnKey(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);
    // $NON-NLS-1$
    chartElement.addElement("imageMap").setText(mapString);
    // $NON-NLS-1$
    chartElement.addElement("image").setText(fileName);
    // }
    return result;
}
Also used : DefaultValueDataset(org.jfree.data.general.DefaultValueDataset) IPentahoRequestContext(org.pentaho.platform.api.engine.IPentahoRequestContext) StandardEntityCollection(org.jfree.chart.entity.StandardEntityCollection) StringWriter(java.io.StringWriter) Element(org.dom4j.Element) ChartRenderingInfo(org.jfree.chart.ChartRenderingInfo) Document(org.dom4j.Document) PrintWriter(java.io.PrintWriter)

Example 3 with DefaultValueDataset

use of org.jfree.data.general.DefaultValueDataset in project dhis2-core by dhis2.

the class DefaultChartService method getJFreeChart.

/**
 * Returns a JFreeChart of type defined in the chart argument.
 */
private JFreeChart getJFreeChart(PlotData plotData) {
    final CategoryDataset[] dataSets = getCategoryDataSet(plotData);
    final CategoryDataset dataSet = dataSets[0];
    final BarRenderer barRenderer = getBarRenderer();
    final LineAndShapeRenderer lineRenderer = getLineRenderer();
    // ---------------------------------------------------------------------
    // Plot
    // ---------------------------------------------------------------------
    CategoryPlot plot;
    if (plotData.isType(VisualizationType.LINE.name())) {
        plot = new CategoryPlot(dataSet, new CategoryAxis(), new NumberAxis(), lineRenderer);
        plot.setOrientation(PlotOrientation.VERTICAL);
    } else if (plotData.isType(VisualizationType.COLUMN.name())) {
        plot = new CategoryPlot(dataSet, new CategoryAxis(), new NumberAxis(), barRenderer);
        plot.setOrientation(PlotOrientation.VERTICAL);
    } else if (plotData.isType(VisualizationType.BAR.name())) {
        plot = new CategoryPlot(dataSet, new CategoryAxis(), new NumberAxis(), barRenderer);
        plot.setOrientation(PlotOrientation.HORIZONTAL);
    } else if (plotData.isType(VisualizationType.AREA.name())) {
        return getStackedAreaChart(plotData, dataSet);
    } else if (plotData.isType(VisualizationType.PIE.name())) {
        return getMultiplePieChart(plotData, dataSets);
    } else if (plotData.isType(VisualizationType.STACKED_COLUMN.name())) {
        return getStackedBarChart(plotData, dataSet, false);
    } else if (plotData.isType(VisualizationType.STACKED_BAR.name())) {
        return getStackedBarChart(plotData, dataSet, true);
    } else if (plotData.isType(VisualizationType.RADAR.name())) {
        return getRadarChart(plotData, dataSet);
    } else if (plotData.isType(VisualizationType.GAUGE.name())) {
        Number number = dataSet.getValue(0, 0);
        ValueDataset valueDataSet = new DefaultValueDataset(number);
        return getGaugeChart(plotData, valueDataSet);
    } else {
        throw new IllegalArgumentException("Illegal or no chart type: " + plotData.getType());
    }
    if (plotData.isRegression()) {
        plot.setDataset(1, dataSets[1]);
        plot.setRenderer(1, lineRenderer);
    }
    JFreeChart jFreeChart = new JFreeChart(plotData.getName(), TITLE_FONT, plot, !plotData.isHideLegend());
    setBasicConfig(jFreeChart, plotData);
    if (plotData.isTargetLine()) {
        plot.addRangeMarker(getMarker(plotData.getTargetLineValue(), plotData.getTargetLineLabel()));
    }
    if (plotData.isBaseLine()) {
        plot.addRangeMarker(getMarker(plotData.getBaseLineValue(), plotData.getBaseLineLabel()));
    }
    if (plotData.isHideSubtitle()) {
        jFreeChart.addSubtitle(getSubTitle(plotData));
    }
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
    // ---------------------------------------------------------------------
    // Category label positions
    // ---------------------------------------------------------------------
    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
    domainAxis.setLabel(plotData.getDomainAxisLabel());
    ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.setLabel(plotData.getRangeAxisLabel());
    return jFreeChart;
}
Also used : DefaultValueDataset(org.jfree.data.general.DefaultValueDataset) LineAndShapeRenderer(org.jfree.chart.renderer.category.LineAndShapeRenderer) NumberAxis(org.jfree.chart.axis.NumberAxis) BarRenderer(org.jfree.chart.renderer.category.BarRenderer) StackedBarRenderer(org.jfree.chart.renderer.category.StackedBarRenderer) ValueDataset(org.jfree.data.general.ValueDataset) DefaultValueDataset(org.jfree.data.general.DefaultValueDataset) CategoryPlot(org.jfree.chart.plot.CategoryPlot) JFreeChart(org.jfree.chart.JFreeChart) CategoryAxis(org.jfree.chart.axis.CategoryAxis) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset) CategoryDataset(org.jfree.data.category.CategoryDataset) ValueAxis(org.jfree.chart.axis.ValueAxis)

Example 4 with DefaultValueDataset

use of org.jfree.data.general.DefaultValueDataset in project SIMVA-SoS by SESoS.

the class CompassPlotTest method testCloning.

/**
 * Confirm that cloning works.
 */
@Test
public void testCloning() throws CloneNotSupportedException {
    CompassPlot p1 = new CompassPlot(new DefaultValueDataset(15.0));
    CompassPlot p2 = (CompassPlot) p1.clone();
    assertTrue(p1 != p2);
    assertTrue(p1.getClass() == p2.getClass());
    assertTrue(p1.equals(p2));
}
Also used : DefaultValueDataset(org.jfree.data.general.DefaultValueDataset) Test(org.junit.Test)

Example 5 with DefaultValueDataset

use of org.jfree.data.general.DefaultValueDataset in project SIMVA-SoS by SESoS.

the class MeterPlotTest method testSerialization2.

/**
 * Serialize an instance, restore it, and check for equality.
 */
@Test
public void testSerialization2() {
    MeterPlot p1 = new MeterPlot(new DefaultValueDataset(1.23));
    MeterPlot p2 = (MeterPlot) TestUtilities.serialised(p1);
    assertEquals(p1, p2);
}
Also used : DefaultValueDataset(org.jfree.data.general.DefaultValueDataset) Test(org.junit.Test)

Aggregations

DefaultValueDataset (org.jfree.data.general.DefaultValueDataset)9 JFreeChart (org.jfree.chart.JFreeChart)5 Font (java.awt.Font)3 MeterPlot (org.jfree.chart.plot.MeterPlot)3 Range (org.jfree.data.Range)3 Test (org.junit.Test)3 BasicStroke (java.awt.BasicStroke)2 Color (java.awt.Color)2 MColorSchema (org.compiere.model.MColorSchema)2 CategoryAxis (org.jfree.chart.axis.CategoryAxis)2 NumberAxis (org.jfree.chart.axis.NumberAxis)2 ValueAxis (org.jfree.chart.axis.ValueAxis)2 CategoryDataset (org.jfree.data.category.CategoryDataset)2 DefaultCategoryDataset (org.jfree.data.category.DefaultCategoryDataset)2 ValueDataset (org.jfree.data.general.ValueDataset)2 RectangleInsets (org.jfree.ui.RectangleInsets)2 Graphics2D (java.awt.Graphics2D)1 Rectangle2D (java.awt.geom.Rectangle2D)1 BufferedImage (java.awt.image.BufferedImage)1 PrintWriter (java.io.PrintWriter)1