1 Reply Latest reply: Feb 11, 2013 3:19 PM by jsmith RSS

    Issue with Range in  JSlider Axis Range

    989985
      Hello,
      i am trying to make a simple chart bar, but I have several issues with the range of the sliderbar. When I make zoom with setMouseZoomable(true, false); I want to change the range of the slider, for example if I make a zoom in minutes I want the slider range to keep on "minutes", if zoom is for days, slider range in days and so on.

      Please, could you tell me how could i do it ?

      Thanks in advance

      Andres

      package demo;

      import java.awt.BorderLayout;
      import java.util.Date;
      import javax.swing.*;
      import javax.swing.border.Border;
      import javax.swing.event.ChangeEvent;
      import javax.swing.event.ChangeListener;

      import org.jfree.chart.ChartPanel;
      import org.jfree.chart.JFreeChart;
      import org.jfree.chart.axis.DateAxis;
      import org.jfree.chart.axis.NumberAxis;
      import org.jfree.chart.plot.XYPlot;
      import org.jfree.chart.renderer.xy.XYBarRenderer;
      import org.jfree.data.time.DateRange;
      import org.jfree.data.time.Day;
      import org.jfree.data.time.RegularTimePeriod;
      import org.jfree.data.time.TimeSeries;
      import org.jfree.data.time.TimeSeriesCollection;
      import org.jfree.ui.ApplicationFrame;
      import org.jfree.ui.RefineryUtilities;

      public class DomainTranslateDemo extends ApplicationFrame {
           private static final long serialVersionUID = 1L;

           private static class DemoPanel extends JPanel implements ChangeListener {
           private static final long serialVersionUID = 1L;
      private static int SLIDER_INITIAL_VALUE = 50;
      private JSlider slider;
      private DateAxis domainAxis;


      private int lastValue = SLIDER_INITIAL_VALUE;

      // one month (milliseconds, seconds, minutes, hours, days)
      private long delta = (long)1000 * 60 * 60 * 24 * 30;

      public DemoPanel() {
      super(new BorderLayout());
      JFreeChart chart = createChart();
      ChartPanel chartPanel = new ChartPanel(chart);
      chartPanel.setPreferredSize(new java.awt.Dimension(600, 270));
      //chartPanel.setDomainZoomable(true);
      chartPanel.setMouseZoomable(true);
      chartPanel.setMouseZoomable(true, false);
      chartPanel.setRangeZoomable(true);
      Border border = BorderFactory.createCompoundBorder(
      BorderFactory.createEmptyBorder(4, 4, 4, 4),
      BorderFactory.createEtchedBorder()
      );
      chartPanel.setBorder(border);
      add(chartPanel);

      JPanel dashboard = new JPanel(new BorderLayout());


      dashboard.setBorder(BorderFactory.createEmptyBorder(0, 4, 4, 4));

      this.slider = new JSlider(0, 100, SLIDER_INITIAL_VALUE);
      this.slider.addChangeListener(this);
      dashboard.add(this.slider);
      add(dashboard, BorderLayout.SOUTH);
      }

      private JFreeChart createChart() {

      TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
      TimeSeries series = createSerie(500,20);
      timeSeriesCollection.addSeries(series );

      this.domainAxis = new DateAxis("Time");
      NumberAxis rangeAxis = new NumberAxis("");
      XYBarRenderer renderer = new XYBarRenderer();
      renderer.setShadowVisible(false);
      XYPlot plot = new XYPlot(timeSeriesCollection, domainAxis, rangeAxis, renderer);

      JFreeChart chart = new JFreeChart(
      "Title",
      JFreeChart.DEFAULT_TITLE_FONT,
      plot,
      true);
      // performance
      chart.setAntiAlias(false);

      return chart;
      }

      private TimeSeries createSerie(int domainCount,int rangeCount) {
      TimeSeries timeSeries = new TimeSeries("timeSeries1");
      Day d = new Day(new Date());
      RegularTimePeriod regularTimePeriod = d.next();
      for (int index = 0; index < domainCount; index++) {
      if (index % 2 == 0) {
      double value = (Math.random() * rangeCount);
      timeSeries.add(regularTimePeriod,value);
      }
      regularTimePeriod = regularTimePeriod.next();
      }
      return timeSeries;

      }

      @Override
      public void stateChanged(ChangeEvent event) {
      int value = this.slider.getValue();
      long minimum = domainAxis.getMinimumDate().getTime();
      long maximum = domainAxis.getMaximumDate().getTime();
      if (value<lastValue) { // left
      minimum = minimum - delta;
      maximum = maximum - delta;
      } else { // right
      minimum = minimum + delta;
      maximum = maximum + delta;
      }
      DateRange range = new DateRange(minimum,maximum);
      domainAxis.setRange(range);
      lastValue = value;

      domainAxis.setAutoTickUnitSelection(false);
      //System.out.println(domainAxis.calculateLowestVisibleTickValue(unit) );
      }

      }

      public DomainTranslateDemo(String title) {
      super(title);
      setContentPane(new DemoPanel());
      }

      public static JPanel createDemoPanel() {
      return new DemoPanel();
      }

      public static void main(String[] args) {
      DomainTranslateDemo demo = new DomainTranslateDemo("Translate Demo");
      demo.pack();
      RefineryUtilities.centerFrameOnScreen(demo);
      demo.setVisible(true);

      }

      }
        • 1. Re: Issue with Range in  JSlider Axis Range
          jsmith
          This is a JavaFX forum.

          The code you have posted is not JavaFX code, so you are unlikely to get any help with it from this forum.

          If you would like to attempt a JavaFX solution to your issue, you can rewrite your application as a JavaFX application and look at the corresponding JavaFX controls.
          Should you do this and still have issues with your solution, posting here will probably get you some assistance.

          Links to JavaFX documentation if you wish to re-implement your code in JavaFX:
          http://docs.oracle.com/javafx/2/ui_controls/slider.htm
          http://docs.oracle.com/javafx/2/charts/jfxpub-charts.htm

          Otherwise you probably want to find a JFreeChart forum and post to there or StackOverflow to request assistance.