📄 crosshairdemo2.java
字号:
/**
* Creates the demo chart.
*
* @return The chart.
*/
private JFreeChart createChart() {
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Crosshair Demo 2",
"Time of Day",
"Value",
null,
true,
true,
false
);
XYDataset[] datasets = new XYDataset[SERIES_COUNT];
for (int i = 0; i < SERIES_COUNT; i++) {
datasets[i] = createDataset(
i, "Series " + i, 100.0 + i * 200.0, new Minute(), 200
);
if (i == 0) {
chart.getXYPlot().setDataset(datasets[i]);
}
else {
XYPlot plot = chart.getXYPlot();
plot.setDataset(i, datasets[i]);
plot.setRangeAxis(i, new NumberAxis("Axis " + (i + 1)));
plot.mapDatasetToRangeAxis(i, i);
plot.setRenderer(i, new XYLineAndShapeRenderer(true, false));
}
}
chart.addChangeListener(this);
chart.addProgressListener(this);
chart.setBackgroundPaint(Color.white);
XYPlot plot = chart.getXYPlot();
plot.setOrientation(PlotOrientation.VERTICAL);
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
plot.setDomainCrosshairVisible(true);
plot.setDomainCrosshairLockedOnData(false);
plot.setRangeCrosshairVisible(false);
XYItemRenderer renderer = plot.getRenderer();
renderer.setPaint(Color.black);
return chart;
}
// /**
// * Handles a state change event.
// *
// * @param event the event.
// */
// public void stateChanged(ChangeEvent event) {
// int value = this.slider.getValue();
// XYPlot plot = this.chart.getXYPlot();
// ValueAxis domainAxis = plot.getDomainAxis();
// Range range = domainAxis.getRange();
// double c = domainAxis.getLowerBound() + (value / 100.0) * range.getLength();
// plot.setDomainCrosshairValue(c);
// }
//
/**
* Handles a chart progress event.
*
* @param event
* the event.
*/
public void chartProgress(ChartProgressEvent event) {
if (event.getType() != ChartProgressEvent.DRAWING_FINISHED) {
return;
}
if (this.chartPanel != null) {
JFreeChart c = this.chartPanel.getChart();
if (c != null) {
XYPlot plot = c.getXYPlot();
XYDataset dataset = plot.getDataset();
Comparable seriesKey = dataset.getSeriesKey(0);
double xx = plot.getDomainCrosshairValue();
// update the table...
this.model.setValueAt(seriesKey, 0, 0);
long millis = (long) xx;
this.model.setValueAt(new Long(millis), 0, 1);
for (int i = 0; i < SERIES_COUNT; i++) {
int itemIndex = this.series[i].getIndex(new Minute(new Date(millis)));
if (itemIndex >= 0) {
TimeSeriesDataItem item = this.series[i].getDataItem(Math.min(199, Math.max(0, itemIndex)));
TimeSeriesDataItem prevItem = this.series[i].getDataItem(Math.max(0, itemIndex - 1));
TimeSeriesDataItem nextItem = this.series[i].getDataItem(Math.min(199, itemIndex + 1));
long x = item.getPeriod().getMiddleMillisecond();
double y = item.getValue().doubleValue();
long prevX = prevItem.getPeriod().getMiddleMillisecond();
double prevY = prevItem.getValue().doubleValue();
long nextX = nextItem.getPeriod().getMiddleMillisecond();
double nextY = nextItem.getValue().doubleValue();
this.model.setValueAt(new Long(x), i, 1);
this.model.setValueAt(new Double(y), i, 2);
this.model.setValueAt(new Long(prevX), i, 3);
this.model.setValueAt(new Double(prevY), i, 4);
this.model.setValueAt(new Long(nextX), i, 5);
this.model.setValueAt(new Double(nextY), i, 6);
}
}
}
}
}
}
/**
* A demonstration application showing how to...
*
* @param title the frame title.
*/
public CrosshairDemo2(String title) {
super(title);
setContentPane(new DemoPanel());
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel() {
return new DemoPanel();
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
CrosshairDemo2 demo = new CrosshairDemo2("Crosshair Demo 2");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
/**
* A demo table model.
*/
static class DemoTableModel extends AbstractTableModel implements TableModel {
private Object[][] data;
/**
* Creates a new table model
*
* @param rows the row count.
*/
public DemoTableModel(int rows) {
this.data = new Object[rows][7];
}
/**
* Returns the column count.
*
* @return 7.
*/
public int getColumnCount() {
return 7;
}
/**
* Returns the row count.
*
* @return The row count.
*/
public int getRowCount() {
return this.data.length;
}
/**
* Returns the value at the specified cell in the table.
*
* @param row the row index.
* @param column the column index.
*
* @return The value.
*/
public Object getValueAt(int row, int column) {
return this.data[row][column];
}
/**
* Sets the value at the specified cell.
*
* @param value the value.
* @param row the row index.
* @param column the column index.
*/
public void setValueAt(Object value, int row, int column) {
this.data[row][column] = value;
fireTableDataChanged();
}
/**
* Returns the column name.
*
* @param column the column index.
*
* @return The column name.
*/
public String getColumnName(int column) {
switch(column) {
case 0 : return "Series Name:";
case 1 : return "X:";
case 2 : return "Y:";
case 3 : return "X (prev)";
case 4 : return "Y (prev):";
case 5 : return "X (next):";
case 6 : return "Y (next):";
}
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -