JFreeChart StackedXYAreaRenderer causes “crimp” in chart

后端 未结 1 1445
再見小時候
再見小時候 2021-01-15 01:25

I\'m using JFreeChart to show a stacked line chart of two sets of data over time, in this example dogs and cats.

import jav         


        
1条回答
  •  孤城傲影
    2021-01-15 02:10

    ChartFactory.createStackedXYAreaChart() instantiates StackedXYAreaRenderer2 to avoid this problem. Your example replaces it with an instance of StackedXYAreaRenderer. Either,

    • Use the factory's renderer and a custom DateAxis.

      private JFreeChart createChart(TimeTableXYDataset chartData) {
          JFreeChart chart = ChartFactory.createStackedXYAreaChart(
              "Dogs and Cats", "Time", "Count", chartData,
              PlotOrientation.VERTICAL, false, true, false);
          DateAxis dateAxis = new DateAxis();
          dateAxis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));
          dateAxis.setTickLabelFont(dateAxis.getTickLabelFont().deriveFont(20f));
          XYPlot plot = (XYPlot) chart.getPlot();
          plot.setDomainAxis(dateAxis);
          return chart;
      }
      
    • Recapitulate the factory, as shown here, in your createChart() method.

      private JFreeChart createChart(TimeTableXYDataset chartData) {
          DateAxis dateAxis = new DateAxis("Time");
          dateAxis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));
          dateAxis.setTickLabelFont(dateAxis.getTickLabelFont().deriveFont(20f));
          NumberAxis yAxis = new NumberAxis("Count");
          XYToolTipGenerator toolTipGenerator = new StandardXYToolTipGenerator();
          StackedXYAreaRenderer2 renderer = new StackedXYAreaRenderer2(
              toolTipGenerator, null);
          renderer.setOutline(true);
          XYPlot plot = new XYPlot(chartData, dateAxis, yAxis, renderer);
          plot.setOrientation(PlotOrientation.VERTICAL);
          plot.setRangeAxis(yAxis);  // forces recalculation of the axis range
          JFreeChart chart = new JFreeChart("Dogs and Cats",
              JFreeChart.DEFAULT_TITLE_FONT, plot, false);
          new StandardChartTheme("JFree").apply(chart);
          return chart;
      }
      

    Can you expand a little bit on why the StackedXYRenderer causes that crimp?

    The author writes, "StackedXYAreaRenderer2 uses a different drawing approach, calculating a polygon for each data point and filling that." In contrast, StackedXYAreaRenderer appears to close a single Shape by connecting the endpoints with a straight line.

    0 讨论(0)
提交回复
热议问题