作者:记忆里的Angle | 来源:互联网 | 2023-10-16 15:44
我有一个动态的区域图表,我想在我的图表显示的最后一个点上添加一个绿点(我图表上的最近值).例如,显示线条边框我希望在我的图标上显示一个绿色的大点面积图?我正在使用示例shownhere.
我有一个动态的区域图表,我想在我的图表显示的最后一个点上添加一个绿点(我图表上的最近值).例如,显示线条边框我希望在我的图标上显示一个绿色的大点面积图?我正在使用示例shown here.
这该怎么做?有我的代码:
public class essaijfree2 extends ApplicationFrame {
private static final String TITLE = "Dynamic Series";
private static final String START = "Start";
private static final String STOP = "Stop";
private static final float MINMAX = 100;
private static final int COUNT = 2 * 60;
private static final int FAST = 100;
private static final int SLOW = FAST * 5;
private static final Random random = new Random();
private Timer timer;
public essaijfree2(final String title) {
super(title);
final DynamicTimeSeriesCollection dataset =
new DynamicTimeSeriesCollection(1, COUNT, new Second());
dataset.setTimeBase(new Second(0, 0, 0, 1, 1, 2011));
dataset.addSeries(gaussianData(), 0, "Gaussian data");
JFreeChart chart = createChart(dataset);
XYPlot xyPlot = (XYPlot) chart.getPlot();
XYDifferenceRenderer r = new XYDifferenceRenderer(Color.green,Color.red, true);
xyPlot.setRenderer(r);
final JComboBox combo = new JComboBox();
combo.addItem("Fast");
combo.addItem("Slow");
combo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("Fast".equals(combo.getSelectedItem())) {timer.setDelay(FAST);
} else {timer.setDelay(SLOW);
}
}
});
this.add(new ChartPanel(chart), BorderLayout.CENTER);
JPanel btnPanel = new JPanel(new FlowLayout());
btnPanel.add(combo);
this.add(btnPanel, BorderLayout.SOUTH);
timer = new Timer(FAST, new ActionListener() {
float[] newData = new float[1];
@Override
public void actionPerformed(ActionEvent e) {
newData[0] = randomValue();
dataset.advanceTime();
dataset.appendData(newData);
}
});
}
private float randomValue() {
return (float) (random.nextGaussian() * MINMAX / 3);
}
private float[] gaussianData() {
float[] a = new float[COUNT];
for (int i = 0; i a[i] = randomValue();
}
return a;
}
private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createXYAreaChart(
TITLE, "hh:mm:ss", "milliVolts", dataset,PlotOrientation.VERTICAL, true, true, false);
final XYPlot plot = result.getXYPlot();
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
ValueAxis range = plot.getRangeAxis();
range.setRange(-MINMAX, MINMAX);
return result;
}
public void start() {
timer.start();
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
essaijfree2 demo = new essaijfree2(TITLE);
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
demo.start();
}
});
}
解决方法:
我建议您创建自己的XYAnnotation,a)不使用预定义的x和y值,而是从数据集中选择它们,b)使用在Java2D空间中定义边界的形状,以便缩放时形状大小保持不变在.
这是一个例子:
public class ItemAnnotationDemo {
public static void main(String[] args) {
int count = 20;
double[][] data = new double[2][count];
for(int i = 0; i data[0][i] = i;
data[1][i] = i;
}
DefaultXYDataset dataset = new DefaultXYDataset();
dataset.addSeries("Values", data);
NumberAxis xAxis = new NumberAxis("x axis");
NumberAxis yAxis = new NumberAxis("y axis");
XYItemRenderer renderer = new XYLineAndShapeRenderer(true, true);
renderer.addAnnotation(new XYItemAnnotation(
new Rectangle2D.Double(-10, -8, 20, 16),
new Color(128,128,128,128),
new BasicStroke(3.0f),
Color.RED,0,Integer.MAX_VALUE));
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
JFreeChart chart = new JFreeChart(plot);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ChartPanel(chart));
frame.pack();
frame.setVisible(true);
}
}
class XYItemAnnotation extends AbstractXYAnnotation{
private Shape shape;
private Paint outline;
private Paint fill;
private Stroke stroke;
private int si;
private int ii;
public XYItemAnnotation(Shape shape, Paint fillPaint, Stroke outlineStroke, Paint outlinePaint, int seriesIndex, int itemIndex){
super();
this.shape = shape;
this.fill = fillPaint;
this.stroke = outlineStroke;
this.outline = outlinePaint;
this.si = seriesIndex;
this.ii = itemIndex;
}
public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis domainAxis, ValueAxis rangeAxis, int datasetIndex, PlotRenderingInfo info){
XYDataset dataset = plot.getDataset(datasetIndex);
if(dataset == null) return;
if(si > dataset.getSeriesCount() - 1 ) return;
int item = Math.max(0, ii);
item = Math.min(item, dataset.getItemCount(si) - 1);
double dx = dataset.getXValue(si, item);
double dy = dataset.getYValue(si, item);
if(Double.isNaN(dx) || Double.isNaN(dy)) return;
if(!domainAxis.getRange().contains(dx) || !rangeAxis.getRange().contains(dy)) return;
PlotOrientation orientation = plot.getOrientation();
RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(plot.getDomainAxisLocation(), orientation);
RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(plot.getRangeAxisLocation(), orientation);
double jx = domainAxis.valueToJava2D(dx, dataArea, domainEdge);
double jy = rangeAxis.valueToJava2D(dy, dataArea, rangeEdge);
if (orientation == PlotOrientation.HORIZONTAL) {
double temp = jx;
jx = jy;
jy = temp;
}
Shape trans = ShapeUtilities.createTranslatedShape(shape, jx, jy);
g2.setPaint(fill);
g2.fill(trans);
g2.setPaint(outline);
g2.setStroke(stroke);
g2.draw(trans);
}
}