我正在尝试水平滚动我的XYBarChart,我正在关注其中一个JfreeChart的Demo“TranslateDemo1.java”,其中的源代码可以在这里找到:
源代码适用于“TimeSeriesChart”。
然而我尝试使用“XYBarChart”,当我滑动BAR时行为不一样。
为了清楚起见,我用以下源代码替换了#157行:
JFreeChart chart1= ChartFactory.createXYBarChart(
"Title",
null,
true,
"Ylabel",
xyDatasetTranslating,
PlotOrientation.VERTICAL,
true,
true,
false);
有谁知道应用程序为什么会以这种方式运行?以及如何解决它。
谢谢,
答案 0 :(得分:8)
这是一个实现简单解决方案的源代码。
import java.awt.BorderLayout;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JSlider;
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 class DemoPanel extends JPanel implements ChangeListener {
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 int delta = 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.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;
}
}
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 :(得分:1)
TranslatingXYDataset
目前正在实施XYDataset
;相反,您需要根据JFreeChart.createXYBarChart
的要求实施IntervalXYDataset
。
答案 2 :(得分:1)
谢谢 trashgod :D但我放弃了这种方法。我需要在这里完成一些工作;)因此,为了在图表上导航,我使用了不同的数据集org.jfree.data.time.DynamicTimeSeriesCollection
。
本课程针对实时应用程序,在这些应用程序中,我们能够以非常快的方式附加新数据并丢弃最旧的数据(取决于您的输入数据)。总而言之,每当有人滚动条形图时,我只需要更改我的基础数据集,这将触发PlotChangeEvent
,而ChartChangeEvent
反过来会传递到图表并导致{{1}}存在烧成。
此事件链用于确保每当对图表的任何组件进行更改时图表都会自动更新。
再一次,非常感谢