在JFreeChart中如何获取图表上特定点的[x,y]值?

huangapple go评论77阅读模式
英文:

In JFreeChart how to get the [x,y] values of a certain point on the chart?

问题

以下是您提供的代码的翻译部分:

这个问题与我的之前的问题有关 [ [https://stackoverflow.com/questions/64148281/how-to-display-volume-in-the-domain-crosshair-and-set-the-crosshair-programmatic][1] ],在@trashgod的帮助下,我得到了大部分问题的答案,但仍有一个问题没有答案,这里我将以不同的角度提问这个问题。

在我的Swing应用程序中有一个日期列表和一些数据当我点击某个日期时我希望我的应用程序显示一个图表并指向该日期的点这样当图像显示出来时我就不必移动鼠标来查找和显示该日期的信息因此应用程序的屏幕截图如下所示

[![enter image description here][2]][2]

正如您从顶部的屏幕截图中可以看到的那样如果我在日期列表上点击2020-07-29图表将加载并且十字线将自动聚焦在2020-07-29上并且相关信息将自动显示出来在底部的屏幕截图中如果我点击日期2020-08-11图表图像将加载并且鼠标将设置在2020-08-11上因此作为用户我不必在图像上搜索并找到该日期的数据

我简化后的应用程序如下

// ...(略去导入部分)

public class PriceVolume_Chart extends JPanel implements ChartMouseListener
{
  // ...(略去其它代码)

  @Override
  public void chartMouseClicked(ChartMouseEvent event)
  {
    // 忽略
  }

  public void chartMouseMoved(ChartMouseEvent cmevent)
  {
    ChartEntity chartentity=cmevent.getEntity();
    if (chartentity instanceof XYItemEntity)
    {
      XYItemEntity e=(XYItemEntity)chartentity;
      XYDataset d=e.getDataset();
      int s=e.getSeriesIndex();
      int i=e.getItem();
      double x=d.getXValue(s,i);
      double y=d.getYValue(s,i);
      Out("x = "+x+"  y = "+y);
      xCrosshair.setValue(x);
      yCrosshair.setValue(y);
    }
  }

  // ...(略去其它代码)

  // 在事件分派线程中创建并显示GUI。为了线程安全,应从事件分派线程中调用此方法。
  static void Create_And_Show_GUI()
  {
    final PriceVolume_Chart demo=new PriceVolume_Chart("ADS");

    JFrame frame=new JFrame("PriceVolume_Chart Frame");
    frame.add(demo);
    frame.addWindowListener(new WindowAdapter()
    {
      // ...(略去其它代码)
    });
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }

  public static void main(String[] args)
  {
    // 安排一个任务给事件分派线程:创建并显示此应用程序的GUI。
    SwingUtilities.invokeLater(new Runnable() { public void run() { Create_And_Show_GUI(); } });
  }
}

class MyRender extends XYBarRenderer
{
  @Override
  public Paint getItemPaint(int row,int col)
  {
    this.setBarAlignmentFactor(0.5);
    // ...(略去其它代码)
  }
}

这是您提供的代码的翻译部分。如果您还有其他问题或需要进一步的帮助,请随时问我。

英文:

This question is related to my previous question [ https://stackoverflow.com/questions/64148281/how-to-display-volume-in-the-domain-crosshair-and-set-the-crosshair-programmatic ], with the help of @trashgod, I got the answers for most of my questions, but one question is still unanswered, here I'll ask this question from a different angle.

In my Swing app, there is a list of dates with some data, when I click on a certain date, I want my app to display a chart and points to that date when the image shows up, so I don't have to move my mouse around to find and display info of that date. So the app screen shots look like the following.

在JFreeChart中如何获取图表上特定点的[x,y]值?

So as you can see from the top screenshot, if I click on 2020-07-29 on the date list, the chart will load and the cross-hairs will be automatically focused on 2020-07-29, and the relative info will auto show up. In the bottom screenshot, if I click on the date 2020-08-11, the chart image will load and the mouse will be set on 2020-08-11, so as a user, I don't have to search on the image and find that date's data.

My simplified app is listed below :

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.entity.*;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.time.*;
import org.jfree.data.xy.XYDataset;
import org.jfree.chart.labels.*;
import org.jfree.chart.panel.*;
import org.jfree.chart.plot.*;
public class PriceVolume_Chart extends JPanel implements ChartMouseListener    // A demo application for price-volume chart.   
{
ChartPanel panel;
TimeSeries Price_series=new TimeSeries("Price");
TimeSeries Volume_Series=new TimeSeries("Volume");
Crosshair xCrosshair,yCrosshair;
static Vector<String> Volume_Color_Vector=new Vector();
public PriceVolume_Chart(String Symbol)
{
JFreeChart chart=createChart(Symbol);
panel=new ChartPanel(chart,true,true,true,false,true);
panel.setPreferredSize(new Dimension(1000,500));
panel.addChartMouseListener(this);
CrosshairOverlay crosshairOverlay=new CrosshairOverlay();
float[] dash={2f,0f,2f};
BasicStroke bs=new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND,1.0f,dash,2f);
xCrosshair=new Crosshair(Double.NaN,Color.GRAY,bs);
xCrosshair.setLabelBackgroundPaint(new Color(0f,0f,0f,1f));
xCrosshair.setLabelFont(xCrosshair.getLabelFont().deriveFont(14f));
xCrosshair.setLabelPaint(new Color(1f,1f,1f,1f));
xCrosshair.setLabelGenerator(new CrosshairLabelGenerator()
{
@Override
public String generateLabel(Crosshair crosshair)
{
long ms=(long)crosshair.getValue();
TimeSeriesDataItem item=null;
for (int i=0;i<Volume_Series.getItemCount();i++)
{
item=Volume_Series.getDataItem(i);
if (ms==item.getPeriod().getFirstMillisecond()) break;
}
long volume=item.getValue().longValue();
return NumberFormat.getInstance().format(volume);
}
});
xCrosshair.setLabelVisible(true);
yCrosshair=new Crosshair(Double.NaN,Color.GRAY,bs);
yCrosshair.setLabelBackgroundPaint(new Color(0f,0f,0f,1f));
yCrosshair.setLabelFont(xCrosshair.getLabelFont().deriveFont(14f));
yCrosshair.setLabelPaint(new Color(1f,1f,1f,1f));
yCrosshair.setLabelVisible(true);
crosshairOverlay.addDomainCrosshair(xCrosshair);
crosshairOverlay.addRangeCrosshair(yCrosshair);
panel.addOverlay(crosshairOverlay);
add(panel);
/*
xCrosshair.setValue(1.5959952E12);
xCrosshair.setVisible(true);
yCrosshair.setValue(45.230579);
yCrosshair.setVisible(true);
*/
}
private JFreeChart createChart(String Symbol)
{
createPriceDataset(Symbol);
XYDataset priceData=new TimeSeriesCollection(Price_series);
JFreeChart chart=ChartFactory.createTimeSeriesChart(Symbol,"Date",getYLabel("Price ( $ )"),priceData,true,true,true);
XYPlot plot=chart.getXYPlot();
plot.setBackgroundPaint(new Color(192,196,196));
NumberAxis rangeAxis1=(NumberAxis)plot.getRangeAxis();
rangeAxis1.setLowerMargin(0.40);                                           // Leave room for volume bars
plot.getRenderer().setDefaultToolTipGenerator(new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,new SimpleDateFormat("yyyy-MM-d"),NumberFormat.getCurrencyInstance()));
NumberAxis rangeAxis2=new NumberAxis("Volume");
rangeAxis2.setUpperMargin(1.00);                                           // Leave room for price line   
rangeAxis2.setNumberFormatOverride(NumberFormat.getNumberInstance());
plot.setRangeAxis(1,rangeAxis2);
plot.setDataset(1,new TimeSeriesCollection(Volume_Series));
plot.setRangeAxis(1,rangeAxis2);
plot.mapDatasetToRangeAxis(1,1);
MyRender Renderer=new MyRender();
Renderer.setShadowVisible(false);
plot.setRenderer(1,Renderer);
DateAxis domainAxis=(DateAxis) plot.getDomainAxis();                     // Consider adjusting the lower margin of the domain axis for symmetry.
domainAxis.setLowerMargin(0.05);
return chart;
}
private void createPriceDataset(String Symbol)
{
String Lines[]=new String[21],Items[],Date;
int Year, Month, Day;
long Volume,Last_Volume=0;
double Price;
Lines[0]="Date,Open,High,Low,Close,Adj Close,Volume";
Lines[1]="2020-07-17,44.110001,44.369999,41.919998,42.509998,42.323395,849700";
Lines[2]="2020-07-20,41.630001,41.680000,39.669998,40.119999,39.943886,1319300";
Lines[3]="2020-07-21,40.880001,42.860001,40.860001,42.270000,42.084450,2070300";
Lines[4]="2020-07-22,41.919998,42.700001,41.090000,42.570000,42.383133,1317600";
Lines[5]="2020-07-23,43.919998,46.389999,43.279999,44.759998,44.563519,1917700";
Lines[6]="2020-07-24,46.500000,46.500000,43.950001,44.410000,44.215057,1384600";
Lines[7]="2020-07-27,44.000000,44.240002,42.610001,43.860001,43.667469,799800";
Lines[8]="2020-07-28,43.389999,44.590000,42.930000,43.020000,42.831158,699700";
Lines[9]="2020-07-29,42.759998,45.590000,42.740002,45.430000,45.230579,826200";
Lines[10]="2020-07-30,44.160000,44.639999,42.959999,44.500000,44.304661,798100";
Lines[11]="2020-07-31,44.330002,44.419998,42.580002,44.360001,44.165276,1037800";
Lines[12]="2020-08-03,44.560001,45.599998,43.419998,44.939999,44.742729,797000";
Lines[13]="2020-08-04,44.900002,45.500000,43.450001,43.540001,43.348877,971100";
Lines[14]="2020-08-05,44.860001,45.389999,43.650002,45.330002,45.131020,902000";
Lines[15]="2020-08-06,45.049999,46.279999,44.330002,45.299999,45.101147,645200";
Lines[16]="2020-08-07,44.849998,46.189999,44.189999,46.150002,45.947418,604900";
Lines[17]="2020-08-10,46.669998,48.410000,46.549999,47.290001,47.082417,960200";
Lines[18]="2020-08-11,49.110001,50.849998,48.799999,48.910000,48.695301,1187700";
Lines[19]="2020-08-12,49.759998,50.009998,47.060001,47.840000,47.630001,752800";
Lines[20]="2020-08-13,46.950001,48.369999,46.459999,47.110001,47.110001,535700";
for (int i=1;i<Lines.length;i++)
{
Items=Lines[i].split(",");
Date=Items[0].replace("-0","-");
Price=Double.parseDouble(Items[5]);
Volume=Long.parseLong(Items[6]);
Items=Date.split("-");
Year=Integer.parseInt(Items[0]);
Month=Integer.parseInt(Items[1]);
Day=Integer.parseInt(Items[2]);
Price_series.add(new Day(Day,Month,Year),Price);
Volume_Series.add(new Day(Day,Month,Year),Volume);
Volume_Color_Vector.add(Volume>=Last_Volume?"+":"-");
Last_Volume=Volume;
}
}
@Override
public void chartMouseClicked(ChartMouseEvent event)
{
// ignore
}
public void chartMouseMoved(ChartMouseEvent cmevent)
{
ChartEntity chartentity=cmevent.getEntity();
if (chartentity instanceof XYItemEntity)
{
XYItemEntity e=(XYItemEntity)chartentity;
XYDataset d=e.getDataset();
int s=e.getSeriesIndex();
int i=e.getItem();
double x=d.getXValue(s,i);
double y=d.getYValue(s,i);
Out("x = "+x+"  y = "+y);
xCrosshair.setValue(x);
yCrosshair.setValue(y);
}
}
String getYLabel(String Text)
{
String Result="";
for (int i=0;i<Text.length();i++) Result+=Text.charAt(i)+(i<Text.length()-1?"\u2009":"");
//    Out(Result);
return Result;
}
private static void out(String message) { System.out.print(message); }
private static void Out(String message) { System.out.println(message); }
// Create the GUI and show it. For thread safety, this method should be invoked from the event-dispatching thread.
static void Create_And_Show_GUI()
{
final PriceVolume_Chart demo=new PriceVolume_Chart("ADS");
JFrame frame=new JFrame("PriceVolume_Chart Frame");
frame.add(demo);
frame.addWindowListener(new WindowAdapter()
{
public void windowActivated(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowClosing(WindowEvent e) { System.exit(0); }
public void windowDeactivated(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { demo.repaint(); }
public void windowGainedFocus(WindowEvent e) { demo.repaint(); }
public void windowIconified(WindowEvent e) { }
public void windowLostFocus(WindowEvent e) { }
public void windowOpening(WindowEvent e) { demo.repaint(); }
public void windowOpened(WindowEvent e) { }
public void windowResized(WindowEvent e) { demo.repaint(); }
public void windowStateChanged(WindowEvent e) { demo.repaint(); }
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args)
{
// Schedule a job for the event-dispatching thread : creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() { public void run() { Create_And_Show_GUI(); } });
}
}
class MyRender extends XYBarRenderer
{
@Override
public Paint getItemPaint(int row,int col)
{
this.setBarAlignmentFactor(0.5);
//    System.out.println(row+" "+col+" "+super.getItemPaint(row,col));
return PriceVolume_Chart.Volume_Color_Vector.elementAt(col).equals("+")?super.getItemPaint(row,col):new Color(0.56f,0.2f,0.5f,1f);
}
} 

I know how to set the cross-hair on a certain point on the chart [ Commented out : xCrosshair.setValue(1.5959952E12); ], yet I do not know the [x,y] values of a certain date on a certain chart, therefore my question is : on a JFreeChart like the one I have here, how to get the [x,y] values of a vertain date [ e.g. 2020-07-29 ], or how to get the [x,y] values of the N-th date [ e.g. 9-th date = 2020-07-29 ] ?

答案1

得分: 1

专注于问题“如何获取特定日期的[x,y]值”,NumberAxis 方法 java2DToValue()valueToJava2D() 展示了解决方法。具体示例可参考 CrosshairOverlayDemo1,通用原理在此处进行了讨论。

幸运的是,您可以仅使用数据来突出显示图表元素。特别地,AnnotationMarkerCrosshair 的实现都与数据坐标一起工作。以具体示例来说,让 PriceVolume_Chart 实现 ActionListener,并在图表的构造函数中添加一个 javax.swing.Timer

public PriceVolume_Chart(String Symbol) {
…
Timer t = new Timer(500, this);
t.start();
}

然后,给定一个适当的 index 和监听器,您将看到十字线以每秒 2 次的频率依次移动到每个日期。

private int index;
@Override
public void actionPerformed(ActionEvent e) {
TimeSeriesDataItem item = Volume_Series.getDataItem(index);
xCrosshair.setValue(item.getPeriod().getFirstMillisecond());
item = Price_series.getDataItem(index);
yCrosshair.setValue(item.getValue().doubleValue());
index++;
if (index == Volume_Series.getItemCount()) {
index = 0;
}
}

当用户选择特定数据条目时,您可以使用类似的监听器来移动所选指示器。具体细节取决于您对观察者模式的实现,可以在此处进行讨论。由于您希望图表和表视图共享对公共模型的访问权,您可以考虑扩展 AbstractXYDataset,示例见此处,并实现一个 Swing 的 TableModel;或者,考虑扩展 AbstractTableModel,示例见此处,并实现 XYDataset 接口。

英文:

Focusing narrowly on the question of "how to get the [x,y] values of a certain date," the NumberAxis methods java2DToValue()
and valueToJava2D() illustrate the approach. CrosshairOverlayDemo1 is a concrete example, and the general principle is examined here.

Fortunately, you can highlight a chart element using just its data. In particular, the implementations of Annotation, Marker and Crosshair all work with data coordinates. As a concrete example, let PriceVolume_Chart implement ActionListener and add a javax.swing.Timer to the chart's constructor:

public PriceVolume_Chart(String Symbol) {
…
Timer t = new Timer(500, this);
t.start();
}

Then, given a suitable index and listener, you'll see the crosshairs move to each date in succession at 2 Hz.

private int index;
@Override
public void actionPerformed(ActionEvent e) {
TimeSeriesDataItem item = Volume_Series.getDataItem(index);
xCrosshair.setValue(item.getPeriod().getFirstMillisecond());
item = Price_series.getDataItem(index);
yCrosshair.setValue(item.getValue().doubleValue());
index++;
if (index == Volume_Series.getItemCount()) {
index = 0;
}
}

You can use a similar listener to move your chosen indicator when the user selects a particular data entry. The exact details depend on your implementation of the observer pattern, discussed here. As you'll want to let both chart and table views share access to a common model, you may consider extending AbstractXYDataset, shown here and implementing a Swing TableModel; alternatively, consider extending AbstractTableModel, shown here and implementing the XYDataset interface.

答案2

得分: 1

Alright, @trashgod was inspirational. I finally tried his suggestions and got to the answer I was looking for; here is the essential solution:

TimeSeriesDataItem itemX = Volume_Series.getDataItem(Index);
xCrosshair.setValue(itemX.getPeriod().getFirstMillisecond());

TimeSeriesDataItem itemY = Price_series.getDataItem(Index);
yCrosshair.setValue(itemY.getValue().doubleValue());

Complete example:

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.entity.*;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.time.*;
import org.jfree.data.xy.XYDataset;
import org.jfree.chart.labels.*;
import org.jfree.chart.panel.*;
import org.jfree.chart.plot.*;

public class PriceVolume_Chart extends JPanel implements ChartMouseListener {
  // ... (rest of the code)
}

class MyRender extends XYBarRenderer {
  // ... (rest of the code)
}

Please note that due to the complexity of the code and the limitations of the text format, some details and context might be lost in the translation.

英文:

Alright, @trashgod was inspirational. I finally tried his suggestions and got to the answer I was looking for; here is the essential solution:

TimeSeriesDataItem itemX = Volume_Series.getDataItem(Index);
xCrosshair.setValue(itemX.getPeriod().getFirstMillisecond());
TimeSeriesDataItem itemY = Price_series.getDataItem(Index);
yCrosshair.setValue(itemY.getValue().doubleValue());

Complete example:

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.entity.*;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.time.*;
import org.jfree.data.xy.XYDataset;
import org.jfree.chart.labels.*;
import org.jfree.chart.panel.*;
import org.jfree.chart.plot.*;
public class PriceVolume_Chart extends JPanel implements ChartMouseListener   // A demo application for price-volume chart.   
{
ChartPanel panel;
TimeSeries Price_series=new TimeSeries("Price");
TimeSeries Volume_Series=new TimeSeries("Volume");
Crosshair xCrosshair,yCrosshair;
static Vector<String> Volume_Color_Vector=new Vector();
private int index;
public PriceVolume_Chart(String Symbol,int Index)
{
JFreeChart chart=createChart(Symbol);
panel=new ChartPanel(chart,true,true,true,false,true);
panel.setPreferredSize(new Dimension(1000,500));
panel.addChartMouseListener(this);
CrosshairOverlay crosshairOverlay=new CrosshairOverlay();
float[] dash={2f,0f,2f};
BasicStroke bs=new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND,1.0f,dash,2f);
xCrosshair=new Crosshair(Double.NaN,Color.GRAY,bs);
xCrosshair.setLabelBackgroundPaint(new Color(0f,0f,0f,1f));
xCrosshair.setLabelFont(xCrosshair.getLabelFont().deriveFont(14f));
xCrosshair.setLabelPaint(new Color(1f,1f,1f,1f));
xCrosshair.setLabelGenerator(new CrosshairLabelGenerator()
{
@Override
public String generateLabel(Crosshair crosshair)
{
long ms=(long)crosshair.getValue();
TimeSeriesDataItem item=null;
for (int i=0;i<Volume_Series.getItemCount();i++)
{
item=Volume_Series.getDataItem(i);
if (ms==item.getPeriod().getFirstMillisecond()) break;
}
long volume=item.getValue().longValue();
return NumberFormat.getInstance().format(volume);
}
});
xCrosshair.setLabelVisible(true);
yCrosshair=new Crosshair(Double.NaN,Color.GRAY,bs);
yCrosshair.setLabelBackgroundPaint(new Color(0f,0f,0f,1f));
yCrosshair.setLabelFont(xCrosshair.getLabelFont().deriveFont(14f));
yCrosshair.setLabelPaint(new Color(1f,1f,1f,1f));
yCrosshair.setLabelVisible(true);
crosshairOverlay.addDomainCrosshair(xCrosshair);
crosshairOverlay.addRangeCrosshair(yCrosshair);
panel.addOverlay(crosshairOverlay);
add(panel);
TimeSeriesDataItem itemX=Volume_Series.getDataItem(Index);
xCrosshair.setValue(itemX.getPeriod().getFirstMillisecond());
TimeSeriesDataItem itemY=Price_series.getDataItem(Index);
yCrosshair.setValue(itemY.getValue().doubleValue());
}
private JFreeChart createChart(String Symbol)
{
createPriceDataset(Symbol);
XYDataset priceData=new TimeSeriesCollection(Price_series);
JFreeChart chart=ChartFactory.createTimeSeriesChart(Symbol,"Date",getYLabel("Price ( $ )"),priceData,true,true,true);
XYPlot plot=chart.getXYPlot();
plot.setBackgroundPaint(new Color(192,196,196));
NumberAxis rangeAxis1=(NumberAxis)plot.getRangeAxis();
rangeAxis1.setLowerMargin(0.40);                                           // Leave room for volume bars
plot.getRenderer().setDefaultToolTipGenerator(new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,new SimpleDateFormat("yyyy-MM-d"),NumberFormat.getCurrencyInstance()));
NumberAxis rangeAxis2=new NumberAxis("Volume");
rangeAxis2.setUpperMargin(1.00);                                           // Leave room for price line   
rangeAxis2.setNumberFormatOverride(NumberFormat.getNumberInstance());
plot.setRangeAxis(1,rangeAxis2);
plot.setDataset(1,new TimeSeriesCollection(Volume_Series));
plot.setRangeAxis(1,rangeAxis2);
plot.mapDatasetToRangeAxis(1,1);
MyRender Renderer=new MyRender();
Renderer.setShadowVisible(false);
plot.setRenderer(1,Renderer);
DateAxis domainAxis=(DateAxis) plot.getDomainAxis();                     // Consider adjusting the lower margin of the domain axis for symmetry.
domainAxis.setLowerMargin(0.05);
return chart;
}
private void createPriceDataset(String Symbol)
{
String Lines[]=new String[21],Items[],Date;
int Year, Month, Day;
long Volume,Last_Volume=0;
double Price;
Lines[0]="Date,Open,High,Low,Close,Adj Close,Volume";
Lines[1]="2020-07-17,44.110001,44.369999,41.919998,42.509998,42.323395,849700";
Lines[2]="2020-07-20,41.630001,41.680000,39.669998,40.119999,39.943886,1319300";
Lines[3]="2020-07-21,40.880001,42.860001,40.860001,42.270000,42.084450,2070300";
Lines[4]="2020-07-22,41.919998,42.700001,41.090000,42.570000,42.383133,1317600";
Lines[5]="2020-07-23,43.919998,46.389999,43.279999,44.759998,44.563519,1917700";
Lines[6]="2020-07-24,46.500000,46.500000,43.950001,44.410000,44.215057,1384600";
Lines[7]="2020-07-27,44.000000,44.240002,42.610001,43.860001,43.667469,799800";
Lines[8]="2020-07-28,43.389999,44.590000,42.930000,43.020000,42.831158,699700";
Lines[9]="2020-07-29,42.759998,45.590000,42.740002,45.430000,45.230579,826200";
Lines[10]="2020-07-30,44.160000,44.639999,42.959999,44.500000,44.304661,798100";
Lines[11]="2020-07-31,44.330002,44.419998,42.580002,44.360001,44.165276,1037800";
Lines[12]="2020-08-03,44.560001,45.599998,43.419998,44.939999,44.742729,797000";
Lines[13]="2020-08-04,44.900002,45.500000,43.450001,43.540001,43.348877,971100";
Lines[14]="2020-08-05,44.860001,45.389999,43.650002,45.330002,45.131020,902000";
Lines[15]="2020-08-06,45.049999,46.279999,44.330002,45.299999,45.101147,645200";
Lines[16]="2020-08-07,44.849998,46.189999,44.189999,46.150002,45.947418,604900";
Lines[17]="2020-08-10,46.669998,48.410000,46.549999,47.290001,47.082417,960200";
Lines[18]="2020-08-11,49.110001,50.849998,48.799999,48.910000,48.695301,1187700";
Lines[19]="2020-08-12,49.759998,50.009998,47.060001,47.840000,47.630001,752800";
Lines[20]="2020-08-13,46.950001,48.369999,46.459999,47.110001,47.110001,535700";
for (int i=1;i<Lines.length;i++)
{
Items=Lines[i].split(",");
Date=Items[0].replace("-0","-");
Price=Double.parseDouble(Items[5]);
Volume=Long.parseLong(Items[6]);
Items=Date.split("-");
Year=Integer.parseInt(Items[0]);
Month=Integer.parseInt(Items[1]);
Day=Integer.parseInt(Items[2]);
Price_series.add(new Day(Day,Month,Year),Price);
Volume_Series.add(new Day(Day,Month,Year),Volume);
Volume_Color_Vector.add(Volume>=Last_Volume?"+":"-");
Last_Volume=Volume;
}
}
@Override
public void chartMouseClicked(ChartMouseEvent event)
{
// ignore
}
public void chartMouseMoved(ChartMouseEvent cmevent)
{
ChartEntity chartentity=cmevent.getEntity();
if (chartentity instanceof XYItemEntity)
{
XYItemEntity e=(XYItemEntity)chartentity;
XYDataset d=e.getDataset();
int s=e.getSeriesIndex();
int i=e.getItem();
double x=d.getXValue(s,i);
double y=d.getYValue(s,i);
Out("x = "+x+"  y = "+y);
xCrosshair.setValue(x);
yCrosshair.setValue(y);
}
}
String getYLabel(String Text)
{
String Result="";
for (int i=0;i<Text.length();i++) Result+=Text.charAt(i)+(i<Text.length()-1?"\u2009":"");
//    Out(Result);
return Result;
}
private static void out(String message) { System.out.print(message); }
private static void Out(String message) { System.out.println(message); }
// Create the GUI and show it. For thread safety, this method should be invoked from the event-dispatching thread.
static void Create_And_Show_GUI()
{
final PriceVolume_Chart demo=new PriceVolume_Chart("ADS",19);
JFrame frame=new JFrame("PriceVolume_Chart Frame");
frame.add(demo);
frame.addWindowListener(new WindowAdapter()
{
public void windowActivated(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowClosing(WindowEvent e) { System.exit(0); }
public void windowDeactivated(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { demo.repaint(); }
public void windowGainedFocus(WindowEvent e) { demo.repaint(); }
public void windowIconified(WindowEvent e) { }
public void windowLostFocus(WindowEvent e) { }
public void windowOpening(WindowEvent e) { demo.repaint(); }
public void windowOpened(WindowEvent e) { }
public void windowResized(WindowEvent e) { demo.repaint(); }
public void windowStateChanged(WindowEvent e) { demo.repaint(); }
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args)
{
// Schedule a job for the event-dispatching thread : creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() { public void run() { Create_And_Show_GUI(); } });
}
}
class MyRender extends XYBarRenderer
{
@Override
public Paint getItemPaint(int row,int col)
{
this.setBarAlignmentFactor(0.5);
//    System.out.println(row+" "+col+" "+super.getItemPaint(row,col));
return PriceVolume_Chart.Volume_Color_Vector.elementAt(col).equals("+")?super.getItemPaint(row,col):new Color(0.56f,0.2f,0.5f,1f);
}
}

huangapple
  • 本文由 发表于 2020年10月5日 10:27:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64201748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定