About Dynamic ValueMarker and actionPerformed

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
stbatuhan
Posts: 3
Joined: Fri Dec 02, 2016 10:43 am
antibot: No, of course not.

About Dynamic ValueMarker and actionPerformed

Post by stbatuhan » Fri Dec 02, 2016 10:52 am

Hi everyone, i recently started a work about forex. I have to make a dynamic candlestick chart. For now i'm just trying to use XYPlot. I can draw it with randomized time and values on actionPerformed. Also able to draw a line for specified value but i have to change that value to randomized one in actionPerformed. So is there a way that you can suggest?

Code: Select all

package DynamicLineAndTimeSeriesChart;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;

public class java extends ApplicationFrame implements ActionListener {

    private TimeSeries series;
    public String den;

    private double lastValue = 100.0;
    private Timer timer = new Timer(500, this);
    public java(final String title) {

        super(title);
        this.series = new TimeSeries("Randommm Data", Millisecond.class);

        final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
        final JFreeChart chart = createChart(dataset);
        timer.setInitialDelay(1000);

        chart.setBackgroundPaint(Color.LIGHT_GRAY);

        final JPanel content = new JPanel(new BorderLayout());

        final ChartPanel chartPanel = new ChartPanel(chart);

        content.add(chartPanel);

        //chartPanel.setPreferredSize(new java.awt.Dimension(800, 500));

        setContentPane(content);

        timer.start();

    }

    @Override
    public void actionPerformed(final ActionEvent e) {

        final double factor = 0.9 + 0.2*Math.random();
        this.lastValue = this.lastValue * factor;

        final Millisecond now = new Millisecond();
        this.series.add(new Millisecond(), this.lastValue);
        
        den = String.valueOf(lastValue);
        System.out.println("Current Time in Milliseconds = " + now.toString()+", Current Value : "+this.lastValue);
    }
    
    private JFreeChart createChart(final XYDataset dataset) {
        final JFreeChart result = ChartFactory.createTimeSeriesChart(
            "DLaTSChart",
            "Time",
            "Value",
            dataset,
            true,
            true,
            false
        );
        final XYPlot plot = result.getXYPlot();
        ValueMarker marker= new ValueMarker(85);
        marker.setLabel(""+ den +"");
        marker.setLabelTextAnchor(TextAnchor.BOTTOM_RIGHT);
        marker.setLabelAnchor(RectangleAnchor.RIGHT);
        marker.setPaint(Color.ORANGE);
        plot.addRangeMarker(marker);
        
        plot.setBackgroundPaint(new Color(0xffffe0));
        plot.setDomainGridlinesVisible(true);
        plot.setDomainGridlinePaint(Color.lightGray);
        plot.setRangeGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.lightGray);

        ValueAxis xaxis = plot.getDomainAxis();
        xaxis.setAutoRange(true);

        xaxis.setFixedAutoRange(60000.0);
        xaxis.setVerticalTickLabels(true);

        ValueAxis yaxis = plot.getRangeAxis();
        //yaxis.setRange(0.0, 300.0);
        yaxis.setAutoRange(true);

        return result;
    }


    public static void main(final String[] args) {

        final java demo = new java("DLATSChart");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }


} 
And another point /*but not important as the main topic*/ is that i cannot use "chartPanel.setPreferredSize(new java.awt.Dimension(800, 500));" cannot find symbol on awt

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: About Dynamic ValueMarker and actionPerformed

Post by paradoxoff » Fri Dec 02, 2016 8:28 pm

Make the ValueMarker marker an instance variable (as you have done with the TimeSeries series), and call its setValue method in the actionPerformed.
Regarding the second error: your code looks ok.

stbatuhan
Posts: 3
Joined: Fri Dec 02, 2016 10:43 am
antibot: No, of course not.

Re: About Dynamic ValueMarker and actionPerformed

Post by stbatuhan » Mon Dec 05, 2016 3:15 pm

Thank you for the reply, i will try your suggestion. Can i do timebased candlestick. Example, for every 1 minute there will be a new candlestick on table. So it will be random ofcourse for now. I'm able to do randomized candlestick but not related on time and update style.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: About Dynamic ValueMarker and actionPerformed

Post by paradoxoff » Tue Dec 06, 2016 4:31 pm

Shall the x or the y values of your candle stick be time based? In any case, you must install a DateAxis as domain or range axis, and make sure that the x or y values indeed represent time stamps.

stbatuhan
Posts: 3
Joined: Fri Dec 02, 2016 10:43 am
antibot: No, of course not.

Re: About Dynamic ValueMarker and actionPerformed

Post by stbatuhan » Fri Dec 09, 2016 8:09 am

X axis is time based and y axis is kind of standart for money. Hmm i'm gonna search examples for DateAxis class.
Thank you for your reply.

Locked