/** Replaces the chart's dataset and then checks that the new dataset is OK. */
  public void testReplaceDataset() {

    // create a dataset...
    Number[][] data =
        new Integer[][] {
          {new Integer(-30), new Integer(-20)},
          {new Integer(-10), new Integer(10)},
          {new Integer(20), new Integer(30)}
        };

    CategoryDataset newData = DatasetUtilities.createCategoryDataset("S", "C", data);

    LocalListener l = new LocalListener();
    this.chart.addChangeListener(l);
    CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
    plot.setDataset(newData);
    assertEquals(true, l.flag);
    ValueAxis axis = plot.getRangeAxis();
    Range range = axis.getRange();
    assertTrue(
        "Expecting the lower bound of the range to be around -30: " + range.getLowerBound(),
        range.getLowerBound() <= -30);
    assertTrue(
        "Expecting the upper bound of the range to be around 30: " + range.getUpperBound(),
        range.getUpperBound() >= 30);
  }
 /**
  * Create a bar chart with sample data in the range -3 to +3.
  *
  * @return The chart.
  */
 private static JFreeChart createBarChart() {
   Number[][] data =
       new Integer[][] {
         {new Integer(-3), new Integer(-2)},
         {new Integer(-1), new Integer(1)},
         {new Integer(2), new Integer(3)}
       };
   CategoryDataset dataset = DatasetUtilities.createCategoryDataset("S", "C", data);
   return ChartFactory.createBarChart("Bar Chart", "Domain", "Range", dataset);
 }
  /**
   * Displays a signal (using the JFreeChart package).
   *
   * @param useChart if set to true, the signal is displaied as a bar chart (this is used for
   *     histograms).
   */
  public void display(boolean useChart) {

    JFreeChart chart;
    int nbSamples = getNbSamples();

    if (useChart) {
      String[] categories = new String[nbSamples];
      for (int i = 0; i < nbSamples; i++) {
        categories[i] = data.getX(i).toString();
      }
      String[] categoryNames = {"Histogram"};
      double[][] categoryData = new double[1][nbSamples];
      for (int i = 0; i < nbSamples; i++) {
        categoryData[0][i] = data.getY(i).doubleValue();
      }

      CategoryDataset categoryDataset =
          DatasetUtilities.createCategoryDataset(categoryNames, categories, categoryData);

      chart =
          ChartFactory.createBarChart(
              "Histogram", // Title
              "Data Value", // X axis label
              "Number of Elements", // Y axis label
              categoryDataset, // dataset
              PlotOrientation.VERTICAL, // orientation
              true, // legends
              true, // tool tips
              true);
    } else {
      XYDataset xyDataSet = new XYSeriesCollection(this.data);
      chart =
          ChartFactory.createXYLineChart(
              "Example Dataset", // Title
              "Abscissa", // X axis label
              "Ordinate", // Y axis label
              xyDataSet, // dataset
              PlotOrientation.VERTICAL, // orientation
              true, // legends
              true, // tool tips
              true); // urls

      XYPlot plot = (XYPlot) chart.getPlot();
      XYItemRenderer r = plot.getRenderer();
      if (r instanceof XYLineAndShapeRenderer) {
        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
        renderer.setSeriesShapesVisible(0, true);
        renderer.setSeriesShapesFilled(0, true);
      }
    }

    ChartFrame frame = new ChartFrame("Frame Title", chart);
    frame.setVisible(true);
    frame.setSize(512, 512);
  }
  /**
   * 创建一个数据集
   *
   * @return
   */
  private CategoryDataset getCategoryDataset() {

    DefaultKeyedValues keyedValues = new DefaultKeyedValues();
    keyedValues.addValue("1月", 310);
    keyedValues.addValue("2月", 489);
    keyedValues.addValue("3月", 512);
    keyedValues.addValue("4月", 589);
    keyedValues.addValue("5月", 359);
    keyedValues.addValue("6月", 402);
    CategoryDataset dataset = DatasetUtilities.createCategoryDataset("java book", keyedValues);
    return dataset;
  }
Exemple #5
0
 /**
  * Creates a sample dataset.
  *
  * @return A sample dataset.
  */
 private CategoryDataset createDataset() {
   final double[][] data =
       new double[][] {
         {3.0, 4.0, 3.0, 5.0},
         {5.0, 7.0, 6.0, 8.0},
         {5.0, 7.0, 3.0, 8.0},
         {1.0, 2.0, 3.0, 4.0},
         {2.0, 3.0, 2.0, 3.0}
       };
   final CategoryDataset dataset =
       DatasetUtilities.createCategoryDataset("Region ", "Sales/Q", data);
   return dataset;
 }
  /**
   * 创建一个数据集
   *
   * @return
   */
  private CategoryDataset getCategoryDataset() {

    DefaultKeyedValues keyedValues = new DefaultKeyedValues();
    keyedValues.addValue("1", 310);
    keyedValues.addValue("2", 489);
    keyedValues.addValue("3", 512);
    keyedValues.addValue("4", 589);
    keyedValues.addValue("5", 359);
    keyedValues.addValue("6", 402);
    // 排序
    keyedValues.sortByValues(SortOrder.ASCENDING);
    CategoryDataset dataset = DatasetUtilities.createCategoryDataset("JAVA图书", keyedValues);

    return dataset;
  }
  /**
   * Create a bar chart with sample data in the range -3 to +3.
   *
   * @return The chart.
   */
  private static JFreeChart createWaterfallChart() {

    // create a dataset...
    Number[][] data =
        new Integer[][] {
          {new Integer(-3), new Integer(-2)},
          {new Integer(-1), new Integer(1)},
          {new Integer(2), new Integer(3)}
        };

    CategoryDataset dataset = DatasetUtilities.createCategoryDataset("S", "C", data);

    // create the chart...
    return ChartFactory.createWaterfallChart(
        "Waterfall Chart",
        "Domain",
        "Range",
        dataset,
        PlotOrientation.HORIZONTAL,
        true, // include legend
        true,
        true);
  }
Exemple #8
0
  /**
   * Starting point for the demo.
   *
   * @param args ignored.
   */
  public static void main(final String[] args) {

    // create a chart
    final double[][] data =
        new double[][] {
          {56.0, -12.0, 34.0, 76.0, 56.0, 100.0, 67.0, 45.0},
          {37.0, 45.0, 67.0, 25.0, 34.0, 34.0, 100.0, 53.0},
          {43.0, 54.0, 34.0, 34.0, 87.0, 64.0, 73.0, 12.0}
        };
    final CategoryDataset dataset =
        DatasetUtilities.createCategoryDataset("Series ", "Type ", data);

    JFreeChart chart = null;
    final boolean drilldown = true;

    if (drilldown) {
      final CategoryAxis categoryAxis = new CategoryAxis("Category");
      final ValueAxis valueAxis = new NumberAxis("Value");
      final BarRenderer renderer = new BarRenderer();
      renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
      renderer.setItemURLGenerator(new StandardCategoryURLGenerator("bar_chart_detail.jsp"));
      final CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer);
      plot.setOrientation(PlotOrientation.VERTICAL);
      chart = new JFreeChart("Bar Chart", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
    } else {
      chart =
          ChartFactory.createBarChart(
              "Vertical Bar Chart", // chart title
              "Category", // domain axis label
              "Value", // range axis label
              dataset, // data
              PlotOrientation.VERTICAL,
              true, // include legend
              true,
              false);
    }
    chart.setBackgroundPaint(java.awt.Color.white);

    // ****************************************************************************
    // * JFREECHART DEVELOPER GUIDE                                               *
    // * The JFreeChart Developer Guide, written by David Gilbert, is available   *
    // * to purchase from Object Refinery Limited:                                *
    // *                                                                          *
    // * http://www.object-refinery.com/jfreechart/guide.html                     *
    // *                                                                          *
    // * Sales are used to provide funding for the JFreeChart project - please    *
    // * support us so that we can continue developing free software.             *
    // ****************************************************************************

    // save it to an image
    try {
      final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
      final File file1 = new File("barchart100.png");
      ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);

      // write an HTML page incorporating the image with an image map
      final File file2 = new File("barchart100.html");
      final OutputStream out = new BufferedOutputStream(new FileOutputStream(file2));
      final PrintWriter writer = new PrintWriter(out);
      writer.println("<HTML>");
      writer.println("<HEAD><TITLE>JFreeChart Image Map Demo</TITLE></HEAD>");
      writer.println("<BODY>");
      ChartUtilities.writeImageMap(writer, "chart", info, true);
      writer.println(
          "<IMG SRC=\"barchart100.png\" "
              + "WIDTH=\"600\" HEIGHT=\"400\" BORDER=\"0\" USEMAP=\"#chart\">");
      writer.println("</BODY>");
      writer.println("</HTML>");
      writer.close();

    } catch (IOException e) {
      System.out.println(e.toString());
    }
  }
  public static String getBarSeries(Map dataSource, String objectName, HttpSession session)
      throws Exception {
    DefaultKeyedValues barValues = new DefaultKeyedValues();
    DefaultKeyedValues seriesValues = new DefaultKeyedValues();
    Element chartObject =
        XMLHandler.getElementByAttribute(getChartObjectList(), "name", objectName);

    Element barField = chartObject.getChild("BarFields").getChild("Field");
    Element seriesField = chartObject.getChild("SeriesFields").getChild("Field");

    for (int i = 0; i < dataSource.size(); i++) {
      Map rec = (Map) dataSource.get("ROW" + i);
      barValues.addValue(
          DataFilter.show(rec, chartObject.getChildText("ColumnLabel")),
          Double.parseDouble(rec.get(barField.getAttributeValue("name")).toString()));
      seriesValues.addValue(
          DataFilter.show(rec, chartObject.getChildText("ColumnLabel")),
          Double.parseDouble(rec.get(seriesField.getAttributeValue("name")).toString()));
    }

    CategoryDataset dataset =
        DatasetUtilities.createCategoryDataset(barField.getAttributeValue("label"), barValues);

    PlotOrientation plotOrientation =
        chartObject.getAttributeValue("plotOrientation").equalsIgnoreCase("VERTICAL")
            ? PlotOrientation.VERTICAL
            : PlotOrientation.HORIZONTAL;
    JFreeChart chart =
        ChartFactory.createBarChart3D(
            chartObject.getAttributeValue("title"),
            chartObject.getAttributeValue("categoryAxisLabel"),
            chartObject.getAttributeValue("valueAxisLabel"),
            dataset,
            plotOrientation,
            chartObject.getAttribute("showLegend").getBooleanValue(),
            chartObject.getAttribute("showToolTips").getBooleanValue(),
            chartObject.getAttribute("urls").getBooleanValue());

    CategoryPlot categoryplot = chart.getCategoryPlot();
    LineRenderer3D lineRenderer = new LineRenderer3D();
    CategoryDataset datasetSeries =
        DatasetUtilities.createCategoryDataset(
            seriesField.getAttributeValue("label"), seriesValues);

    categoryplot.setDataset(1, datasetSeries);
    categoryplot.setRangeAxis(1, new NumberAxis3D(seriesField.getAttributeValue("label")));
    categoryplot.setRenderer(1, lineRenderer);
    categoryplot.mapDatasetToRangeAxis(1, 1);

    BarRenderer3D barrenderer = (BarRenderer3D) categoryplot.getRenderer();
    barrenderer.setLabelGenerator(new StandardCategoryLabelGenerator());
    barrenderer.setItemLabelsVisible(true);
    barrenderer.setPositiveItemLabelPosition(
        new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.BASELINE_CENTER));

    //	        lineRenderer.setLabelGenerator(new StandardCategoryLabelGenerator());
    //	        lineRenderer.setItemLabelsVisible(true);
    //	        lineRenderer.setPositiveItemLabelPosition(
    //	                new ItemLabelPosition(ItemLabelAnchor.OUTSIDE10, TextAnchor.CENTER));

    float alpha = 0.7F;
    if (chartObject.getAttribute("alpha") != null) {
      alpha = chartObject.getAttribute("alpha").getFloatValue();
    }
    categoryplot.setForegroundAlpha(alpha);

    int width, height;
    if (chartObject.getAttributeValue("width").equalsIgnoreCase("auto")) {
      width = (50 * dataSource.size()) + 100;
    } else {
      width = Integer.parseInt(chartObject.getAttributeValue("width"));
    }
    if (chartObject.getAttributeValue("height").equalsIgnoreCase("auto")) {
      height = (50 * dataSource.size()) + 100;
    } else {
      height = Integer.parseInt(chartObject.getAttributeValue("height"));
    }

    return ServletUtilities.saveChartAsPNG(chart, width, height, session);
  }
  public static String getSimpleBarChart(Map dataSource, String objectName, HttpSession session)
      throws Throwable {
    Element chartObject =
        XMLHandler.getElementByAttribute(getChartObjectList(), "name", objectName);

    List invokeFields = chartObject.getChild("InvokeFields").getChildren("Field");
    double[][] data = new double[invokeFields.size()][dataSource.size()];
    String[] rowKeys = new String[invokeFields.size()];
    String[] columnKeys = new String[dataSource.size()];
    String columnLabel = chartObject.getChildText("ColumnLabel");

    for (int i = 0; i < dataSource.size(); i++) {
      Map rec = (Map) dataSource.get("ROW" + i);
      columnKeys[i] = DataFilter.show(rec, columnLabel);
      for (int j = 0; j < invokeFields.size(); j++) {
        data[j][i] =
            Double.parseDouble(
                rec.get(((Element) invokeFields.get(j)).getAttributeValue("name")).toString());
      }
    }
    for (int i = 0; i < invokeFields.size(); i++) {
      rowKeys[i] = ((Element) invokeFields.get(i)).getAttributeValue("label");
    }

    CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);

    PlotOrientation plotOrientation =
        chartObject.getAttributeValue("plotOrientation").equalsIgnoreCase("VERTICAL")
            ? PlotOrientation.VERTICAL
            : PlotOrientation.HORIZONTAL;
    JFreeChart chart =
        ChartFactory.createBarChart3D(
            chartObject.getAttributeValue("title"),
            chartObject.getAttributeValue("categoryAxisLabel"),
            chartObject.getAttributeValue("valueAxisLabel"),
            dataset,
            plotOrientation,
            chartObject.getAttribute("showLegend").getBooleanValue(),
            chartObject.getAttribute("showToolTips").getBooleanValue(),
            chartObject.getAttribute("urls").getBooleanValue());

    CategoryPlot C3dplot = (CategoryPlot) chart.getPlot();
    if (chartObject.getAttribute("alpha") != null) {
      C3dplot.setForegroundAlpha(chartObject.getAttribute("alpha").getFloatValue());
    }

    BarRenderer3D barrenderer = (BarRenderer3D) C3dplot.getRenderer();
    barrenderer.setLabelGenerator(new StandardCategoryLabelGenerator());
    barrenderer.setItemLabelsVisible(true);
    barrenderer.setPositiveItemLabelPosition(
        new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.BASELINE_CENTER));

    int width, height;
    if (chartObject.getAttributeValue("width").equalsIgnoreCase("auto")) {
      width = (50 * dataSource.size()) * invokeFields.size() + 100;
    } else {
      width = Integer.parseInt(chartObject.getAttributeValue("width"));
    }
    if (chartObject.getAttributeValue("height").equalsIgnoreCase("auto")) {
      height = (50 * dataSource.size()) * invokeFields.size() + 100;
    } else {
      height = Integer.parseInt(chartObject.getAttributeValue("height"));
    }

    return ServletUtilities.saveChartAsPNG(chart, width, height, session);
  }