First great compliment to all the Jfree Developers! Its a great tool.
Im new in the Java business. I already created a candlestick chart that i want to combine it width sliders to move and zoom....
I combine the sliders from the CompassFormatDemo1 width the chart.
But i dont get the connection together.
I get the data dynamicly from my website.
Would be great if someone can help me. Hope its not too confused.
PS: Delete the spaces in url
Code: Select all
//package demo;
import java.awt.BorderLayout;
import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.io.*;
import java.awt.*;
import java.awt.font.*;
import javax.swing.JSlider;
import org.jfree.data.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.axis.*;
import java.util.ArrayList;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.AbstractXYDataset;
import org.jfree.data.xy.DefaultOHLCDataset;
import org.jfree.data.xy.OHLCDataItem;
import java.util.Date;
import javax.swing.JPanel;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
//import org.jfree.chart.XYToolTipGenerator;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.DefaultHighLowDataset;
//import org.jfree.data.XYToolTipGenerator;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.chart.labels.HighLowItemLabelGenerator;
import org.jfree.data.xy.OHLCDataset;
import org.jfree.date.DateUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.data.time.MovingAverage;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Minute;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.util.Collections;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Date;
import org.jfree.chart.event.AxisChangeEvent;
import org.jfree.chart.event.AxisChangeListener;
/**
* A demo showing a candlestick chart.
*/
public class Watcher extends ApplicationFrame {
// REGLER
private static class regler extends JPanel implements ChangeListener {
private JSlider Yposition;
private JSlider Ysize;
private JSlider Xposition;
private JSlider Xsize;
private ModuloAxis rangeAxis;
private ModuloAxis PeriodAxis;
private double Ydirection = 0.0;
private double Yzoom = 45.0;
private double Xdirection = 0.0;
private double Xzoom = 45.0;
/**
* Scrollbaren anlegen
*/
public regler() {
super(new BorderLayout());
JPanel controlPanel = new JPanel(new GridLayout(1, 2));
this.Ysize = new JSlider(SwingConstants.VERTICAL, 10, 180, 45);
this.Ysize.setPaintLabels(true);
this.Ysize.setPaintTicks(true);
this.Ysize.setMajorTickSpacing(10);
this.Ysize.setMinorTickSpacing(5);
this.Ysize.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
this.Ysize.addChangeListener(this);
this.Yposition = new JSlider(SwingConstants.VERTICAL, 0, 360, 0);
this.Yposition.setMajorTickSpacing(30);
this.Yposition.setMinorTickSpacing(5);
this.Yposition.setPaintLabels(true);
this.Yposition.setPaintTicks(true);
this.Yposition.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
this.Yposition.setPaintTrack(true);
this.Yposition.addChangeListener(this);
controlPanel.add(this.Yposition);
controlPanel.add(this.Ysize);
JPanel controlPanel2 = new JPanel(new GridLayout(1, 2));
this.Xsize = new JSlider(SwingConstants.HORIZONTAL, 10, 180, 45);
this.Xsize.setPaintLabels(true);
this.Xsize.setPaintTicks(true);
this.Xsize.setMajorTickSpacing(10);
this.Xsize.setMinorTickSpacing(5);
this.Xsize.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
this.Xsize.addChangeListener(this);
this.Xposition = new JSlider(SwingConstants.HORIZONTAL, 0, 360, 0);
this.Xposition.setMajorTickSpacing(30);
this.Xposition.setMinorTickSpacing(5);
this.Xposition.setPaintLabels(true);
this.Xposition.setPaintTicks(true);
this.Xposition.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
this.Xposition.setPaintTrack(true);
this.Xposition.addChangeListener(this);
controlPanel2.add(this.Xposition);
controlPanel2.add(this.Xsize);
JFreeChart chart = createChart(createDataset());
ChartPanel chartPanel = new ChartPanel(chart); // ANZEIGE FÜR CHART
chartPanel.setPreferredSize(new java.awt.Dimension(800, 500)); // GESAMTGRÖSSE
add(controlPanel, BorderLayout.EAST);
add(controlPanel2, BorderLayout.SOUTH);
add(chartPanel, BorderLayout.CENTER);
//add(chartPanel);
//return new ChartPanel(chart);
//return chartPanel;
}
private XYDataset createYdirectionDataset(int count) {
TimeSeriesCollection dataset = new TimeSeriesCollection();
TimeSeries s1 = new TimeSeries("Wind Ydirection", Minute.class);
RegularTimePeriod start = new Minute();
double Ydirection = 0.0;
for (int i = 0; i < count; i++) {
s1.add(start, Ydirection);
start = start.next();
Ydirection = Ydirection + (Math.random() - 0.5) * 15.0;
if (Ydirection < 0.0) {
Ydirection = Ydirection + 360.0;
}
else if (Ydirection > 360.0) {
Ydirection = Ydirection - 360.0;
}
}
dataset.addSeries(s1);
return dataset;
}
private JFreeChart createChart(OHLCDataset dataset) {
XYDataset Ydirection = createYdirectionDataset(100);
JFreeChart chart = ChartFactory.createHighLowChart(
"",
"Datum",
"Kurs",
dataset,
false
);
XYPlot plot = (XYPlot) chart.getPlot();
plot.getDomainAxis().setLowerMargin(0.0);
plot.getDomainAxis().setUpperMargin(0.0);
//NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
//System.out.println(this);
this.rangeAxis = new ModuloAxis("gogo", new Range(0, 360));
TickUnits units = new TickUnits();
units.add(new NumberTickUnit(180.0, new CompassFormat()));
units.add(new NumberTickUnit(90.0, new CompassFormat()));
units.add(new NumberTickUnit(45.0, new CompassFormat()));
units.add(new NumberTickUnit(22.5, new CompassFormat()));
rangeAxis.setStandardTickUnits(units);
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setBaseLinesVisible(true);
renderer.setBaseShapesVisible(true);
plot.setRenderer(renderer);
//KONSTRUKTOR FÜR CANDLE
CandlestickRenderer renderer3 = new CandlestickRenderer();
renderer3.setUpPaint(new Color(0x00FF00));
renderer3.setDownPaint(new Color(0xff0000));
renderer3.setCandleWidth(5);
renderer3.setUseOutlinePaint(true);
// MIT KONSTRUKTOR VERBINDEN
plot.setRenderer(renderer3);
//plot.setRenderer(beschriftung);
// Einstellungen für chart HOLEN
NumberAxis axis = (NumberAxis) plot.getRangeAxis();
axis.setAutoRangeIncludesZero(false);
axis.setUpperMargin(0.0);
axis.setLowerMargin(0.0);
//getContentPane().add(new lst(), BorderLayout.WEST );
//getContentPane().add(new regler(), BorderLayout.EAST );
//getContentPane().add(controlPanel);
return chart;
}
public void stateChanged(ChangeEvent event) {
System.out.println(this);
if (event.getSource() == this.Yposition) {
this.Ydirection = this.Yposition.getValue();
this.rangeAxis.setDisplayRange(this.Ydirection - this.Yzoom, this.Ydirection + this.Yzoom);
}
else if (event.getSource() == this.Ysize) {
this.Yzoom = this.Ysize.getValue();
this.rangeAxis.setDisplayRange(this.Ydirection - this.Yzoom, this.Ydirection + this.Yzoom);
}
else if (event.getSource() == this.Xposition) {
this.Xdirection = this.Xposition.getValue();
this.PeriodAxis.setDisplayRange(this.Ydirection - this.Yzoom, this.Ydirection + this.Yzoom);
}
else if (event.getSource() == this.Xsize) {
this.Xzoom = this.Xsize.getValue();
this.PeriodAxis.setDisplayRange(this.Xdirection - this.Xzoom, this.Ydirection + this.Yzoom);
}
}
// CANDLESTICK ERZEUGEN
/**
* Creates a sample high low dataset.
*
* @return a sample high low dataset.
*/
// DATEN HOLEN
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* CHARTFENSTER ERZEUGEN
*/
public static OHLCDataset createDataset() {
ArrayList liste = new ArrayList();
Date[] date = new Date[1800];
double[] high = new double[1800];
double[] low = new double[1800];
double[] open = new double[1800];
double[] close = new double[1800];
double[] volume = new double[1800];
double[] linie = new double[1800];
try {
String strUrl= "http: // www .fleitec. com/baseportal/aktien/watcher/tts&.csv";
URL url = new URL(strUrl);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
DateFormat df = new SimpleDateFormat("y-M-d");
String inputLine;
String ges= in.readLine();
System.out.println(ges);
int count=0;
while ((inputLine = in.readLine()) != null) {
String[] x = inputLine.split (",");
String date1;
date[count] = df.parse( x[0] );
high[count] = Double.parseDouble( x[1] );
low[count] = Double.parseDouble( x[2] );
open[count] = Double.parseDouble( x[3] );
close[count] = Double.parseDouble( x[4] );
linie[count] = Double.parseDouble( x[4] );
volume[count] = Double.parseDouble( x[5] );
count=count+1;
}
in.close();
}
catch (Exception e) {
}
return new DefaultHighLowDataset("Series 1", date, high, low, open,
close, volume);
}
// Auswahl der Aktien
public Watcher(String title) {
super(title);
getContentPane().setLayout( new BorderLayout() );
getContentPane().add(new lst(), BorderLayout.WEST );
getContentPane().add(new regler(), BorderLayout.CENTER );
// grösser kleiner
//add(controlPanel, BorderLayout.EAST);
//add(controlPanel2, BorderLayout.SOUTH);
//chartPanel.add(new regler(),BorderLayout.WEST);
//d(chartPanel);
//add(new regler());
}
public class lst extends List{
public lst(){
List lst = new List(4, false);
lst.add("Mercury");
lst.add("Venus");
lst.add("Earth");
lst.add("JavaSoft");
lst.add("Mars");
lst.add("Jupiter");
lst.add("Saturn");
lst.add("Uranus");
lst.add("Neptune");
lst.add("Pluto");
//cnt.add(lst);
//JPanel aktienliste = new JPanel();
//aktienliste.setLayout(
//new BoxLayout( aktienliste, BoxLayout.Y_AXIS ) );
//aktienliste.add( new JLabel("Aktien") );
//aktienliste.add(lst);
//return lst;
}
}
private XYDataset createYdirectionDataset(int count) {
TimeSeriesCollection dataset = new TimeSeriesCollection();
TimeSeries s1 = new TimeSeries("Wind Ydirection", Minute.class);
RegularTimePeriod start = new Minute();
double Ydirection = 0.0;
for (int i = 0; i < count; i++) {
s1.add(start, Ydirection);
start = start.next();
Ydirection = Ydirection + (Math.random() - 0.5) * 15.0;
if (Ydirection < 0.0) {
Ydirection = Ydirection + 360.0;
}
else if (Ydirection > 360.0) {
Ydirection = Ydirection - 360.0;
}
}
dataset.addSeries(s1);
return dataset;
}
public static class CustomTickNumberAxis extends org.jfree.chart.axis.NumberAxis {
/**
* Holds value of property lowestVisibleTickValue.
* The lowest visible tick unit is the bottom anchor for a vertical
* number axis.
* NOTE: Make sure this value is >= the lowerBound of the NumberAxis, as
* otherwise it will be ignored as it cannot be displayed correctly.
* See {@link #calculateLowestVisibleTickValue}.
*/
private Double lowestVisibleTickValue;
/**
* Calculates the value of the lowest visible tick on the axis or
* returns any previously specified. See {@link #lowestVisibleTickValue}.
* If the specified value is smaller than the lower bound of the axis, the
* default method (see {@link NumberAxis#calculateLowestVisibleTickValue})
* is used.
* @return The value of the lowest visible tick on the axis.
*/
protected double calculateLowestVisibleTickValue() {
if (lowestVisibleTickValue != null && lowestVisibleTickValue.doubleValue() >= getRange().getLowerBound()) {
return lowestVisibleTickValue.doubleValue();
} else
return super.calculateLowestVisibleTickValue();
}
/**
* Getter for property lowestVisibleTickValue.
* @return Value of property lowestVisibleTickValue.
*/
public Double getLowestVisibleTickValue() {
return this.lowestVisibleTickValue;
}
/**
* Setter for property lowestVisibleTickValue.
* @param lowestVisibleTickValue New value of property lowestVisibleTickValue.
*/
public void setLowestVisibleTickValue(Double lowestVisibleTickValue) {
this.lowestVisibleTickValue = lowestVisibleTickValue;
}
}
// Anfang Methoden
// DATEN FÜR CHART HOLEN
/**
* Creates a new demo instance.
*
* @param title the frame title.
*/
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
//public Watcher(String title) {
// super(title);
// setContentPane(new regler());
//}
public static JPanel createDemoPanel() {
return new regler();
}
/**
* Starting point for the demonstration application.
*
* WATCHER STARTEN
*/
public static void main(String[] args) {
Watcher demo = new Watcher(
"TITEL");
demo.pack();
demo.setVisible(true);
}
// Ende Methoden
public void axisChanged(AxisChangeEvent ace){
final Axis axis = ace.getAxis();
if(axis instanceof NumberAxis){
System.out.println("Number Tick Unit size for axis "+axis.getLabel()+" is :" +((NumberAxis)axis).getTickUnit().getSize());
SwingUtilities.invokeLater(new Runnable(){
public void run(){
System.out.println("Number Tick Unit size for xAxis after drawing in EDT, axisChanged: "+((NumberAxis)axis).getTickUnit().getSize());
};
});
}
}
}
Thanks
Flei